Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import java.util.List;
import java.util.UUID;

import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;

@RestController
@RequestMapping(value = "/" + StudyApi.API_VERSION)
public class NetworkModificationController {
Expand Down Expand Up @@ -43,13 +45,14 @@ public ResponseEntity<String> getLineTypeWithLimits(@PathVariable UUID uuid, @Re

@GetMapping(value = "/network-composite-modifications/network-modifications")
public ResponseEntity<String> getNetworkModificationsFromComposite(@RequestParam("uuids") List<UUID> compositeModificationUuids,
@RequestParam(name = "onlyMetadata", defaultValue = "true") boolean onlyMetadata) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkModificationService.getNetworkModificationsFromComposite(compositeModificationUuids, onlyMetadata));
@RequestParam(name = "onlyMetadata", defaultValue = "true") boolean onlyMetadata,
@RequestHeader(HEADER_USER_ID) String userId) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkModificationService.getNetworkModificationsFromComposite(compositeModificationUuids, onlyMetadata, userId));
}

@GetMapping(value = "/network-modifications/{uuid}")
public ResponseEntity<String> getNetworkModification(@PathVariable UUID uuid) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkModificationService.getNetworkModification(uuid));
public ResponseEntity<String> getNetworkModification(@PathVariable UUID uuid, @RequestHeader(HEADER_USER_ID) String userId) {
return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(networkModificationService.getNetworkModification(uuid, userId));
}

@GetMapping(value = "/network-modifications/busbar-sections-for-new-coupler")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -966,9 +966,10 @@ public ResponseEntity<String> getNetworkModifications(@Parameter(description = "
@Parameter(description = "Node UUID") @PathVariable("nodeUuid") UUID nodeUuid,
@RequestParam(name = "onlyStashed", required = false, defaultValue = "false") Boolean onlyStashed,
@Parameter(description = "Only metadata") @RequestParam(name = "onlyMetadata", required = false,
defaultValue = "false") Boolean onlyMetadata) {
defaultValue = "false") Boolean onlyMetadata,
@RequestHeader(HEADER_USER_ID) String userId) {
studyService.assertIsStudyAndNodeExist(studyUuid, nodeUuid);
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(networkModificationTreeService.getNetworkModifications(nodeUuid, onlyStashed, onlyMetadata));
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(networkModificationTreeService.getNetworkModifications(nodeUuid, onlyStashed, onlyMetadata, userId));
}

@GetMapping(value = "/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications/export", produces = MediaType.APPLICATION_JSON_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,17 @@ public String getLineTypeWithLimits(UUID lineTypeUuid, String area, String tempe
return restTemplate.getForObject(getNetworkModificationServerURI(false) + path, String.class);
}

public String getNetworkModificationsFromComposite(List<UUID> compositeModificationUuids, boolean onlyMetadata) {
public String getNetworkModificationsFromComposite(List<UUID> compositeModificationUuids, boolean onlyMetadata, String userId) {
String path = UriComponentsBuilder.fromPath(COMPOSITE_PATH + NETWORK_MODIFICATIONS_PATH)
.queryParam(UUIDS, compositeModificationUuids)
.queryParam("onlyMetadata", onlyMetadata)
.build().toUriString();
return restTemplate.getForObject(getNetworkModificationServerURI(false) + path, String.class);
return restTemplate.exchange(getNetworkModificationServerURI(false) + path, HttpMethod.GET, userIdEntity(userId), String.class).getBody();
}

public String getNetworkModification(UUID networkModificationUuid) {
public String getNetworkModification(UUID networkModificationUuid, String userId) {
String path = UriComponentsBuilder.fromPath(NETWORK_MODIFICATIONS_PATH + "/{uuid}").buildAndExpand(networkModificationUuid).toUriString();
return restTemplate.getForObject(getNetworkModificationServerURI(false) + path, String.class);
return restTemplate.exchange(getNetworkModificationServerURI(false) + path, HttpMethod.GET, userIdEntity(userId), String.class).getBody();
}

public String getBusBarSectionsForNewCoupler(String voltageLevelId, Integer busBarCount, Integer sectionCount, List<String> switchKindList) {
Expand Down Expand Up @@ -145,7 +145,11 @@ public void updateNetworkModificationsMetadata(List<UUID> networkModificationUui
restTemplate.exchange(getNetworkModificationServerURI(false) + path, HttpMethod.PUT, new HttpEntity<>(metadata, headers), Void.class);
}

public String getModifications(UUID groupUUid, boolean stashedModifications, boolean onlyMetadata) {
/**
* @param userId the user the modifications are read for, null for a read of our own: the network-modification
* server then answers no permission on the shared modifications, which it has no one to resolve them for
*/
public String getModifications(UUID groupUUid, boolean stashedModifications, boolean onlyMetadata, String userId) {
Objects.requireNonNull(groupUUid);
var path = UriComponentsBuilder.fromPath(GROUP_PATH + DELIMITER + NETWORK_MODIFICATIONS_PATH)
.queryParam(QUERY_PARAM_ERROR_ON_GROUP_NOT_FOUND, false)
Expand All @@ -154,7 +158,19 @@ public String getModifications(UUID groupUUid, boolean stashedModifications, boo
.buildAndExpand(groupUUid)
.toUriString();

return restTemplate.exchange(getNetworkModificationServerURI(false) + path, HttpMethod.GET, null, String.class).getBody();
return restTemplate.exchange(getNetworkModificationServerURI(false) + path, HttpMethod.GET, userIdEntity(userId), String.class).getBody();
}

/**
* @return what carries the user to the network-modification server, nothing when there is no user to carry
*/
private static HttpEntity<Void> userIdEntity(String userId) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be used everywhere in the file or to remove ?
Maybe a bit overkilled

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You want me to generalise everywhere in the file ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, to be homogeneous in the file

if (userId == null) {
return null;
}
HttpHeaders headers = new HttpHeaders();
headers.set(HEADER_USER_ID, userId);
return new HttpEntity<>(headers);
}

public String getModificationsToExport(UUID groupUUid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,8 @@ public UUID getModificationGroupUuid(UUID nodeUuid) {
}

@Transactional(readOnly = true)
public String getNetworkModifications(@NonNull UUID nodeUuid, boolean onlyStashed, boolean onlyMetadata) {
return networkModificationService.getModifications(self.getModificationGroupUuid(nodeUuid), onlyStashed, onlyMetadata);
public String getNetworkModifications(@NonNull UUID nodeUuid, boolean onlyStashed, boolean onlyMetadata, String userId) {
return networkModificationService.getModifications(self.getModificationGroupUuid(nodeUuid), onlyStashed, onlyMetadata, userId);
}

private Integer getNetworkModificationsCount(@NonNull UUID nodeUuid, boolean stashed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,7 @@ public String getVoltageInitModifications(@NonNull UUID nodeUuid, @NonNull UUID
throw new StudyException(NO_VOLTAGE_INIT_RESULTS_FOR_NODE, String.format("Missing results for rootNetwork %s on node %s", rootNetworkUuid, nodeUuid));
}
UUID voltageInitModificationsGroupUuid = voltageInitRestService.getModificationsGroupUuid(nodeUuid, resultUuid);
return networkModificationService.getModifications(voltageInitModificationsGroupUuid, false, false);
return networkModificationService.getModifications(voltageInitModificationsGroupUuid, false, false, null);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.gridsuite.study.server.error.StudyBusinessErrorCode.NOT_FOUND;
import static org.gridsuite.study.server.notification.NotificationService.*;
import static org.gridsuite.study.server.service.NetworkModificationTreeService.ROOT_NODE_NAME;
Expand Down Expand Up @@ -1538,17 +1539,17 @@ void testGetNetworkModificationsNode() throws Exception {
randomUuidsResultStack(), BuildStatus.BUILT);
createNode(root.getStudyId(), root, node, userId);

mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId()))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId()).header(HEADER_USER_ID, "userId"))
.andExpect(status().isNotFound());

// No network modification for a root node
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), root.getId()))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), root.getId()).header(HEADER_USER_ID, "userId"))
.andExpect(status().isNotFound());

node = NetworkModificationTreeTest.buildNetworkModificationConstructionNode("modification node 2", "", MODIFICATION_GROUP_UUID, VARIANT_ID,
randomUuidsResultStack(), BuildStatus.BUILT);
createNode(root.getStudyId(), root, node, userId);
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId()))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId()).header(HEADER_USER_ID, "userId"))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString();
}
Expand Down Expand Up @@ -1594,7 +1595,7 @@ void testGetNetworkModificationsToRestoreByNode() throws Exception {
randomUuidsResultStack(), BuildStatus.BUILT);
createNode(root.getStudyId(), root, node, userId);

mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId(), true))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId(), true).header(HEADER_USER_ID, "userId"))
.andExpect(status().isNotFound());

node = NetworkModificationTreeTest.buildNetworkModificationConstructionNode("modification node 3", "", UUID.fromString(MODIFICATION_GROUP_UUID_STRING), VARIANT_ID,
Expand All @@ -1617,11 +1618,11 @@ void testGetNetworkModificationsToStashByNode() throws Exception {
randomUuidsResultStack(), BuildStatus.BUILT);
createNode(root.getStudyId(), root, node, userId);

mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId(), false))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), node.getId(), false).header(HEADER_USER_ID, "userId"))
.andExpect(status().isNotFound());

// No network modification for a root node
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), root.getId(), false))
mockMvc.perform(get("/v1/studies/{studyUuid}/nodes/{nodeUuid}/network-modifications", root.getStudyId(), root.getId(), false).header(HEADER_USER_ID, "userId"))
.andExpect(status().isNotFound());

node = NetworkModificationTreeTest.buildNetworkModificationConstructionNode("modification node 3", "", UUID.fromString(MODIFICATION_GROUP_UUID_STRING), VARIANT_ID,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.util.List;
import java.util.UUID;

import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
Expand All @@ -31,6 +33,7 @@ class NetworkModificationControllerTest {

private static final String BASE_URL = "/v1";
private static final String RESPONSE = "{\"name\":\"modification\"}";
private static final String USER_ID = "userId";

@Mock
private NetworkModificationService networkModificationService;
Expand Down Expand Up @@ -88,29 +91,38 @@ void testGetLineTypeWithLimits() throws Exception {
void testGetNetworkModificationsFromComposite() throws Exception {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
when(networkModificationService.getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false)).thenReturn(RESPONSE);
when(networkModificationService.getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false, USER_ID)).thenReturn(RESPONSE);

mockMvc.perform(get(BASE_URL + "/network-composite-modifications/network-modifications")
.param("uuids", firstUuid.toString(), secondUuid.toString())
.param("onlyMetadata", "false"))
.param("onlyMetadata", "false")
.header(HEADER_USER_ID, USER_ID))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(RESPONSE));

verify(networkModificationService).getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false);
verify(networkModificationService).getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false, USER_ID);
}

@Test
void testGetNetworkModification() throws Exception {
UUID modificationUuid = UUID.randomUUID();
when(networkModificationService.getNetworkModification(modificationUuid)).thenReturn(RESPONSE);
when(networkModificationService.getNetworkModification(modificationUuid, USER_ID)).thenReturn(RESPONSE);

mockMvc.perform(get(BASE_URL + "/network-modifications/{uuid}", modificationUuid))
mockMvc.perform(get(BASE_URL + "/network-modifications/{uuid}", modificationUuid).header(HEADER_USER_ID, USER_ID))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(content().json(RESPONSE));

verify(networkModificationService).getNetworkModification(modificationUuid);
verify(networkModificationService).getNetworkModification(modificationUuid, USER_ID);
}

@Test
void testReadingAModificationWithoutUserIsRejected() throws Exception {
mockMvc.perform(get(BASE_URL + "/network-modifications/{uuid}", UUID.randomUUID()))
.andExpect(status().isBadRequest());

verifyNoInteractions(networkModificationService);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.gridsuite.study.server.StudyConstants.HEADER_USER_ID;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
Expand All @@ -35,6 +37,7 @@ class NetworkModificationServiceTest {

private static final String NETWORK_MODIFICATION_SERVER_URI = "http://network-modification-server";
private static final String RESPONSE = "{\"id\":\"modification\"}";
private static final String USER_ID = "userId";
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated

@Mock
private RemoteServicesProperties remoteServicesProperties;
Expand Down Expand Up @@ -84,18 +87,18 @@ void testGetNetworkModificationsFromComposite() {
UUID firstUuid = UUID.randomUUID();
UUID secondUuid = UUID.randomUUID();
String expectedUrl = NETWORK_MODIFICATION_SERVER_URI + "/v1/network-composite-modifications/network-modifications?uuids=" + firstUuid + "&uuids=" + secondUuid + "&onlyMetadata=false";
when(restTemplate.getForObject(expectedUrl, String.class)).thenReturn(RESPONSE);
expectUserIdIsForwarded(expectedUrl);

assertThat(networkModificationService.getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false)).isEqualTo(RESPONSE);
assertThat(networkModificationService.getNetworkModificationsFromComposite(List.of(firstUuid, secondUuid), false, USER_ID)).isEqualTo(RESPONSE);
}

@Test
void testGetNetworkModification() {
UUID modificationUuid = UUID.randomUUID();
String expectedUrl = NETWORK_MODIFICATION_SERVER_URI + "/v1/network-modifications/" + modificationUuid;
when(restTemplate.getForObject(expectedUrl, String.class)).thenReturn(RESPONSE);
expectUserIdIsForwarded(expectedUrl);

assertThat(networkModificationService.getNetworkModification(modificationUuid)).isEqualTo(RESPONSE);
assertThat(networkModificationService.getNetworkModification(modificationUuid, USER_ID)).isEqualTo(RESPONSE);
}

@Test
Expand Down Expand Up @@ -196,4 +199,11 @@ void testHasModificationReferences() {
networkModificationService.hasModificationReferences(List.of(modificationUuid));
verify(restTemplate).getForObject(eq(expectedUrl), eq(Boolean.class));
}

private void expectUserIdIsForwarded(String expectedUrl) {
HttpHeaders headers = new HttpHeaders();
headers.set(HEADER_USER_ID, USER_ID);
when(restTemplate.exchange(eq(expectedUrl), eq(HttpMethod.GET), eq(new HttpEntity<>(headers)), eq(String.class)))
.thenReturn(ResponseEntity.ok(RESPONSE));
}
}