Skip to content

Commit 7d07a9c

Browse files
Refactor elements endpoint (#550)
* refactor elements endpoint, add specifics params in QUERY_PARAM_ADDITIONAL_PARAMS map * include infoType in the additional parametters --------- Signed-off-by: jamal-khey <[email protected]> Co-authored-by: Mathieu Deharbe <[email protected]>
1 parent b150a55 commit 7d07a9c

File tree

7 files changed

+65
-20
lines changed

7 files changed

+65
-20
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ private StudyConstants() {
4444
public static final String QUERY_PARAM_EQUIPMENT_TYPE = "equipmentType";
4545
public static final String QUERY_PARAM_ELEMENT_TYPE = "elementType";
4646
public static final String QUERY_PARAM_INFO_TYPE = "infoType";
47-
public static final String QUERY_PARAM_OPERATION = "operation";
47+
public static final String QUERY_PARAM_OPTIONAL_PARAMS = "optionalParameters";
48+
public static final String QUERY_FORMAT_OPTIONAL_PARAMS = QUERY_PARAM_OPTIONAL_PARAMS + "[%s]";
4849
public static final String QUERY_PARAM_SUBSTATIONS_IDS = "substationsIds";
4950
public static final String QUERY_PARAM_SUBSTATION_ID = "substationId";
50-
public static final String QUERY_PARAM_DC_POWERFACTOR = "dcPowerFactor";
5151

5252
public static final String GROUP_UUID = "groupUuid";
5353
public static final String REPORT_UUID = "reportUuid";

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@
5353
import java.nio.charset.StandardCharsets;
5454
import java.util.*;
5555

56-
import static org.gridsuite.study.server.StudyConstants.CASE_FORMAT;
57-
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
56+
import static org.gridsuite.study.server.StudyConstants.*;
5857

5958
/**
6059
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
@@ -472,11 +471,9 @@ public ResponseEntity<String> getNetworkElementInfos(
472471
@Parameter(description = "Node uuid") @PathVariable("nodeUuid") UUID nodeUuid,
473472
@Parameter(description = "Element id") @PathVariable("elementId") String elementId,
474473
@Parameter(description = "Element type") @RequestParam(name = "elementType") String elementType,
475-
@Parameter(description = "Info type") @RequestParam(name = "infoType") String infoType,
476-
@Parameter(description = "Operation") @RequestParam(name = "operation", required = false) String operation,
477-
@Parameter(description = "Should get in upstream built node ?") @RequestParam(value = "inUpstreamBuiltParentNode", required = false, defaultValue = "false") boolean inUpstreamBuiltParentNode) {
478-
479-
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getNetworkElementInfos(studyUuid, nodeUuid, elementType, infoType, elementId, operation, inUpstreamBuiltParentNode));
474+
@Parameter(description = "Should get in upstream built node ?") @RequestParam(value = "inUpstreamBuiltParentNode", required = false, defaultValue = "false") boolean inUpstreamBuiltParentNode,
475+
@Parameter(description = "Info type parameters") InfoTypeParameters infoTypeParameters) {
476+
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(studyService.getNetworkElementInfos(studyUuid, nodeUuid, elementType, infoTypeParameters, elementId, inUpstreamBuiltParentNode));
480477
}
481478

482479
@GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-map/countries")
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
package org.gridsuite.study.server.dto;
8+
9+
import lombok.Builder;
10+
import lombok.Data;
11+
12+
import java.util.HashMap;
13+
import java.util.Map;
14+
15+
@Data
16+
@Builder
17+
public class InfoTypeParameters {
18+
public static final String QUERY_PARAM_DC_POWERFACTOR = "dcPowerFactor";
19+
public static final String QUERY_PARAM_OPERATION = "operation";
20+
private String infoType;
21+
private Map<String, String> optionalParameters;
22+
23+
public InfoTypeParameters(String infoType, Map<String, String> optionalParameters) {
24+
this.infoType = infoType;
25+
this.optionalParameters = optionalParameters == null ? new HashMap<>() : optionalParameters;
26+
}
27+
}

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import org.gridsuite.study.server.RemoteServicesProperties;
1616
import org.gridsuite.study.server.StudyException;
1717
import org.gridsuite.study.server.dto.IdentifiableInfos;
18+
import org.gridsuite.study.server.dto.InfoTypeParameters;
1819
import org.springframework.beans.factory.annotation.Autowired;
1920
import org.springframework.core.ParameterizedTypeReference;
2021
import org.springframework.http.HttpMethod;
@@ -24,11 +25,15 @@
2425
import org.springframework.web.client.RestTemplate;
2526
import org.springframework.web.util.UriComponentsBuilder;
2627

28+
import java.util.HashMap;
2729
import java.util.List;
30+
import java.util.Map;
2831
import java.util.UUID;
2932

3033
import static org.gridsuite.study.server.StudyConstants.*;
3134
import static org.gridsuite.study.server.StudyException.Type.*;
35+
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_DC_POWERFACTOR;
36+
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_OPERATION;
3237
import static org.gridsuite.study.server.utils.StudyUtils.handleHttpError;
3338

3439
@Service
@@ -57,8 +62,11 @@ public String getElementsInfos(UUID networkUuid, String variantId, List<String>
5762
}
5863
builder = builder.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType);
5964
builder = builder.queryParam(QUERY_PARAM_INFO_TYPE, infoType);
60-
builder = builder.queryParam(QUERY_PARAM_DC_POWERFACTOR, dcPowerFactor);
6165

66+
InfoTypeParameters infoTypeParameters = InfoTypeParameters.builder()
67+
.optionalParameters(Map.of(QUERY_PARAM_DC_POWERFACTOR, String.valueOf(dcPowerFactor)))
68+
.build();
69+
queryParamInfoTypeParameters(infoTypeParameters, builder);
6270
String url = builder.buildAndExpand(networkUuid).toUriString();
6371
return restTemplate.getForObject(networkMapServerBaseUri + url, String.class);
6472
}
@@ -69,12 +77,18 @@ public String getElementInfos(UUID networkUuid, String variantId, String element
6977
if (!StringUtils.isBlank(variantId)) {
7078
builder = builder.queryParam(QUERY_PARAM_VARIANT_ID, variantId);
7179
}
72-
if (!StringUtils.isBlank(operation)) {
73-
builder = builder.queryParam(QUERY_PARAM_OPERATION, operation);
74-
}
7580
builder = builder.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType);
7681
builder = builder.queryParam(QUERY_PARAM_INFO_TYPE, infoType);
77-
builder = builder.queryParam(QUERY_PARAM_DC_POWERFACTOR, dcPowerFactor);
82+
Map<String, String> optionalParams = new HashMap<>();
83+
if (operation != null) {
84+
optionalParams.put(QUERY_PARAM_OPERATION, operation);
85+
}
86+
87+
optionalParams.put(QUERY_PARAM_DC_POWERFACTOR, String.valueOf(dcPowerFactor));
88+
InfoTypeParameters infoTypeParameters = InfoTypeParameters.builder()
89+
.optionalParameters(optionalParams)
90+
.build();
91+
queryParamInfoTypeParameters(infoTypeParameters, builder);
7892

7993
try {
8094
return restTemplate.getForObject(networkMapServerBaseUri + builder.build().toUriString(), String.class, networkUuid, elementId);
@@ -89,6 +103,10 @@ public String getElementInfos(UUID networkUuid, String variantId, String element
89103
}
90104
}
91105

106+
private void queryParamInfoTypeParameters(InfoTypeParameters infoTypesParameters, UriComponentsBuilder builder) {
107+
infoTypesParameters.getOptionalParameters().forEach((key, value) -> builder.queryParam(String.format(QUERY_FORMAT_OPTIONAL_PARAMS, key), value));
108+
}
109+
92110
public String getCountries(UUID networkUuid, String variantId) {
93111
String path = DELIMITER + NETWORK_MAP_API_VERSION + "/networks/{networkUuid}/countries";
94112
UriComponentsBuilder builder = UriComponentsBuilder.fromPath(path);

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@
7373

7474
import static org.gridsuite.study.server.StudyException.Type.*;
7575
import static org.gridsuite.study.server.dto.ComputationType.*;
76+
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_OPERATION;
7677
import static org.gridsuite.study.server.service.NetworkModificationTreeService.ROOT_NODE_NAME;
7778
import static org.gridsuite.study.server.utils.StudyUtils.handleHttpError;
7879

@@ -618,12 +619,12 @@ public String getNetworkElementsInfos(UUID studyUuid, UUID nodeUuid, List<String
618619
substationsIds, elementType, infoType, loadFlowParameters.getDcPowerFactor());
619620
}
620621

621-
public String getNetworkElementInfos(UUID studyUuid, UUID nodeUuid, String elementType, String infoType, String elementId, String operation, boolean inUpstreamBuiltParentNode) {
622+
public String getNetworkElementInfos(UUID studyUuid, UUID nodeUuid, String elementType, InfoTypeParameters infoTypeParameters, String elementId, boolean inUpstreamBuiltParentNode) {
622623
UUID nodeUuidToSearchIn = getNodeUuidToSearchIn(nodeUuid, inUpstreamBuiltParentNode);
623624
StudyEntity studyEntity = studyRepository.findById(studyUuid).orElseThrow(() -> new StudyException(STUDY_NOT_FOUND));
624625
LoadFlowParameters loadFlowParameters = getLoadFlowParameters(studyEntity);
625626
return networkMapService.getElementInfos(networkStoreService.getNetworkUuid(studyUuid), networkModificationTreeService.getVariantId(nodeUuidToSearchIn),
626-
elementType, infoType, operation, loadFlowParameters.getDcPowerFactor(), elementId);
627+
elementType, infoTypeParameters.getInfoType(), infoTypeParameters.getOptionalParameters().getOrDefault(QUERY_PARAM_OPERATION, null), loadFlowParameters.getDcPowerFactor(), elementId);
627628
}
628629

629630
public String getNetworkCountries(UUID studyUuid, UUID nodeUuid, boolean inUpstreamBuiltParentNode) {

src/test/java/org/gridsuite/study/server/NetworkMapTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161

6262
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
6363
import static org.gridsuite.study.server.StudyConstants.*;
64+
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_DC_POWERFACTOR;
6465
import static org.hamcrest.MatcherAssert.assertThat;
6566
import static org.junit.Assert.assertEquals;
6667
import static org.junit.Assert.assertTrue;
@@ -540,7 +541,7 @@ private MvcResult getNetworkElementsInfos(UUID studyUuid, UUID rootNodeUuid, Str
540541
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network/elements", studyUuid, rootNodeUuid)
541542
.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType)
542543
.queryParam(QUERY_PARAM_INFO_TYPE, infoType)
543-
.queryParam(QUERY_PARAM_DC_POWERFACTOR, Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR));
544+
.queryParam(String.format(QUERY_FORMAT_OPTIONAL_PARAMS, QUERY_PARAM_DC_POWERFACTOR), Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR));
544545
if (!substationsIds.isEmpty()) {
545546
mockHttpServletRequestBuilder.queryParam(QUERY_PARAM_SUBSTATIONS_IDS, substationsIds.stream().toArray(String[]::new));
546547
}
@@ -572,7 +573,7 @@ private MvcResult getNetworkElementInfosNotFound(UUID studyUuid, UUID rootNodeUu
572573
MvcResult mvcResult = mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network/elements/{elementId}", studyUuid, rootNodeUuid, elementId)
573574
.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType)
574575
.queryParam(QUERY_PARAM_INFO_TYPE, infoType)
575-
.queryParam(QUERY_PARAM_DC_POWERFACTOR, Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR))
576+
.queryParam(String.format(QUERY_FORMAT_OPTIONAL_PARAMS, QUERY_PARAM_DC_POWERFACTOR), Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR))
576577
)
577578
.andExpect(status().isNotFound())
578579
.andReturn();
@@ -587,7 +588,7 @@ private void getNetworkElementInfosWithError(UUID studyUuid, UUID rootNodeUuid,
587588
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network/elements/{elementId}", studyUuid, rootNodeUuid, elementId)
588589
.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType)
589590
.queryParam(QUERY_PARAM_INFO_TYPE, infoType)
590-
.queryParam(QUERY_PARAM_DC_POWERFACTOR, Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR))
591+
.queryParam(String.format(QUERY_FORMAT_OPTIONAL_PARAMS, QUERY_PARAM_DC_POWERFACTOR), Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR))
591592
)
592593
.andExpectAll(status().isInternalServerError(), content().string("Internal Server Error"));
593594
wireMockUtils.verifyNetworkElementInfosGet(stubUuid, NETWORK_UUID_STRING, elementType, infoType, elementId);

src/test/java/org/gridsuite/study/server/SingleLineDiagramTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666

6767
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
6868
import static org.gridsuite.study.server.StudyConstants.*;
69+
import static org.gridsuite.study.server.dto.InfoTypeParameters.QUERY_PARAM_DC_POWERFACTOR;
6970
import static org.hamcrest.MatcherAssert.assertThat;
7071
import static org.junit.Assert.*;
7172
import static org.mockito.ArgumentMatchers.any;
@@ -590,7 +591,7 @@ private MvcResult getNetworkElementsInfos(UUID studyUuid, UUID rootNodeUuid, Str
590591
MockHttpServletRequestBuilder mockHttpServletRequestBuilder = get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network/elements", studyUuid, rootNodeUuid)
591592
.queryParam(QUERY_PARAM_ELEMENT_TYPE, elementType)
592593
.queryParam(QUERY_PARAM_INFO_TYPE, infoType)
593-
.queryParam(QUERY_PARAM_DC_POWERFACTOR, Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR));
594+
.queryParam(String.format(QUERY_FORMAT_OPTIONAL_PARAMS, QUERY_PARAM_DC_POWERFACTOR), Double.toString(LoadFlowParameters.DEFAULT_DC_POWER_FACTOR));
594595
if (!substationsIds.isEmpty()) {
595596
mockHttpServletRequestBuilder.queryParam(QUERY_PARAM_SUBSTATIONS_IDS, substationsIds.stream().toArray(String[]::new));
596597
}

0 commit comments

Comments
 (0)