Skip to content
Merged
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 @@ -20,8 +20,12 @@
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.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/**
* @author Franck Lecuyer <franck.lecuyer at rte-france.com>
Expand Down Expand Up @@ -196,11 +200,11 @@ public ResponseEntity<LineTypeInfos> getOneLineTypeWithLimits(@PathVariable("uui
return ResponseEntity.ok().body(lineTypesCatalogService.getLineTypesWithLimits(uuid));
}

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,35 @@
*/
package org.gridsuite.modification.server.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.gridsuite.modification.server.dto.catalog.LineTypeInfos;
import org.gridsuite.modification.server.entities.catalog.LineTypeEntity;
import org.gridsuite.modification.server.repositories.LineTypesCatalogRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.zip.GZIPInputStream;

/**
* @author Sylvain Bouzols <sylvain.bouzols at rte-france.com>
*/
@Service
public class LineTypesCatalogService {
private final LineTypesCatalogRepository lineTypesCatalogRepository;
private final ObjectMapper mapper;

public LineTypesCatalogService(LineTypesCatalogRepository lineTypesCatalogRepository) {
public LineTypesCatalogService(LineTypesCatalogRepository lineTypesCatalogRepository, ObjectMapper objectMapper) {
this.lineTypesCatalogRepository = lineTypesCatalogRepository;
this.mapper = objectMapper;
}

@Transactional(readOnly = true)
Expand All @@ -43,14 +54,20 @@ public void deleteLineTypesCatalog() {
lineTypesCatalogRepository.deleteAll();
}

public void resetLineTypes(List<LineTypeInfos> lineTypes) {
deleteLineTypesCatalog();
// remove duplicates in file
Set<LineTypeInfos> lineTypesSet = lineTypes.stream().collect(Collectors.toSet());
public void resetLineTypes(MultipartFile file) {
try (GZIPInputStream gzipInputStream = new GZIPInputStream(file.getInputStream())) {
List<LineTypeInfos> lineTypes = mapper.readValue(gzipInputStream, new TypeReference<>() {
});
deleteLineTypesCatalog();
// remove duplicates in file
Set<LineTypeInfos> lineTypesSet = lineTypes.stream().collect(Collectors.toSet());

List<LineTypeEntity> lineTypesEntities = lineTypesSet.stream()
.map(LineTypeInfos::toEntity)
.collect(Collectors.toList());
lineTypesCatalogRepository.saveAll(lineTypesEntities);
List<LineTypeEntity> lineTypesEntities = lineTypesSet.stream()
.map(LineTypeInfos::toEntity)
.collect(Collectors.toList());
lineTypesCatalogRepository.saveAll(lineTypesEntities);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.network.store.client.PreloadingStrategy;
import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl;
import jakarta.servlet.ServletException;
import org.apache.commons.collections4.ListUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.gridsuite.modification.NetworkModificationException;
Expand Down Expand Up @@ -53,9 +54,13 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
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.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -102,9 +107,10 @@ class ModificationControllerTest {
private static final String URI_COMPOSITE_NETWORK_MODIF_BASE = "/v1/network-composite-modifications";
private static final String URI_GET_COMPOSITE_NETWORK_MODIF_CONTENT = "/v1/network-composite-modification/";
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";
private static final String LINE_TYPES_CATALOG_JSON_FILE_2 = "/line_types_catalog_2.json";
private static final String LINE_TYPES_CATALOG_JSON_FILE_3 = "/line_types_catalog_3.json";
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 @@ -1514,9 +1520,9 @@ void testGetLineTypesCatalog() throws Exception {
assertEquals(0, emptyLineTypes.size());

// Create the catalog with some line types
String lineTypesCatalogJson1 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_1);
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson1).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
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
Expand All @@ -1529,9 +1535,9 @@ void testGetLineTypesCatalog() throws Exception {
assertEquals(8, lineTypes.size());

// Check if catalog is completely updated
String lineTypesCatalogJson2 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_2);
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson2).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk());
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))
Expand Down Expand Up @@ -1570,8 +1576,8 @@ void testGetLineTypeWithLimitsCatalog() throws Exception {
assertEquals(0, emptyLineTypes.size());

// Create the catalog with some line types
String lineTypesCatalogJson1 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_3);
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson1).contentType(MediaType.APPLICATION_JSON))
mockMvc.perform(multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_3)))
.andExpect(status().isOk());

mvcResult = mockMvc
Expand Down Expand Up @@ -1600,6 +1606,20 @@ void testGetLineTypeWithLimitsCatalog() throws Exception {
assertEquals("1", selectedLineType.getLimitsForLineType().getFirst().getArea());
}

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 IOException {
MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = multipart(URI_LINE_CATALOG)
.file(createMockMultipartFile(NOT_EXISTING_JSON_FILE));
String message = assertThrows(ServletException.class, () -> mockMvc.perform(mockMultipartHttpServletRequestBuilder)).getMessage();
assertEquals("Request processing failed: java.io.UncheckedIOException: java.io.EOFException", message);
}

@Test
void testCreateVoltageInitModification() throws Exception {
// Create the modification
Expand Down
28 changes: 0 additions & 28 deletions src/test/resources/line_types_catalog_2.json

This file was deleted.

Binary file added src/test/resources/line_types_catalog_2.json.gz
Binary file not shown.
66 changes: 0 additions & 66 deletions src/test/resources/line_types_catalog_3.json

This file was deleted.

Binary file added src/test/resources/line_types_catalog_3.json.gz
Binary file not shown.
Loading