Skip to content

Commit

Permalink
add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dbraquart committed Feb 8, 2024
1 parent 9419c0f commit fffd1eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ public void setDirectoryServerBaseUri(String directoryServerBaseUri) {
}

public ElementAttributes createElement(ElementAttributes elementAttributes, UUID directoryUuid, String userId) {
return createNewElement(elementAttributes, directoryUuid, userId, false);
return createElementWithNewName(elementAttributes, directoryUuid, userId, false);
}

public ElementAttributes createNewElement(ElementAttributes elementAttributes, UUID directoryUuid, String userId, boolean allowNewName) {
public ElementAttributes createElementWithNewName(ElementAttributes elementAttributes, UUID directoryUuid, String userId, boolean allowNewName) {
String path = UriComponentsBuilder
.fromPath(DIRECTORIES_SERVER_ROOT_PATH + "/{directoryUuid}/elements?allowNewName={allowNewName}")
.buildAndExpand(directoryUuid, allowNewName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ public void createNetworkModifications(List<ElementAttributes> modificationAttri
final ElementAttributes attributes = modificationAttributeIterator.next();
ElementAttributes elementAttributes = new ElementAttributes(newid, attributes.getElementName(), MODIFICATION,
null, userId, 0L, attributes.getDescription());
directoryService.createNewElement(elementAttributes, parentDirectoryUuid, userId, true);
directoryService.createElementWithNewName(elementAttributes, parentDirectoryUuid, userId, true);
}
}
}
61 changes: 50 additions & 11 deletions src/test/java/org/gridsuite/explore/server/ExploreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public class ExploreTest {
private static final UUID CONTINGENCY_LIST_UUID = UUID.randomUUID();
private static final UUID INVALID_ELEMENT_UUID = UUID.randomUUID();
private static final UUID PARAMETERS_UUID = UUID.randomUUID();
private static final UUID MODIFICATION_UUID = UUID.randomUUID();
private static final String STUDY_ERROR_NAME = "studyInError";
private static final String STUDY1 = "study1";
private static final String CASE1 = "case1";
Expand All @@ -81,6 +82,7 @@ public class ExploreTest {
private Map<String, Object> specificMetadata = new HashMap<>();
private Map<String, Object> specificMetadata2 = new HashMap<>();
private Map<String, Object> caseSpecificMetadata = new HashMap<>();
private Map<String, Object> modificationSpecificMetadata = new HashMap<>();

private static final UUID SCRIPT_ID_BASE_FORM_CONTINGENCY_LIST_UUID = UUID.randomUUID();

Expand All @@ -95,6 +97,8 @@ public class ExploreTest {
@Autowired
private StudyService studyService;
@Autowired
private NetworkModificationService networkModificationService;
@Autowired
private CaseService caseService;
@Autowired
private RemoteServicesProperties remoteServicesProperties;
Expand All @@ -105,8 +109,6 @@ public class ExploreTest {
@Before
public void setup() throws IOException {
server = new MockWebServer();

// Start the server.
server.start();

// Ask the server for its URL. You'll need this to make HTTP requests.
Expand All @@ -117,16 +119,14 @@ public void setup() throws IOException {
studyService.setStudyServerBaseUri(baseUrl);
filterService.setFilterServerBaseUri(baseUrl);
contingencyListService.setActionsServerBaseUri(baseUrl);
networkModificationService.setNetworkModificationServerBaseUri(baseUrl);
caseService.setBaseUri(baseUrl);
remoteServicesProperties.getServices().forEach(s -> s.setBaseUri(baseUrl));
specificMetadata.put("id", FILTER_UUID);

specificMetadata2.put("equipmentType", "LINE");
specificMetadata2.put("id", FILTER_UUID_2);

caseSpecificMetadata.put("uuid", CASE_UUID);
caseSpecificMetadata.put("name", TEST_FILE);
caseSpecificMetadata.put("format", "XIIDM");
specificMetadata = Map.of("id", FILTER_UUID);
specificMetadata2 = Map.of("equipmentType", "LINE", "id", FILTER_UUID_2);
caseSpecificMetadata = Map.of("uuid", CASE_UUID, "name", TEST_FILE, "format", "XIIDM");
modificationSpecificMetadata = Map.of("id", MODIFICATION_UUID, "type", "LOAD_MODIFICATION");

String privateStudyAttributesAsString = mapper.writeValueAsString(new ElementAttributes(PRIVATE_STUDY_UUID, STUDY1, "STUDY", new AccessRightsAttributes(true), USER1, 0, null));
String listOfPrivateStudyAttributesAsString = mapper.writeValueAsString(List.of(new ElementAttributes(PRIVATE_STUDY_UUID, STUDY1, "STUDY", new AccessRightsAttributes(true), USER1, 0, null)));
Expand All @@ -142,6 +142,9 @@ public void setup() throws IOException {
String parametersElementAttributesAsString = mapper.writeValueAsString(new ElementAttributes(PARAMETERS_UUID, "voltageInitParametersName", ParametersType.VOLTAGE_INIT_PARAMETERS.name(), new AccessRightsAttributes(true), USER1, 0, null));
String listElementsAttributesAsString = "[" + filterAttributesAsString + "," + privateStudyAttributesAsString + "," + formContingencyListAttributesAsString + "]";
String caseInfosAttributesAsString = mapper.writeValueAsString(List.of(caseSpecificMetadata));
String modificationElementAttributesAsString = mapper.writeValueAsString(new ElementAttributes(MODIFICATION_UUID, "one modif", "MODIFICATION", new AccessRightsAttributes(true), USER1, 0L, null));
String modificationInfosAttributesAsString = mapper.writeValueAsString(List.of(modificationSpecificMetadata));
String modificationIdsAsString = mapper.writeValueAsString(List.of(MODIFICATION_UUID));

final Dispatcher dispatcher = new Dispatcher() {
@SneakyThrows
Expand All @@ -168,10 +171,10 @@ public MockResponse dispatch(RecordedRequest request) {
} else {
return new MockResponse().setResponseCode(200);
}
} else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_UUID + "/elements\\?allowNewName=false") && "POST".equals(request.getMethod())) {
} else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_UUID + "/elements\\?allowNewName=.*") && "POST".equals(request.getMethod())) {
return new MockResponse().setBody(privateStudyAttributesAsString).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_WITH_ERROR_UUID + "/elements\\?allowNewName=false") && "POST".equals(request.getMethod())) {
} else if (path.matches("/v1/directories/" + PARENT_DIRECTORY_WITH_ERROR_UUID + "/elements\\?allowNewName=.*") && "POST".equals(request.getMethod())) {
return new MockResponse().setResponseCode(500);
} else if (path.matches("/v1/elements/" + CONTINGENCY_LIST_UUID) && "GET".equals(request.getMethod())) {
return new MockResponse().setBody(formContingencyListAttributesAsString).setResponseCode(200)
Expand Down Expand Up @@ -205,6 +208,10 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setBody("[" + caseElementAttributesAsString + "]")
.setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/elements\\?ids=" + MODIFICATION_UUID) && "GET".equals(request.getMethod())) {
return new MockResponse().setBody("[" + modificationElementAttributesAsString + "]")
.setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/filters/metadata\\?ids=" + FILTER_UUID + "," + FILTER_UUID_2) && "GET".equals(request.getMethod())) {
return new MockResponse().setBody("[" + mapper.writeValueAsString(specificMetadata) + "," + mapper.writeValueAsString(specificMetadata2) + "]")
.setResponseCode(200)
Expand Down Expand Up @@ -246,6 +253,8 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/parameters.*")) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/network-modifications/duplicate")) {
return new MockResponse().setBody(modificationIdsAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if ("GET".equals(request.getMethod())) {
if (path.matches("/v1/elements/" + INVALID_ELEMENT_UUID)) {
return new MockResponse().setBody(invalidElementAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
Expand All @@ -257,6 +266,8 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setBody(listOfFilterAttributesAsString.replace("elementUuid", "id")).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/cases/metadata[?]ids=" + CASE_UUID)) {
return new MockResponse().setBody(caseInfosAttributesAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/network-modifications/metadata[?]ids=" + MODIFICATION_UUID)) {
return new MockResponse().setBody(modificationInfosAttributesAsString).setResponseCode(200).addHeader("Content-Type", "application/json; charset=utf-8");
} else if (path.matches("/v1/studies/metadata[?]ids=" + PRIVATE_STUDY_UUID)) {
return new MockResponse().setBody(listOfPrivateStudyAttributesAsString.replace("elementUuid", "id")).setResponseCode(200)
.addHeader("Content-Type", "application/json; charset=utf-8");
Expand All @@ -280,6 +291,8 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/elements/" + PARAMETERS_UUID)) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/elements/" + MODIFICATION_UUID)) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/(cases|elements)/" + CASE_UUID)) {
return new MockResponse().setResponseCode(200);
} else if (path.matches("/v1/parameters/" + PARAMETERS_UUID)) {
Expand Down Expand Up @@ -460,6 +473,7 @@ public void testDeleteElement() throws Exception {
deleteElement(PARENT_DIRECTORY_UUID);
deleteElement(CASE_UUID);
deleteElement(PARAMETERS_UUID);
deleteElement(MODIFICATION_UUID);
}

@Test
Expand Down Expand Up @@ -642,4 +656,29 @@ public void testGetMetadata() throws Exception {
assertEquals(1, elementsMetadata.size());
assertEquals(mapper.writeValueAsString(elementsMetadata.get(0)), caseAttributesAsString);
}

@Test
@SneakyThrows
public void testcreateNetworkModifications() {
final String body = mapper.writeValueAsString(List.of(new ElementAttributes(MODIFICATION_UUID, "one modif", "", null, USER1, 0L, "a description")));
mockMvc.perform(post("/v1/explore/modifications?parentDirectoryUuid={parentDirectoryUuid}", PARENT_DIRECTORY_UUID)
.header("userId", USER1)
.contentType(MediaType.APPLICATION_JSON)
.content(body)
).andExpect(status().isOk());
}

@Test
@SneakyThrows
public void testGetModificationMetadata() {
final String expectedResult = mapper.writeValueAsString(new ElementAttributes(MODIFICATION_UUID, "one modif", "MODIFICATION", new AccessRightsAttributes(true), USER1, 0L, null, modificationSpecificMetadata));
MvcResult result = mockMvc.perform(get("/v1/explore/elements/metadata?ids=" + MODIFICATION_UUID)
.header("userId", USER1))
.andExpect(status().isOk())
.andReturn();
String response = result.getResponse().getContentAsString();
List<ElementAttributes> elementsMetadata = mapper.readValue(response, new TypeReference<>() { });
assertEquals(1, elementsMetadata.size());
assertEquals(mapper.writeValueAsString(elementsMetadata.get(0)), expectedResult);
}
}

0 comments on commit fffd1eb

Please sign in to comment.