Skip to content

Commit d8020a2

Browse files
authored
allow to load gzip file for line type catalog (#677)
Signed-off-by: Etienne LESOT <[email protected]>
1 parent 212b32d commit d8020a2

File tree

9 files changed

+66
-247
lines changed

9 files changed

+66
-247
lines changed

src/main/java/org/gridsuite/modification/server/NetworkModificationController.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@
2020
import org.springframework.http.MediaType;
2121
import org.springframework.http.ResponseEntity;
2222
import org.springframework.web.bind.annotation.*;
23+
import org.springframework.web.multipart.MultipartFile;
2324

24-
import java.util.*;
25+
import java.util.List;
26+
import java.util.Map;
27+
import java.util.Set;
28+
import java.util.UUID;
2529

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

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

src/main/java/org/gridsuite/modification/server/service/LineTypesCatalogService.java

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,35 @@
66
*/
77
package org.gridsuite.modification.server.service;
88

9+
import com.fasterxml.jackson.core.type.TypeReference;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
911
import org.gridsuite.modification.server.dto.catalog.LineTypeInfos;
1012
import org.gridsuite.modification.server.entities.catalog.LineTypeEntity;
1113
import org.gridsuite.modification.server.repositories.LineTypesCatalogRepository;
1214
import org.springframework.stereotype.Service;
1315
import org.springframework.transaction.annotation.Transactional;
16+
import org.springframework.web.multipart.MultipartFile;
1417

15-
import java.util.*;
18+
import java.io.IOException;
19+
import java.io.UncheckedIOException;
20+
import java.util.List;
21+
import java.util.Optional;
22+
import java.util.Set;
23+
import java.util.UUID;
1624
import java.util.stream.Collectors;
25+
import java.util.zip.GZIPInputStream;
1726

1827
/**
1928
* @author Sylvain Bouzols <sylvain.bouzols at rte-france.com>
2029
*/
2130
@Service
2231
public class LineTypesCatalogService {
2332
private final LineTypesCatalogRepository lineTypesCatalogRepository;
33+
private final ObjectMapper mapper;
2434

25-
public LineTypesCatalogService(LineTypesCatalogRepository lineTypesCatalogRepository) {
35+
public LineTypesCatalogService(LineTypesCatalogRepository lineTypesCatalogRepository, ObjectMapper objectMapper) {
2636
this.lineTypesCatalogRepository = lineTypesCatalogRepository;
37+
this.mapper = objectMapper;
2738
}
2839

2940
@Transactional(readOnly = true)
@@ -43,14 +54,20 @@ public void deleteLineTypesCatalog() {
4354
lineTypesCatalogRepository.deleteAll();
4455
}
4556

46-
public void resetLineTypes(List<LineTypeInfos> lineTypes) {
47-
deleteLineTypesCatalog();
48-
// remove duplicates in file
49-
Set<LineTypeInfos> lineTypesSet = lineTypes.stream().collect(Collectors.toSet());
57+
public void resetLineTypes(MultipartFile file) {
58+
try (GZIPInputStream gzipInputStream = new GZIPInputStream(file.getInputStream())) {
59+
List<LineTypeInfos> lineTypes = mapper.readValue(gzipInputStream, new TypeReference<>() {
60+
});
61+
deleteLineTypesCatalog();
62+
// remove duplicates in file
63+
Set<LineTypeInfos> lineTypesSet = lineTypes.stream().collect(Collectors.toSet());
5064

51-
List<LineTypeEntity> lineTypesEntities = lineTypesSet.stream()
52-
.map(LineTypeInfos::toEntity)
53-
.collect(Collectors.toList());
54-
lineTypesCatalogRepository.saveAll(lineTypesEntities);
65+
List<LineTypeEntity> lineTypesEntities = lineTypesSet.stream()
66+
.map(LineTypeInfos::toEntity)
67+
.collect(Collectors.toList());
68+
lineTypesCatalogRepository.saveAll(lineTypesEntities);
69+
} catch (IOException e) {
70+
throw new UncheckedIOException(e);
71+
}
5572
}
5673
}

src/test/java/org/gridsuite/modification/server/ModificationControllerTest.java

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import com.powsybl.network.store.client.NetworkStoreService;
2020
import com.powsybl.network.store.client.PreloadingStrategy;
2121
import com.powsybl.network.store.iidm.impl.NetworkFactoryImpl;
22+
import jakarta.servlet.ServletException;
2223
import org.apache.commons.collections4.ListUtils;
2324
import org.apache.commons.lang3.tuple.Pair;
2425
import org.gridsuite.modification.NetworkModificationException;
@@ -53,9 +54,13 @@
5354
import org.springframework.boot.test.context.SpringBootTest;
5455
import org.springframework.boot.test.mock.mockito.MockBean;
5556
import org.springframework.http.MediaType;
57+
import org.springframework.mock.web.MockMultipartFile;
5658
import org.springframework.test.web.servlet.MockMvc;
5759
import org.springframework.test.web.servlet.MvcResult;
60+
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
5861

62+
import java.io.IOException;
63+
import java.io.InputStream;
5964
import java.util.*;
6065
import java.util.stream.Collectors;
6166

@@ -102,9 +107,10 @@ class ModificationControllerTest {
102107
private static final String URI_COMPOSITE_NETWORK_MODIF_BASE = "/v1/network-composite-modifications";
103108
private static final String URI_GET_COMPOSITE_NETWORK_MODIF_CONTENT = "/v1/network-composite-modification/";
104109
private static final String URI_LINE_CATALOG = URI_NETWORK_MODIF_BASE + "/catalog/line_types";
105-
private static final String LINE_TYPES_CATALOG_JSON_FILE_1 = "/lines-catalog.json";
106-
private static final String LINE_TYPES_CATALOG_JSON_FILE_2 = "/line_types_catalog_2.json";
107-
private static final String LINE_TYPES_CATALOG_JSON_FILE_3 = "/line_types_catalog_3.json";
110+
private static final String LINE_TYPES_CATALOG_JSON_FILE_1 = "/lines-catalog.json.gz";
111+
private static final String LINE_TYPES_CATALOG_JSON_FILE_2 = "/line_types_catalog_2.json.gz";
112+
private static final String LINE_TYPES_CATALOG_JSON_FILE_3 = "/line_types_catalog_3.json.gz";
113+
private static final String NOT_EXISTING_JSON_FILE = "/not_existing_file.json.gz";
108114
private static final String NETWORK_MODIFICATION_URI = URI_NETWORK_MODIF_BASE + "?groupUuid=" + TEST_GROUP_ID;
109115

110116
@Autowired
@@ -1514,9 +1520,9 @@ void testGetLineTypesCatalog() throws Exception {
15141520
assertEquals(0, emptyLineTypes.size());
15151521

15161522
// Create the catalog with some line types
1517-
String lineTypesCatalogJson1 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_1);
1518-
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson1).contentType(MediaType.APPLICATION_JSON))
1519-
.andExpect(status().isOk());
1523+
mockMvc.perform(multipart(URI_LINE_CATALOG)
1524+
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_1)))
1525+
.andExpect(status().isOk());
15201526

15211527
// Check if the catalog is complete avoiding the duplicate entry
15221528
mvcResult = mockMvc
@@ -1529,9 +1535,9 @@ void testGetLineTypesCatalog() throws Exception {
15291535
assertEquals(8, lineTypes.size());
15301536

15311537
// Check if catalog is completely updated
1532-
String lineTypesCatalogJson2 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_2);
1533-
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson2).contentType(MediaType.APPLICATION_JSON))
1534-
.andExpect(status().isOk());
1538+
mockMvc.perform(multipart(URI_LINE_CATALOG)
1539+
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_2)))
1540+
.andExpect(status().isOk());
15351541

15361542
mvcResult = mockMvc
15371543
.perform(get(URI_LINE_CATALOG).contentType(MediaType.APPLICATION_JSON))
@@ -1570,8 +1576,8 @@ void testGetLineTypeWithLimitsCatalog() throws Exception {
15701576
assertEquals(0, emptyLineTypes.size());
15711577

15721578
// Create the catalog with some line types
1573-
String lineTypesCatalogJson1 = TestUtils.resourceToString(LINE_TYPES_CATALOG_JSON_FILE_3);
1574-
mockMvc.perform(post(URI_LINE_CATALOG).content(lineTypesCatalogJson1).contentType(MediaType.APPLICATION_JSON))
1579+
mockMvc.perform(multipart(URI_LINE_CATALOG)
1580+
.file(createMockMultipartFile(LINE_TYPES_CATALOG_JSON_FILE_3)))
15751581
.andExpect(status().isOk());
15761582

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

1609+
private MockMultipartFile createMockMultipartFile(String fileName) throws IOException {
1610+
try (InputStream inputStream = getClass().getResourceAsStream(fileName)) {
1611+
return new MockMultipartFile("file", fileName, MediaType.MULTIPART_FORM_DATA_VALUE, inputStream);
1612+
}
1613+
}
1614+
1615+
@Test
1616+
void testPostLineTypeWithLimitsCatalogError() throws IOException {
1617+
MockMultipartHttpServletRequestBuilder mockMultipartHttpServletRequestBuilder = multipart(URI_LINE_CATALOG)
1618+
.file(createMockMultipartFile(NOT_EXISTING_JSON_FILE));
1619+
String message = assertThrows(ServletException.class, () -> mockMvc.perform(mockMultipartHttpServletRequestBuilder)).getMessage();
1620+
assertEquals("Request processing failed: java.io.UncheckedIOException: java.io.EOFException", message);
1621+
}
1622+
16031623
@Test
16041624
void testCreateVoltageInitModification() throws Exception {
16051625
// Create the modification

src/test/resources/line_types_catalog_2.json

Lines changed: 0 additions & 28 deletions
This file was deleted.
301 Bytes
Binary file not shown.

src/test/resources/line_types_catalog_3.json

Lines changed: 0 additions & 66 deletions
This file was deleted.
479 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)