Skip to content

Commit f0962b8

Browse files
committed
test: controller adapter tests
1 parent 0881302 commit f0962b8

2 files changed

Lines changed: 648 additions & 0 deletions

File tree

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
package fr.insee.genesis.controller.adapter;
2+
3+
import fr.insee.genesis.controller.sources.json.LunaticJsonSurveyUnit;
4+
import fr.insee.genesis.domain.model.surveyunit.DataState;
5+
import fr.insee.genesis.domain.model.surveyunit.Mode;
6+
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Nested;
9+
import org.junit.jupiter.api.Test;
10+
11+
import java.time.LocalDateTime;
12+
13+
import static org.assertj.core.api.Assertions.assertThat;
14+
15+
@DisplayName("LunaticJsonAdapter tests")
16+
class LunaticJsonAdapterTest {
17+
18+
private final LunaticJsonAdapter adapter = new LunaticJsonAdapter();
19+
20+
@Nested
21+
@DisplayName("convert() tests")
22+
class ConvertTests {
23+
24+
@Test
25+
@DisplayName("Should map questionnaireId to collectionInstrumentId")
26+
void convert_shouldMapQuestionnaireIdToCollectionInstrumentId() {
27+
// GIVEN
28+
LunaticJsonSurveyUnit su = buildSurveyUnit("questionnaire-123", "interrogation-456");
29+
30+
// WHEN
31+
SurveyUnitModel result = adapter.convert(su);
32+
33+
// THEN
34+
assertThat(result.getCollectionInstrumentId()).isEqualTo("questionnaire-123");
35+
}
36+
37+
@Test
38+
@DisplayName("Should map interrogationId")
39+
void convert_shouldMapInterrogationId() {
40+
// GIVEN
41+
LunaticJsonSurveyUnit su = buildSurveyUnit("questionnaire-123", "interrogation-456");
42+
43+
// WHEN
44+
SurveyUnitModel result = adapter.convert(su);
45+
46+
// THEN
47+
assertThat(result.getInterrogationId()).isEqualTo("interrogation-456");
48+
}
49+
50+
@Test
51+
@DisplayName("Should set state to COLLECTED")
52+
void convert_shouldSetStateToCollected() {
53+
// GIVEN
54+
LunaticJsonSurveyUnit su = buildSurveyUnit("q1", "i1");
55+
56+
// WHEN
57+
SurveyUnitModel result = adapter.convert(su);
58+
59+
// THEN
60+
assertThat(result.getState()).isEqualTo(DataState.COLLECTED);
61+
}
62+
63+
@Test
64+
@DisplayName("Should set mode to WEB")
65+
void convert_shouldSetModeToWeb() {
66+
// GIVEN
67+
LunaticJsonSurveyUnit su = buildSurveyUnit("q1", "i1");
68+
69+
// WHEN
70+
SurveyUnitModel result = adapter.convert(su);
71+
72+
// THEN
73+
assertThat(result.getMode()).isEqualTo(Mode.WEB);
74+
}
75+
76+
@Test
77+
@DisplayName("Should set campaignId to empty string")
78+
void convert_shouldSetCampaignIdToEmptyString() {
79+
// GIVEN
80+
LunaticJsonSurveyUnit su = buildSurveyUnit("q1", "i1");
81+
82+
// WHEN
83+
SurveyUnitModel result = adapter.convert(su);
84+
85+
// THEN
86+
assertThat(result.getCampaignId()).isEmpty();
87+
}
88+
89+
@Test
90+
@DisplayName("Should set a non-null recordDate close to now")
91+
void convert_shouldSetRecordDateCloseToNow() {
92+
// GIVEN
93+
LunaticJsonSurveyUnit su = buildSurveyUnit("q1", "i1");
94+
LocalDateTime before = LocalDateTime.now();
95+
96+
// WHEN
97+
SurveyUnitModel result = adapter.convert(su);
98+
99+
// THEN
100+
LocalDateTime after = LocalDateTime.now();
101+
assertThat(result.getRecordDate())
102+
.isNotNull()
103+
.isAfterOrEqualTo(before)
104+
.isBeforeOrEqualTo(after);
105+
}
106+
107+
@Test
108+
@DisplayName("Should return a non-null model")
109+
void convert_shouldReturnNonNullModel() {
110+
// GIVEN
111+
LunaticJsonSurveyUnit su = buildSurveyUnit("q1", "i1");
112+
113+
// WHEN
114+
SurveyUnitModel result = adapter.convert(su);
115+
116+
// THEN
117+
assertThat(result).isNotNull();
118+
}
119+
}
120+
121+
//UTILS
122+
private LunaticJsonSurveyUnit buildSurveyUnit(String questionnaireId, String interrogationId) {
123+
LunaticJsonSurveyUnit su = new LunaticJsonSurveyUnit();
124+
su.setQuestionnaireId(questionnaireId);
125+
su.setInterrogationId(interrogationId);
126+
return su;
127+
}
128+
}

0 commit comments

Comments
 (0)