-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathLunaticModelDefinitions.java
More file actions
209 lines (175 loc) · 9.16 KB
/
LunaticModelDefinitions.java
File metadata and controls
209 lines (175 loc) · 9.16 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
package cucumber.functional_tests;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import fr.insee.genesis.TestConstants;
import fr.insee.genesis.controller.rest.LunaticModelController;
import fr.insee.genesis.controller.rest.responses.QuestionnaireController;
import fr.insee.genesis.domain.model.surveyunit.SurveyUnitModel;
import fr.insee.genesis.domain.service.context.DataProcessingContextService;
import fr.insee.genesis.domain.service.lunaticmodel.LunaticModelService;
import fr.insee.genesis.domain.service.metadata.QuestionnaireMetadataService;
import fr.insee.genesis.domain.service.surveyunit.SurveyUnitService;
import fr.insee.genesis.domain.utils.JsonUtils;
import fr.insee.genesis.infrastructure.document.lunaticmodel.LunaticModelDocument;
import fr.insee.genesis.infrastructure.utils.FileUtils;
import fr.insee.genesis.stubs.ConfigStub;
import fr.insee.genesis.stubs.DataProcessingContextPersistancePortStub;
import fr.insee.genesis.stubs.LunaticModelPersistanceStub;
import fr.insee.genesis.stubs.QuestionnaireMetadataPersistencePortStub;
import fr.insee.genesis.stubs.SurveyUnitPersistencePortStub;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.Assertions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.resttestclient.TestRestTemplate;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Stream;
@Slf4j
public class LunaticModelDefinitions {
private static final String LUNATIC_FILE_PATTERN = "lunatic[\\w,\\s-]+\\.json";
LunaticModelPersistanceStub lunaticModelPersistanceStub = new LunaticModelPersistanceStub();
LunaticModelController lunaticModelController = new LunaticModelController(new LunaticModelService(lunaticModelPersistanceStub));
SurveyUnitPersistencePortStub surveyUnitPersistencePortStub = new SurveyUnitPersistencePortStub();
static QuestionnaireMetadataPersistencePortStub questionnaireMetadataPersistencePortStub = new QuestionnaireMetadataPersistencePortStub();
QuestionnaireController questionnaireController = new QuestionnaireController(
new SurveyUnitService(
surveyUnitPersistencePortStub,
new QuestionnaireMetadataService(questionnaireMetadataPersistencePortStub),
new FileUtils(new ConfigStub())
),
new DataProcessingContextService(
new DataProcessingContextPersistancePortStub(),
surveyUnitPersistencePortStub
)
);
private String baseUrl;
@LocalServerPort
int port;
@Autowired
TestRestTemplate rest;
//Test variables
private Path lunaticModelJsonPath;
private String lunaticModelSaveBody;
private ResponseEntity<String> lastResponse;
@Before
public void init(){
lunaticModelPersistanceStub.getMongoStub().clear();
baseUrl = "http://localhost:" + port + "/";
log.info("rest autowired : {}", rest.getRootUri());
}
//GIVENS
@Given("We have a lunatic model in database with questionnaire id {string} from the json of {string}")
public void load_lunatic_model_json(
String questionnaireId,
String campaignId
) throws Exception {
lunaticModelJsonPath = getLunaticJsonPath(campaignId);
LunaticModelDocument lunaticModelDocument = LunaticModelDocument.builder()
.questionnaireId(questionnaireId)
.lunaticModel(JsonUtils.jsonToMap(Files.readString(lunaticModelJsonPath)))
.build();
lunaticModelPersistanceStub.getMongoStub().add(lunaticModelDocument);
}
@Given("We have a lunatic model json file in spec folder {string}")
public void load_lunatic_model_json_file(String specFolderName) throws IOException {
lunaticModelJsonPath = getLunaticJsonPath(specFolderName);
}
@Given("We have a response in database with campaign id {string}, questionnaire id {string} and interrogation id " +
"{string}")
public void load_response(String campaignId, String questionnaireId, String interrogationId) {
surveyUnitPersistencePortStub.getMongoStub().add(
SurveyUnitModel.builder()
.campaignId(campaignId)
.collectionInstrumentId(questionnaireId)
.interrogationId(interrogationId)
.build()
);
}
//WHENS
@When("We save that lunatic model json file with questionnaire id {string}")
public void save_lunatic_model(String questionnaireId) throws Exception {
lunaticModelSaveBody = Files.readString(lunaticModelJsonPath);
lastResponse = lunaticModelController.saveRawResponsesFromJsonBody(
questionnaireId,
JsonUtils.jsonToMap(lunaticModelSaveBody)
);
}
@When("We try to save that lunatic model json file with questionnaire id {string} with Spring context")
public void save_lunatic_model_spring(String questionnaireId) throws IOException {
lunaticModelSaveBody = Files.readString(lunaticModelJsonPath);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("Authorization", "Bearer fake_token");
String url = String.format("%slunatic-model/save?questionnaireId=%s",
baseUrl,
questionnaireId
);
HttpEntity<String> requestEntity = new HttpEntity<>(lunaticModelSaveBody,headers);
lastResponse = rest.exchange(url, HttpMethod.PUT, requestEntity, String.class);
}
@When("We get lunatic model for questionnaire {string}")
public void get_lunatic_model(String questionnaireId) throws JsonProcessingException {
lastResponse = lunaticModelController.getLunaticModelFromQuestionnaireId(questionnaireId);
}
@When("We get questionnaire id for interrogation {string}")
public void get_questionnaire_id(String interrogationId) {
lastResponse = questionnaireController.getQuestionnaireByInterrogation(interrogationId);
}
//THENS
@Then("We should have that lunatic model as response")
public void check_lunatic_model() throws Exception {
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
JsonNode originalLunaticModel = objectMapper.readTree(Files.readString(lunaticModelJsonPath));
JsonNode gotLunaticModel = objectMapper.readTree(lastResponse.getBody());
Assertions.assertThat(originalLunaticModel).isEqualTo(gotLunaticModel);
}
@Then("We should have a document with id {string} and the contents from the body")
public void check_lunatic_model_document(String documentQuestionnaireId) throws Exception {
if(!lastResponse.getStatusCode().is2xxSuccessful()) {
log.error("Got error code {} with body {}",
lastResponse.getStatusCode().value(), lastResponse.getBody());
}
List<LunaticModelDocument> lunaticModelDocuments = lunaticModelPersistanceStub.getMongoStub().stream().filter(
document -> document.questionnaireId().equals(documentQuestionnaireId)
).toList();
Assertions.assertThat(lunaticModelDocuments).hasSize(1);
Assertions.assertThat(lunaticModelDocuments.getFirst().lunaticModel()).isEqualTo(JsonUtils.jsonToMap(lunaticModelSaveBody));
}
@Then("We should have {string} as response")
public void check_response(String expectedResponse) {
if(!lastResponse.getStatusCode().is2xxSuccessful()) {
log.error("Got error code {} with body {}",
lastResponse.getStatusCode().value(), lastResponse.getBody());
}
Assertions.assertThat(lastResponse.getBody()).isEqualTo(expectedResponse);
}
@Then("We should have a {int} error code")
public void check_error_code(int expectedErrorCode) {
Assertions.assertThat(lastResponse.getStatusCode().value()).isEqualTo(expectedErrorCode);
}
//UTILS
private Path getLunaticJsonPath(String campaignId) throws IOException {
Path campaignPath = Path.of(TestConstants.TEST_RESOURCES_DIRECTORY).resolve("specs").resolve(campaignId);
return lookForLunaticJsonPath(campaignPath);
}
private Path lookForLunaticJsonPath(Path inPath) throws IOException {
try (Stream<Path> files = Files.find(Path.of(String.valueOf(inPath)), 10,
(path, basicFileAttributes) -> path.toFile().getName().toLowerCase().matches(LUNATIC_FILE_PATTERN))) {
return files.findFirst()
.orElseThrow(() -> new RuntimeException("No file (%s) found in ".formatted(LUNATIC_FILE_PATTERN) + inPath));
}
}
}