Skip to content

Commit c7a7af7

Browse files
save the nad associated to the user's profile with the newly created study (#828)
* save the nad associated to the user's profile with the newly created study * enhance coverage for nad association with study * remove unused log, and respect to project indentation * removing the use of duplicateLayoutConfig after code review * clean code, renaming Signed-off-by: David BRAQUART <[email protected]> --------- Signed-off-by: David BRAQUART <[email protected]> Co-authored-by: dbraquart <[email protected]> Co-authored-by: David BRAQUART <[email protected]>
1 parent 74f3b13 commit c7a7af7

File tree

8 files changed

+177
-11
lines changed

8 files changed

+177
-11
lines changed

src/main/java/org/gridsuite/study/server/StudyException.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ public enum Type {
156156
UPDATE_SPREADSHEET_CONFIG_FAILED,
157157
NETWORK_EXPORT_FAILED,
158158
GET_LOADFLOW_PROVIDER_FAILED,
159+
CREATE_DIAGRAM_GRID_LAYOUT_FAILED,
160+
DUPLICATE_DIAGRAM_GRID_LAYOUT_FAILED,
159161
TOO_MANY_NAD_CONFIGS,
160162
TOO_MANY_MAP_CARDS
161163
}

src/main/java/org/gridsuite/study/server/controller/StudyController.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -217,10 +217,10 @@ public ResponseEntity<Void> updateRootNetwork(@PathVariable("studyUuid") UUID st
217217
}
218218

219219
@RequestMapping(method = RequestMethod.HEAD, value = "/studies/{studyUuid}/root-networks", params = {"name"})
220-
@Operation(summary = "Check if an element with this name and this type already exists in the given directory")
221-
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The element exists"),
222-
@ApiResponse(responseCode = "204", description = "The element doesn't exist")})
223-
public ResponseEntity<Void> elementExists(@PathVariable("studyUuid") UUID studyUuid,
220+
@Operation(summary = "Check if a root network already exists")
221+
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "The root network exists"),
222+
@ApiResponse(responseCode = "204", description = "The root network doesn't exist")})
223+
public ResponseEntity<Void> rootNetworkExists(@PathVariable("studyUuid") UUID studyUuid,
224224
@RequestParam("name") String rootNetworkName) {
225225
HttpStatus status = rootNetworkService.isRootNetworkNameExistsInStudy(studyUuid, rootNetworkName) ? HttpStatus.OK : HttpStatus.NO_CONTENT;
226226
return ResponseEntity.status(status).contentType(MediaType.APPLICATION_JSON).build();

src/main/java/org/gridsuite/study/server/dto/UserProfileInfos.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,6 @@ public class UserProfileInfos {
4343
private UUID spreadsheetConfigCollectionId;
4444

4545
private UUID networkVisualizationParameterId;
46+
47+
private UUID diagramConfigId;
4648
}

src/main/java/org/gridsuite/study/server/service/ConsumerService.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,14 +294,27 @@ private void insertStudy(UUID studyUuid, String userId, NetworkInfos networkInfo
294294
UUID dynamicSecurityAnalysisParametersUuid = createDefaultDynamicSecurityAnalysisParameters(userId, userProfileInfos);
295295
UUID stateEstimationParametersUuid = createDefaultStateEstimationParameters();
296296
UUID spreadsheetConfigCollectionUuid = createDefaultSpreadsheetConfigCollection(userId, userProfileInfos);
297+
UUID diagramGridLayoutUuid = createGridLayoutFromNadDiagram(userId, userProfileInfos);
297298

298299
studyService.insertStudy(studyUuid, userId, networkInfos, caseInfos, loadFlowParametersUuid,
299300
shortCircuitParametersUuid, DynamicSimulationService.toEntity(dynamicSimulationParameters, objectMapper),
300301
voltageInitParametersUuid, securityAnalysisParametersUuid, sensitivityAnalysisParametersUuid,
301-
networkVisualizationParametersUuid, dynamicSecurityAnalysisParametersUuid, stateEstimationParametersUuid, spreadsheetConfigCollectionUuid,
302+
networkVisualizationParametersUuid, dynamicSecurityAnalysisParametersUuid, stateEstimationParametersUuid, spreadsheetConfigCollectionUuid, diagramGridLayoutUuid,
302303
importParameters, importReportUuid);
303304
}
304305

306+
private UUID createGridLayoutFromNadDiagram(String userId, UserProfileInfos userProfileInfos) {
307+
if (userProfileInfos != null && userProfileInfos.getDiagramConfigId() != null) {
308+
try {
309+
return studyConfigService.createGridLayoutFromNadDiagram(userProfileInfos.getDiagramConfigId());
310+
} catch (Exception e) {
311+
LOGGER.error(String.format("Could not create a diagram grid layout with NAD elment id '%s' from user/profile '%s/%s'. No layout created",
312+
userProfileInfos.getDiagramConfigId(), userId, userProfileInfos.getName()), e);
313+
}
314+
}
315+
return null;
316+
}
317+
305318
private UserProfileInfos getUserProfile(String userId) {
306319
try {
307320
return userAdminService.getUserProfile(userId).orElse(null);

src/main/java/org/gridsuite/study/server/service/StudyConfigService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import org.gridsuite.study.server.RemoteServicesProperties;
1111
import org.gridsuite.study.server.StudyException;
1212
import org.gridsuite.study.server.dto.diagramgridlayout.DiagramGridLayout;
13+
import org.gridsuite.study.server.dto.diagramgridlayout.diagramlayout.NetworkAreaDiagramLayout;
1314
import org.gridsuite.study.server.repository.StudyEntity;
1415
import org.springframework.beans.factory.annotation.Autowired;
1516
import org.springframework.http.HttpEntity;
@@ -476,4 +477,32 @@ public void updateDiagramGridLayout(UUID diagramGridLayoutUuid, DiagramGridLayou
476477
throw e;
477478
}
478479
}
480+
481+
public UUID createGridLayoutFromNadDiagram(UUID nadDiagramConfigId) {
482+
if (nadDiagramConfigId == null) {
483+
return null;
484+
}
485+
DiagramGridLayout diagramGridLayout = DiagramGridLayout.builder()
486+
.diagramLayouts(List.of(NetworkAreaDiagramLayout.builder()
487+
.originalNadConfigUuid(nadDiagramConfigId)
488+
.currentNadConfigUuid(nadDiagramConfigId)
489+
.build()))
490+
.build();
491+
492+
var path = UriComponentsBuilder
493+
.fromPath(DELIMITER + STUDY_CONFIG_API_VERSION + DIAGRAM_GRID_LAYOUT_URI)
494+
.buildAndExpand()
495+
.toUriString();
496+
HttpHeaders headers = new HttpHeaders();
497+
headers.setContentType(MediaType.APPLICATION_JSON);
498+
HttpEntity<DiagramGridLayout> httpEntity = new HttpEntity<>(diagramGridLayout, headers);
499+
UUID uuid;
500+
try {
501+
uuid = restTemplate.exchange(studyConfigServerBaseUri + path, HttpMethod.POST, httpEntity, UUID.class).getBody();
502+
} catch (HttpStatusCodeException e) {
503+
throw handleHttpError(e, CREATE_SPREADSHEET_CONFIG_COLLECTION_FAILED);
504+
}
505+
return uuid;
506+
}
507+
479508
}

src/main/java/org/gridsuite/study/server/service/StudyService.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -614,7 +614,7 @@ public CreatedStudyBasicInfos insertStudy(UUID studyUuid, String userId, Network
614614
UUID shortCircuitParametersUuid, DynamicSimulationParametersEntity dynamicSimulationParametersEntity,
615615
UUID voltageInitParametersUuid, UUID securityAnalysisParametersUuid, UUID sensitivityAnalysisParametersUuid,
616616
UUID networkVisualizationParametersUuid, UUID dynamicSecurityAnalysisParametersUuid, UUID stateEstimationParametersUuid,
617-
UUID spreadsheetConfigCollectionUuid, Map<String, String> importParameters, UUID importReportUuid) {
617+
UUID spreadsheetConfigCollectionUuid, UUID diagramGridLayoutUuid, Map<String, String> importParameters, UUID importReportUuid) {
618618
Objects.requireNonNull(studyUuid);
619619
Objects.requireNonNull(userId);
620620
Objects.requireNonNull(networkInfos.getNetworkUuid());
@@ -628,7 +628,7 @@ public CreatedStudyBasicInfos insertStudy(UUID studyUuid, String userId, Network
628628
shortCircuitParametersUuid, dynamicSimulationParametersEntity,
629629
voltageInitParametersUuid, securityAnalysisParametersUuid,
630630
sensitivityAnalysisParametersUuid, networkVisualizationParametersUuid, dynamicSecurityAnalysisParametersUuid,
631-
stateEstimationParametersUuid, spreadsheetConfigCollectionUuid, importParameters, importReportUuid);
631+
stateEstimationParametersUuid, spreadsheetConfigCollectionUuid, diagramGridLayoutUuid, importParameters, importReportUuid);
632632

633633
// Need to deal with the study creation (with a default root network ?)
634634
CreatedStudyBasicInfos createdStudyBasicInfos = toCreatedStudyBasicInfos(studyEntity);
@@ -911,7 +911,7 @@ public void sendLoadflowRequest(UUID studyUuid, UUID nodeUuid, UUID rootNetworkU
911911

912912
handleLoadflowRequest(studyUuid, nodeUuid, rootNetworkUuid, loadflowResultUuid, withRatioTapChangers, userId);
913913
}
914-
914+
915915
private void handleLoadflowRequest(UUID studyUuid, UUID nodeUuid, UUID rootNetworkUuid, UUID loadflowResultUuid, boolean withRatioTapChangers, String userId) {
916916
StudyEntity studyEntity = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND));
917917
UUID lfParametersUuid = loadflowService.getLoadFlowParametersOrDefaultsUuid(studyEntity);
@@ -1445,7 +1445,7 @@ private StudyEntity saveStudyThenCreateBasicTree(UUID studyUuid, NetworkInfos ne
14451445
UUID shortCircuitParametersUuid, DynamicSimulationParametersEntity dynamicSimulationParametersEntity,
14461446
UUID voltageInitParametersUuid, UUID securityAnalysisParametersUuid, UUID sensitivityAnalysisParametersUuid,
14471447
UUID networkVisualizationParametersUuid, UUID dynamicSecurityAnalysisParametersUuid, UUID stateEstimationParametersUuid,
1448-
UUID spreadsheetConfigCollectionUuid, Map<String, String> importParameters, UUID importReportUuid) {
1448+
UUID spreadsheetConfigCollectionUuid, UUID diagramGridLayoutUuid, Map<String, String> importParameters, UUID importReportUuid) {
14491449

14501450
StudyEntity studyEntity = StudyEntity.builder()
14511451
.id(studyUuid)
@@ -1462,6 +1462,7 @@ private StudyEntity saveStudyThenCreateBasicTree(UUID studyUuid, NetworkInfos ne
14621462
.dynamicSecurityAnalysisParametersUuid(dynamicSecurityAnalysisParametersUuid)
14631463
.stateEstimationParametersUuid(stateEstimationParametersUuid)
14641464
.spreadsheetConfigCollectionUuid(spreadsheetConfigCollectionUuid)
1465+
.diagramGridLayoutUuid(diagramGridLayoutUuid)
14651466
.monoRoot(true)
14661467
.build();
14671468

@@ -2336,7 +2337,7 @@ private ReportPage getParentNodesReportLogs(UUID nodeUuid, UUID rootNetworkUuid,
23362337
Map<UUID, UUID> modificationReportsMap = networkModificationTreeService.getModificationReports(nodeUuid, rootNetworkUuid);
23372338

23382339
List<UUID> reportUuids = nodeIds.stream()
2339-
.map(nodeId -> modificationReportsMap.getOrDefault(nodeId,
2340+
.map(nodeId -> modificationReportsMap.getOrDefault(nodeId,
23402341
networkModificationTreeService.getReportUuid(nodeId, rootNetworkUuid)))
23412342
.filter(Objects::nonNull)
23422343
.toList();
@@ -2356,7 +2357,7 @@ private String getSearchTermMatchesInParentNodesFilteredLogs(UUID nodeUuid, UUID
23562357
Map<UUID, UUID> modificationReportsMap = networkModificationTreeService.getModificationReports(nodeUuid, rootNetworkUuid);
23572358

23582359
List<UUID> reportUuids = nodeIds.stream()
2359-
.map(nodeId -> modificationReportsMap.getOrDefault(nodeId,
2360+
.map(nodeId -> modificationReportsMap.getOrDefault(nodeId,
23602361
networkModificationTreeService.getReportUuid(nodeId, rootNetworkUuid)))
23612362
.filter(Objects::nonNull)
23622363
.toList();
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package org.gridsuite.study.server.service;
2+
3+
import org.gridsuite.study.server.dto.UserProfileInfos;
4+
import org.junit.jupiter.api.BeforeEach;
5+
import org.junit.jupiter.api.Test;
6+
import org.mockito.InjectMocks;
7+
import org.mockito.Mock;
8+
import org.mockito.MockitoAnnotations;
9+
10+
import java.lang.reflect.Method;
11+
import java.util.UUID;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.mockito.Mockito.*;
15+
16+
class ConsumerServiceTest {
17+
18+
@Mock
19+
private StudyConfigService studyConfigService;
20+
21+
@InjectMocks
22+
private ConsumerService consumerService;
23+
24+
private Method createGridLayoutFromNadDiagram;
25+
26+
@BeforeEach
27+
void setUp() throws Exception {
28+
MockitoAnnotations.openMocks(this);
29+
createGridLayoutFromNadDiagram = ConsumerService.class
30+
.getDeclaredMethod("createGridLayoutFromNadDiagram", String.class, UserProfileInfos.class);
31+
createGridLayoutFromNadDiagram.setAccessible(true);
32+
}
33+
34+
@Test
35+
void whenProfileHasAssociatedNadConfig() throws Exception {
36+
UUID configId = UUID.randomUUID();
37+
UUID expected = UUID.randomUUID();
38+
UserProfileInfos profile = UserProfileInfos.builder().diagramConfigId(configId).name("name").build();
39+
when(studyConfigService.createGridLayoutFromNadDiagram(configId)).thenReturn(expected);
40+
41+
UUID result = (UUID) createGridLayoutFromNadDiagram.invoke(consumerService, "user", profile);
42+
43+
assertEquals(expected, result);
44+
verify(studyConfigService).createGridLayoutFromNadDiagram(configId);
45+
}
46+
47+
@Test
48+
void whenProfileHasNoAssociatedNadConfig() throws Exception {
49+
UUID expected = null;
50+
UserProfileInfos profile = UserProfileInfos.builder().diagramConfigId(null).build();
51+
when(studyConfigService.createGridLayoutFromNadDiagram(null)).thenReturn(expected);
52+
UUID result = (UUID) createGridLayoutFromNadDiagram.invoke(consumerService, "user", profile);
53+
assertEquals(expected, result);
54+
}
55+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package org.gridsuite.study.server.service;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.github.tomakehurst.wiremock.WireMockServer;
6+
import org.gridsuite.study.server.RemoteServicesProperties;
7+
import org.gridsuite.study.server.dto.diagramgridlayout.DiagramGridLayout;
8+
import org.gridsuite.study.server.dto.diagramgridlayout.diagramlayout.NetworkAreaDiagramLayout;
9+
import org.junit.jupiter.api.AfterEach;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.Test;
12+
import org.springframework.web.client.RestTemplate;
13+
14+
import java.util.List;
15+
import java.util.UUID;
16+
17+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
18+
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
19+
import static org.gridsuite.study.server.StudyConstants.DELIMITER;
20+
import static org.gridsuite.study.server.StudyConstants.STUDY_CONFIG_API_VERSION;
21+
import static org.junit.jupiter.api.Assertions.assertEquals;
22+
23+
class StudyConfigServiceTest {
24+
25+
private WireMockServer wireMockServer;
26+
private StudyConfigService studyConfigService;
27+
private final ObjectMapper objectMapper = new ObjectMapper();
28+
29+
@BeforeEach
30+
void setUp() {
31+
wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
32+
wireMockServer.start();
33+
RestTemplate restTemplate = new RestTemplate();
34+
studyConfigService = new StudyConfigService(new RemoteServicesProperties(), restTemplate);
35+
studyConfigService.setStudyConfigServerBaseUri(wireMockServer.baseUrl() + "/");
36+
}
37+
38+
@AfterEach
39+
void tearDown() {
40+
wireMockServer.stop();
41+
}
42+
43+
@Test
44+
void testCreateDiagramGridLayoutFromNadConfig() throws JsonProcessingException {
45+
UUID diagramConfigId = UUID.randomUUID();
46+
UUID expectedUuid = UUID.randomUUID();
47+
DiagramGridLayout requestBody = DiagramGridLayout.builder()
48+
.diagramLayouts(List.of(NetworkAreaDiagramLayout.builder()
49+
.originalNadConfigUuid(diagramConfigId)
50+
.currentNadConfigUuid(diagramConfigId)
51+
.build()))
52+
.build();
53+
54+
wireMockServer.stubFor(post(urlPathEqualTo(DELIMITER + STUDY_CONFIG_API_VERSION + "/diagram-grid-layout"))
55+
.withRequestBody(equalToJson(objectMapper.writeValueAsString(requestBody)))
56+
.willReturn(okJson(objectMapper.writeValueAsString(expectedUuid))));
57+
58+
UUID result = studyConfigService.createGridLayoutFromNadDiagram(diagramConfigId);
59+
60+
assertEquals(expectedUuid, result);
61+
wireMockServer.verify(postRequestedFor(urlPathEqualTo(DELIMITER + STUDY_CONFIG_API_VERSION + "/diagram-grid-layout"))
62+
.withRequestBody(equalToJson(objectMapper.writeValueAsString(requestBody))));
63+
}
64+
}

0 commit comments

Comments
 (0)