Skip to content

Commit d85acba

Browse files
Merge pull request #379 from InseeFr/fixDataProcessingContextId
fix: get context by collectionInstrumentId
2 parents 6ed056b + 7c5066e commit d85acba

File tree

5 files changed

+161
-35
lines changed

5 files changed

+161
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# Changelog
2+
## 2.1.0 [TODO]
3+
### Fixed
4+
- Fixed withReview still based on campaignId
5+
26
## 2.0.4 [2026-01-13]
37
### Added
48
- Debug endpoints (Get raw data by collectionInstrumentId, delete last execution)

src/main/java/fr/insee/genesis/domain/service/context/DataProcessingContextService.java

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -233,42 +233,25 @@ public long countSchedules() {
233233
public DataProcessingContextModel getContext(String interrogationId) throws GenesisException {
234234
List<SurveyUnitModel> surveyUnitModels = surveyUnitPersistencePort.findByInterrogationId(interrogationId);
235235
if(surveyUnitModels.isEmpty()){
236-
throw new GenesisException(404,"No interrogation in database with id %s".formatted(interrogationId));
236+
throw new GenesisException(404, "No interrogation in database with id %s".formatted(interrogationId));
237237
}
238-
Set<String> partitionIds = new HashSet<>();
239-
Set<String> campaignIds = new HashSet<>();
240238
Set<String> collectionInstrumentIds = new HashSet<>();
241239

242240
for (SurveyUnitModel su : surveyUnitModels){
243-
if (su.getCampaignId()!=null){
244-
campaignIds.add(su.getCampaignId());
245-
}
246-
if (su.getCollectionInstrumentId()!=null){
241+
if (su.getCollectionInstrumentId() != null){
247242
collectionInstrumentIds.add(su.getCollectionInstrumentId());
248243
}
249244
}
250-
if(campaignIds.size() > 1 || collectionInstrumentIds.size()>1){
251-
throw new GenesisException(500,"Multiple partitions for interrogation %s".formatted(interrogationId));
252-
}
253-
254-
if(campaignIds.isEmpty() && collectionInstrumentIds.isEmpty()){
255-
return null;
256-
}
257-
258245

259-
DataProcessingContextModel contextModel = new DataProcessingContextModel();
260-
if (!campaignIds.isEmpty()){
261-
contextModel = DataProcessingContextMapper.INSTANCE.documentToModel(
262-
dataProcessingContextPersistancePort.findByPartitionId(campaignIds.stream().toList().getFirst())
263-
);
246+
if(collectionInstrumentIds.size() > 1){
247+
throw new GenesisException(500,"Multiple collection instruments for interrogation %s".formatted(interrogationId));
264248
}
265249

266-
if (contextModel.getPartitionId()==null && !collectionInstrumentIds.isEmpty()) {
267-
contextModel = DataProcessingContextMapper.INSTANCE.documentToModel(
268-
dataProcessingContextPersistancePort.findByPartitionId(collectionInstrumentIds.stream().toList().getFirst()));
250+
if(collectionInstrumentIds.isEmpty()){
251+
throw new GenesisException(404,"No collection instrument found for interrogation %s".formatted(interrogationId));
269252
}
270253

271-
return contextModel;
254+
return dataProcessingContextPersistancePort.findByCollectionInstrumentId(collectionInstrumentIds.stream().toList().getFirst());
272255
}
273256

274257
@Override

src/test/java/fr/insee/genesis/controller/rest/responses/ResponseControllerTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ void getLatestByStatesSurveyDataTest() throws GenesisException {
229229

230230
DataProcessingContextDocument doc = new DataProcessingContextDocument();
231231
doc.setPartitionId("TEST-TABLEAUX");
232+
doc.setCollectionInstrumentId(DEFAULT_COLLECTION_INSTRUMENT_ID);
232233
doc.setKraftwerkExecutionScheduleList(new ArrayList<>());
233234
doc.setWithReview(true);
234235
dataProcessingContextPersistancePortStub.getMongoStub().add(doc);
@@ -324,6 +325,7 @@ void getLatestByStatesSurveyDataTest_invalid_collected() throws GenesisException
324325

325326
DataProcessingContextDocument doc = new DataProcessingContextDocument();
326327
doc.setPartitionId("TEST-TABLEAUX");
328+
doc.setCollectionInstrumentId(DEFAULT_COLLECTION_INSTRUMENT_ID);
327329
doc.setKraftwerkExecutionScheduleList(new ArrayList<>());
328330
doc.setWithReview(true);
329331
dataProcessingContextPersistancePortStub.getMongoStub().add(doc);

src/test/java/fr/insee/genesis/domain/service/context/DataProcessingContextServiceTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ void reset(){
5151
));
5252
DataProcessingContextDocument doc = new DataProcessingContextDocument();
5353
doc.setPartitionId("TEST");
54+
doc.setCollectionInstrumentId("TEST");
5455
doc.setKraftwerkExecutionScheduleList(kraftwerkExecutionScheduleList);
5556
doc.setWithReview(false);
5657
dataProcessingContextPersistencePortStub.getMongoStub().add(doc);
@@ -129,7 +130,7 @@ void updateLastExecutionName_test() throws GenesisException {
129130
LocalDateTime localDateTime = LocalDateTime.now();
130131

131132
//When
132-
dataProcessingContextService.updateLastExecutionDate("TEST", localDateTime);
133+
dataProcessingContextService.updateLastExecutionDateByCollectionInstrumentId("TEST", localDateTime);
133134

134135
//Then
135136
Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub()).hasSize(1);
@@ -171,7 +172,7 @@ void removeExpiredSchedules_test_delete_document() throws GenesisException {
171172
null
172173
));
173174
DataProcessingContextDocument doc = new DataProcessingContextDocument();
174-
doc.setPartitionId("TEST2");
175+
doc.setCollectionInstrumentId("TEST2");
175176
doc.setKraftwerkExecutionScheduleList(kraftwerkExecutionScheduleList);
176177
doc.setWithReview(false);
177178
dataProcessingContextPersistencePortStub.getMongoStub().add(doc);
@@ -183,23 +184,23 @@ void removeExpiredSchedules_test_delete_document() throws GenesisException {
183184
//Survey schedule document deleted
184185
Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub()).hasSize(2);
185186
Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub().stream().filter(
186-
scheduleDocument -> scheduleDocument.getPartitionId().equals("TEST2")
187+
scheduleDocument -> scheduleDocument.getCollectionInstrumentId().equals("TEST2")
187188
).toList()).hasSize(1);
188189
Assertions.assertThat(dataProcessingContextPersistencePortStub.getMongoStub().stream().filter(
189-
scheduleDocument -> scheduleDocument.getPartitionId().equals("TEST2")
190+
scheduleDocument -> scheduleDocument.getCollectionInstrumentId().equals("TEST2")
190191
).toList().getFirst().getKraftwerkExecutionScheduleList()).isEmpty();
191192

192193
}
193194

194195
@Test
195-
void getContext_shouldThrow500IfMultiplePartitions() {
196+
void getContext_shouldThrow500IfMultipleCollectionInstruments() {
196197
// Given
197198
SurveyUnitModel su1 = SurveyUnitModel.builder()
198-
.campaignId("CAMPAIGN1")
199+
.collectionInstrumentId("CAMPAIGN1")
199200
.interrogationId("00001")
200201
.build();
201202
SurveyUnitModel su2 = SurveyUnitModel.builder()
202-
.campaignId("CAMPAIGN2")
203+
.collectionInstrumentId("CAMPAIGN2")
203204
.interrogationId("00001")
204205
.build();
205206
List<SurveyUnitModel> sus = new ArrayList<>();
@@ -212,7 +213,7 @@ void getContext_shouldThrow500IfMultiplePartitions() {
212213
//To ensure test is portable on Unix/Linux/macOS and windows systems
213214
String normalizedMessage = ex.getMessage().replaceAll("\\r?\\n", "");
214215
Assertions.assertThat(ex.getStatus()).isEqualTo(500);
215-
Assertions.assertThat(normalizedMessage).isEqualTo("Multiple partitions for interrogation 00001");
216+
Assertions.assertThat(normalizedMessage).isEqualTo("Multiple collection instruments for interrogation 00001");
216217
}
217218

218219
@Test
@@ -224,10 +225,10 @@ void getContext_shouldThrow404IfNoInterrogations() {
224225
}
225226

226227
@Test
227-
void getContext_shouldReturnContextIfOnePartition() throws GenesisException {
228+
void getContext_shouldReturnContextIfOneCollectionInstrument() throws GenesisException {
228229
// Given
229230
SurveyUnitModel su1 = SurveyUnitModel.builder()
230-
.campaignId("TEST")
231+
.collectionInstrumentId("TEST")
231232
.interrogationId("00001")
232233
.build();
233234
List<SurveyUnitModel> sus = new ArrayList<>();
@@ -237,7 +238,7 @@ void getContext_shouldReturnContextIfOnePartition() throws GenesisException {
237238

238239
// When & Then
239240
Assertions.assertThat(result).isNotNull();
240-
Assertions.assertThat(result.getPartitionId()).isEqualTo("TEST");
241+
Assertions.assertThat(result.getCollectionInstrumentId()).isEqualTo("TEST");
241242
Assertions.assertThat(result.isWithReview()).isFalse();
242243
}
243244

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
package fr.insee.genesis.domain.service.context;
2+
3+
import fr.insee.genesis.TestConstants;
4+
import fr.insee.genesis.domain.model.context.DataProcessingContextModel;
5+
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
6+
import fr.insee.genesis.domain.ports.spi.DataProcessingContextPersistancePort;
7+
import fr.insee.genesis.domain.ports.spi.SurveyUnitPersistencePort;
8+
import fr.insee.genesis.exceptions.GenesisException;
9+
import lombok.SneakyThrows;
10+
import org.assertj.core.api.Assertions;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.Test;
13+
14+
import java.util.ArrayList;
15+
import java.util.Collections;
16+
import java.util.List;
17+
18+
import static org.mockito.ArgumentMatchers.any;
19+
import static org.mockito.Mockito.doReturn;
20+
import static org.mockito.Mockito.mock;
21+
22+
class DataProcessingContextServiceUnitTest {
23+
24+
static DataProcessingContextService dataProcessingContextService;
25+
static DataProcessingContextPersistancePort dataProcessingContextPersistancePort;
26+
static SurveyUnitPersistencePort surveyUnitPersistencePort;
27+
28+
@BeforeEach
29+
void setUp() {
30+
dataProcessingContextPersistancePort = mock(DataProcessingContextPersistancePort.class);
31+
surveyUnitPersistencePort = mock(SurveyUnitPersistencePort.class);
32+
dataProcessingContextService = new DataProcessingContextService(
33+
dataProcessingContextPersistancePort,
34+
surveyUnitPersistencePort
35+
);
36+
}
37+
38+
@Test
39+
@SneakyThrows
40+
void getContext_test() {
41+
//GIVEN
42+
String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID;
43+
String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID;
44+
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
45+
.interrogationId(interrogationId)
46+
.collectionInstrumentId(collectionInstrumentId)
47+
.build();
48+
doReturn(Collections.singletonList(surveyUnitModel))
49+
.when(surveyUnitPersistencePort)
50+
.findByInterrogationId(any());
51+
DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder()
52+
.collectionInstrumentId(collectionInstrumentId)
53+
.withReview(true)
54+
.build();
55+
doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any());
56+
57+
//WHEN
58+
DataProcessingContextModel returnedContext =
59+
dataProcessingContextService.getContext(interrogationId);
60+
61+
//THEN
62+
Assertions.assertThat(returnedContext.getCollectionInstrumentId()).isEqualTo(collectionInstrumentId);
63+
Assertions.assertThat(returnedContext.isWithReview()).isTrue();
64+
}
65+
66+
@Test
67+
@SneakyThrows
68+
void getContext_no_surveyUnit_exception_test() {
69+
//GIVEN
70+
doReturn(new ArrayList<>())
71+
.when(surveyUnitPersistencePort)
72+
.findByInterrogationId(any());
73+
//WHEN + THEN
74+
Assertions.assertThatThrownBy(() ->
75+
dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID))
76+
.isInstanceOf(GenesisException.class);
77+
}
78+
79+
@Test
80+
@SneakyThrows
81+
void getContext_multiple_CollectionInstruementIds_test() {
82+
//GIVEN
83+
String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID;
84+
String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID;
85+
List<SurveyUnitModel> surveyUnitModelList = new ArrayList<>();
86+
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
87+
.interrogationId(interrogationId)
88+
.collectionInstrumentId(collectionInstrumentId)
89+
.build();
90+
surveyUnitModelList.add(surveyUnitModel);
91+
SurveyUnitModel surveyUnitModel2 = SurveyUnitModel.builder()
92+
.interrogationId(interrogationId)
93+
.collectionInstrumentId(collectionInstrumentId + "2")
94+
.build();
95+
surveyUnitModelList.add(surveyUnitModel2);
96+
doReturn(surveyUnitModelList)
97+
.when(surveyUnitPersistencePort)
98+
.findByInterrogationId(any());
99+
DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder()
100+
.collectionInstrumentId(collectionInstrumentId)
101+
.withReview(true)
102+
.build();
103+
doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any());
104+
105+
//WHEN + THEN
106+
Assertions.assertThatThrownBy(() ->
107+
dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID))
108+
.isInstanceOf(GenesisException.class);
109+
}
110+
111+
@Test
112+
@SneakyThrows
113+
void getContext_no_CollectionInstruementIds_test() {
114+
//GIVEN
115+
//GIVEN
116+
String collectionInstrumentId = TestConstants.DEFAULT_COLLECTION_INSTRUMENT_ID;
117+
String interrogationId = TestConstants.DEFAULT_INTERROGATION_ID;
118+
SurveyUnitModel surveyUnitModel = SurveyUnitModel.builder()
119+
.interrogationId(interrogationId)
120+
.collectionInstrumentId(null)
121+
.build();
122+
doReturn(Collections.singletonList(surveyUnitModel))
123+
.when(surveyUnitPersistencePort)
124+
.findByInterrogationId(any());
125+
DataProcessingContextModel dataProcessingContextModel = DataProcessingContextModel.builder()
126+
.collectionInstrumentId(collectionInstrumentId)
127+
.withReview(true)
128+
.build();
129+
doReturn(dataProcessingContextModel).when(dataProcessingContextPersistancePort).findByCollectionInstrumentId(any());
130+
131+
//WHEN + THEN
132+
Assertions.assertThatThrownBy(() ->
133+
dataProcessingContextService.getContext(TestConstants.DEFAULT_INTERROGATION_ID))
134+
.isInstanceOf(GenesisException.class);
135+
}
136+
}

0 commit comments

Comments
 (0)