Skip to content

Commit f27d6f8

Browse files
review
1 parent fdd4078 commit f27d6f8

File tree

3 files changed

+62
-40
lines changed

3 files changed

+62
-40
lines changed

src/main/java/org/gridsuite/shortcircuit/server/ShortCircuitController.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import io.swagger.v3.oas.annotations.responses.ApiResponses;
1818
import io.swagger.v3.oas.annotations.tags.Tag;
1919
import org.gridsuite.shortcircuit.server.dto.*;
20-
import org.gridsuite.shortcircuit.server.service.ShortCircuitRunContext;
2120
import org.gridsuite.shortcircuit.server.service.ShortCircuitService;
2221
import org.springframework.data.domain.Page;
2322
import org.springframework.data.domain.Pageable;
@@ -26,13 +25,13 @@
2625
import org.springframework.web.bind.annotation.*;
2726

2827
import java.util.List;
29-
import java.util.Objects;
3028
import java.util.UUID;
3129

3230
import static com.powsybl.shortcircuit.Fault.FaultType;
3331
import static org.gridsuite.shortcircuit.server.service.NotificationService.HEADER_USER_ID;
34-
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;
35-
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM_VALUE;
32+
import static org.gridsuite.shortcircuit.server.util.Utils.APPLICATION_JSON_PATCH_VALUE;
33+
import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
34+
import static org.springframework.http.MediaType.*;
3635

3736
/**
3837
* @author Etienne Homer <etienne.homer at rte-france.com>
@@ -47,23 +46,23 @@ public ShortCircuitController(ShortCircuitService shortCircuitService) {
4746
this.shortCircuitService = shortCircuitService;
4847
}
4948

50-
@PostMapping(value = "/networks/{networkUuid}/run-and-save", produces = APPLICATION_JSON_VALUE, consumes = APPLICATION_JSON_VALUE)
49+
@PostMapping(value = "/networks/{networkUuid}/run-and-save", produces = TEXT_PLAIN_VALUE, consumes = {APPLICATION_JSON_VALUE, APPLICATION_JSON_PATCH_VALUE})
5150
@Operation(summary = "Run a short circuit analysis on a network")
52-
@ApiResponses(value = {@ApiResponse(responseCode = "200",
53-
description = "The short circuit analysis has been performed",
54-
content = {@Content(mediaType = APPLICATION_JSON_VALUE,
55-
schema = @Schema(implementation = ShortCircuitParameters.class))})})
51+
@ApiResponse(responseCode = "200", description = "The short circuit analysis has been performed")
52+
@io.swagger.v3.oas.annotations.parameters.RequestBody(required = false, content = {
53+
@Content(schema = @Schema(implementation = ShortCircuitParameters.class))})
5654
public ResponseEntity<UUID> runAndSave(@Parameter(description = "Network UUID") @PathVariable("networkUuid") UUID networkUuid,
5755
@Parameter(description = "Variant Id") @RequestParam(name = "variantId", required = false) String variantId,
5856
@Parameter(description = "Result receiver") @RequestParam(name = "receiver", required = false) String receiver,
5957
@Parameter(description = "reportUuid") @RequestParam(name = "reportUuid", required = false) UUID reportUuid,
6058
@Parameter(description = "reporterId") @RequestParam(name = "reporterId", required = false) String reporterId,
6159
@Parameter(description = "The type name for the report") @RequestParam(name = "reportType", required = false) String reportType,
6260
@Parameter(description = "Bus Id - Used for analysis targeting one bus") @RequestParam(name = "busId", required = false) String busId,
63-
@RequestBody(required = false) ShortCircuitParameters parameters,
61+
@Parameter(description = "ID of parameters to use") @RequestParam(name = "parametersUuid", required = false) UUID parametersUuid,
62+
@RequestHeader(value = CONTENT_TYPE, required = false) MediaType contentType,
63+
@RequestBody(required = false) String parametersPatch,
6464
@RequestHeader(HEADER_USER_ID) String userId) {
65-
final ShortCircuitParameters nonNullParameters = Objects.requireNonNullElseGet(parameters, ShortCircuitParameters::new);
66-
return ResponseEntity.ok(shortCircuitService.runAndSaveResult(new ShortCircuitRunContext(networkUuid, variantId, receiver, nonNullParameters, reportUuid, reporterId, reportType, userId, busId)));
65+
return ResponseEntity.ok().contentType(TEXT_PLAIN).body(shortCircuitService.runAndSaveResult(networkUuid, variantId, receiver, reportUuid, reporterId, reportType, userId, busId, parametersUuid, contentType, parametersPatch));
6766
}
6867

6968
@GetMapping(value = "/results/{resultUuid}", produces = APPLICATION_JSON_VALUE)

src/main/java/org/gridsuite/shortcircuit/server/service/ShortCircuitService.java

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.univocity.parsers.csv.CsvWriterSettings;
2121
import jakarta.persistence.EntityManager;
2222
import lombok.SneakyThrows;
23+
import org.apache.commons.lang3.StringUtils;
2324
import org.gridsuite.shortcircuit.server.ShortCircuitException;
2425
import org.gridsuite.shortcircuit.server.dto.*;
2526
import org.gridsuite.shortcircuit.server.entities.*;
@@ -80,9 +81,22 @@ public ShortCircuitService(NotificationService notificationService,
8081
this.entityManager = entityManager;
8182
}
8283

83-
public UUID runAndSaveResult(ShortCircuitRunContext runContext) {
84-
Objects.requireNonNull(runContext);
85-
var resultUuid = uuidGeneratorService.generate();
84+
@SneakyThrows
85+
public UUID runAndSaveResult(UUID networkUuid, String variantId, String receiver, UUID reportUuid, String reporterId, String reportType,
86+
String userId, String busId,
87+
@Nullable final UUID parametersUuid, @Nullable final MediaType contentType, @Nullable final String parametersPatch) {
88+
//TODO Objects.requireNonNull(???)
89+
final UUID resultUuid = uuidGeneratorService.generate();
90+
ShortCircuitParameters parameters = Optional.ofNullable(parametersUuid)
91+
.flatMap(parametersRepository::findById)
92+
.map(ShortCircuitService::fromEntity)
93+
.map(ShortCircuitParametersInfos::parameters)
94+
.orElseGet(ShortCircuitService::getDefaultShortCircuitParameters);
95+
if (parametersPatch != null) {
96+
parameters = updateDto(parameters, contentType, parametersPatch, ShortCircuitParameters.class);
97+
}
98+
parameters.setWithFortescueResult(StringUtils.isBlank(busId));
99+
ShortCircuitRunContext runContext = new ShortCircuitRunContext(networkUuid, variantId, receiver, parameters, reportUuid, reporterId, reportType, userId, busId);
86100

87101
// update status to running status
88102
setStatus(List.of(resultUuid), ShortCircuitAnalysisStatus.RUNNING.name());
@@ -445,13 +459,13 @@ public Optional<UUID> duplicateParameters(UUID sourceParametersUuid) {
445459
.map(AnalysisParametersEntity::getId);
446460
}
447461

448-
private ShortCircuitParametersInfos updateDto(ShortCircuitParametersInfos parameters, @Nullable final MediaType contentType, final String parametersPatch)
462+
private <T> T updateDto(T parameters, @Nullable final MediaType contentType, final String parametersPatch, final Class<T> clazz)
449463
throws IllegalArgumentException, IOException, JsonPatchException {
450464
if (contentType == null) {
451465
throw new IllegalArgumentException("Content-Type is mandatory");
452466
} else if (Utils.APPLICATION_JSON_PATCH.equalsTypeAndSubtype(contentType)) {
453467
return objectMapper.treeToValue(JsonPatch.fromJson(objectMapper.readValue(parametersPatch, ArrayNode.class))
454-
.apply(objectMapper.valueToTree(parameters)), ShortCircuitParametersInfos.class);
468+
.apply(objectMapper.valueToTree(parameters)), clazz);
455469
} else if (MediaType.APPLICATION_JSON.equalsTypeAndSubtype(contentType)) {
456470
return objectMapper.readerForUpdating(parameters).readValue(parametersPatch);
457471
} else {
@@ -463,7 +477,7 @@ private ShortCircuitParametersInfos updateDto(ShortCircuitParametersInfos parame
463477
public UUID createParameters(@Nullable final MediaType contentType, @Nullable final String parametersPatch) {
464478
ShortCircuitParametersInfos parameters = getDefaultDtoParameters();
465479
if (parametersPatch != null) {
466-
parameters = updateDto(parameters, contentType, parametersPatch);
480+
parameters = updateDto(parameters, contentType, parametersPatch, ShortCircuitParametersInfos.class);
467481
}
468482
return parametersRepository.save(toEntity(parameters)).getId();
469483
}
@@ -478,7 +492,7 @@ public boolean updateParameters(final UUID parametersUuid, @Nullable final Media
478492
// we simply reset
479493
updateEntity(parameters, getDefaultDtoParameters());
480494
} else {
481-
updateEntity(parameters, updateDto(fromEntity(parameters), contentType, parametersPatch));
495+
updateEntity(parameters, updateDto(fromEntity(parameters), contentType, parametersPatch, ShortCircuitParametersInfos.class));
482496
}
483497
parametersRepository.save(parameters);
484498
return true;

src/test/java/org/gridsuite/shortcircuit/server/ShortCircuitAnalysisControllerTest.java

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,13 @@ public void runTest() throws Exception {
358358
MvcResult result = mockMvc.perform(post(
359359
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=AllBusesShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
360360
.header(HEADER_USER_ID, "userId")
361+
.accept(MediaType.TEXT_PLAIN_VALUE)
361362
.contentType(MediaType.APPLICATION_JSON)
362363
.content(parametersJson))
363364
.andExpect(status().isOk())
364-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
365+
.andExpect(content().contentType(MediaType.TEXT_PLAIN_VALUE))
365366
.andReturn();
366-
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
367+
assertEquals(RESULT_UUID, UUID.fromString(result.getResponse().getContentAsString()));
367368

368369
Message<byte[]> resultMessage = output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
369370
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
@@ -510,12 +511,13 @@ public void testDeterministicResults() throws Exception {
510511
MvcResult result = mockMvc.perform(post(
511512
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=AllBusesShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
512513
.header(HEADER_USER_ID, "userId")
514+
.accept(MediaType.TEXT_PLAIN)
513515
.contentType(MediaType.APPLICATION_JSON)
514516
.content(parametersJson))
515517
.andExpect(status().isOk())
516-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
518+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
517519
.andReturn();
518-
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
520+
assertEquals(RESULT_UUID, UUID.fromString(result.getResponse().getContentAsString()));
519521

520522
Message<byte[]> resultMessage = output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
521523
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
@@ -561,12 +563,13 @@ public void runWithBusIdTest() throws Exception {
561563
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
562564
.param(HEADER_BUS_ID, "NGEN")
563565
.header(HEADER_USER_ID, "userId")
566+
.accept(MediaType.TEXT_PLAIN)
564567
.contentType(MediaType.APPLICATION_JSON)
565568
.content(parametersJson))
566569
.andExpect(status().isOk())
567-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
570+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
568571
.andReturn();
569-
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
572+
assertEquals(RESULT_UUID, UUID.fromString(result.getResponse().getContentAsString()));
570573

571574
Message<byte[]> resultMessage = output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
572575
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
@@ -624,12 +627,13 @@ public void runWithBusBarSectionIdTest() throws Exception {
624627
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
625628
.param(HEADER_BUS_ID, "S1VL2_BBS1")
626629
.header(HEADER_USER_ID, "userId")
630+
.accept(MediaType.TEXT_PLAIN)
627631
.contentType(MediaType.APPLICATION_JSON)
628632
.content(parametersJson))
629633
.andExpect(status().isOk())
630-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
634+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
631635
.andReturn();
632-
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
636+
assertEquals(RESULT_UUID, UUID.fromString(result.getResponse().getContentAsString()));
633637

634638
Message<byte[]> resultMessage = output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
635639
assertEquals(RESULT_UUID.toString(), resultMessage.getHeaders().get("resultUuid"));
@@ -662,11 +666,12 @@ public void runWithBusBarSectionIdAndErrorTest() throws Exception {
662666
MvcResult result = mockMvc.perform(post(
663667
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=OneBusShortCircuitAnalysis&receiver=me&variantId=" + NODE_BREAKER_NETWORK_VARIANT_ID, NODE_BREAKER_NETWORK_UUID)
664668
.param(HEADER_BUS_ID, "BUSBARSECTION_ID_NOT_EXISTING")
665-
.header(HEADER_USER_ID, "userId"))
669+
.header(HEADER_USER_ID, "userId")
670+
.accept(MediaType.TEXT_PLAIN))
666671
.andExpect(status().isOk())
667-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
672+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
668673
.andReturn();
669-
assertEquals(RESULT_UUID, mapper.readValue(result.getResponse().getContentAsString(), UUID.class));
674+
assertEquals(RESULT_UUID, UUID.fromString(result.getResponse().getContentAsString()));
670675

671676
Message<byte[]> runMessage = output.receive(TIMEOUT, shortCircuitAnalysisRunDestination);
672677
assertEquals(RESULT_UUID.toString(), runMessage.getHeaders().get("resultUuid"));
@@ -697,9 +702,10 @@ public void stopTest() throws Exception {
697702

698703
mockMvc.perform(post(
699704
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reportType=AllBusesShortCircuitAnalysis&receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
700-
.header(HEADER_USER_ID, "userId"))
705+
.header(HEADER_USER_ID, "userId")
706+
.accept(MediaType.TEXT_PLAIN))
701707
.andExpect(status().isOk())
702-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
708+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
703709
.andReturn();
704710

705711
// stop shortcircuit analysis
@@ -767,9 +773,10 @@ public void runWithReportTest() {
767773

768774
mockMvc.perform(post(
769775
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportType=AllBusesShortCircuitAnalysis&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
770-
.header(HEADER_USER_ID, "user"))
776+
.header(HEADER_USER_ID, "user")
777+
.accept(MediaType.TEXT_PLAIN))
771778
.andExpect(status().isOk())
772-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
779+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
773780
.andReturn();
774781

775782
Message<byte[]> runMessage = output.receive(TIMEOUT, shortCircuitAnalysisRunDestination);
@@ -791,9 +798,10 @@ public void runWithNoShortcircuitDataTest() {
791798

792799
mockMvc.perform(post(
793800
"/" + VERSION + "/networks/{networkUuid}/run-and-save?reporterId=myReporter&receiver=me&reportType=AllBusesShortCircuitAnalysis&reportUuid=" + REPORT_UUID + "&variantId=" + VARIANT_2_ID, NETWORK_UUID)
794-
.header(HEADER_USER_ID, "user"))
801+
.header(HEADER_USER_ID, "user")
802+
.accept(MediaType.TEXT_PLAIN))
795803
.andExpect(status().isOk())
796-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
804+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
797805
.andReturn();
798806

799807
Message<byte[]> runMessage = output.receive(TIMEOUT, shortCircuitAnalysisRunDestination);
@@ -822,10 +830,11 @@ public void checkShortCircuitLimitsTest() throws Exception {
822830
mockMvc.perform(post(
823831
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_2_ID, NETWORK_UUID)
824832
.header(HEADER_USER_ID, "userId")
833+
.accept(MediaType.TEXT_PLAIN)
825834
.contentType(MediaType.APPLICATION_JSON)
826835
.content(parametersJson))
827836
.andExpect(status().isOk())
828-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
837+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
829838
.andReturn();
830839
output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
831840
output.receive(TIMEOUT, shortCircuitAnalysisRunDestination);
@@ -846,17 +855,17 @@ public void checkShortCircuitLimitsTest() throws Exception {
846855
mockMvc.perform(post(
847856
"/" + VERSION + "/networks/{networkUuid}/run-and-save?receiver=me&variantId=" + VARIANT_4_ID, NETWORK1_UUID)
848857
.header(HEADER_USER_ID, "userId")
858+
.accept(MediaType.TEXT_PLAIN)
849859
.contentType(MediaType.APPLICATION_JSON)
850860
.content(parametersJson))
851861
.andExpect(status().isOk())
852-
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
862+
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
853863
.andReturn();
854864

855865
output.receive(TIMEOUT, shortCircuitAnalysisResultDestination);
856866
output.receive(TIMEOUT, shortCircuitAnalysisRunDestination);
857867

858-
result = mockMvc.perform(get(
859-
"/" + VERSION + "/results/{resultUuid}", RESULT_UUID))
868+
result = mockMvc.perform(get("/" + VERSION + "/results/{resultUuid}", RESULT_UUID))
860869
.andExpect(status().isOk())
861870
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
862871
.andReturn();

0 commit comments

Comments
 (0)