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 69f69c7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 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);
}
}
}
22 changes: 20 additions & 2 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 @@ -95,6 +96,8 @@ public class ExploreTest {
@Autowired
private StudyService studyService;
@Autowired
private NetworkModificationService networkModificationService;
@Autowired
private CaseService caseService;
@Autowired
private RemoteServicesProperties remoteServicesProperties;
Expand All @@ -117,6 +120,7 @@ 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);
Expand All @@ -142,6 +146,7 @@ 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 modificationsIdsAsString = mapper.writeValueAsString(List.of(MODIFICATION_UUID));

final Dispatcher dispatcher = new Dispatcher() {
@SneakyThrows
Expand All @@ -168,10 +173,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 @@ -246,6 +251,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(modificationsIdsAsString).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 Down Expand Up @@ -642,4 +649,15 @@ 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());
}
}

0 comments on commit 69f69c7

Please sign in to comment.