Skip to content

Commit

Permalink
review & fix sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Oct 23, 2024
1 parent 71f42f4 commit b8a7d29
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ void testModificationGroups() throws Exception {
assertEquals(bsicListResult, List.of());
mvcResult = mockMvc.perform(post(URI_NETWORK_MODIF).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
assertApplicationStatusOK(mvcResult);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// switch opening to create the default group
mvcResult = mockMvc.perform(get("/v1/groups")).andExpectAll(
Expand Down Expand Up @@ -291,7 +291,7 @@ void testRestoreNetworkModifications() throws Exception {

mvcResult = mockMvc.perform(post(URI_NETWORK_MODIF).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
assertApplicationStatusOK(mvcResult);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

List<ModificationInfos> modifications = modificationRepository.getModifications(TEST_GROUP_ID, true, true, false);
assertEquals(1, modifications.size());
Expand Down Expand Up @@ -319,7 +319,7 @@ void testStashNetworkModifications() throws Exception {

mvcResult = mockMvc.perform(post(URI_NETWORK_MODIF).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
assertApplicationStatusOK(mvcResult);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

List<ModificationInfos> modifications = modificationRepository.getModifications(TEST_GROUP_ID, false, true);
assertEquals(1, modifications.size());
Expand Down Expand Up @@ -347,7 +347,7 @@ void testDisableNetworkModifications() throws Exception {

mvcResult = mockMvc.perform(post(URI_NETWORK_MODIF).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
assertApplicationStatusOK(mvcResult);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

List<ModificationInfos> modifications = modificationRepository.getModifications(TEST_GROUP_ID, true, true);
assertEquals(1, modifications.size());
Expand Down Expand Up @@ -375,7 +375,7 @@ void testDeleteModification() throws Exception {

mvcResult = mockMvc.perform(post(URI_NETWORK_MODIF).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
assertApplicationStatusOK(mvcResult);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

List<ModificationInfos> modifications = modificationRepository.getModifications(TEST_GROUP_ID, false, true);
assertEquals(1, modifications.size());
Expand Down Expand Up @@ -856,7 +856,7 @@ void testDuplicateModificationGroup() throws Exception {
.andReturn();
assertApplicationStatusOK(mvcResult);
String resultAsString = mvcResult.getResponse().getContentAsString();
testBranchCreationImpacts(mapper, resultAsString, IdentifiableType.LINE, "idLine1", Set.of("s1", "s2"));
testBranchCreationImpacts(mapper, resultAsString, Set.of("s1", "s2"));

testNetworkModificationsCount(TEST_GROUP_ID, 2);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ public static void testElementImpacts(ObjectMapper mapper, String resultAsString
assertThat(networkModificationResult.get()).recursivelyEquals(resultExpected);
}

public static void testElementModificationImpact(ObjectMapper mapper, String resultAsString, IdentifiableType elementType, String elementId, Set<String> substationIds) throws Exception {
testElementImpact(SimpleImpactType.MODIFICATION, mapper, resultAsString, elementType, elementId, substationIds);
public static void testElementModificationImpact(ObjectMapper mapper, String resultAsString, Set<String> substationIds) throws Exception {
testElementImpact(mapper, resultAsString, substationIds);
}

public static void testElementImpact(SimpleImpactType type, ObjectMapper mapper, String resultAsString, IdentifiableType elementType, String elementId, Set<String> substationIds) throws Exception {
public static void testElementImpact(ObjectMapper mapper, String resultAsString, Set<String> substationIds) throws Exception {
Optional<NetworkModificationResult> networkModificationResult = mapper.readValue(resultAsString, new TypeReference<>() { });
assertTrue(networkModificationResult.isPresent());
NetworkModificationResult resultExpected = NetworkModificationResult.builder()
Expand Down Expand Up @@ -122,7 +122,7 @@ public static void testBranchCreationImpacts(ObjectMapper mapper, String resultA
testBranchImpacts(mapper, SimpleImpactType.CREATION, resultAsString, branchType, branchId, breakerId1, disconnectorId1, substationId1, breakerId2, disconnectorId2, substationId2);
}

public static void testBranchCreationImpacts(ObjectMapper mapper, String resultAsString, IdentifiableType elementType, String elementId, Set<String> substationIds) throws Exception {
public static void testBranchCreationImpacts(ObjectMapper mapper, String resultAsString, Set<String> substationIds) throws Exception {
List<AbstractBaseImpact> substationsImpacts = createSubstationImpacts(substationIds);
testElementImpacts(mapper, resultAsString, substationsImpacts);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,12 @@ protected String getNetworkModificationUriWithBadVariant() {

protected abstract void assertAfterNetworkModificationDeletion();

@SuppressWarnings("java:S1130") // Exceptions are throws by overrides
protected void testCreationModificationMessage(ModificationInfos modificationInfos) throws Exception {
assertEquals("{}", modificationInfos.getMessageValues());
}

@SuppressWarnings("java:S1130") // Exceptions are throws by overrides
protected void testUpdateModificationMessage(ModificationInfos modificationInfos) throws Exception {
assertEquals("{}", modificationInfos.getMessageValues());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ void testEquipmentAttributeModificationInfos() throws Exception {
// switch opening
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "v1b1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// switch in variant VARIANT_ID opening
switchStatusModificationInfos.setEquipmentId("break1Variant");
switchStatusModificationInfosJson = mapper.writeValueAsString(switchStatusModificationInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri() + "&variantId=" + NetworkCreation.VARIANT_ID).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, "break1Variant", Set.of("s1Variant"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1Variant"));
}

@Test
Expand Down Expand Up @@ -108,21 +108,21 @@ private void switchModifications(String extraParams, String switchId1, String sw
switchStatusModificationInfos.setEquipmentAttributeValue(true);
switchStatusModificationInfosJson = mapper.writeValueAsString(switchStatusModificationInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri() + extraParams).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, switchId1, substationsIds);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), substationsIds);

// switch closing
switchStatusModificationInfos.setEquipmentId(switchId2);
switchStatusModificationInfos.setEquipmentAttributeValue(false);
switchStatusModificationInfosJson = mapper.writeValueAsString(switchStatusModificationInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri() + extraParams).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, switchId2, substationsIds);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), substationsIds);

// switch opening on another substation
switchStatusModificationInfos.setEquipmentId(switchId3);
switchStatusModificationInfos.setEquipmentAttributeValue(true);
switchStatusModificationInfosJson = mapper.writeValueAsString(switchStatusModificationInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri() + extraParams).content(switchStatusModificationInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.SWITCH, switchId3, otherSubstationsIds);
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), otherSubstationsIds);

testNetworkModificationsCount(getGroupId(), modificationsCount);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -557,14 +557,16 @@ void testGenerationDispatchWithSubstationsHierarchy() throws Exception {

@Test
void testGenerationDispatchErrorCheck() {
final Network network = Network.read("testGenerationDispatch.xiidm", getClass().getResourceAsStream("/testGenerationDispatch.xiidm"));
setNetwork(network);

GenerationDispatchInfos modification = GenerationDispatchInfos.builder().lossCoefficient(150.).defaultOutageRate(0.).build();
setNetwork(Network.read("testGenerationDispatch.xiidm", getClass().getResourceAsStream("/testGenerationDispatch.xiidm")));
final GenerationDispatch generationDispatch1 = new GenerationDispatch(modification);
assertThrows(NetworkModificationException.class, () -> generationDispatch1.check(getNetwork()), "GENERATION_DISPATCH_ERROR : The loss coefficient must be between 0 and 100");
assertThrows(NetworkModificationException.class, () -> generationDispatch1.check(network), "GENERATION_DISPATCH_ERROR : The loss coefficient must be between 0 and 100");

modification = GenerationDispatchInfos.builder().lossCoefficient(20.).defaultOutageRate(140.).build();
final GenerationDispatch generationDispatch2 = new GenerationDispatch(modification);
assertThrows(NetworkModificationException.class, () -> generationDispatch2.check(getNetwork()), "GENERATION_DISPATCH_ERROR : The default outage rate must be between 0 and 100");
assertThrows(NetworkModificationException.class, () -> generationDispatch2.check(network), "GENERATION_DISPATCH_ERROR : The default outage rate must be between 0 and 100");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
*/
package org.gridsuite.modification.server.modifications;

import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.Network;
import org.gridsuite.modification.server.NetworkModificationException;
import org.gridsuite.modification.server.dto.GroovyScriptInfos;
Expand Down Expand Up @@ -74,42 +73,42 @@ void testGroovy() throws Exception {
// apply groovy script with generator target P modification
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.GENERATOR, "idGenerator", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// apply groovy script with load type modification
groovyScriptInfos.setScript("network.getLoad('v1load').loadType=com.powsybl.iidm.network.LoadType.FICTITIOUS\n");
groovyScriptInfosJson = mapper.writeValueAsString(groovyScriptInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.LOAD, "v1load", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// apply groovy script with lcc converter station power factor modification
groovyScriptInfos.setScript("network.getLccConverterStation('v1lcc').powerFactor=1\n");
groovyScriptInfosJson = mapper.writeValueAsString(groovyScriptInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.HVDC_CONVERTER_STATION, "v1lcc", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// apply groovy script with line R modification
groovyScriptInfos.setScript("network.getLine('line1').r=2\n");
groovyScriptInfosJson = mapper.writeValueAsString(groovyScriptInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.LINE, "line1", Set.of("s1", "s2"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1", "s2"));

// apply groovy script with two windings transformer ratio tap modification
groovyScriptInfos.setScript("network.getTwoWindingsTransformer('trf1').getRatioTapChanger().tapPosition=2\n");
groovyScriptInfosJson = mapper.writeValueAsString(groovyScriptInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.TWO_WINDINGS_TRANSFORMER, "trf1", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

// apply groovy script with three windings transformer phase tap modification
groovyScriptInfos.setScript("network.getThreeWindingsTransformer('trf6').getLeg1().getPhaseTapChanger().tapPosition=0\n");
groovyScriptInfosJson = mapper.writeValueAsString(groovyScriptInfos);
mvcResult = mockMvc.perform(post(getNetworkModificationUri()).content(groovyScriptInfosJson).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk())
.andReturn();
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), IdentifiableType.THREE_WINDINGS_TRANSFORMER, "trf6", Set.of("s1"));
testElementModificationImpact(mapper, mvcResult.getResponse().getContentAsString(), Set.of("s1"));

testNetworkModificationsCount(getGroupId(), 6);
}
Expand Down
Loading

0 comments on commit b8a7d29

Please sign in to comment.