Skip to content

Commit

Permalink
add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Seddik Yengui <seddik.yengui_externe@rte-france.com>
  • Loading branch information
Seddik Yengui committed Dec 24, 2024
1 parent 1988015 commit 94f7706
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public class RestNetworkStoreClient implements NetworkStoreClient {
private static final Logger LOGGER = LoggerFactory.getLogger(RestNetworkStoreClient.class);

private static final int RESOURCES_CREATION_CHUNK_SIZE = 1000;
public static final String RETRYING = "Retrying...";
private static final String STR_NETWORK = "network";
private static final String URL_NETWORK_UUID = "/networks/{networkUuid}";
private static final String STR_SUBSTATION = "substation";
Expand All @@ -54,6 +55,9 @@ public class RestNetworkStoreClient implements NetworkStoreClient {
private static final String STR_HVDC_LINE = "hvdc line";
private static final String STR_TIE_LINE = "tie line";
private static final String STR_GROUND = "ground";
public static final String STR_LOAD = "load";
public static final String STR_LINE = "line";
public static final String STR_CONFIGURED_BUSES = "bus";

private final RestClient restClient;

Expand Down Expand Up @@ -85,7 +89,7 @@ private <T extends IdentifiableAttributes> void create(String target, String url
} catch (ResourceAccessException e) {
LOGGER.error(e.toString(), e);
// retry only one time
LOGGER.info("Retrying...");
LOGGER.info(RETRYING);
restClient.createAll(url, resourcePartition, uriVariables);
}
stopwatch.stop();
Expand Down Expand Up @@ -184,7 +188,7 @@ private <T extends IdentifiableAttributes> void updateAll(String target, String
} catch (ResourceAccessException e) {
LOGGER.error(e.toString(), e);
// retry only one time
LOGGER.info("Retrying...");
LOGGER.info(RETRYING);
updatePartition(target, url, attributeFilter, resourcePartition, uriVariables);
}
stopwatch.stop();
Expand All @@ -209,7 +213,7 @@ private void removeAll(String target, String url, UUID networkUuid, int variantN
} catch (ResourceAccessException e) {
LOGGER.error(e.toString(), e);
// retry only one time
LOGGER.info("Retrying...");
LOGGER.info(RETRYING);
restClient.deleteAll(url, idsPartition, networkUuid, variantNum);
}
stopwatch.stop();
Expand Down Expand Up @@ -392,7 +396,7 @@ public void removeBatteries(UUID networkUuid, int variantNum, List<String> batte

@Override
public List<Resource<LoadAttributes>> getVoltageLevelLoads(UUID networkUuid, int variantNum, String voltageLevelId) {
return getAll("load", "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/loads", networkUuid, variantNum, voltageLevelId);
return getAll(STR_LOAD, "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/loads", networkUuid, variantNum, voltageLevelId);
}

@Override
Expand Down Expand Up @@ -457,12 +461,12 @@ public void removeThreeWindingsTransformers(UUID networkUuid, int variantNum, Li

@Override
public List<Resource<LineAttributes>> getVoltageLevelLines(UUID networkUuid, int variantNum, String voltageLevelId) {
return getAll("line", "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/lines", networkUuid, variantNum, voltageLevelId);
return getAll(STR_LINE, "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/lines", networkUuid, variantNum, voltageLevelId);
}

@Override
public void removeLines(UUID networkUuid, int variantNum, List<String> linesId) {
removeAll("line", "/networks/{networkUuid}/{variantNum}/lines", networkUuid, variantNum, linesId);
removeAll(STR_LINE, "/networks/{networkUuid}/{variantNum}/lines", networkUuid, variantNum, linesId);
}

@Override
Expand Down Expand Up @@ -523,27 +527,27 @@ public void updateBusbarSections(UUID networkUuid, List<Resource<BusbarSectionAt

@Override
public void createLoads(UUID networkUuid, List<Resource<LoadAttributes>> loadResources) {
create("load", "/networks/{networkUuid}/loads", loadResources, networkUuid);
create(STR_LOAD, "/networks/{networkUuid}/loads", loadResources, networkUuid);
}

@Override
public List<Resource<LoadAttributes>> getLoads(UUID networkUuid, int variantNum) {
return getAll("load", "/networks/{networkUuid}/{variantNum}/loads", networkUuid, variantNum);
return getAll(STR_LOAD, "/networks/{networkUuid}/{variantNum}/loads", networkUuid, variantNum);
}

@Override
public Optional<Resource<LoadAttributes>> getLoad(UUID networkUuid, int variantNum, String loadId) {
return get("load", "/networks/{networkUuid}/{variantNum}/loads/{loadId}", networkUuid, variantNum, loadId);
return get(STR_LOAD, "/networks/{networkUuid}/{variantNum}/loads/{loadId}", networkUuid, variantNum, loadId);
}

@Override
public void updateLoads(UUID networkUuid, List<Resource<LoadAttributes>> loadResources, AttributeFilter attributeFilter) {
updateAll("load", "/networks/{networkUuid}/loads", loadResources, attributeFilter, networkUuid);
updateAll(STR_LOAD, "/networks/{networkUuid}/loads", loadResources, attributeFilter, networkUuid);
}

@Override
public void removeLoads(UUID networkUuid, int variantNum, List<String> loadsId) {
removeAll("load", "/networks/{networkUuid}/{variantNum}/loads", networkUuid, variantNum, loadsId);
removeAll(STR_LOAD, "/networks/{networkUuid}/{variantNum}/loads", networkUuid, variantNum, loadsId);
}

// generator
Expand Down Expand Up @@ -638,22 +642,22 @@ public void updateThreeWindingsTransformers(UUID networkUuid, List<Resource<Thre

@Override
public void createLines(UUID networkUuid, List<Resource<LineAttributes>> lineResources) {
create("line", "/networks/{networkUuid}/lines", lineResources, networkUuid);
create(STR_LINE, "/networks/{networkUuid}/lines", lineResources, networkUuid);
}

@Override
public List<Resource<LineAttributes>> getLines(UUID networkUuid, int variantNum) {
return getAll("line", "/networks/{networkUuid}/{variantNum}/lines", networkUuid, variantNum);
return getAll(STR_LINE, "/networks/{networkUuid}/{variantNum}/lines", networkUuid, variantNum);
}

@Override
public Optional<Resource<LineAttributes>> getLine(UUID networkUuid, int variantNum, String lineId) {
return get("line", "/networks/{networkUuid}/{variantNum}/lines/{lineId}", networkUuid, variantNum, lineId);
return get(STR_LINE, "/networks/{networkUuid}/{variantNum}/lines/{lineId}", networkUuid, variantNum, lineId);
}

@Override
public void updateLines(UUID networkUuid, List<Resource<LineAttributes>> lineResources, AttributeFilter attributeFilter) {
updateAll("line", "/networks/{networkUuid}/lines", lineResources, attributeFilter, networkUuid);
updateAll(STR_LINE, "/networks/{networkUuid}/lines", lineResources, attributeFilter, networkUuid);
}

// shunt compensator
Expand Down Expand Up @@ -802,32 +806,32 @@ public void updateDanglingLines(UUID networkUuid, List<Resource<DanglingLineAttr

@Override
public void createConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesResources) {
create("bus", "/networks/{networkUuid}/configured-buses", busesResources, networkUuid);
create(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/configured-buses", busesResources, networkUuid);
}

@Override
public List<Resource<ConfiguredBusAttributes>> getConfiguredBuses(UUID networkUuid, int variantNum) {
return getAll("bus", "/networks/{networkUuid}/{variantNum}/configured-buses", networkUuid, variantNum);
return getAll(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/{variantNum}/configured-buses", networkUuid, variantNum);
}

@Override
public List<Resource<ConfiguredBusAttributes>> getVoltageLevelConfiguredBuses(UUID networkUuid, int variantNum, String voltageLevelId) {
return getAll("bus", "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/configured-buses", networkUuid, variantNum, voltageLevelId);
return getAll(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/{variantNum}/voltage-levels/{voltageLevelId}/configured-buses", networkUuid, variantNum, voltageLevelId);
}

@Override
public Optional<Resource<ConfiguredBusAttributes>> getConfiguredBus(UUID networkUuid, int variantNum, String busId) {
return get("bus", "/networks/{networkUuid}/{variantNum}/configured-buses/{busId}", networkUuid, variantNum, busId);
return get(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/{variantNum}/configured-buses/{busId}", networkUuid, variantNum, busId);
}

@Override
public void updateConfiguredBuses(UUID networkUuid, List<Resource<ConfiguredBusAttributes>> busesResources, AttributeFilter attributeFilter) {
updateAll("bus", "/networks/{networkUuid}/configured-buses", busesResources, attributeFilter, networkUuid);
updateAll(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/configured-buses", busesResources, attributeFilter, networkUuid);
}

@Override
public void removeConfiguredBuses(UUID networkUuid, int variantNum, List<String> busesId) {
removeAll("bus", "/networks/{networkUuid}/{variantNum}/configured-buses", networkUuid, variantNum, busesId);
removeAll(STR_CONFIGURED_BUSES, "/networks/{networkUuid}/{variantNum}/configured-buses", networkUuid, variantNum, busesId);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.HttpClientErrorException;

import java.io.IOException;
import java.time.ZonedDateTime;
Expand Down Expand Up @@ -233,16 +232,6 @@ public void setUp() throws IOException {
server.expect(requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/tie-lines"))
.andExpect(method(GET))
.andRespond(withSuccess(tieLineJson, MediaType.APPLICATION_JSON));

server.expect(requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/substations"))
.andExpect(method(DELETE))
.andExpect(content().string("[\"s1\"]"))
.andRespond(withSuccess());

server.expect(requestTo("/networks/" + networkUuid + "/" + Resource.INITIAL_VARIANT_NUM + "/substations"))
.andExpect(method(DELETE))
.andExpect(content().string("[]"))
.andRespond(withBadRequest());
}

@Test
Expand Down Expand Up @@ -309,10 +298,6 @@ public void test() {
tieLines = network.getTieLineStream().collect(Collectors.toList());
assertEquals(1, tieLines.size());
assertEquals("tieLine2", tieLines.get(0).getNameOrId());

assertDoesNotThrow(() -> restClient.deleteAll("/networks/{networkUuid}/{variantNum}/substations", List.of("s1"), networkUuid, Resource.INITIAL_VARIANT_NUM));
HttpClientErrorException httpClientErrorException = assertThrows(HttpClientErrorException.class, () -> restClient.deleteAll("/networks/{networkUuid}/{variantNum}/substations", List.of(), networkUuid, Resource.INITIAL_VARIANT_NUM));
assertEquals("400 Bad Request", httpClientErrorException.getMessage());
}
}

Expand Down

0 comments on commit 94f7706

Please sign in to comment.