Commit 4d812f17 authored by Kevin Robert's avatar Kevin Robert
Browse files

UNAPLLYREC-25 : Correction de la récupération d'un écran par sa référence....

UNAPLLYREC-25 : Correction de la récupération d'un écran par sa référence. Ajout de la restriction sur le formulaire pour éviter de remonter des écrans sans rapport.
parent 9dfd6f72
......@@ -175,8 +175,8 @@ public class NavigationService {
* @param screenReference La référence de l'écran.
* @return L'écran s'il existe.
*/
public ScreenDTO retrieveScreen(final String screenReference) {
return screenService.findOneByReference(screenReference).orElseThrow(() -> new ScreenNotFoundException(screenReference));
public ScreenDTO retrieveScreen(final String screenReference, final Long formId) {
return screenService.findOneByReferenceAndFormId(screenReference, formId).orElseThrow(() -> new ScreenNotFoundException(screenReference));
}
/**
......
......@@ -108,7 +108,7 @@ public class ScreenNavigationService {
.findFirst();
return matchingExpression
.map(NextScreenExpressionDTO::getNextScreenReference)
.map(this::getScreenByRef)
.map(s -> getScreenByRef(s, screen.getFormId()))
.orElseGet(() -> getDefaultNextScreen(screen));
//@formatter:on
}
......@@ -127,7 +127,7 @@ public class ScreenNavigationService {
if (StringUtils.isBlank(screen.getDefaultNextScreenReference())) {
return getScreenByIndexAndFormId(screen.getNextIndex(), screen.getFormId());
}
return getScreenByRef(screen.getDefaultNextScreenReference());
return getScreenByRef(screen.getDefaultNextScreenReference(), screen.getFormId());
}
/**
......@@ -141,14 +141,14 @@ public class ScreenNavigationService {
}
/**
* Wrapper de la méthode {@link ScreenService#findOneByReference(String)}; Jette une exception si l'écran n'existe pas.
* Wrapper de la méthode {@link ScreenService#findOneByReferenceAndFormId(String, Long)}; Jette une exception si l'écran n'existe pas.
*
* @param screenRef
* @return
*/
private ScreenDTO getScreenByRef(final String screenRef) {
private ScreenDTO getScreenByRef(final String screenRef, final Long formId) {
return screenService
.findOneByReference(screenRef)
.findOneByReferenceAndFormId(screenRef, formId)
.orElseThrow(() -> new UnableToFindNextScreenException("l'écran n'existe pas en base"));
}
......
......@@ -23,7 +23,7 @@ public interface ScreenRepository extends JpaRepository<Screen, Long> {
* @param reference La référence de l'écran.
* @return L'écran s'il existe.
*/
Optional<Screen> findByReference(final @NotNull String reference);
Optional<Screen> findByReferenceAndFormId(final @NotNull String reference, final @NotNull Long idForm);
/**
* Rcupération du premier écran du formulaire.
......
......@@ -133,9 +133,9 @@ public class ScreenService {
* @param reference La référence de l'écran.
* @return L'écran s'il existe.
*/
public Optional<ScreenDTO> findOneByReference(final String reference) {
public Optional<ScreenDTO> findOneByReferenceAndFormId(final String reference, final Long idForm) {
log.debug("Récupération de l'écran ayant pour référence '{}'.", reference);
return screenRepository.findByReference(reference).map(screenMapper::toDto);
return screenRepository.findByReferenceAndFormId(reference, idForm).map(screenMapper::toDto);
}
/**
......
......@@ -84,7 +84,7 @@ public class FormApiDelegateImpl implements FormApiDelegate {
@Override
public ResponseEntity<FormAPI> next(final String formReference, final String sessionId, final String screenReference, final List<AnswersAPI> answers) {
final FormDTO form = navigationService.retrieveForm(formReference);
final ScreenDTO screen = navigationService.retrieveScreen(screenReference);
final ScreenDTO screen = navigationService.retrieveScreen(screenReference, form.getId());
final List<AnswerElements> answerElements = convertAnswersApiToAnswerElements(answers);
final ScreenDTO nextScreen = navigationService.saveAnswerProcess(sessionId, screen, answerElements);
final FormAPI screenAPI = initializeResponseModel(sessionId, nextScreen, form);
......@@ -94,7 +94,7 @@ public class FormApiDelegateImpl implements FormApiDelegate {
@Override
public ResponseEntity<FormAPI> previous(final String referenceForm, final String sessionId, final String referenceScreen) {
final FormDTO form = navigationService.retrieveForm(referenceForm);
final ScreenDTO screen = navigationService.retrieveScreen(referenceScreen);
final ScreenDTO screen = navigationService.retrieveScreen(referenceScreen, form.getId());
final ScreenDTO previousScreen = navigationService.retrievePreviousScreenProcess(sessionId, screen);
final FormAPI formAPI = initializeResponseModel(sessionId, previousScreen, form);
return new ResponseEntity<>(formAPI, HttpStatus.OK);
......@@ -103,7 +103,7 @@ public class FormApiDelegateImpl implements FormApiDelegate {
@Override
public ResponseEntity<FormAPI> screen(final String referenceForm, final String sessionId, final String referenceScreen) {
final FormDTO form = navigationService.retrieveForm(referenceForm);
final ScreenDTO screen = navigationService.retrieveScreen(referenceScreen);
final ScreenDTO screen = navigationService.retrieveScreen(referenceScreen, form.getId());
final ScreenDTO displayableScreen = navigationService.loadScreenProcess(sessionId, screen);
final FormAPI formAPI = initializeResponseModel(sessionId, displayableScreen, form);
return new ResponseEntity<>(formAPI, HttpStatus.OK);
......
......@@ -199,7 +199,7 @@ class NavigationServiceTest {
*/
@Test
void retrieve_screen() {
final ScreenDTO screen = navigationService.retrieveScreen(FIRST_SCREEN_REFERENCE);
final ScreenDTO screen = navigationService.retrieveScreen(FIRST_SCREEN_REFERENCE, form.getId());
assertEquals(FIRST_SCREEN_TITLE, screen.getName());
}
......@@ -209,7 +209,7 @@ class NavigationServiceTest {
*/
@Test
void retrieve_screen_bad_reference() {
assertThrows(ScreenNotFoundException.class, () -> navigationService.retrieveScreen("erreur"));
assertThrows(ScreenNotFoundException.class, () -> navigationService.retrieveScreen("erreur", form.getId()));
}
/**
......
......@@ -104,7 +104,7 @@ class ScreenNavigationServiceTest extends AbstractTest {
when(answerService.getSessionAnswers(DEFAULT_SESSION_ID, form.getId())).thenReturn(Collections.singletonList(answerMapper.toDto(answer)));
screen2.setDefaultNextScreenReference("S04");
ScreenDTO screen4DTO = screenMapper.toDto(screen4);
when(screenService.findOneByReference("S04")).thenReturn(Optional.of(screen4DTO));
when(screenService.findOneByReferenceAndFormId("S04", form.getId())).thenReturn(Optional.of(screen4DTO));
when(screenService.findScreenByIndexAndFormId(screen3.getIndex(), form.getId())).thenReturn(Optional.of(screenMapper.toDto(screen3)));
ScreenDTO nextScreen = screenNavigationService.getNextScreen(DEFAULT_SESSION_ID, screenMapper.toDto(screen2));
assertEquals("S04", nextScreen.getReference());
......@@ -114,7 +114,7 @@ class ScreenNavigationServiceTest extends AbstractTest {
assertTrue(ReadOnlyTextItem.class.isInstance(expectedVarItem));
assertEquals("answers[S01_souhait_parcours_staps] && answers[S01_projet_pro_1] : false", ReadOnlyTextItem.class.cast(expectedVarItem).getContent());
assertEquals("moy_gen", nextScreen.getItemsList().get(1).getReference());
verify(screenService).findOneByReference("S04");
verify(screenService).findOneByReferenceAndFormId("S04", form.getId());
verify(answerService).getSessionAnswers(DEFAULT_SESSION_ID, form.getId());
verify(screenService).findScreenByIndexAndFormId(screen3.getIndex(), form.getId());
}
......
......@@ -397,7 +397,7 @@ public class ScreenResourceIT {
}
private void checkIndex(final String screenReference, final Integer expectedIndex) {
Integer actualIndex = screenRepository.findByReference(screenReference).orElseThrow(IllegalStateException::new).getIndex();
Integer actualIndex = screenRepository.findByReferenceAndFormId(screenReference, form.getId()).orElseThrow(IllegalStateException::new).getIndex();
Assert.assertEquals(expectedIndex, actualIndex);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment