Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
<liquibase-hibernate-package>org.gridsuite.voltageinit.server</liquibase-hibernate-package>
<sonar.organization>gridsuite</sonar.organization>
<sonar.projectKey>org.gridsuite:voltage-init-server</sonar.projectKey>
<!-- To remove after when using gridsuite dependencies release and computation version containing merged PR: https://github.com/gridsuite/computation/pull/19 -->
<gridsuite-computation.version>1.7.0</gridsuite-computation.version>
<powsybl-ws-commons.version>1.34.0</powsybl-ws-commons.version>
<gridsuite-filter.version>1.15.0</gridsuite-filter.version>
</properties>

<build>
Expand Down Expand Up @@ -90,6 +94,24 @@

<dependencyManagement>
<dependencies>
<!-- overrides of imports -->
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<version>${powsybl-ws-commons.version}</version>
</dependency>

<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-computation</artifactId>
<version>${gridsuite-computation.version}</version>
</dependency>

<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-filter</artifactId>
<version>${gridsuite-filter.version}</version>
</dependency>

<dependency><!-- To remove when integrate in next release of gridsuite-dependencies or powsybl-ws-dependencies -->
<groupId>com.squareup.okhttp3</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.voltageinit.server;

import com.powsybl.ws.commons.error.ServerNameProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@Component
public class PropertyServerNameProvider implements ServerNameProvider {

private final String name;

public PropertyServerNameProvider(@Value("${spring.application.name:voltage-init-server}") String name) {
this.name = name;
}

@Override
public String serverName() {
return name;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.gridsuite.voltageinit.server;

import com.powsybl.network.store.client.NetworkStoreService;
import org.gridsuite.computation.error.ComputationExceptionHandler;
import org.gridsuite.computation.service.NotificationService;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand All @@ -15,7 +16,7 @@
* @author Etienne Homer <etienne.homer at rte-france.com>
*/
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
@SpringBootApplication(scanBasePackageClasses = { VoltageInitApplication.class, NetworkStoreService.class, NotificationService.class })
@SpringBootApplication(scanBasePackageClasses = {VoltageInitApplication.class, NetworkStoreService.class, NotificationService.class, ComputationExceptionHandler.class})
public class VoltageInitApplication {
public static void main(String[] args) {
SpringApplication.run(VoltageInitApplication.class, args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.gridsuite.voltageinit.server.error;

import com.powsybl.ws.commons.error.BusinessErrorCode;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
public enum VoltageInitBusinessErrorCode implements BusinessErrorCode {
MISSING_FILTER("voltageInit.missingFilter");

private final String code;

VoltageInitBusinessErrorCode(String code) {
this.code = code;
}

public String value() {
return code;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,25 @@
*/
package org.gridsuite.voltageinit.server.error;

import com.powsybl.ws.commons.error.AbstractBusinessException;
import lombok.NonNull;

import java.util.Objects;

/**
* @author Mohamed Ben-rejeb {@literal <mohamed.ben-rejeb at rte-france.com>}
*/
public class VoltageInitException extends RuntimeException {
public class VoltageInitException extends AbstractBusinessException {

private final VoltageInitBusinessErrorCode errorCode;

public VoltageInitException(String message) {
public VoltageInitException(VoltageInitBusinessErrorCode errorCode, String message) {
super(Objects.requireNonNull(message, "message must not be null"));
this.errorCode = Objects.requireNonNull(errorCode, "errorCode must not be null");
}

@Override
public @NonNull VoltageInitBusinessErrorCode getBusinessErrorCode() {
return errorCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.voltageinit.server.error;

import com.powsybl.ws.commons.error.AbstractBusinessExceptionHandler;
import com.powsybl.ws.commons.error.PowsyblWsProblemDetail;
import com.powsybl.ws.commons.error.ServerNameProvider;
import jakarta.servlet.http.HttpServletRequest;
import lombok.NonNull;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
@ControllerAdvice
public class VoltageInitExceptionHandler extends AbstractBusinessExceptionHandler<VoltageInitException, VoltageInitBusinessErrorCode> {
protected VoltageInitExceptionHandler(ServerNameProvider serverNameProvider) {
super(serverNameProvider);
}

@Override
protected @NonNull VoltageInitBusinessErrorCode getBusinessCode(VoltageInitException e) {
return e.getBusinessErrorCode();
}

@Override
protected HttpStatus mapStatus(VoltageInitBusinessErrorCode businessErrorCode) {
return switch (businessErrorCode) {
case MISSING_FILTER -> HttpStatus.INTERNAL_SERVER_ERROR;
};
}

@ExceptionHandler(VoltageInitException.class)
protected ResponseEntity<PowsyblWsProblemDetail> handleVoltageInitException(
VoltageInitException exception, HttpServletRequest request) {
return super.handleDomainException(exception, request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
import org.gridsuite.filter.AbstractFilter;
import org.gridsuite.filter.utils.EquipmentType;
import org.gridsuite.voltageinit.server.dto.parameters.FilterEquipments;
import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.gridsuite.voltageinit.server.error.VoltageInitException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -45,9 +47,10 @@ public class FilterService extends AbstractFilterService {

public static final String FILTERS_NOT_FOUND = "Filters not found";

public FilterService(NetworkStoreService networkStoreService,
public FilterService(RestTemplateBuilder restTemplateBuilder,
NetworkStoreService networkStoreService,
@Value("${gridsuite.services.filter-server.base-uri:http://filter-server/}") String filterServerBaseUri) {
super(networkStoreService, filterServerBaseUri);
super(restTemplateBuilder, networkStoreService, filterServerBaseUri);
}

public List<FilterEquipments> exportFilters(List<UUID> filtersUuids, UUID networkUuid, String variantId) {
Expand Down Expand Up @@ -85,7 +88,7 @@ public void ensureFiltersExist(Map<UUID, String> filterNamesByUuid) {
.filter(filterId -> !validFilters.contains(filterId))
.toList();
if (!missingFilters.isEmpty()) {
throw new VoltageInitException(buildMissingFiltersMessage(missingFilters, filterNamesByUuid));
throw new VoltageInitException(VoltageInitBusinessErrorCode.MISSING_FILTER, buildMissingFiltersMessage(missingFilters, filterNamesByUuid));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.voltageinit.server;

import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
class PropertyServerNameProviderTest {

@Test
void returnsProvidedName() {
PropertyServerNameProvider provider = new PropertyServerNameProvider("custom-server");
assertThat(provider.serverName()).isEqualTo("custom-server");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package org.gridsuite.voltageinit.server;

import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;

import static org.gridsuite.voltageinit.utils.assertions.Assertions.assertThat;

/**
* @author Hugo Marcellin <hugo.marcelin at rte-france.com>
*/
class VoltageInitBusinessErrorCodeTest {
@ParameterizedTest
@EnumSource(VoltageInitBusinessErrorCode.class)
void valueMatchesEnumName(VoltageInitBusinessErrorCode code) {
assertThat(code.value()).startsWith("voltageInit.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.boot.web.client.RestTemplateBuilder;

import java.util.List;
import java.util.Optional;
Expand All @@ -38,7 +39,7 @@ class FilterServiceTest {

@BeforeEach
void setUp() {
filterService = spy(new FilterService(Mockito.mock(NetworkStoreService.class), "http://filter-server/"));
filterService = spy(new FilterService(Mockito.mock(RestTemplateBuilder.class), Mockito.mock(NetworkStoreService.class), "http://filter-server/"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.gridsuite.voltageinit.server.entities.parameters.FilterEquipmentsEmbeddable;
import org.gridsuite.voltageinit.server.entities.parameters.VoltageInitParametersEntity;
import org.gridsuite.voltageinit.server.entities.parameters.VoltageLimitEntity;
import org.gridsuite.voltageinit.server.error.VoltageInitBusinessErrorCode;
import org.gridsuite.voltageinit.server.error.VoltageInitException;
import org.gridsuite.voltageinit.server.service.VoltageInitRunContext;
import org.gridsuite.voltageinit.server.util.EquipmentsSelectionType;
Expand Down Expand Up @@ -211,7 +212,7 @@ void buildOpenReacParametersThrowsWhenFilterMissing() {
.withResourceBundles("i18n.reports")
.withMessageTemplate(COMPUTATION_TYPE).build());

Mockito.doThrow(new VoltageInitException(FilterService.FILTERS_NOT_FOUND + " [" + FILTER_1 + "]"))
Mockito.doThrow(new VoltageInitException(VoltageInitBusinessErrorCode.MISSING_FILTER, FilterService.FILTERS_NOT_FOUND + " [" + FILTER_1 + "]"))
.when(filterService)
.ensureFiltersExist(Mockito.anyMap());

Expand Down