Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -21,7 +21,6 @@
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.concurrent.CompletableFuture;
Expand Down Expand Up @@ -222,23 +221,6 @@ public ResponseEntity<LineTypeInfos> getOneLineTypeWithLimits(@PathVariable UUID
return ResponseEntity.ok().body(test);
}

@PostMapping(value = "/network-modifications/catalog/line_types", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "Create or add a line types catalog")
@ApiResponse(responseCode = "200", description = "The line types catalog is created or added")
public ResponseEntity<Void> addLineTypes(@RequestParam("file") MultipartFile file) {
lineTypesCatalogService.addLineTypes(file);
return ResponseEntity.ok().build();
}

// TODO : delete this endpoint
@DeleteMapping(value = "/network-modifications/catalog/line_types", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Delete line types catalog")
@ApiResponse(responseCode = "200", description = "The line types catalog is deleted")
public ResponseEntity<Void> deleteLineTypesCatalog() {
lineTypesCatalogService.deleteLineTypesCatalog();
return ResponseEntity.ok().build();
}

@PutMapping(value = "/network-modifications", produces = MediaType.APPLICATION_JSON_VALUE, params = "stashed")
@Operation(summary = "stash or unstash network modifications")
@ApiResponse(responseCode = "200", description = "The network modifications were stashed")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.gridsuite.modification.server.service.LineTypesCatalogService;
import org.gridsuite.modification.server.service.SupervisionService;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.UUID;
Expand All @@ -29,11 +31,14 @@
public class SupervisionController {
private final SupervisionService supervisionService;

private final LineTypesCatalogService lineTypesCatalogService;

private final RestClient restClient;

public SupervisionController(SupervisionService supervisionService,
public SupervisionController(SupervisionService supervisionService, LineTypesCatalogService lineTypesCatalogService,
RestClient restClient) {
this.supervisionService = supervisionService;
this.lineTypesCatalogService = lineTypesCatalogService;
this.restClient = restClient;
}

Expand Down Expand Up @@ -94,4 +99,20 @@ public ResponseEntity<String> getModificationsToReindexCount() {
public ResponseEntity<String> getModificationsIndexName() {
return ResponseEntity.ok().contentType(MediaType.TEXT_PLAIN).body(supervisionService.getModificationIndexName());
}

@PostMapping(value = "/network-modifications/catalog/line_types", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
@Operation(summary = "Create or add a line types catalog")
@ApiResponse(responseCode = "200", description = "The line types catalog is created or added")
public ResponseEntity<Void> addLineTypes(@RequestParam("file") MultipartFile file) {
lineTypesCatalogService.addLineTypes(file);
return ResponseEntity.ok().build();
}

@DeleteMapping(value = "/network-modifications/catalog/line_types", produces = MediaType.APPLICATION_JSON_VALUE)
@Operation(summary = "Delete line types catalog")
@ApiResponse(responseCode = "200", description = "The line types catalog is deleted")
public ResponseEntity<Void> deleteLineTypesCatalog() {
lineTypesCatalogService.deleteLineTypesCatalog();
return ResponseEntity.ok().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
import org.gridsuite.modification.dto.*;
import org.gridsuite.modification.dto.LoadCreationInfos.LoadCreationInfosBuilder;
import org.gridsuite.modification.server.dto.*;
import org.gridsuite.modification.server.dto.catalog.AerialLineTypeInfos;
import org.gridsuite.modification.server.dto.catalog.LineTypeInfos;
import org.gridsuite.modification.server.dto.catalog.UndergroundLineTypeInfos;
import org.gridsuite.modification.server.elasticsearch.EquipmentInfosRepository;
import org.gridsuite.modification.server.elasticsearch.EquipmentInfosService;
import org.gridsuite.modification.server.elasticsearch.TombstonedEquipmentInfosRepository;
Expand All @@ -51,27 +48,25 @@
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

import static org.gridsuite.modification.ModificationType.*;
import static org.gridsuite.modification.ModificationType.EQUIPMENT_ATTRIBUTE_MODIFICATION;
import static org.gridsuite.modification.ModificationType.LINE_MODIFICATION;
import static org.gridsuite.modification.NetworkModificationException.Type.*;
import static org.gridsuite.modification.dto.OperationalLimitsGroupInfos.Applicability.SIDE1;
import static org.gridsuite.modification.dto.OperationalLimitsGroupInfos.Applicability.SIDE2;
import static org.gridsuite.modification.server.NetworkModificationServerException.Type.DUPLICATION_ARGUMENT_INVALID;
import static org.gridsuite.modification.server.elasticsearch.EquipmentInfosService.getIndexedEquipmentTypes;
import static org.gridsuite.modification.server.impacts.TestImpactUtils.*;
import static org.gridsuite.modification.server.report.NetworkModificationServerReportResourceBundle.ERROR_MESSAGE_KEY;
import static org.gridsuite.modification.server.utils.TestUtils.*;
import static org.gridsuite.modification.server.utils.TestUtils.assertLogMessage;
import static org.gridsuite.modification.server.utils.TestUtils.runRequestAsync;
import static org.gridsuite.modification.server.utils.assertions.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.*;
Expand Down Expand Up @@ -103,11 +98,6 @@ class ModificationControllerTest {
private static final UUID TEST_REPORT_ID = UUID.randomUUID();

private static final String URI_NETWORK_MODIF_BASE = "/v1/network-modifications";
private static final String URI_LINE_CATALOG = URI_NETWORK_MODIF_BASE + "/catalog/line_types";
private static final String LINE_TYPES_CATALOG_JSON_FILE_1 = "/lines-catalog.json.gz";
private static final String LINE_TYPES_CATALOG_JSON_FILE_2 = "/line_types_catalog_2.json.gz";
private static final String LINE_TYPES_CATALOG_JSON_FILE_3 = "/line_types_catalog_3.json.gz";
private static final String NOT_EXISTING_JSON_FILE = "/not_existing_file.json.gz";
private static final String NETWORK_MODIFICATION_URI = URI_NETWORK_MODIF_BASE + "?groupUuid=" + TEST_GROUP_ID;

@Autowired
Expand Down Expand Up @@ -1406,186 +1396,6 @@ void testGetPositionAfterAndBefore() {
assertEquals(0, ModificationUtils.getInstance().getPosition("bbs1", testNetwork, v1));
}

@Test
void testGetLineTypesCatalog() throws Exception {
MvcResult mvcResult;
String resultAsString;

// Check if the catalog is empty
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> emptyLineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(0, emptyLineTypes.size());

// Create the catalog with some line types
mockMvc.perform(multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_1)))
.andExpect(status().isOk());

// Check if the catalog is complete avoiding the duplicate entry
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> lineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(10, lineTypes.size());

// need to delete before adding the new catalog
mockMvc.perform(delete(URI_LINE_CATALOG))
.andExpect(status().isOk());

// Check if catalog is completely updated
mockMvc.perform(multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_2)))
.andExpect(status().isOk());

mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> lineTypes2 = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(2, lineTypes2.size());

mockMvc.perform(delete(URI_LINE_CATALOG))
.andExpect(status().isOk());

// Check if the catalog is empty
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
emptyLineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(0, emptyLineTypes.size());
}

@Test
void testGetLineTypeWithoutLimitsCatalog() throws Exception {
MvcResult mvcResult;
String resultAsString;

// Check if the catalog is empty
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> emptyLineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(0, emptyLineTypes.size());

// Create the catalog with some line types
mockMvc.perform(multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_3)))
.andExpect(status().isOk());

mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> lineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(2, lineTypes.size());
// getting the whole catalog does not load the limits
assertNull(lineTypes.get(0).getLimitsForLineType());
assertNull(lineTypes.get(1).getLimitsForLineType());

// get one line of the catalog does not load limits too
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG + "/" + lineTypes.get(0).getId()).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
LineTypeInfos selectedLineType = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(2, selectedLineType.getLimitsForLineType().size());
assertNull(selectedLineType.getLimitsForLineType().getFirst().getLimitSetName());
assertNull(selectedLineType.getLimitsForLineType().getFirst().getPermanentLimit());
assertNull(selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits());
assertEquals("1", selectedLineType.getLimitsForLineType().getFirst().getArea());
assertEquals("37", selectedLineType.getLimitsForLineType().getFirst().getTemperature());
mockMvc.perform(delete(URI_LINE_CATALOG))
.andExpect(status().isOk());
}

@Test
void testGetLineTypeWithLimitsCatalog() throws Exception {
// Create the catalog with some line types
mockMvc.perform(multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_3)))
.andExpect(status().isOk());

MvcResult mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
String resultAsString = mvcResult.getResponse().getContentAsString();
List<LineTypeInfos> lineTypes = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(2, lineTypes.size());
UUID aerialLineId = lineTypes.get(0) instanceof AerialLineTypeInfos ? lineTypes.get(0).getId() : lineTypes.get(1).getId();
UUID underGroundLineId = lineTypes.get(0) instanceof UndergroundLineTypeInfos ? lineTypes.get(0).getId() : lineTypes.get(1).getId();

// get one aerial line with limits
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG + "/" + aerialLineId + "/with-limits?area=1&temperature=37").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
LineTypeInfos selectedLineType = mapper.readValue(resultAsString, new TypeReference<>() { });

assertEquals(1, selectedLineType.getLimitsForLineType().size());
assertEquals("LimitSet1", selectedLineType.getLimitsForLineType().getFirst().getLimitSetName());
assertEquals(10.0, selectedLineType.getLimitsForLineType().getFirst().getPermanentLimit());
assertEquals(20.0, selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getLimitValue());
assertEquals("TemporaryLimit1", selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getName());
assertEquals(100, selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getAcceptableDuration());
assertEquals("37", selectedLineType.getLimitsForLineType().getFirst().getTemperature());
assertEquals("1", selectedLineType.getLimitsForLineType().getFirst().getArea());

// get one underground line with limits
mvcResult = mockMvc
.perform(get(URI_LINE_CATALOG + "/" + underGroundLineId + "/with-limits?area=1&shapeFactor=0.9").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn();
resultAsString = mvcResult.getResponse().getContentAsString();
selectedLineType = mapper.readValue(resultAsString, new TypeReference<>() { });
assertEquals(1, selectedLineType.getLimitsForLineType().size());
assertEquals("LimitSet1", selectedLineType.getLimitsForLineType().getFirst().getLimitSetName());
assertEquals(11.0, selectedLineType.getLimitsForLineType().getFirst().getPermanentLimit(), 0.01);
assertEquals(22.0, selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getLimitValue(), 0.01);
assertEquals("TemporaryLimit1", selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getName());
assertEquals(100, selectedLineType.getLimitsForLineType().getFirst().getTemporaryLimits().getFirst().getAcceptableDuration());
assertEquals("37", selectedLineType.getLimitsForLineType().getFirst().getTemperature());
assertEquals("1", selectedLineType.getLimitsForLineType().getFirst().getArea());

mockMvc.perform(delete(URI_LINE_CATALOG))
.andExpect(status().isOk());
}

private MockMultipartFile createMockMultipartFile(String fileName) throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream(fileName)) {
return new MockMultipartFile("file", fileName, MediaType.MULTIPART_FORM_DATA_VALUE, inputStream);
}
}

@Test
void testPostLineTypeWithLimitsCatalogError() throws Exception {
MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(NOT_EXISTING_JSON_FILE));

mockMvc.perform(mockMultipartHttpServletRequestBuilder)
.andExpect(result -> {
assertNotNull(result.getResolvedException());
assertEquals("java.io.EOFException",
result.getResolvedException().getMessage());
});
}

@Test
void testCreateVoltageInitModification() throws Exception {
// Create the modification
Expand Down
Loading
Loading