-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSurveyUnitDocumentMapperImplTest.java
More file actions
301 lines (250 loc) · 15.3 KB
/
Copy pathSurveyUnitDocumentMapperImplTest.java
File metadata and controls
301 lines (250 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
package fr.insee.genesis.infrastructure.mapper;
import fr.insee.genesis.domain.model.surveyunit.DataState;
import fr.insee.genesis.domain.model.surveyunit.Mode;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.model.surveyunit.VariableModel;
import fr.insee.genesis.infrastructure.document.surveyunit.SurveyUnitDocument;
import fr.insee.genesis.infrastructure.document.surveyunit.VariableDocument;
import fr.insee.genesis.infrastructure.mappers.SurveyUnitDocumentMapper;
import fr.insee.genesis.infrastructure.mappers.SurveyUnitDocumentMapperImpl;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
@SuppressWarnings("deprecation") //Useless warnings, we test deprecated formats
class SurveyUnitDocumentMapperImplTest {
//Given
//Test constants
//Current format
public static final String COLLECTION_INSTRUMENT_ID = "TESTCOLLECTIONINSTRUMENTID";
public static final String MODE = "WEB";
public static final String USUAL_SURVEY_UNIT_ID = "TESTUSUALSURVEYUNITID";
public static final String INTERROGATION_ID = "TESTINTERROGATIONID";
public static final String VAR_ID = "TESTVARID";
//Deprecated format
public static final String ID_UE = "TESTIDUE";
public static final String QUESTIONNAIRE_ID = "TESTQUESTIONNAIREID";
static SurveyUnitDocumentMapper surveyUnitDocumentMapperImplStatic;
static SurveyUnitDocument surveyUnitDocumentStatic;
static SurveyUnitDocument deprecatedSurveyUnitDocumentStatic;
static SurveyUnitModel surveyUnitStatic;
@BeforeAll
static void init(){
surveyUnitDocumentMapperImplStatic = new SurveyUnitDocumentMapperImpl();
//Current format document
surveyUnitDocumentStatic = new SurveyUnitDocument();
surveyUnitDocumentStatic.setCollectionInstrumentId(COLLECTION_INSTRUMENT_ID);
surveyUnitDocumentStatic.setMode(MODE);
surveyUnitDocumentStatic.setUsualSurveyUnitId(USUAL_SURVEY_UNIT_ID);
surveyUnitDocumentStatic.setInterrogationId(INTERROGATION_ID);
surveyUnitDocumentStatic.setState("COLLECTED");
LocalDateTime rawRecordDate = LocalDateTime.of(2023, 1, 1, 0, 0, 0);
surveyUnitDocumentStatic.setRawRecordDate(rawRecordDate);
List<VariableDocument> documentExternalVariableList = new ArrayList<>();
VariableDocument externalVariable = new VariableDocument();
externalVariable.setVarId(VAR_ID);
externalVariable.setValue("V1");
documentExternalVariableList.add(externalVariable);
surveyUnitDocumentStatic.setExternalVariables(documentExternalVariableList);
List<VariableDocument> documentCollectedVariableList = new ArrayList<>();
VariableDocument variableDocument = new VariableDocument();
variableDocument.setVarId(VAR_ID);
variableDocument.setValue("V1");
documentCollectedVariableList.add(variableDocument);
surveyUnitDocumentStatic.setCollectedVariables(documentCollectedVariableList);
List<VariableModel> externalVariableModelList = new ArrayList<>();
VariableModel variable =
VariableModel.builder().varId(VAR_ID).value("V1").build();
externalVariableModelList.add(variable);
List<VariableModel> collectedVariableList = new ArrayList<>();
VariableModel collectedVariable = VariableModel.builder()
.varId(VAR_ID)
.value("V1")
.scope("TESTSCOPE")
.parentId("TESTPARENTID")
.iteration(1)
.build();
collectedVariableList.add(collectedVariable);
//TODO deprecated document
deprecatedSurveyUnitDocumentStatic = new SurveyUnitDocument();
deprecatedSurveyUnitDocumentStatic.setQuestionnaireId(QUESTIONNAIRE_ID);
deprecatedSurveyUnitDocumentStatic.setMode(MODE);
deprecatedSurveyUnitDocumentStatic.setIdUE(ID_UE);
deprecatedSurveyUnitDocumentStatic.setInterrogationId(INTERROGATION_ID);
deprecatedSurveyUnitDocumentStatic.setState("COLLECTED");
deprecatedSurveyUnitDocumentStatic.setRawRecordDate(rawRecordDate);
documentExternalVariableList = new ArrayList<>();
externalVariable = new VariableDocument();
externalVariable.setVarId(VAR_ID);
externalVariable.setValue("V1");
documentExternalVariableList.add(externalVariable);
deprecatedSurveyUnitDocumentStatic.setExternalVariables(documentExternalVariableList);
documentCollectedVariableList = new ArrayList<>();
variableDocument = new VariableDocument();
variableDocument.setVarId(VAR_ID);
variableDocument.setValue("V1");
documentCollectedVariableList.add(variableDocument);
deprecatedSurveyUnitDocumentStatic.setCollectedVariables(documentCollectedVariableList);
externalVariableModelList = new ArrayList<>();
variable = VariableModel.builder().varId(VAR_ID).value("V1").build();
externalVariableModelList.add(variable);
collectedVariableList = new ArrayList<>();
collectedVariable = VariableModel.builder()
.varId(VAR_ID)
.value("V1")
.scope("TESTSCOPE")
.parentId("TESTPARENTID")
.iteration(1)
.build();
collectedVariableList.add(collectedVariable);
//Current format model
surveyUnitStatic = SurveyUnitModel.builder()
.mode(Mode.WEB)
.interrogationId(INTERROGATION_ID)
.collectionInstrumentId(COLLECTION_INSTRUMENT_ID)
.state(DataState.COLLECTED)
.fileDate(rawRecordDate)
.rawRecordDate(rawRecordDate)
.recordDate(LocalDateTime.of(2024,1,1,0,0,0).toInstant(ZoneOffset.UTC))
.externalVariables(externalVariableModelList)
.collectedVariables(collectedVariableList)
.build();
}
//When + Then
@Test
@DisplayName("Should return null if null parameter")
void shouldReturnNull(){
Assertions.assertThat(surveyUnitDocumentMapperImplStatic.documentToModel(null)).isNull();
Assertions.assertThat(surveyUnitDocumentMapperImplStatic.modelToDocument(null)).isNull();
Assertions.assertThat(surveyUnitDocumentMapperImplStatic.listDocumentToListModel(null)).isNull();
Assertions.assertThat(surveyUnitDocumentMapperImplStatic.listModelToListDocument(null)).isNull();
}
@Test
@DisplayName("Should convert survey unit document to model")
void shouldReturnModelFromDocument(){
SurveyUnitModel surveyUnit = surveyUnitDocumentMapperImplStatic.documentToModel(surveyUnitDocumentStatic);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnit.getMode()).isEqualTo(Mode.WEB);
Assertions.assertThat(surveyUnit.getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnit.getCollectionInstrumentId()).isEqualTo(COLLECTION_INSTRUMENT_ID);
Assertions.assertThat(surveyUnit.getUsualSurveyUnitId()).isEqualTo(USUAL_SURVEY_UNIT_ID);
Assertions.assertThat(surveyUnit.getState()).isEqualTo(DataState.COLLECTED);
Assertions.assertThat(surveyUnit.getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnit.getExternalVariables()).filteredOn(externalVariableModel ->
externalVariableModel.varId().equals(VAR_ID)
&& externalVariableModel.value().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnit.getCollectedVariables()).filteredOn(variableModel ->
variableModel.varId().equals(VAR_ID)
&& variableModel.value().equals("V1")
).isNotEmpty();
}
@Test
@DisplayName("Should convert deprecated survey unit document to model")
void shouldReturnModelFromDeprecatedDocument(){
SurveyUnitModel surveyUnit = surveyUnitDocumentMapperImplStatic.documentToModel(deprecatedSurveyUnitDocumentStatic);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnit.getMode()).isEqualTo(Mode.WEB);
Assertions.assertThat(surveyUnit.getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnit.getCollectionInstrumentId()).isEqualTo(QUESTIONNAIRE_ID);
Assertions.assertThat(surveyUnit.getUsualSurveyUnitId()).isEqualTo(ID_UE);
Assertions.assertThat(surveyUnit.getState()).isEqualTo(DataState.COLLECTED);
Assertions.assertThat(surveyUnit.getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnit.getExternalVariables()).filteredOn(externalVariableModel ->
externalVariableModel.varId().equals(VAR_ID)
&& externalVariableModel.value().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnit.getCollectedVariables()).filteredOn(variableModel ->
variableModel.varId().equals(VAR_ID)
&& variableModel.value().equals("V1")
).isNotEmpty();
}
@Test
@DisplayName("Should convert survey unit model to document")
void shouldReturnDocumentFromModel(){
SurveyUnitDocument surveyUnitDocument = surveyUnitDocumentMapperImplStatic.modelToDocument(surveyUnitStatic);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnitDocument.getMode()).isEqualTo(MODE);
Assertions.assertThat(surveyUnitDocument.getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnitDocument.getCollectionInstrumentId()).isEqualTo(COLLECTION_INSTRUMENT_ID);
Assertions.assertThat(surveyUnitDocument.getState()).isEqualTo("COLLECTED");
Assertions.assertThat(surveyUnitDocument.getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnitDocument.getExternalVariables()).filteredOn(externalVariableDocument ->
externalVariableDocument.getVarId().equals(VAR_ID)
&& externalVariableDocument.getValue().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnitDocument.getCollectedVariables()).filteredOn(variableDocument ->
variableDocument.getVarId().equals(VAR_ID)
&& variableDocument.getValue().equals("V1")
).isNotEmpty();
}
@Test
@DisplayName("Should convert survey unit document list to model list")
void shouldReturnModelListFromDocumentList(){
List<SurveyUnitDocument> surveyUnitDocumentList = new ArrayList<>();
surveyUnitDocumentList.add(surveyUnitDocumentStatic);
List<SurveyUnitModel> surveyUnitList = surveyUnitDocumentMapperImplStatic.listDocumentToListModel(surveyUnitDocumentList);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnitList.getFirst().getMode()).isEqualTo(Mode.WEB);
Assertions.assertThat(surveyUnitList.getFirst().getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnitList.getFirst().getCollectionInstrumentId()).isEqualTo(COLLECTION_INSTRUMENT_ID);
Assertions.assertThat(surveyUnitList.getFirst().getUsualSurveyUnitId()).isEqualTo(USUAL_SURVEY_UNIT_ID);
Assertions.assertThat(surveyUnitList.getFirst().getState()).isEqualTo(DataState.COLLECTED);
Assertions.assertThat(surveyUnitList.getFirst().getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnitList.getFirst().getExternalVariables()).filteredOn(externalVariableModel ->
externalVariableModel.varId().equals(VAR_ID)
&& externalVariableModel.value().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnitList.getFirst().getCollectedVariables()).filteredOn(variableModel ->
variableModel.varId().equals(VAR_ID)
&& variableModel.value().equals("V1")
).isNotEmpty();
}
@Test
@DisplayName("Should convert deprecated survey unit document list to model list")
void shouldReturnModelListFromDeprecatedDocumentList(){
List<SurveyUnitDocument> surveyUnitDocumentList = new ArrayList<>();
surveyUnitDocumentList.add(deprecatedSurveyUnitDocumentStatic);
List<SurveyUnitModel> surveyUnitList = surveyUnitDocumentMapperImplStatic.listDocumentToListModel(surveyUnitDocumentList);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnitList.getFirst().getMode()).isEqualTo(Mode.WEB);
Assertions.assertThat(surveyUnitList.getFirst().getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnitList.getFirst().getCollectionInstrumentId()).isEqualTo(QUESTIONNAIRE_ID);
Assertions.assertThat(surveyUnitList.getFirst().getUsualSurveyUnitId()).isEqualTo(ID_UE);
Assertions.assertThat(surveyUnitList.getFirst().getState()).isEqualTo(DataState.COLLECTED);
Assertions.assertThat(surveyUnitList.getFirst().getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnitList.getFirst().getExternalVariables()).filteredOn(externalVariableModel ->
externalVariableModel.varId().equals(VAR_ID)
&& externalVariableModel.value().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnitList.getFirst().getCollectedVariables()).filteredOn(variableModel ->
variableModel.varId().equals(VAR_ID)
&& variableModel.value().equals("V1")
).isNotEmpty();
}
@Test
@DisplayName("Should convert survey unit model list to document list")
void shouldReturnDocumentListFromModelList(){
List<SurveyUnitModel> surveyUnitList = new ArrayList<>();
surveyUnitList.add(surveyUnitStatic);
List<SurveyUnitDocument> surveyUnitDocumentList = surveyUnitDocumentMapperImplStatic.listModelToListDocument(surveyUnitList);
LocalDateTime rawRecordDate = LocalDateTime.of(2023,1,1,0,0,0);
Assertions.assertThat(surveyUnitDocumentList.getFirst().getMode()).isEqualTo(MODE);
Assertions.assertThat(surveyUnitDocumentList.getFirst().getInterrogationId()).isEqualTo(INTERROGATION_ID);
Assertions.assertThat(surveyUnitDocumentList.getFirst().getCollectionInstrumentId()).isEqualTo(COLLECTION_INSTRUMENT_ID);
Assertions.assertThat(surveyUnitDocumentList.getFirst().getState()).isEqualTo("COLLECTED");
Assertions.assertThat(surveyUnitDocumentList.getFirst().getRawRecordDate()).isEqualTo(rawRecordDate);
Assertions.assertThat(surveyUnitDocumentList.getFirst().getExternalVariables()).filteredOn(externalVariableDocument ->
externalVariableDocument.getVarId().equals(VAR_ID)
&& externalVariableDocument.getValue().equals("V1")
).isNotEmpty();
Assertions.assertThat(surveyUnitDocumentList.getFirst().getCollectedVariables()).filteredOn(variableDocument ->
variableDocument.getVarId().equals(VAR_ID)
&& variableDocument.getValue().equals("V1")
).isNotEmpty();
}
}