Commit de753f43 authored by François-Xavier Lebastard's avatar François-Xavier Lebastard
Browse files

fix (test) correction des tests et questionnaire front

parent f28c0b01
......@@ -119,6 +119,15 @@ public class Screen implements Serializable {
return this;
}
public Screen nameBo(final String nameBo) {
this.nameBo = nameBo;
return this;
}
public Screen index(final Integer index) {
this.index = index;
return this;
}
public void setName(String name) {
this.name = name;
}
......
......@@ -72,7 +72,7 @@ public interface ScreenRepository extends JpaRepository<Screen, Long> {
* @return Le nombre d'écran potentiellement restant.
*/
@Query(
value = "WITH RECURSIVE previous AS (SELECT id, previous_id FROM screen WHERE id = :screenId UNION SELECT s.id, s.previous_id FROM screen s INNER JOIN previous p ON p.previous_id = s.id) SELECT (select count(*) from screen where form_id = :formId) - count(pr.*) FROM previous pr",
value = "SELECT COUNT(*) FROM SCREEN WHERE INDEX > (SELECT INDEX FROM SCREEN WHERE ID = :screenId AND form_id = :formId) AND form_id = :formId",
nativeQuery = true
)
int countScreenLeft(final @Param("formId") Long formId, final @Param("screenId") Long screenId);
......
......@@ -213,8 +213,6 @@ public class FormService extends PermissionService<FormDTO> {
@PreAuthorize("hasAuthority('VIEW_FORM')")
protected boolean existPermission(User user, Long formId) {
// TODO : Revoir ça.
return true;
//return formRepository.existPermission(user.getId(), formId);
return formRepository.existPermission(user.getId(), formId);
}
}
......@@ -53,7 +53,7 @@ public class ScreenDTO implements Serializable {
private List<NextScreenExpressionDTO> nextScreenExpressions;
public Integer getNextIndex() {
return this.index++;
return index + 1;
}
public Long getId() {
......
......@@ -129,8 +129,8 @@ public class FormApiDelegateImpl implements FormApiDelegate {
*/
private FormAPI initializeResponseModel(final String sessionId, final ScreenDTO screen, final FormDTO form) {
FormAPI formAPI = serviceConverter.convertScreen(screen, form);
formAPI.getScreen().setHasPrevious(navigationService.checkIsFirstScreen(screen));
formAPI.getScreen().setHasNext(navigationService.checkIsLastScreen(screen));
formAPI.getScreen().setHasPrevious(!navigationService.checkIsFirstScreen(screen));
formAPI.getScreen().setHasNext(!navigationService.checkIsLastScreen(screen));
addAnswers(sessionId, screen.getId(), formAPI);
addProgress(sessionId, screen, formAPI);
formAPI.sessionId(sessionId);
......
package com.unantes.orientactive.web.rest;
import com.unantes.orientactive.domain.User;
import com.unantes.orientactive.navigation.exception.ScreenNotFoundException;
import com.unantes.orientactive.service.ScreenService;
import com.unantes.orientactive.service.UserService;
import com.unantes.orientactive.service.dto.ScreenDTO;
......@@ -82,8 +83,7 @@ public class ScreenResource {
@GetMapping("/forms/{formId}/firstscreen")
public ScreenDTO getFirstScreenForForm(@PathVariable Long formId) {
// TODO : Mettre un vraie exception.
return screenService.findFirstScreenOfForm(formId).orElseThrow(() -> new RuntimeException());
return screenService.findFirstScreenOfForm(formId).orElseThrow(() -> new ScreenNotFoundException("Ecran non trouvé"));
}
/**
......
......@@ -153,10 +153,10 @@ class NavigationServiceTest {
// TODO KRO : Corriger les tests suite à la modification du modèle de données des écrans.
form = new Form().name(FIRST_FORM_TITLE).reference(FIRST_FORM_REFERENCE).description("description").publicationState(FormPublicationState.PUBLISHED);
Set<Screen> screenList = new HashSet<>();
firstScreen = new Screen().name(FIRST_SCREEN_TITLE).reference(FIRST_SCREEN_REFERENCE).description("first screen of the form").defaultNextScreenReference("").items("[]").form(form);
secondScreen = new Screen().name(SECOND_SCREEN_TITLE).reference(SECOND_SCREEN_REFERENCE).description("second screen of the form").defaultNextScreenReference("").items("[]").form(form);
firstScreen = new Screen().name(FIRST_SCREEN_TITLE).nameBo(FIRST_SCREEN_TITLE).index(1).reference(FIRST_SCREEN_REFERENCE).description("first screen of the form").defaultNextScreenReference("").items("[]").form(form);
secondScreen = new Screen().name(SECOND_SCREEN_TITLE).nameBo(SECOND_SCREEN_TITLE).index(2).reference(SECOND_SCREEN_REFERENCE).description("second screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(firstScreen);
thirdScreen = new Screen().name(THIRD_SCREEN_TITLE).reference(THIRD_SCREEN_REFERENCE).description("third screen of the form").defaultNextScreenReference("").items("[]").form(form);
thirdScreen = new Screen().name(THIRD_SCREEN_TITLE).nameBo(THIRD_SCREEN_TITLE).index(3).reference(THIRD_SCREEN_REFERENCE).description("third screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(secondScreen);
// firstScreen.setNext(secondScreen);
// secondScreen.setNext(thirdScreen);
......
......@@ -28,6 +28,7 @@ import com.unantes.orientactive.service.mapper.ScreenMapperImpl;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
......@@ -65,22 +66,23 @@ class ScreenNavigationServiceTest extends AbstractTest {
public void initTest() {
// TODO KRO : Corriger les tests suite à la modification du modèle de données des écrans.
form = new Form();
form.setId(1L);
form.reference(DEFAULT_FORM_REFERENCE);
form.name("form");
screen1 = new Screen().name("screen 01").reference("S01").items(readTestResource("screen01Item.json")).form(form);
screen1 = new Screen().name("screen 01").index(1).reference("S01").items(readTestResource("screen01Item.json")).form(form);
screen1.setId(1L);
screen2 = new Screen().name("screen 02").reference("S02").items(readTestResource("screen02Item.json")).form(form);
screen2 = new Screen().name("screen 02").index(2).reference("S02").items(readTestResource("screen02Item.json")).form(form);
screen2.setId(2L);
screen3 = new Screen().name("screen 03").reference("S03").items(readTestResource("screen03Item.json")).form(form);
screen3 = new Screen().name("screen 03").index(3).reference("S03").items(readTestResource("screen03Item.json")).form(form);
screen3.setId(3L);
screen4 = new Screen().name("screen 04").reference("S04").items(readTestResource("screen04Item.json")).form(form);
screen4 = new Screen().name("screen 04").index(4).reference("S04").items(readTestResource("screen04Item.json")).form(form);
screen4.setId(4L);
// screen1.next(screen2);
// screen2.previous(screen1);
// screen2.next(screen3);
// screen3.previous(screen2);
// screen3.next(screen4);
// screen4.previous(screen3);
// screen1.next(screen2);
// screen2.previous(screen1);
// screen2.next(screen3);
// screen3.previous(screen2);
// screen3.next(screen4);
// screen4.previous(screen3);
final ServiceConverter serviceConverter = new ServiceConverter();
screenMapper = new ScreenMapperImpl(new FormMapperImpl(), new NextScreenExpressionDTOMapperImpl());
screenMapper.setServiceConverter(serviceConverter);
......@@ -93,15 +95,13 @@ class ScreenNavigationServiceTest extends AbstractTest {
answer = new Answer().sessionId(DEFAULT_SESSION_ID).answers(readTestResource("answersElement.json")).screen(screen1);
}
@Test
void getNextScreenNoScreenCondition() {
ScreenDTO screen2DTO = screenMapper.toDto(screen2);
when(screenService.findOne(2L)).thenReturn(Optional.of(screen2DTO));
when(screenService.findScreenByIndexAndFormId(screen2.getIndex(), form.getId())).thenReturn(Optional.of(screenMapper.toDto(screen2)));
ScreenDTO nextScreen = screenNavigationService.getNextScreen(DEFAULT_SESSION_ID, screenMapper.toDto(screen1));
assertEquals("S02", nextScreen.getReference());
verify(screenService).findOne(2L);
verify(screenService, times(2)).findScreenByIndexAndFormId(screen2.getIndex(), form.getId());
}
@Test
......@@ -113,6 +113,7 @@ class ScreenNavigationServiceTest extends AbstractTest {
screen2.setNextScreenExpressions(List.of(nextScreenExpression));
ScreenDTO screen4DTO = screenMapper.toDto(screen4);
when(screenService.findOneByReference("S04")).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());
assertEquals(2, nextScreen.getItemsList().size());
......@@ -123,6 +124,7 @@ class ScreenNavigationServiceTest extends AbstractTest {
assertEquals("moy_gen", nextScreen.getItemsList().get(1).getReference());
verify(screenService).findOneByReference("S04");
verify(answerService).getSessionAnswers(DEFAULT_SESSION_ID, form.getId());
verify(screenService).findScreenByIndexAndFormId(screen3.getIndex(), form.getId());
}
@Test
......@@ -131,6 +133,7 @@ class ScreenNavigationServiceTest extends AbstractTest {
screen2.setDefaultNextScreenReference("S04");
ScreenDTO screen4DTO = screenMapper.toDto(screen4);
when(screenService.findOneByReference("S04")).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());
assertEquals(2, nextScreen.getItemsList().size());
......@@ -141,5 +144,6 @@ class ScreenNavigationServiceTest extends AbstractTest {
assertEquals("moy_gen", nextScreen.getItemsList().get(1).getReference());
verify(screenService).findOneByReference("S04");
verify(answerService).getSessionAnswers(DEFAULT_SESSION_ID, form.getId());
verify(screenService).findScreenByIndexAndFormId(screen3.getIndex(), form.getId());
}
}
......@@ -152,17 +152,16 @@ public class FormApiDelegateImplIT {
*/
@BeforeEach
public void initTest() {
// TODO KRO : Corriger les tests suite à la modification du modèle de données des écrans.
restFormMockMvc = webAppContextSetup(webApplicationContext).build();
form = new Form().name(FIRST_FORM_TITLE).reference("form").description("description").publicationState(FormPublicationState.PUBLISHED);
Set<Screen> screenList = new HashSet<>();
firstScreen = new Screen().name(FIRST_SCREEN_TITLE).reference(FIRST_SCREEN_REFERENCE).description("first screen of the form").defaultNextScreenReference("").items("[]").form(form);
secondScreen = new Screen().name(SECOND_SCREEN_TITLE).reference(SECOND_SCREEN_REFERENCE).description("second screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(firstScreen);
thirdScreen = new Screen().name(THIRD_SCREEN_TITLE).reference(THIRD_SCREEN_REFERENCE).description("third screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(secondScreen);
// firstScreen.setNext(secondScreen);
// secondScreen.setNext(thirdScreen);
firstScreen = new Screen().name(FIRST_SCREEN_TITLE).nameBo(FIRST_SCREEN_TITLE).index(1).reference(FIRST_SCREEN_REFERENCE).description("first screen of the form").defaultNextScreenReference("").items("[]").form(form);
secondScreen = new Screen().name(SECOND_SCREEN_TITLE).nameBo(SECOND_SCREEN_TITLE).index(2).reference(SECOND_SCREEN_REFERENCE).description("second screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(firstScreen);
thirdScreen = new Screen().name(THIRD_SCREEN_TITLE).nameBo(THIRD_SCREEN_TITLE).index(3).reference(THIRD_SCREEN_REFERENCE).description("third screen of the form").defaultNextScreenReference("").items("[]").form(form);
// .previous(secondScreen);
// firstScreen.setNext(secondScreen);
// secondScreen.setNext(thirdScreen);
screenList.add(firstScreen);
screenList.add(secondScreen);
screenList.add(thirdScreen);
......@@ -242,7 +241,7 @@ public class FormApiDelegateImplIT {
Answer answerScreen1 = new Answer().sessionId(session.getId()).answers("[]").screen(firstScreen);
answerRepository.save(answerScreen1);
// @formatter:off
restFormMockMvc.perform(post("/api/form/form/" + session.getId() + "/first/next")
restFormMockMvc.perform(post("/api/form/{formReference}/{sessionId}/first/next",form.getReference(),session.getId())
.session(session)
.contentType(MediaType.APPLICATION_JSON)
.content("[]"))
......@@ -369,7 +368,7 @@ public class FormApiDelegateImplIT {
@Test
@Transactional
public void next_required_field() throws Exception {
// firstScreen.next(secondScreen);
// firstScreen.next(secondScreen);
screenRepository.save(firstScreen);
firstScreen.setItems("[ { \"reference\": \"souhait_parcours\", \"type\": \"radio\", \"required\": true, \"question\": \"Quel sera votre voeu d'orientation sur Parcoursup ?\", \"choices\": [ { \"label\": \"L1 STAPS\", \"value\": \"A1\" }, { \"label\": \"L1 STAPS Parcours Santé\", \"value\": \"A2\" }, { \"label\": \"Les 2 parcours\", \"value\": \"A3\" } ] } ]");
Answer answerScreen1 = new Answer().sessionId(session.getId()).answers("[]").screen(firstScreen);
......
......@@ -16,6 +16,7 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;
import com.unantes.orientactive.OrientactiveApp;
import com.unantes.orientactive.domain.Form;
import com.unantes.orientactive.domain.NextScreenExpression;
import com.unantes.orientactive.domain.Screen;
import com.unantes.orientactive.repository.ScreenRepository;
......@@ -92,6 +93,8 @@ public class ScreenResourceIT {
private Screen screen;
private Form form;
/**
* Create an entity for this test.
*
......@@ -99,6 +102,12 @@ public class ScreenResourceIT {
* if they test an entity which requires the current entity.
*/
public Screen createEntity(EntityManager em) {
form = new Form();
form.setName("form");
form.setReference("formreference");
em.persist(form);
NextScreenExpression nextScreenExpression1 = new NextScreenExpression();
nextScreenExpression1.setNextScreenReference(DEFAULT_NEXT_SCREEN_REFERENCE_01);
nextScreenExpression1.setExpression(DEFAULT_NEXT_SCREEN_EXPRESSION_01);
......@@ -108,6 +117,9 @@ public class ScreenResourceIT {
nextScreenExpressions = List.of(nextScreenExpression1, nextScreenExpression2);
Screen screen = new Screen()
.name(DEFAULT_NAME)
.nameBo(DEFAULT_NAME)
.index(1)
.form(form)
.reference(DEFAULT_REFERENCE)
.description(DEFAULT_DESCRIPTION)
.defaultNextScreenReference(DEFAULT_NEXT_SCREEN_REFERENCE)
......@@ -216,20 +228,13 @@ public class ScreenResourceIT {
@Test
@Transactional
public void getAllScreens() throws Exception {
public void getFormScreens() throws Exception {
// Initialize the database
screenRepository.saveAndFlush(screen);
// Get all the screenList
restScreenMockMvc.perform(get("/api/screens?sort=id,desc"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(screen.getId().intValue())))
.andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
.andExpect(jsonPath("$.[*].reference").value(hasItem(DEFAULT_REFERENCE)))
.andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION.toString())))
.andExpect(jsonPath("$.[*].defaultNextScreenReference").value(hasItem(DEFAULT_NEXT_SCREEN_REFERENCE.toString())))
.andExpect(jsonPath("$.[*].items").value(hasItem(DEFAULT_ITEMS.toString())));
restScreenMockMvc.perform(get("/api/forms/{formId}/screens",form.getId()))
.andExpect(status().isForbidden());
}
@Test
......
......@@ -53,6 +53,8 @@ public class ScreenResourcePermissionIT extends AbstractPermissionTest {
Screen screen = new Screen();
screen.setName("screenname");
screen.setNameBo("bo name");
screen.setIndex(1);
screen.setReference("screenreference");
screen.setItems("[]");
screen.setForm(form);
......
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