Skip to content

Commit

Permalink
clean PR
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH committed Mar 22, 2024
1 parent cd42064 commit e5c47e8
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 155 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@
<artifactId>assertj-guava</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,29 @@ public VoltageInitResultContext(UUID resultUuid, VoltageInitRunContext runContex
}

private static String getNonNullHeader(MessageHeaders headers, String name) {
final String header = headers.get(name, String.class);
String header = (String) headers.get(name);
if (header == null) {
throw new PowsyblException("Header '" + name + "' not found");
} else {
return header;
}
return header;
}

public static VoltageInitResultContext fromMessage(Message<String> message) {
Objects.requireNonNull(message);
MessageHeaders headers = message.getHeaders();
UUID resultUuid = UUID.fromString(getNonNullHeader(headers, "resultUuid"));
UUID networkUuid = UUID.fromString(getNonNullHeader(headers, "networkUuid"));
String variantId = headers.get(VARIANT_ID_HEADER, String.class);
String receiver = headers.get(HEADER_RECEIVER, String.class);
String userId = headers.get(HEADER_USER_ID, String.class);
@SuppressWarnings("unchecked")
Map<String, Double> voltageLevelsIdsRestricted = headers.get(VOLTAGE_LEVELS_IDS_RESTRICTED, Map.class);
UUID parametersUuid = headers.containsKey(PARAMETERS_UUID_HEADER) ? UUID.fromString(headers.get(PARAMETERS_UUID_HEADER, String.class)) : null;
UUID reportUuid = headers.containsKey(REPORT_UUID_HEADER) ? UUID.fromString(headers.get(REPORT_UUID_HEADER, String.class)) : null;
String reporterId = headers.containsKey(REPORTER_ID_HEADER) ? headers.get(REPORTER_ID_HEADER, String.class) : null;
String reportType = headers.containsKey(REPORT_TYPE_HEADER) ? headers.get(REPORT_TYPE_HEADER, String.class) : null;
return new VoltageInitResultContext(resultUuid, new VoltageInitRunContext(networkUuid, variantId, receiver, reportUuid, reporterId, reportType, userId, parametersUuid, voltageLevelsIdsRestricted));
String variantId = (String) headers.get(VARIANT_ID_HEADER);
String receiver = (String) headers.get(HEADER_RECEIVER);
String userId = (String) headers.get(HEADER_USER_ID);
Map<String, Double> voltageLevelsIdsRestricted = (Map<String, Double>) headers.get(VOLTAGE_LEVELS_IDS_RESTRICTED);

UUID parametersUuid = headers.containsKey(PARAMETERS_UUID_HEADER) ? UUID.fromString((String) headers.get(PARAMETERS_UUID_HEADER)) : null;
UUID reportUuid = headers.containsKey(REPORT_UUID_HEADER) ? UUID.fromString((String) headers.get(REPORT_UUID_HEADER)) : null;
String reporterId = headers.containsKey(REPORTER_ID_HEADER) ? (String) headers.get(REPORTER_ID_HEADER) : null;
String reportType = headers.containsKey(REPORT_TYPE_HEADER) ? (String) headers.get(REPORT_TYPE_HEADER) : null;
VoltageInitRunContext runContext = new VoltageInitRunContext(networkUuid, variantId, receiver, reportUuid, reporterId, reportType, userId, parametersUuid, voltageLevelsIdsRestricted);
return new VoltageInitResultContext(resultUuid, runContext);
}

public Message<String> toMessage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
*/
package org.gridsuite.voltageinit.server.service;

import com.powsybl.network.store.client.NetworkStoreService;

import org.gridsuite.voltageinit.server.dto.ReactiveSlack;
import org.gridsuite.voltageinit.server.dto.VoltageInitResult;
import org.gridsuite.voltageinit.server.dto.VoltageInitStatus;
import org.gridsuite.voltageinit.server.entities.VoltageInitResultEntity;
import org.gridsuite.voltageinit.server.repository.VoltageInitResultRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -22,6 +25,7 @@
/**
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@ComponentScan(basePackageClasses = {NetworkStoreService.class})
@Service
public class VoltageInitService {
@Autowired
Expand All @@ -45,8 +49,9 @@ public VoltageInitService(NotificationService notificationService,
}

public UUID runAndSaveResult(UUID networkUuid, String variantId, String receiver, UUID reportUuid, String reporterId, String userId, String reportType, UUID parametersUuid) {
final VoltageInitRunContext runContext = new VoltageInitRunContext(networkUuid, variantId, receiver, reportUuid, reporterId, reportType, userId, parametersUuid);
final UUID resultUuid = uuidGeneratorService.generate();
VoltageInitRunContext runContext = new VoltageInitRunContext(networkUuid, variantId, receiver, reportUuid, reporterId, reportType, userId, parametersUuid, new HashMap<>());
Objects.requireNonNull(runContext);
var resultUuid = uuidGeneratorService.generate();

// update status to running status
setStatus(List.of(resultUuid), VoltageInitStatus.RUNNING.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public UUID createParameters(VoltageInitParametersInfos parametersInfos) {
}

public Optional<UUID> duplicateParameters(UUID sourceParametersId) {
return voltageInitParametersRepository.findById(sourceParametersId)
.map(VoltageInitParametersEntity::toVoltageInitParametersInfos)
.map(VoltageInitParametersEntity::new)
.map(entity -> {
voltageInitParametersRepository.save(entity);
return entity.getId();
});
Optional<VoltageInitParametersInfos> sourceVoltageInitParametersInfos = voltageInitParametersRepository.findById(sourceParametersId).map(VoltageInitParametersEntity::toVoltageInitParametersInfos);
if (sourceVoltageInitParametersInfos.isPresent()) {
VoltageInitParametersEntity entity = new VoltageInitParametersEntity(sourceVoltageInitParametersInfos.get());
voltageInitParametersRepository.save(entity);
return Optional.of(entity.getId());
}
return Optional.empty();
}

public VoltageInitParametersInfos getParameters(UUID parametersUuid) {
Expand Down Expand Up @@ -101,68 +101,50 @@ private static void fillSpecificVoltageLimits(List<VoltageLimitOverride> specifi
Map<String, VoltageLimitEntity> voltageLevelDefaultLimits,
VoltageLevel voltageLevel,
Map<String, Double> voltageLevelsIdsRestricted) {
if (voltageLevelDefaultLimits.containsKey(voltageLevel.getId()) || voltageLevelModificationLimits.containsKey(voltageLevel.getId())) {
setLowVoltageLimit(specificVoltageLimits, voltageLevelModificationLimits, voltageLevelDefaultLimits, voltageLevel, voltageLevelsIdsRestricted);
setHighVoltageLimit(specificVoltageLimits, voltageLevelModificationLimits, voltageLevelDefaultLimits, voltageLevel);
}
setLowVoltageLimit(specificVoltageLimits, voltageLevelModificationLimits, voltageLevelDefaultLimits, voltageLevel, voltageLevelsIdsRestricted);
setHighVoltageLimit(specificVoltageLimits, voltageLevelModificationLimits, voltageLevelDefaultLimits, voltageLevel);
}

private static void setLowVoltageLimit(List<VoltageLimitOverride> specificVoltageLimits,
Map<String, VoltageLimitEntity> voltageLevelModificationLimits,
Map<String, VoltageLimitEntity> voltageLevelDefaultLimits,
VoltageLevel voltageLevel,
Map<String, Double> voltageLevelsIdsRestricted) {
final String voltageLevelId = voltageLevel.getId();
final boolean isLowVoltageLimitModificationSet = voltageLevelModificationLimits.containsKey(voltageLevelId) && voltageLevelModificationLimits.get(voltageLevelId).getLowVoltageLimit() != null;
final double lowVoltageLimit = voltageLevel.getLowVoltageLimit();
boolean isLowVoltageLimitModificationSet = voltageLevelModificationLimits.containsKey(voltageLevel.getId()) && voltageLevelModificationLimits.get(voltageLevel.getId()).getLowVoltageLimit() != null;
boolean isLowVoltageLimitDefaultSet = voltageLevelDefaultLimits.containsKey(voltageLevel.getId()) && voltageLevelDefaultLimits.get(voltageLevel.getId()).getLowVoltageLimit() != null;
double newLowVoltageLimit;
if (!Double.isNaN(lowVoltageLimit) && isLowVoltageLimitModificationSet) {
double lowVoltageLimitModification = voltageLevelModificationLimits.get(voltageLevelId).getLowVoltageLimit();
if (lowVoltageLimit + lowVoltageLimitModification < 0) {
newLowVoltageLimit = lowVoltageLimit * -1;
voltageLevelsIdsRestricted.put(voltageLevelId, newLowVoltageLimit);
if (!Double.isNaN(voltageLevel.getLowVoltageLimit()) && isLowVoltageLimitModificationSet) {
double lowVoltageLimitModification = voltageLevelModificationLimits.get(voltageLevel.getId()).getLowVoltageLimit();
if (voltageLevel.getLowVoltageLimit() + lowVoltageLimitModification < 0) {
newLowVoltageLimit = voltageLevel.getLowVoltageLimit() * -1;
voltageLevelsIdsRestricted.put(voltageLevel.getId(), newLowVoltageLimit);
} else {
newLowVoltageLimit = lowVoltageLimitModification;
}
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevelId, VoltageLimitType.LOW_VOLTAGE_LIMIT, true, newLowVoltageLimit));
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevel.getId(), VoltageLimitType.LOW_VOLTAGE_LIMIT, true, newLowVoltageLimit));

} else if (Double.isNaN(lowVoltageLimit)
&& voltageLevelDefaultLimits.containsKey(voltageLevelId)
&& voltageLevelDefaultLimits.get(voltageLevelId).getLowVoltageLimit() != null) {
double voltageLimit = voltageLevelDefaultLimits.get(voltageLevelId).getLowVoltageLimit() + (isLowVoltageLimitModificationSet ? voltageLevelModificationLimits.get(voltageLevelId).getLowVoltageLimit() : 0.);
} else if (Double.isNaN(voltageLevel.getLowVoltageLimit()) && isLowVoltageLimitDefaultSet) {
double voltageLimit = voltageLevelDefaultLimits.get(voltageLevel.getId()).getLowVoltageLimit() + (isLowVoltageLimitModificationSet ? voltageLevelModificationLimits.get(voltageLevel.getId()).getLowVoltageLimit() : 0.);
if (voltageLimit < 0) {
newLowVoltageLimit = 0.0;
voltageLevelsIdsRestricted.put(voltageLevelId, newLowVoltageLimit);
voltageLevelsIdsRestricted.put(voltageLevel.getId(), newLowVoltageLimit);
} else {
newLowVoltageLimit = voltageLimit;
}
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevelId, VoltageLimitType.LOW_VOLTAGE_LIMIT, false, newLowVoltageLimit));
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevel.getId(), VoltageLimitType.LOW_VOLTAGE_LIMIT, false, newLowVoltageLimit));
}
}

private static void setHighVoltageLimit(List<VoltageLimitOverride> specificVoltageLimits,
Map<String, VoltageLimitEntity> voltageLevelModificationLimits,
Map<String, VoltageLimitEntity> voltageLevelDefaultLimits,
VoltageLevel voltageLevel) {
final String voltageLevelId = voltageLevel.getId();
final boolean isHighVoltageLimitModificationSet = voltageLevelModificationLimits.containsKey(voltageLevelId) && voltageLevelModificationLimits.get(voltageLevelId).getHighVoltageLimit() != null;
final double highVoltageLimit = voltageLevel.getHighVoltageLimit();
if (!Double.isNaN(highVoltageLimit) && isHighVoltageLimitModificationSet) {
specificVoltageLimits.add(new VoltageLimitOverride(
voltageLevelId,
VoltageLimitType.HIGH_VOLTAGE_LIMIT,
true,
voltageLevelModificationLimits.get(voltageLevelId).getHighVoltageLimit()
));
} else if (Double.isNaN(highVoltageLimit)
&& voltageLevelDefaultLimits.containsKey(voltageLevelId)
&& voltageLevelDefaultLimits.get(voltageLevelId).getHighVoltageLimit() != null) {
specificVoltageLimits.add(new VoltageLimitOverride(
voltageLevelId,
VoltageLimitType.HIGH_VOLTAGE_LIMIT,
false,
voltageLevelDefaultLimits.get(voltageLevelId).getHighVoltageLimit() + (isHighVoltageLimitModificationSet ? voltageLevelModificationLimits.get(voltageLevelId).getHighVoltageLimit() : 0.)
));
boolean isHighVoltageLimitModificationSet = voltageLevelModificationLimits.containsKey(voltageLevel.getId()) && voltageLevelModificationLimits.get(voltageLevel.getId()).getHighVoltageLimit() != null;
boolean isHighVoltageLimitDefaultSet = voltageLevelDefaultLimits.containsKey(voltageLevel.getId()) && voltageLevelDefaultLimits.get(voltageLevel.getId()).getHighVoltageLimit() != null;
if (!Double.isNaN(voltageLevel.getHighVoltageLimit()) && isHighVoltageLimitModificationSet) {
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevel.getId(), VoltageLimitType.HIGH_VOLTAGE_LIMIT, true, voltageLevelModificationLimits.get(voltageLevel.getId()).getHighVoltageLimit()));
} else if (Double.isNaN(voltageLevel.getHighVoltageLimit()) && isHighVoltageLimitDefaultSet) {
specificVoltageLimits.add(new VoltageLimitOverride(voltageLevel.getId(), VoltageLimitType.HIGH_VOLTAGE_LIMIT, false, voltageLevelDefaultLimits.get(voltageLevel.getId()).getHighVoltageLimit() + (isHighVoltageLimitModificationSet ? voltageLevelModificationLimits.get(voltageLevel.getId()).getHighVoltageLimit() : 0.)));
}
}

Expand Down Expand Up @@ -255,14 +237,14 @@ public OpenReacParameters buildOpenReacParameters(VoltageInitRunContext context,
private List<String> toEquipmentIdsList(UUID networkUuid, String variantId, List<FilterEquipmentsEmbeddable> filters) {
if (filters == null || filters.isEmpty()) {
return List.of();
} else {
return filterService.exportFilters(filters.stream().map(FilterEquipmentsEmbeddable::getFilterId).toList(), networkUuid, variantId)
.stream()
.map(FilterEquipments::getIdentifiableAttributes)
.flatMap(List::stream)
.map(IdentifiableAttributes::getId)
.distinct()
.toList();
}
List<FilterEquipments> equipments = filterService.exportFilters(filters.stream().map(FilterEquipmentsEmbeddable::getFilterId).toList(), networkUuid, variantId);
Set<String> ids = new HashSet<>();
equipments.forEach(filterEquipment ->
filterEquipment.getIdentifiableAttributes().forEach(identifiableAttribute ->
ids.add(identifiableAttribute.getId())
)
);
return new ArrayList<>(ids);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,35 @@
*/
package org.gridsuite.voltageinit.server;

import org.junit.jupiter.api.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import static org.junit.Assert.assertEquals;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@RunWith(SpringRunner.class)
@AutoConfigureMockMvc
@SpringBootTest
class SupervisionControllerTest {
public class SupervisionControllerTest {

@Autowired
private MockMvc mockMvc;

@Test
void testResultCount() throws Exception {
public void testResultCount() throws Exception {
//get the result timeline uuid of the calculation
MvcResult mvcResult = mockMvc.perform(get("/v1/supervision/results-count"))
.andExpect(status().isOk())
Expand All @@ -39,6 +43,7 @@ void testResultCount() throws Exception {

String resultCount = mvcResult.getResponse().getContentAsString();
assertEquals("0", resultCount);

}
}

Expand Down
Loading

0 comments on commit e5c47e8

Please sign in to comment.