diff --git a/src/main/java/org/gridsuite/useradmin/server/UserAdminApplicationProps.java b/src/main/java/org/gridsuite/useradmin/server/UserAdminApplicationProps.java index eb85a00..bde7378 100644 --- a/src/main/java/org/gridsuite/useradmin/server/UserAdminApplicationProps.java +++ b/src/main/java/org/gridsuite/useradmin/server/UserAdminApplicationProps.java @@ -37,6 +37,61 @@ public class UserAdminApplicationProps { */ private Integer defaultMaxAllowedBuilds; + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedLoadflow()} if {@code null}. + */ + private Integer defaultMaxAllowedLoadflow; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedSecurity()} if {@code null}. + */ + private Integer defaultMaxAllowedSecurity; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedSensitivity()} if {@code null}. + */ + private Integer defaultMaxAllowedSensitivity; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedShortCircuit()} if {@code null}. + */ + private Integer defaultMaxAllowedShortCircuit; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedVoltageInit()} if {@code null}. + */ + private Integer defaultMaxAllowedVoltageInit; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedPccMin()} if {@code null}. + */ + private Integer defaultMaxAllowedPccMin; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedStateEstimation()} if {@code null}. + */ + private Integer defaultMaxAllowedStateEstimation; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedBalanceAdjustement()} if {@code null}. + */ + private Integer defaultMaxAllowedBalanceAdjustement; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedDynamicSimulation()} if {@code null}. + */ + private Integer defaultMaxAllowedDynamicSimulation; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedDynamicSecurity()} if {@code null}. + */ + private Integer defaultMaxAllowedDynamicSecurity; + + /** + * Default value of {@link org.gridsuite.useradmin.server.dto.UserInfos#maxAllowedDynamicMargin()} if {@code null}. + */ + private Integer defaultMaxAllowedDynamicMargin; + /** * Cron jobs expression in UTC. */ diff --git a/src/main/java/org/gridsuite/useradmin/server/controller/UserAdminController.java b/src/main/java/org/gridsuite/useradmin/server/controller/UserAdminController.java index 389299e..3589785 100644 --- a/src/main/java/org/gridsuite/useradmin/server/controller/UserAdminController.java +++ b/src/main/java/org/gridsuite/useradmin/server/controller/UserAdminController.java @@ -147,4 +147,93 @@ public ResponseEntity getUserProfileMaxAllowedBuilds(@PathVariable("sub public ResponseEntity> getConnections() { return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(service.getConnections()); } + + @GetMapping(value = "/users/{sub}/profile/max-loadflow") + @Operation(summary = "Get the user's max allowed loadflows") + @ApiResponse(responseCode = "200", description = "The user max allowed loadflows") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedLoadflows(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedLoadflows(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-security") + @Operation(summary = "Get the user's max allowed security analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed security analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedSecurity(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedSecurity(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-sensitivity") + @Operation(summary = "Get the user's max allowed sensitivity analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed sensitivity analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedSensitivity(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedSensitivity(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-shortcircuit") + @Operation(summary = "Get the user's max allowed short circuit analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed short circuit analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedShortCircuit(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedShortCircuit(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-voltage-init") + @Operation(summary = "Get the user's max allowed voltage init analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed voltage init analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedVoltageInit(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedVoltageInit(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-pcc-min") + @Operation(summary = "Get the user's max allowed pcc min analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed pcc min analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedPccMin(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedPccMin(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-state-estimation") + @Operation(summary = "Get the user's max allowed state estimation analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed state estimation analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedStateEstimation(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedStateEstimation(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-balance-adjustement") + @Operation(summary = "Get the user's max allowed balance adjustement analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed balance adjustement analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedBalanceAdjustement(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedBalanceAdjustement(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-dynamic-simulation") + @Operation(summary = "Get the user's max allowed dynamic simulation analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed dynamic simulation analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedDynamicSimulation(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedDynamicSimulation(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-dynamic-security") + @Operation(summary = "Get the user's max allowed dynamic security analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed dynamic security analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedDynamicSecurity(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedDynamicSecurity(sub)); + } + + @GetMapping(value = "/users/{sub}/profile/max-dynamic-margin") + @Operation(summary = "Get the user's max allowed dynamic margin analysis") + @ApiResponse(responseCode = "200", description = "The user max allowed dynamic margin analysis") + @ApiResponse(responseCode = "404", description = "The user doesn't exist") + public ResponseEntity getUserProfileMaxAllowedDynamicMargin(@PathVariable String sub) { + return ResponseEntity.ok().body(service.getUserProfileMaxAllowedDynamicMargin(sub)); + } + } diff --git a/src/main/java/org/gridsuite/useradmin/server/dto/UserInfos.java b/src/main/java/org/gridsuite/useradmin/server/dto/UserInfos.java index 8a5ab53..17ab4f1 100644 --- a/src/main/java/org/gridsuite/useradmin/server/dto/UserInfos.java +++ b/src/main/java/org/gridsuite/useradmin/server/dto/UserInfos.java @@ -19,6 +19,17 @@ public record UserInfos( Integer maxAllowedCases, Integer numberCasesUsed, Integer maxAllowedBuilds, + Integer maxAllowedLoadflow, + Integer maxAllowedSecurity, + Integer maxAllowedSensitivity, + Integer maxAllowedShortCircuit, + Integer maxAllowedVoltageInit, + Integer maxAllowedPccMin, + Integer maxAllowedStateEstimation, + Integer maxAllowedBalanceAdjustement, + Integer maxAllowedDynamicSimulation, + Integer maxAllowedDynamicSecurity, + Integer maxAllowedDynamicMargin, Set groups ) { /** @@ -39,6 +50,17 @@ public UserInfos withIdentity(UserIdentity identity) { maxAllowedCases, numberCasesUsed, maxAllowedBuilds, + maxAllowedLoadflow, + maxAllowedSecurity, + maxAllowedSensitivity, + maxAllowedShortCircuit, + maxAllowedVoltageInit, + maxAllowedPccMin, + maxAllowedStateEstimation, + maxAllowedBalanceAdjustement, + maxAllowedDynamicSimulation, + maxAllowedDynamicSecurity, + maxAllowedDynamicMargin, groups ); } diff --git a/src/main/java/org/gridsuite/useradmin/server/dto/UserProfile.java b/src/main/java/org/gridsuite/useradmin/server/dto/UserProfile.java index 41b652b..d86d1f0 100644 --- a/src/main/java/org/gridsuite/useradmin/server/dto/UserProfile.java +++ b/src/main/java/org/gridsuite/useradmin/server/dto/UserProfile.java @@ -1,4 +1,4 @@ -/* +/** * Copyright (c) 2024, 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 @@ -6,42 +6,84 @@ */ package org.gridsuite.useradmin.server.dto; +import lombok.Builder; +import lombok.Getter; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; -public record UserProfile( - UUID id, - String name, - UUID loadFlowParameterId, - UUID securityAnalysisParameterId, - UUID sensitivityAnalysisParameterId, - UUID shortcircuitParameterId, - UUID pccMinParameterId, - UUID voltageInitParameterId, - Boolean allLinksValid, - Integer maxAllowedCases, - Integer maxAllowedBuilds, - UUID spreadsheetConfigCollectionId, - UUID networkVisualizationParameterId, - UUID workspaceId -) { +/** + * @author Etienne Lesot + */ +@Getter +@Builder +public final class UserProfile { public static final String DEFAULT_PROFILE_NAME = "default profile"; + public static final String MAX_ALLOWED_CASES = "maxAllowedCases"; + public static final String MAX_ALLOWED_BUILD = "maxAllowedBuilds"; + public static final String MAX_ALLOWED_LOADFLOW = "maxAllowedLoadflow"; + public static final String MAX_ALLOWED_SECURITY = "maxAllowedSecurity"; + public static final String MAX_ALLOWED_SENSITIVITY = "maxAllowedSensitivity"; + public static final String MAX_ALLOWED_SHORT_CIRCUIT = "maxAllowedShortCircuit"; + public static final String MAX_ALLOWED_VOLTAGE_INIT = "maxAllowedVoltageInit"; + public static final String MAX_ALLOWED_PCC_MIN = "maxAllowedPccMin"; + public static final String MAX_ALLOWED_STATE_ESTIMATION = "maxAllowedStateEstimation"; + public static final String MAX_ALLOWED_BALANCE_ADJUSTEMENT = "maxAllowedBalanceAdjustement"; + public static final String MAX_ALLOWED_DYNAMIC_SIMULATION = "maxAllowedDynamicSimulation"; + public static final String MAX_ALLOWED_DYNAMIC_SECURITY = "maxAllowedDynamicSecurity"; + public static final String MAX_ALLOWED_DYNAMIC_MARGIN = "maxAllowedDynamicMargin"; + private final UUID id; + private final String name; + private final UUID loadFlowParameterId; + private final UUID securityAnalysisParameterId; + private final UUID sensitivityAnalysisParameterId; + private final UUID shortcircuitParameterId; + private final UUID pccMinParameterId; + private final UUID voltageInitParameterId; + private final Boolean allLinksValid; + private final Map maxAllowValuesMap; + private final UUID spreadsheetConfigCollectionId; + private final UUID networkVisualizationParameterId; + private final UUID workspaceId; + + public UserProfile( + UUID id, + String name, + UUID loadFlowParameterId, + UUID securityAnalysisParameterId, + UUID sensitivityAnalysisParameterId, + UUID shortcircuitParameterId, + UUID pccMinParameterId, + UUID voltageInitParameterId, + Boolean allLinksValid, + Map maxAllowValuesMap, + UUID spreadsheetConfigCollectionId, + UUID networkVisualizationParameterId, + UUID workspaceId + ) { + this.id = id; + this.name = name; + this.loadFlowParameterId = loadFlowParameterId; + this.securityAnalysisParameterId = securityAnalysisParameterId; + this.sensitivityAnalysisParameterId = sensitivityAnalysisParameterId; + this.shortcircuitParameterId = shortcircuitParameterId; + this.pccMinParameterId = pccMinParameterId; + this.voltageInitParameterId = voltageInitParameterId; + this.allLinksValid = allLinksValid; + this.maxAllowValuesMap = maxAllowValuesMap == null ? new HashMap<>() : new HashMap<>(maxAllowValuesMap); + this.spreadsheetConfigCollectionId = spreadsheetConfigCollectionId; + this.networkVisualizationParameterId = networkVisualizationParameterId; + this.workspaceId = workspaceId; + + } + + public Map getMaxAllowValuesMap() { + return Collections.unmodifiableMap(maxAllowValuesMap); + } - public static UserProfile createDefaultProfile(Integer maxAllowedCases, Integer maxAllowedBuilds) { - return new UserProfile( - null, - DEFAULT_PROFILE_NAME, - null, - null, - null, - null, - null, - null, - null, - maxAllowedCases, - maxAllowedBuilds, - null, - null, - null - ); + public static UserProfile createDefaultProfile(Map maxAllowValues) { + return UserProfile.builder().name(DEFAULT_PROFILE_NAME).maxAllowValuesMap(maxAllowValues).build(); } } diff --git a/src/main/java/org/gridsuite/useradmin/server/entity/UserInfosEntity.java b/src/main/java/org/gridsuite/useradmin/server/entity/UserInfosEntity.java index 6bafca6..83dbe0d 100644 --- a/src/main/java/org/gridsuite/useradmin/server/entity/UserInfosEntity.java +++ b/src/main/java/org/gridsuite/useradmin/server/entity/UserInfosEntity.java @@ -50,17 +50,42 @@ public UserInfosEntity(String sub) { private UserInfos toUserInfos(Integer maxAllowedCases, Integer numberCasesUsed, - Integer maxAllowedBuilds) { + Integer maxAllowedBuilds, + Integer maxAllowedLoadflow, + Integer maxAllowedSecurity, + Integer maxAllowedSensitivity, + Integer maxAllowedShortCircuit, + Integer maxAllowedVoltageInit, + Integer maxAllowedPccMin, + Integer maxAllowedStateEstimation, + Integer maxAllowedBalanceAdjustement, + Integer maxAllowedDynamicSimulation, + Integer maxAllowedDynamicSecurity, + Integer maxAllowedDynamicMargin + ) { String profileName = getProfile() == null ? null : getProfile().getName(); Set groupNames = getGroups() == null ? null : getGroups().stream().map(GroupInfosEntity::getName).collect(Collectors.toSet()); - return new UserInfos(getSub(), null, null, profileName, maxAllowedCases, numberCasesUsed, maxAllowedBuilds, groupNames); + return new UserInfos(getSub(), null, null, profileName, maxAllowedCases, numberCasesUsed, maxAllowedBuilds, + maxAllowedLoadflow, maxAllowedSecurity, maxAllowedSensitivity, maxAllowedShortCircuit, maxAllowedVoltageInit, + maxAllowedPccMin, maxAllowedStateEstimation, maxAllowedBalanceAdjustement, maxAllowedDynamicSimulation, + maxAllowedDynamicSecurity, maxAllowedDynamicMargin, groupNames); } public static UserInfos toDto(@Nullable final UserInfosEntity entity) { - return entity == null ? null : entity.toUserInfos(null, null, null); + return entity == null ? null : entity.toUserInfos(null, null, null, + null, null, null, null, + null, null, null, null, + null, null, null); } - public static UserInfos toDtoWithDetail(@Nullable final UserInfosEntity entity, Integer maxAllowedCases, Integer numberCasesUsed, Integer maxAllowedBuilds) { - return entity == null ? null : entity.toUserInfos(maxAllowedCases, numberCasesUsed, maxAllowedBuilds); + public static UserInfos toDtoWithDetail(@Nullable final UserInfosEntity entity, Integer maxAllowedCases, Integer numberCasesUsed, + Integer maxAllowedBuilds, Integer maxAllowedLoadflow, Integer maxAllowedSecurity, + Integer maxAllowedSensitivity, Integer maxAllowedShortCircuit, Integer maxAllowedVoltageInit, + Integer maxAllowedPccMin, Integer maxAllowedStateEstimation, Integer maxAllowedBalanceAdjustement, + Integer maxAllowedDynamicSimulation, Integer maxAllowedDynamicSecurity, Integer maxAllowedDynamicMargin) { + return entity == null ? null : entity.toUserInfos(maxAllowedCases, numberCasesUsed, maxAllowedBuilds, + maxAllowedLoadflow, maxAllowedSecurity, maxAllowedSensitivity, maxAllowedShortCircuit, maxAllowedVoltageInit, + maxAllowedPccMin, maxAllowedStateEstimation, maxAllowedBalanceAdjustement, maxAllowedDynamicSimulation, + maxAllowedDynamicSecurity, maxAllowedDynamicMargin); } } diff --git a/src/main/java/org/gridsuite/useradmin/server/entity/UserProfileEntity.java b/src/main/java/org/gridsuite/useradmin/server/entity/UserProfileEntity.java index 3575b90..773762c 100644 --- a/src/main/java/org/gridsuite/useradmin/server/entity/UserProfileEntity.java +++ b/src/main/java/org/gridsuite/useradmin/server/entity/UserProfileEntity.java @@ -56,6 +56,39 @@ public class UserProfileEntity { @Column(name = "maxAllowedBuilds") private Integer maxAllowedBuilds; + @Column(name = "maxAllowedLoadflow") + private Integer maxAllowedLoadflow; + + @Column(name = "maxAllowedSecurity") + private Integer maxAllowedSecurity; + + @Column(name = "maxAllowedSensitivity") + private Integer maxAllowedSensitivity; + + @Column(name = "maxAllowedShortCircuit") + Integer maxAllowedShortCircuit; + + @Column(name = "maxAllowedVoltageInit") + Integer maxAllowedVoltageInit; + + @Column(name = "maxAllowedPccMin") + Integer maxAllowedPccMin; + + @Column(name = "maxAllowedStateEstimation") + Integer maxAllowedStateEstimation; + + @Column(name = "maxAllowedBalanceAdjustement") + Integer maxAllowedBalanceAdjustement; + + @Column(name = "maxAllowedDynamicSimulation") + Integer maxAllowedDynamicSimulation; + + @Column(name = "maxAllowedDynamicSecurity") + Integer maxAllowedDynamicSecurity; + + @Column(name = "maxAllowedDynamicMargin") + Integer maxAllowedDynamicMargin; + @Column(name = "spreadsheetConfigCollectionId") private UUID spreadsheetConfigCollectionId; diff --git a/src/main/java/org/gridsuite/useradmin/server/service/UserAdminService.java b/src/main/java/org/gridsuite/useradmin/server/service/UserAdminService.java index b0430fe..94b7371 100644 --- a/src/main/java/org/gridsuite/useradmin/server/service/UserAdminService.java +++ b/src/main/java/org/gridsuite/useradmin/server/service/UserAdminService.java @@ -24,6 +24,8 @@ import java.util.*; import java.util.stream.Collectors; +import static org.gridsuite.useradmin.server.dto.UserProfile.*; + /** * @author Etienne Homer */ @@ -204,7 +206,7 @@ public List getUserGroups(String sub) { @Transactional(readOnly = true) public Integer getUserProfileMaxAllowedCases(String sub) { UserProfile profile = doGetUserProfile(sub); - return Optional.ofNullable(profile.maxAllowedCases()) + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)) .orElse(applicationProps.getDefaultMaxAllowedCases()); } @@ -215,15 +217,107 @@ public Integer getCasesAlertThreshold() { @Transactional(readOnly = true) public Integer getUserProfileMaxAllowedBuilds(String sub) { UserProfile profile = doGetUserProfile(sub); - return Optional.ofNullable(profile.maxAllowedBuilds()) + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)) .orElse(applicationProps.getDefaultMaxAllowedBuilds()); } + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedLoadflows(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_LOADFLOW)) + .orElse(applicationProps.getDefaultMaxAllowedLoadflow()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedSecurity(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_SECURITY)) + .orElse(applicationProps.getDefaultMaxAllowedSecurity()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedSensitivity(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_SENSITIVITY)) + .orElse(applicationProps.getDefaultMaxAllowedSensitivity()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedShortCircuit(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_SHORT_CIRCUIT)) + .orElse(applicationProps.getDefaultMaxAllowedShortCircuit()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedVoltageInit(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_VOLTAGE_INIT)) + .orElse(applicationProps.getDefaultMaxAllowedVoltageInit()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedPccMin(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_PCC_MIN)) + .orElse(applicationProps.getDefaultMaxAllowedPccMin()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedStateEstimation(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_STATE_ESTIMATION)) + .orElse(applicationProps.getDefaultMaxAllowedStateEstimation()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedBalanceAdjustement(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_BALANCE_ADJUSTEMENT)) + .orElse(applicationProps.getDefaultMaxAllowedBalanceAdjustement()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedDynamicSimulation(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SIMULATION)) + .orElse(applicationProps.getDefaultMaxAllowedDynamicSimulation()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedDynamicSecurity(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SECURITY)) + .orElse(applicationProps.getDefaultMaxAllowedDynamicSecurity()); + } + + @Transactional(readOnly = true) + public Integer getUserProfileMaxAllowedDynamicMargin(String sub) { + UserProfile profile = doGetUserProfile(sub); + return Optional.ofNullable(profile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_MARGIN)) + .orElse(applicationProps.getDefaultMaxAllowedDynamicMargin()); + } + + private Map getDefaultMaxAllowedValues() { + Map maxAllowedValuesMap = new HashMap<>(); + maxAllowedValuesMap.put(MAX_ALLOWED_CASES, applicationProps.getDefaultMaxAllowedCases()); + maxAllowedValuesMap.put(MAX_ALLOWED_BUILD, applicationProps.getDefaultMaxAllowedBuilds()); + maxAllowedValuesMap.put(MAX_ALLOWED_LOADFLOW, applicationProps.getDefaultMaxAllowedLoadflow()); + maxAllowedValuesMap.put(MAX_ALLOWED_SECURITY, applicationProps.getDefaultMaxAllowedSecurity()); + maxAllowedValuesMap.put(MAX_ALLOWED_SENSITIVITY, applicationProps.getDefaultMaxAllowedSensitivity()); + maxAllowedValuesMap.put(MAX_ALLOWED_SHORT_CIRCUIT, applicationProps.getDefaultMaxAllowedShortCircuit()); + maxAllowedValuesMap.put(MAX_ALLOWED_VOLTAGE_INIT, applicationProps.getDefaultMaxAllowedVoltageInit()); + maxAllowedValuesMap.put(MAX_ALLOWED_PCC_MIN, applicationProps.getDefaultMaxAllowedPccMin()); + maxAllowedValuesMap.put(MAX_ALLOWED_STATE_ESTIMATION, applicationProps.getDefaultMaxAllowedStateEstimation()); + maxAllowedValuesMap.put(MAX_ALLOWED_BALANCE_ADJUSTEMENT, applicationProps.getDefaultMaxAllowedBalanceAdjustement()); + maxAllowedValuesMap.put(MAX_ALLOWED_DYNAMIC_SIMULATION, applicationProps.getDefaultMaxAllowedDynamicSimulation()); + maxAllowedValuesMap.put(MAX_ALLOWED_DYNAMIC_SECURITY, applicationProps.getDefaultMaxAllowedDynamicSecurity()); + maxAllowedValuesMap.put(MAX_ALLOWED_DYNAMIC_MARGIN, applicationProps.getDefaultMaxAllowedDynamicMargin()); + return maxAllowedValuesMap; + } + private UserProfile createDefaultProfile() { - return UserProfile.createDefaultProfile( - applicationProps.getDefaultMaxAllowedCases(), - applicationProps.getDefaultMaxAllowedBuilds() - ); + return UserProfile.createDefaultProfile(getDefaultMaxAllowedValues()); } /** diff --git a/src/main/java/org/gridsuite/useradmin/server/service/UserIdentityService.java b/src/main/java/org/gridsuite/useradmin/server/service/UserIdentityService.java index 28399d2..4f829f7 100644 --- a/src/main/java/org/gridsuite/useradmin/server/service/UserIdentityService.java +++ b/src/main/java/org/gridsuite/useradmin/server/service/UserIdentityService.java @@ -92,7 +92,8 @@ public Map getIdentities(Collection subs) { url, HttpMethod.GET, null, - new ParameterizedTypeReference() { } + new ParameterizedTypeReference() { + } ).getBody(); if (result == null || result.data() == null) { diff --git a/src/main/java/org/gridsuite/useradmin/server/service/UserInfosService.java b/src/main/java/org/gridsuite/useradmin/server/service/UserInfosService.java index 4979f16..0797122 100644 --- a/src/main/java/org/gridsuite/useradmin/server/service/UserInfosService.java +++ b/src/main/java/org/gridsuite/useradmin/server/service/UserInfosService.java @@ -40,7 +40,55 @@ public UserInfos toDtoUserInfo(final UserInfosEntity userInfosEntity, Integer ca Integer maxAllowedBuilds = Optional.ofNullable(userInfosEntity.getProfile()) .map(UserProfileEntity::getMaxAllowedBuilds) .orElse(applicationProps.getDefaultMaxAllowedBuilds()); - return UserInfosEntity.toDtoWithDetail(userInfosEntity, maxAllowedCases, casesUsed, maxAllowedBuilds); + + Integer maxAllowedLoadflow = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedLoadflow) + .orElse(applicationProps.getDefaultMaxAllowedLoadflow()); + + Integer maxAllowedSecurity = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedSecurity) + .orElse(applicationProps.getDefaultMaxAllowedSecurity()); + + Integer maxAllowedSensitivity = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedSensitivity) + .orElse(applicationProps.getDefaultMaxAllowedSensitivity()); + + Integer maxAllowedShortCircuit = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedShortCircuit) + .orElse(applicationProps.getDefaultMaxAllowedShortCircuit()); + + Integer maxAllowedVoltageInit = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedVoltageInit) + .orElse(applicationProps.getDefaultMaxAllowedVoltageInit()); + + Integer maxAllowedPccMin = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedPccMin) + .orElse(applicationProps.getDefaultMaxAllowedPccMin()); + + Integer maxAllowedStateEstimation = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedStateEstimation) + .orElse(applicationProps.getDefaultMaxAllowedStateEstimation()); + + Integer maxAllowedBalanceAdjustement = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedBalanceAdjustement) + .orElse(applicationProps.getDefaultMaxAllowedBalanceAdjustement()); + + Integer maxAllowedDynamicSimulation = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedDynamicSimulation) + .orElse(applicationProps.getDefaultMaxAllowedDynamicSimulation()); + + Integer maxAllowedDynamicSecurity = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedDynamicSecurity) + .orElse(applicationProps.getDefaultMaxAllowedDynamicSecurity()); + + Integer maxAllowedDynamicMargin = Optional.ofNullable(userInfosEntity.getProfile()) + .map(UserProfileEntity::getMaxAllowedDynamicMargin) + .orElse(applicationProps.getDefaultMaxAllowedDynamicMargin()); + + return UserInfosEntity.toDtoWithDetail(userInfosEntity, maxAllowedCases, casesUsed, maxAllowedBuilds, + maxAllowedLoadflow, maxAllowedSecurity, maxAllowedSensitivity, maxAllowedShortCircuit, maxAllowedVoltageInit, + maxAllowedPccMin, maxAllowedStateEstimation, maxAllowedBalanceAdjustement, + maxAllowedDynamicSimulation, maxAllowedDynamicSecurity, maxAllowedDynamicMargin); } @Transactional(readOnly = true) @@ -69,6 +117,17 @@ private UserInfos createDefaultUserInfo(String sub, Integer casesUsed) { applicationProps.getDefaultMaxAllowedCases(), casesUsed, applicationProps.getDefaultMaxAllowedBuilds(), + applicationProps.getDefaultMaxAllowedLoadflow(), + applicationProps.getDefaultMaxAllowedSecurity(), + applicationProps.getDefaultMaxAllowedSensitivity(), + applicationProps.getDefaultMaxAllowedShortCircuit(), + applicationProps.getDefaultMaxAllowedVoltageInit(), + applicationProps.getDefaultMaxAllowedPccMin(), + applicationProps.getDefaultMaxAllowedStateEstimation(), + applicationProps.getDefaultMaxAllowedBalanceAdjustement(), + applicationProps.getDefaultMaxAllowedDynamicSimulation(), + applicationProps.getDefaultMaxAllowedDynamicSecurity(), + applicationProps.getDefaultMaxAllowedDynamicMargin(), Set.of() ); } diff --git a/src/main/java/org/gridsuite/useradmin/server/service/UserProfileService.java b/src/main/java/org/gridsuite/useradmin/server/service/UserProfileService.java index 2b6400b..53f0b77 100644 --- a/src/main/java/org/gridsuite/useradmin/server/service/UserProfileService.java +++ b/src/main/java/org/gridsuite/useradmin/server/service/UserProfileService.java @@ -19,6 +19,8 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static org.gridsuite.useradmin.server.dto.UserProfile.*; + /** * @author David Braquart */ @@ -120,25 +122,36 @@ public void updateProfile(UUID profileUuid, UserProfile userProfile) { adminRightService.assertIsAdmin(); UserProfileEntity profile = userProfileRepository.findById(profileUuid) .orElseThrow(() -> UserAdminException.profileNotFound(profileUuid)); - profile.setName(userProfile.name()); - profile.setLoadFlowParameterId(userProfile.loadFlowParameterId()); - profile.setSecurityAnalysisParameterId(userProfile.securityAnalysisParameterId()); - profile.setSensitivityAnalysisParameterId(userProfile.sensitivityAnalysisParameterId()); - profile.setShortcircuitParameterId(userProfile.shortcircuitParameterId()); - profile.setPccminParameterId(userProfile.pccMinParameterId()); - profile.setVoltageInitParameterId(userProfile.voltageInitParameterId()); - profile.setMaxAllowedCases(userProfile.maxAllowedCases()); - profile.setMaxAllowedBuilds(userProfile.maxAllowedBuilds()); - profile.setSpreadsheetConfigCollectionId(userProfile.spreadsheetConfigCollectionId()); - profile.setNetworkVisualizationParameterId(userProfile.networkVisualizationParameterId()); - profile.setWorkspaceId(userProfile.workspaceId()); + profile.setName(userProfile.getName()); + profile.setLoadFlowParameterId(userProfile.getLoadFlowParameterId()); + profile.setSecurityAnalysisParameterId(userProfile.getSecurityAnalysisParameterId()); + profile.setSensitivityAnalysisParameterId(userProfile.getSensitivityAnalysisParameterId()); + profile.setShortcircuitParameterId(userProfile.getShortcircuitParameterId()); + profile.setPccminParameterId(userProfile.getPccMinParameterId()); + profile.setVoltageInitParameterId(userProfile.getVoltageInitParameterId()); + profile.setMaxAllowedCases(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)); + profile.setMaxAllowedBuilds(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)); + profile.setMaxAllowedLoadflow(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_LOADFLOW)); + profile.setMaxAllowedSecurity(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SECURITY)); + profile.setMaxAllowedSensitivity(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SENSITIVITY)); + profile.setMaxAllowedShortCircuit(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SHORT_CIRCUIT)); + profile.setMaxAllowedVoltageInit(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_VOLTAGE_INIT)); + profile.setMaxAllowedPccMin(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_PCC_MIN)); + profile.setMaxAllowedStateEstimation(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_STATE_ESTIMATION)); + profile.setMaxAllowedBalanceAdjustement(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_BALANCE_ADJUSTEMENT)); + profile.setMaxAllowedDynamicSimulation(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SIMULATION)); + profile.setMaxAllowedDynamicSecurity(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SECURITY)); + profile.setMaxAllowedDynamicMargin(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_MARGIN)); + profile.setSpreadsheetConfigCollectionId(userProfile.getSpreadsheetConfigCollectionId()); + profile.setNetworkVisualizationParameterId(userProfile.getNetworkVisualizationParameterId()); + profile.setWorkspaceId(userProfile.getWorkspaceId()); } @Transactional public void createProfile(UserProfile userProfile) { adminRightService.assertIsAdmin(); - if (userProfileRepository.findByName(userProfile.name()).isPresent()) { - throw UserAdminException.profileAlreadyExists(userProfile.name()); + if (userProfileRepository.findByName(userProfile.getName()).isPresent()) { + throw UserAdminException.profileAlreadyExists(userProfile.getName()); } UserProfileEntity userProfileEntity = toEntity(userProfile); userProfileRepository.save(userProfileEntity); @@ -162,29 +175,64 @@ private UserProfile toDto(final UserProfileEntity entity, Boolean allLinksValid) if (entity == null) { return null; } - return new UserProfile(entity.getId(), entity.getName(), entity.getLoadFlowParameterId(), - entity.getSecurityAnalysisParameterId(), entity.getSensitivityAnalysisParameterId(), - entity.getShortcircuitParameterId(), entity.getPccminParameterId(), entity.getVoltageInitParameterId(), - allLinksValid, entity.getMaxAllowedCases(), entity.getMaxAllowedBuilds(), entity.getSpreadsheetConfigCollectionId(), - entity.getNetworkVisualizationParameterId(), entity.getWorkspaceId()); + Map maxAllowedValues = new HashMap<>(); + maxAllowedValues.put(MAX_ALLOWED_CASES, entity.getMaxAllowedCases()); + maxAllowedValues.put(MAX_ALLOWED_BUILD, entity.getMaxAllowedBuilds()); + maxAllowedValues.put(MAX_ALLOWED_LOADFLOW, entity.getMaxAllowedLoadflow()); + maxAllowedValues.put(MAX_ALLOWED_SECURITY, entity.getMaxAllowedSecurity()); + maxAllowedValues.put(MAX_ALLOWED_SENSITIVITY, entity.getMaxAllowedSensitivity()); + maxAllowedValues.put(MAX_ALLOWED_SHORT_CIRCUIT, entity.getMaxAllowedShortCircuit()); + maxAllowedValues.put(MAX_ALLOWED_VOLTAGE_INIT, entity.getMaxAllowedVoltageInit()); + maxAllowedValues.put(MAX_ALLOWED_PCC_MIN, entity.getMaxAllowedPccMin()); + maxAllowedValues.put(MAX_ALLOWED_STATE_ESTIMATION, entity.getMaxAllowedStateEstimation()); + maxAllowedValues.put(MAX_ALLOWED_BALANCE_ADJUSTEMENT, entity.getMaxAllowedBalanceAdjustement()); + maxAllowedValues.put(MAX_ALLOWED_DYNAMIC_SIMULATION, entity.getMaxAllowedDynamicSimulation()); + maxAllowedValues.put(MAX_ALLOWED_DYNAMIC_SECURITY, entity.getMaxAllowedDynamicSecurity()); + maxAllowedValues.put(MAX_ALLOWED_DYNAMIC_MARGIN, entity.getMaxAllowedDynamicMargin()); + return UserProfile.builder() + .id(entity.getId()) + .name(entity.getName()) + .loadFlowParameterId(entity.getLoadFlowParameterId()) + .securityAnalysisParameterId(entity.getSecurityAnalysisParameterId()) + .sensitivityAnalysisParameterId(entity.getSensitivityAnalysisParameterId()) + .shortcircuitParameterId(entity.getShortcircuitParameterId()) + .pccMinParameterId(entity.getPccminParameterId()) + .voltageInitParameterId(entity.getVoltageInitParameterId()) + .allLinksValid(allLinksValid) + .maxAllowValuesMap(maxAllowedValues) + .spreadsheetConfigCollectionId(entity.getSpreadsheetConfigCollectionId()) + .networkVisualizationParameterId(entity.getNetworkVisualizationParameterId()) + .workspaceId(entity.getWorkspaceId()) + .build(); } private UserProfileEntity toEntity(final UserProfile userProfile) { Objects.requireNonNull(userProfile); return new UserProfileEntity( UUID.randomUUID(), - userProfile.name(), - userProfile.loadFlowParameterId(), - userProfile.securityAnalysisParameterId(), - userProfile.sensitivityAnalysisParameterId(), - userProfile.shortcircuitParameterId(), - userProfile.pccMinParameterId(), - userProfile.voltageInitParameterId(), - Optional.ofNullable(userProfile.maxAllowedCases()).orElse(applicationProps.getDefaultMaxAllowedCases()), - Optional.ofNullable(userProfile.maxAllowedBuilds()).orElse(applicationProps.getDefaultMaxAllowedBuilds()), - userProfile.spreadsheetConfigCollectionId(), - userProfile.networkVisualizationParameterId(), - userProfile.workspaceId() + userProfile.getName(), + userProfile.getLoadFlowParameterId(), + userProfile.getSecurityAnalysisParameterId(), + userProfile.getSensitivityAnalysisParameterId(), + userProfile.getShortcircuitParameterId(), + userProfile.getPccMinParameterId(), + userProfile.getVoltageInitParameterId(), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)).orElse(applicationProps.getDefaultMaxAllowedCases()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)).orElse(applicationProps.getDefaultMaxAllowedBuilds()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_LOADFLOW)).orElse(applicationProps.getDefaultMaxAllowedLoadflow()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SECURITY)).orElse(applicationProps.getDefaultMaxAllowedSecurity()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SENSITIVITY)).orElse(applicationProps.getDefaultMaxAllowedSensitivity()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_SHORT_CIRCUIT)).orElse(applicationProps.getDefaultMaxAllowedShortCircuit()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_VOLTAGE_INIT)).orElse(applicationProps.getDefaultMaxAllowedVoltageInit()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_PCC_MIN)).orElse(applicationProps.getDefaultMaxAllowedPccMin()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_STATE_ESTIMATION)).orElse(applicationProps.getDefaultMaxAllowedStateEstimation()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_BALANCE_ADJUSTEMENT)).orElse(applicationProps.getDefaultMaxAllowedBalanceAdjustement()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SIMULATION)).orElse(applicationProps.getDefaultMaxAllowedDynamicSimulation()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_SECURITY)).orElse(applicationProps.getDefaultMaxAllowedDynamicSecurity()), + Optional.ofNullable(userProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_DYNAMIC_MARGIN)).orElse(applicationProps.getDefaultMaxAllowedDynamicMargin()), + userProfile.getSpreadsheetConfigCollectionId(), + userProfile.getNetworkVisualizationParameterId(), + userProfile.getWorkspaceId() ); } } diff --git a/src/main/resources/config/application.yaml b/src/main/resources/config/application.yaml index 9715cb2..93df5ed 100644 --- a/src/main/resources/config/application.yaml +++ b/src/main/resources/config/application.yaml @@ -20,4 +20,15 @@ useradmin: announcement-clean: 0 0 2 * * ? defaultMaxAllowedCases: 20 # Default allowed cases for a user defaultMaxAllowedBuilds: 20 # Default allowed builds for a user + defaultMaxAllowedLoadflow: 2 + defaultMaxAllowedSecurity: 2 + defaultMaxAllowedSensitivity: 1 + defaultMaxAllowedShortCircuit: 1 + defaultMaxAllowedVoltageInit: 2 + defaultMaxAllowedPccMin: 1 + defaultMaxAllowedStateEstimation: 1 + defaultMaxAllowedBalanceAdjustement: 1 + defaultMaxAllowedDynamicSimulation: 1 + defaultMaxAllowedDynamicSecurity: 1 + defaultMaxAllowedDynamicMargin: 1 casesAlertThreshold: 90 # Default usage threshold (percentage) when user gets a warning when uploading cases diff --git a/src/main/resources/db/changelog/changesets/changelog_20260616T125854Z.xml b/src/main/resources/db/changelog/changesets/changelog_20260616T125854Z.xml new file mode 100644 index 0000000..70fb178 --- /dev/null +++ b/src/main/resources/db/changelog/changesets/changelog_20260616T125854Z.xml @@ -0,0 +1,58 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/db/changelog/db.changelog-master.yaml b/src/main/resources/db/changelog/db.changelog-master.yaml index 02f815c..02bbed9 100644 --- a/src/main/resources/db/changelog/db.changelog-master.yaml +++ b/src/main/resources/db/changelog/db.changelog-master.yaml @@ -44,3 +44,6 @@ databaseChangeLog: - include: file: changesets/changelog_20260122T160112Z.xml relativeToChangelogFile: true + - include: + file: changesets/changelog_20260616T125854Z.xml + relativeToChangelogFile: true diff --git a/src/test/java/org/gridsuite/useradmin/server/DtoConverterTest.java b/src/test/java/org/gridsuite/useradmin/server/DtoConverterTest.java index 3c0d59c..3a5d5e4 100644 --- a/src/test/java/org/gridsuite/useradmin/server/DtoConverterTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/DtoConverterTest.java @@ -24,30 +24,46 @@ void testConversionToDtoOfUserInfos() { // no profile and no group assertThat(UserInfosEntity.toDto(new UserInfosEntity(uuid, "sub_user", null, null))) .as("dto result") - .isEqualTo(new UserInfos("sub_user", null, null, null, null, null, null, null)); + .isEqualTo(new UserInfos("sub_user", null, null, null, null, null, null, null, null, null, + null, null, null, null, null, + null, null, null, null)); // with profile but without group - UserProfileEntity profile = new UserProfileEntity(UUID.randomUUID(), "a profile", null, null, null, null, null, null, 5, 6, null, null, null); + UserProfileEntity profile = new UserProfileEntity(UUID.randomUUID(), "a profile", null, null, null, null, null, null, 5, 6, null, null, null, null, null, + null, null, null, null, null, + null, null, null, null); // Test mapping without quota assertThat(UserInfosEntity.toDto(new UserInfosEntity(uuid, "sub_user", profile, null))) .as("dto result") - .isEqualTo(new UserInfos("sub_user", null, null, "a profile", null, null, null, null)); + .isEqualTo(new UserInfos("sub_user", null, null, "a profile", null, null, null, null, null, null, + null, null, null, null, null, + null, null, null, null)); // Test mapping with quota - assertThat(UserInfosEntity.toDtoWithDetail(new UserInfosEntity(uuid, "sub_user", profile, null), 5, 2, 6)) + assertThat(UserInfosEntity.toDtoWithDetail(new UserInfosEntity(uuid, "sub_user", profile, null), 5, 2, 6, null, null, null, + null, null, null, null, null, + null, null, null)) .as("dto result") - .isEqualTo(new UserInfos("sub_user", null, null, "a profile", 5, 2, 6, null)); + .isEqualTo(new UserInfos("sub_user", null, null, "a profile", 5, 2, 6, null, null, null, + null, null, null, null, null, + null, null, null, null)); // with profile and groups GroupInfosEntity group1 = new GroupInfosEntity(UUID.randomUUID(), "group1", Set.of()); GroupInfosEntity group2 = new GroupInfosEntity(UUID.randomUUID(), "group2", Set.of()); // Test mapping without quota assertThat(UserInfosEntity.toDto(new UserInfosEntity(uuid, "sub_user", profile, Set.of(group1, group2)))) - .as("dto result") - .isEqualTo(new UserInfos("sub_user", null, null, "a profile", null, null, null, Set.of("group1", "group2"))); + .as("dto result") + .isEqualTo(new UserInfos("sub_user", null, null, "a profile", null, null, null, null, null, + null, null, null, null, null, + null, null, null, null, Set.of("group1", "group2"))); // Test mapping with quota - assertThat(UserInfosEntity.toDtoWithDetail(new UserInfosEntity(uuid, "sub_user", profile, Set.of(group1, group2)), 5, 2, 6)) - .as("dto result") - .isEqualTo(new UserInfos("sub_user", null, null, "a profile", 5, 2, 6, Set.of("group1", "group2"))); + assertThat(UserInfosEntity.toDtoWithDetail(new UserInfosEntity(uuid, "sub_user", profile, Set.of(group1, group2)), 5, 2, 6, null, null, null, + null, null, null, null, null, + null, null, null)) + .as("dto result") + .isEqualTo(new UserInfos("sub_user", null, null, "a profile", 5, 2, 6, null, null, null, + null, null, null, null, null, + null, null, null, Set.of("group1", "group2"))); } @Test @@ -63,9 +79,9 @@ void testConversionToDtoOfUserConnection() { final UUID uuid = UUID.randomUUID(); final Clock clock = Clock.fixed(Instant.now(), ZoneOffset.UTC); assertThat(ConnectionEntity.toDto(new ConnectionEntity(uuid, "user1", LocalDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC), - LocalDateTime.now(clock), false))) - .as("dto result") - .isEqualTo(new UserConnection("user1", Instant.EPOCH, Instant.now(clock), false)); + LocalDateTime.now(clock), false))) + .as("dto result") + .isEqualTo(new UserConnection("user1", Instant.EPOCH, Instant.now(clock), false)); } @Test diff --git a/src/test/java/org/gridsuite/useradmin/server/NoQuotaTest.java b/src/test/java/org/gridsuite/useradmin/server/NoQuotaTest.java index 3e4f6f4..d0bc3d2 100644 --- a/src/test/java/org/gridsuite/useradmin/server/NoQuotaTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/NoQuotaTest.java @@ -13,6 +13,7 @@ import org.gridsuite.useradmin.server.entity.UserProfileEntity; import org.gridsuite.useradmin.server.repository.UserInfosRepository; import org.gridsuite.useradmin.server.repository.UserProfileRepository; +import org.jspecify.annotations.NonNull; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -22,8 +23,13 @@ import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; + +import java.util.HashMap; +import java.util.Map; import java.util.Optional; + import static org.gridsuite.useradmin.server.Utils.ROLES_HEADER; +import static org.gridsuite.useradmin.server.dto.UserProfile.*; import static org.gridsuite.useradmin.server.utils.TestConstants.*; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.http.MediaType.APPLICATION_JSON; @@ -74,9 +80,9 @@ void cleanDB() { @Test void testProfileCreation() throws Exception { - createProfile(PROFILE_ONE, null, null); + createProfile(); // test with quotas - createProfile(PROFILE_TWO, 10, 20); + createProfile(PROFILE_TWO, createMaxAllowedCases()); } @Test @@ -84,30 +90,76 @@ void testUserCreationWithoutProfile() throws Exception { createUser(USER_SUB); assertTrue(getMaxAllowedBuilds(USER_SUB).isEmpty()); - assertTrue(getMaxAllowedCases(USER_SUB).isEmpty()); + assertTrue(createMaxAllowedCases(USER_SUB).isEmpty()); } @Test void testUserCreationWithProfile() throws Exception { //profile with no quotas - createProfile(PROFILE_ONE, null, null); + createProfile(); createUser(USER_SUB); associateProfileToUser(USER_SUB, PROFILE_ONE); assertTrue(getMaxAllowedBuilds(USER_SUB).isEmpty()); - assertTrue(getMaxAllowedCases(USER_SUB).isEmpty()); + assertTrue(createMaxAllowedCases(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedLoadflow(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedSecurity(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedSensitivity(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedShortCircuit(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedVoltageInit(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedPccMin(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedStateEstimation(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedBalanceAdjustement(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedDynamicSimulation(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedDynamicSecurity(USER_SUB).isEmpty()); + assertTrue(getMaxAllowedDynamicMargin(USER_SUB).isEmpty()); + + Map maxAllowedCases = createMaxAllowedCases(); //profile with quotas - createProfile(PROFILE_TWO, 10, 20); + createProfile(PROFILE_TWO, maxAllowedCases); createUser(USER_SUB_TWO); associateProfileToUser(USER_SUB_TWO, PROFILE_TWO); - assertEquals("10", getMaxAllowedCases(USER_SUB_TWO)); + assertEquals("10", createMaxAllowedCases(USER_SUB_TWO)); assertEquals("20", getMaxAllowedBuilds(USER_SUB_TWO)); + assertEquals("2", getMaxAllowedLoadflow(USER_SUB_TWO)); + assertEquals("2", getMaxAllowedSecurity(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedSensitivity(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedShortCircuit(USER_SUB_TWO)); + assertEquals("2", getMaxAllowedVoltageInit(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedPccMin(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedStateEstimation(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedBalanceAdjustement(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedDynamicSimulation(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedDynamicSecurity(USER_SUB_TWO)); + assertEquals("1", getMaxAllowedDynamicMargin(USER_SUB_TWO)); + } + + private static @NonNull Map createMaxAllowedCases() { + Map maxAllowedCases = new HashMap<>(); + maxAllowedCases.put(MAX_ALLOWED_CASES, 10); + maxAllowedCases.put(MAX_ALLOWED_BUILD, 20); + maxAllowedCases.put(MAX_ALLOWED_LOADFLOW, 2); + maxAllowedCases.put(MAX_ALLOWED_SECURITY, 2); + maxAllowedCases.put(MAX_ALLOWED_SENSITIVITY, 1); + maxAllowedCases.put(MAX_ALLOWED_SHORT_CIRCUIT, 1); + maxAllowedCases.put(MAX_ALLOWED_VOLTAGE_INIT, 2); + maxAllowedCases.put(MAX_ALLOWED_PCC_MIN, 1); + maxAllowedCases.put(MAX_ALLOWED_STATE_ESTIMATION, 1); + maxAllowedCases.put(MAX_ALLOWED_BALANCE_ADJUSTEMENT, 1); + maxAllowedCases.put(MAX_ALLOWED_DYNAMIC_SIMULATION, 1); + maxAllowedCases.put(MAX_ALLOWED_DYNAMIC_SECURITY, 1); + maxAllowedCases.put(MAX_ALLOWED_DYNAMIC_MARGIN, 1); + return maxAllowedCases; + } + + private void createProfile() throws Exception { + createProfile(NoQuotaTest.PROFILE_ONE, new HashMap<>()); } - private void createProfile(String profileName, Integer maxAllowedCases, Integer maxAllowedBuilds) throws Exception { - UserProfile profileInfo = new UserProfile(null, profileName, null, null, null, null, null, null, false, maxAllowedCases, maxAllowedBuilds, null, null, null); + private void createProfile(String profileName, Map maxAllowedValues) throws Exception { + UserProfile profileInfo = UserProfile.builder().name(profileName).allLinksValid(false).maxAllowValuesMap(maxAllowedValues).build(); performPost(API_BASE_PATH + "/profiles", profileInfo); Optional createdProfile = userProfileRepository.findByName(profileName); @@ -118,8 +170,19 @@ private void createProfile(String profileName, Integer maxAllowedCases, Integer assertNull(createdProfile.get().getShortcircuitParameterId()); assertNull(createdProfile.get().getPccminParameterId()); assertNull(createdProfile.get().getVoltageInitParameterId()); - assertEquals(maxAllowedCases, createdProfile.get().getMaxAllowedCases()); - assertEquals(maxAllowedBuilds, createdProfile.get().getMaxAllowedBuilds()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_BUILD), createdProfile.get().getMaxAllowedBuilds()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_CASES), createdProfile.get().getMaxAllowedCases()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_LOADFLOW), createdProfile.get().getMaxAllowedLoadflow()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_SECURITY), createdProfile.get().getMaxAllowedSecurity()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_SENSITIVITY), createdProfile.get().getMaxAllowedSensitivity()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_SHORT_CIRCUIT), createdProfile.get().getMaxAllowedShortCircuit()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_VOLTAGE_INIT), createdProfile.get().getMaxAllowedVoltageInit()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_PCC_MIN), createdProfile.get().getMaxAllowedPccMin()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_STATE_ESTIMATION), createdProfile.get().getMaxAllowedStateEstimation()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_BALANCE_ADJUSTEMENT), createdProfile.get().getMaxAllowedBalanceAdjustement()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_DYNAMIC_SIMULATION), createdProfile.get().getMaxAllowedDynamicSimulation()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_DYNAMIC_SECURITY), createdProfile.get().getMaxAllowedDynamicSecurity()); + assertEquals(maxAllowedValues.get(MAX_ALLOWED_DYNAMIC_MARGIN), createdProfile.get().getMaxAllowedDynamicMargin()); assertNull(createdProfile.get().getSpreadsheetConfigCollectionId()); assertNull(createdProfile.get().getNetworkVisualizationParameterId()); } @@ -140,17 +203,68 @@ private UserInfos getUserInfos(String userSub) throws Exception { } private void associateProfileToUser(String userSub, String profileName) throws Exception { - UserInfos userInfos = new UserInfos(userSub, null, null, profileName, null, null, null, null); + UserInfos userInfos = new UserInfos(userSub, null, null, profileName, null, + null, null, null, null, + null, null, null, null, + null, null, null, null, + null, null); performPut(API_BASE_PATH + "/users/" + userSub, userInfos); } private String getMaxAllowedBuilds(String userSub) throws Exception { - MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub + "/profile/max-builds"); - return result.getResponse().getContentAsString(); + return getMaxAllowedElement(userSub, "max-builds"); + } + + private String createMaxAllowedCases(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-cases"); + } + + private String getMaxAllowedLoadflow(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-loadflow"); + } + + private String getMaxAllowedSecurity(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-security"); + } + + private String getMaxAllowedSensitivity(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-sensitivity"); + } + + private String getMaxAllowedShortCircuit(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-shortcircuit"); + } + + private String getMaxAllowedVoltageInit(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-voltage-init"); + } + + private String getMaxAllowedPccMin(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-pcc-min"); + } + + private String getMaxAllowedStateEstimation(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-state-estimation"); + } + + private String getMaxAllowedBalanceAdjustement(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-balance-adjustement"); + } + + private String getMaxAllowedDynamicSimulation(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-dynamic-simulation"); + } + + private String getMaxAllowedDynamicSecurity(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-dynamic-security"); + } + + private String getMaxAllowedDynamicMargin(String userSub) throws Exception { + return getMaxAllowedElement(userSub, "max-dynamic-margin"); } - private String getMaxAllowedCases(String userSub) throws Exception { - MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub + "/profile/max-cases"); + private String getMaxAllowedElement(String userSub, String elementName) throws Exception { + MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub + "/profile/" + elementName); return result.getResponse().getContentAsString(); } diff --git a/src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java b/src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java index d1a560c..f228253 100644 --- a/src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java @@ -29,11 +29,15 @@ import org.springframework.http.MediaType; import org.springframework.test.context.ActiveProfiles; import org.springframework.test.web.servlet.MockMvc; + import java.time.LocalDateTime; import java.time.ZoneOffset; +import java.util.HashMap; import java.util.List; import java.util.Set; + import static org.gridsuite.useradmin.server.Utils.ROLES_HEADER; +import static org.gridsuite.useradmin.server.dto.UserProfile.*; import static org.gridsuite.useradmin.server.utils.TestConstants.*; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.http.MediaType.APPLICATION_JSON; @@ -95,7 +99,8 @@ void testUserAdmin() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(0, userInfos.size()); @@ -118,7 +123,8 @@ void testUserAdmin() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(1, userInfos.size()); @@ -158,7 +164,8 @@ void testUserAdmin() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(0, userInfos.size()); mockMvc.perform(delete("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB) @@ -212,18 +219,21 @@ void testUserAdmin() throws Exception { @Test void testUpdateUser() throws Exception { createUser(USER_SUB); - createProfile(PROFILE_1); + createProfile(); createGroup(GROUP_1); createGroup(GROUP_2); // udpate the user: change its name and link it to the profile and to the first group - UserInfos userInfo = new UserInfos(USER_SUB2, null, null, PROFILE_1, null, null, null, Set.of(GROUP_1)); + UserInfos userInfo = new UserInfos(USER_SUB2, null, null, PROFILE_1, null, null, + null, null, null, null, null, + null, null, null, null, null, + null, null, Set.of(GROUP_1)); updateUserWithAdmin(USER_SUB, userInfo, HttpStatus.OK); // Get and check user profile UserProfile userProfile = getUserProfile(USER_SUB2, HttpStatus.OK); assertNotNull(userProfile); - assertEquals(PROFILE_1, userProfile.name()); + assertEquals(PROFILE_1, userProfile.getName()); // Get and check user groups List userGroups = getUserGroups(USER_SUB2, HttpStatus.OK); @@ -231,7 +241,9 @@ void testUpdateUser() throws Exception { assertEquals(GROUP_1, userGroups.get(0).name()); // udpate the user: change groups - userInfo = new UserInfos(USER_SUB2, PROFILE_1, null, null, null, null, null, Set.of(GROUP_2)); + userInfo = new UserInfos(USER_SUB2, PROFILE_1, null, null, null, null, null, null, null, null, + null, null, null, null, null, + null, null, null, Set.of(GROUP_2)); updateUserWithAdmin(USER_SUB2, userInfo, HttpStatus.OK); // Get and check user groups @@ -242,31 +254,36 @@ void testUpdateUser() throws Exception { @Test void testUpdateUserNotFound() throws Exception { - updateUserWithAdmin("nofFound", new UserInfos("nofFound", null, null, "prof", null, null, null, null), HttpStatus.NOT_FOUND); + updateUserWithAdmin("nofFound", new UserInfos("nofFound", null, null, "prof", null, null, null, null, null, null, null, null, + null, null, null, null, null, + null, null), HttpStatus.NOT_FOUND); } @Test void testUpdateUserForbidden() throws Exception { - updateUserWithNotAdmin("dummy", new UserInfos("dummy", null, null, "prof", null, null, null, null)); + updateUserWithNotAdmin("dummy", new UserInfos("dummy", null, null, "prof", null, + null, null, null, null, null, null, + null, null, null, null, null, + null, null, null)); } @Test void testGetUserProfileNotFound() throws Exception { UserProfile profile = getUserProfile("BadUser", HttpStatus.OK); assertNotNull(profile); - assertEquals(UserProfile.DEFAULT_PROFILE_NAME, profile.name()); - assertNull(profile.id()); - assertEquals(userAdminApplicationProps.getDefaultMaxAllowedCases(), profile.maxAllowedCases()); - assertEquals(userAdminApplicationProps.getDefaultMaxAllowedBuilds(), profile.maxAllowedBuilds()); - assertNull(profile.loadFlowParameterId()); - assertNull(profile.securityAnalysisParameterId()); - assertNull(profile.sensitivityAnalysisParameterId()); - assertNull(profile.shortcircuitParameterId()); - assertNull(profile.pccMinParameterId()); - assertNull(profile.voltageInitParameterId()); - assertNull(profile.allLinksValid()); - assertNull(profile.spreadsheetConfigCollectionId()); - assertNull(profile.networkVisualizationParameterId()); + assertEquals(UserProfile.DEFAULT_PROFILE_NAME, profile.getName()); + assertNull(profile.getId()); + assertEquals(userAdminApplicationProps.getDefaultMaxAllowedCases(), profile.getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)); + assertEquals(userAdminApplicationProps.getDefaultMaxAllowedBuilds(), profile.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)); + assertNull(profile.getLoadFlowParameterId()); + assertNull(profile.getSecurityAnalysisParameterId()); + assertNull(profile.getSensitivityAnalysisParameterId()); + assertNull(profile.getShortcircuitParameterId()); + assertNull(profile.getPccMinParameterId()); + assertNull(profile.getVoltageInitParameterId()); + assertNull(profile.getAllLinksValid()); + assertNull(profile.getSpreadsheetConfigCollectionId()); + assertNull(profile.getNetworkVisualizationParameterId()); } @Test @@ -292,7 +309,8 @@ void testGetConnections() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(2, userInfos.size()); @@ -313,7 +331,8 @@ void testGetConnections() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(2, connectionEntities.size()); @@ -328,7 +347,8 @@ void testGetConnections() throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); assertEquals(2, connectionEntities.size()); mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/connections") @@ -351,7 +371,8 @@ private void createUser(String userName) throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); // the new user has no profile by default assertNotNull(userInfos); assertNull(userInfos.profileName()); @@ -361,12 +382,12 @@ private void createUser(String userName) throws Exception { mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/users/{sub}", userName) .header("userId", ADMIN_USER) .header(ROLES_HEADER, USER_ADMIN_ROLE)) - .andExpect(status().isBadRequest()); + .andExpect(status().isBadRequest()); } - private void createProfile(String profileName) throws Exception { + private void createProfile() throws Exception { ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter(); - UserProfile profileInfo = new UserProfile(null, profileName, null, null, null, null, null, null, false, null, null, null, null, null); + UserProfile profileInfo = UserProfile.builder().name(UserAdminTest.PROFILE_1).allLinksValid(false).maxAllowValuesMap(new HashMap<>()).build(); mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/profiles") .content(objectWriter.writeValueAsString(profileInfo)) .contentType(MediaType.APPLICATION_JSON) @@ -391,7 +412,8 @@ private void createGroup(String groupName) throws Exception { .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); // the new group has no users by default assertNotNull(groupInfos); assertTrue(CollectionUtils.isEmpty(groupInfos.users())); @@ -414,7 +436,8 @@ private void updateUserWithAdmin(String updatedUserName, UserInfos userInfos, Ht .contentType(APPLICATION_JSON)) .andExpect(status().isOk()) .andReturn().getResponse().getContentAsString(), - new TypeReference<>() { }); + new TypeReference<>() { + }); // the new user has the new name and profile assertNotNull(updatedUserInfos); assertEquals(userInfos.sub(), updatedUserInfos.sub()); @@ -433,22 +456,24 @@ private void updateUserWithNotAdmin(String updatedUserName, UserInfos userInfos) private UserProfile getUserProfile(String userName, HttpStatusCode status) throws Exception { String response = mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/users/" + userName + "/profile") - .contentType(APPLICATION_JSON)) - .andExpect(status().is(status.value())) - .andReturn().getResponse().getContentAsString(); + .contentType(APPLICATION_JSON)) + .andExpect(status().is(status.value())) + .andReturn().getResponse().getContentAsString(); if (status == HttpStatus.OK) { - return objectMapper.readValue(response, new TypeReference<>() { }); + return objectMapper.readValue(response, new TypeReference<>() { + }); } return null; } private List getUserGroups(String userName, HttpStatusCode status) throws Exception { String response = mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/users/" + userName + "/groups") - .contentType(APPLICATION_JSON)) - .andExpect(status().is(status.value())) - .andReturn().getResponse().getContentAsString(); + .contentType(APPLICATION_JSON)) + .andExpect(status().is(status.value())) + .andReturn().getResponse().getContentAsString(); if (status == HttpStatus.OK) { - return objectMapper.readValue(response, new TypeReference<>() { }); + return objectMapper.readValue(response, new TypeReference<>() { + }); } return null; } diff --git a/src/test/java/org/gridsuite/useradmin/server/UserProfileTest.java b/src/test/java/org/gridsuite/useradmin/server/UserProfileTest.java index 00e373a..782faa2 100644 --- a/src/test/java/org/gridsuite/useradmin/server/UserProfileTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/UserProfileTest.java @@ -33,14 +33,13 @@ import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.UUID; +import java.util.*; import java.util.stream.Collectors; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.gridsuite.useradmin.server.Utils.ROLES_HEADER; +import static org.gridsuite.useradmin.server.dto.UserProfile.MAX_ALLOWED_BUILD; +import static org.gridsuite.useradmin.server.dto.UserProfile.MAX_ALLOWED_CASES; import static org.gridsuite.useradmin.server.utils.TestConstants.USER_ADMIN_ROLE; import static org.junit.jupiter.api.Assertions.*; import static org.springframework.http.MediaType.APPLICATION_JSON; @@ -113,18 +112,18 @@ void testCreateProfile() throws Exception { List userProfiles = getProfileList(false); assertEquals(1, userProfiles.size()); - assertEquals(PROFILE_1, userProfiles.get(0).name()); - assertNull(userProfiles.get(0).loadFlowParameterId()); - assertNull(userProfiles.get(0).securityAnalysisParameterId()); - assertNull(userProfiles.get(0).sensitivityAnalysisParameterId()); - assertNull(userProfiles.get(0).shortcircuitParameterId()); - assertNull(userProfiles.getFirst().pccMinParameterId()); - assertNull(userProfiles.get(0).voltageInitParameterId()); - assertNull(userProfiles.get(0).allLinksValid()); - assertEquals(10, userProfiles.get(0).maxAllowedCases()); - assertEquals(15, userProfiles.get(0).maxAllowedBuilds()); - assertNull(userProfiles.get(0).spreadsheetConfigCollectionId()); - assertNull(userProfiles.get(0).networkVisualizationParameterId()); + assertEquals(PROFILE_1, userProfiles.getFirst().getName()); + assertNull(userProfiles.getFirst().getLoadFlowParameterId()); + assertNull(userProfiles.getFirst().getSecurityAnalysisParameterId()); + assertNull(userProfiles.getFirst().getSensitivityAnalysisParameterId()); + assertNull(userProfiles.getFirst().getShortcircuitParameterId()); + assertNull(userProfiles.getFirst().getPccMinParameterId()); + assertNull(userProfiles.getFirst().getVoltageInitParameterId()); + assertNull(userProfiles.getFirst().getAllLinksValid()); + assertEquals(10, userProfiles.getFirst().getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)); + assertEquals(15, userProfiles.getFirst().getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)); + assertNull(userProfiles.getFirst().getSpreadsheetConfigCollectionId()); + assertNull(userProfiles.getFirst().getNetworkVisualizationParameterId()); createProfile(PROFILE_2, ADMIN_USER, USER_ADMIN_ROLE, null, null, HttpStatus.CREATED); createProfile(PROFILE_1, ADMIN_USER, USER_ADMIN_ROLE, null, null, HttpStatus.BAD_REQUEST); // profile already exists @@ -155,18 +154,12 @@ void testDeleteProfileNotFound() throws Exception { @Test void testProfileUpdateNotFound() throws Exception { - updateProfile(new UserProfile(UUID.randomUUID(), PROFILE_2, null, null, null, null, null, null, null, null, null, null, null, null), - ADMIN_USER, - USER_ADMIN_ROLE, - HttpStatus.NOT_FOUND); + updateProfile(UserProfile.builder().id(UUID.randomUUID()).name(PROFILE_2).build(), ADMIN_USER, USER_ADMIN_ROLE, HttpStatus.NOT_FOUND); } @Test void testProfileUpdateForbidden() throws Exception { - updateProfile(new UserProfile(UUID.randomUUID(), PROFILE_2, null, null, null, null, null, null, null, null, null, null, null, null), - NOT_ADMIN, - "USER", - HttpStatus.FORBIDDEN); + updateProfile(UserProfile.builder().id(UUID.randomUUID()).name(PROFILE_2).build(), NOT_ADMIN, "USER", HttpStatus.FORBIDDEN); } @Test @@ -181,7 +174,9 @@ void testProfileUpdateValidityKo() throws Exception { @Test void testGetProfileMaxAllowedCases() throws Exception { - UserProfileEntity userProfileEntity = new UserProfileEntity(UUID.randomUUID(), "profileName", null, null, null, null, null, null, 15, null, null, null, null); + UserProfileEntity userProfileEntity = new UserProfileEntity(UUID.randomUUID(), "profileName", null, null, null, null, null, null, 15, null, null, null, null, null, null, + null, null, null, null, null, + null, null, null, null); UserInfosEntity userInfosEntity = new UserInfosEntity(UUID.randomUUID(), ADMIN_USER, userProfileEntity, null); userProfileRepository.save(userProfileEntity); userInfosRepository.save(userInfosEntity); @@ -197,7 +192,11 @@ void testGetProfileMaxAllowedCases() throws Exception { @Test void testGetProfileMaxAllowedBuilds() throws Exception { - UserProfileEntity userProfileEntity = new UserProfileEntity(UUID.randomUUID(), "profileName", null, null, null, null, null, null, null, 15, null, null, null); + UserProfileEntity userProfileEntity = new UserProfileEntity(UUID.randomUUID(), "profileName", null, null, + null, null, null, null, null, 15, + null, null, null, null, null, + null, null, null, null, null, + null, null, null, null); UserInfosEntity userInfosEntity = new UserInfosEntity(UUID.randomUUID(), ADMIN_USER, userProfileEntity, null); userProfileRepository.save(userProfileEntity); userInfosRepository.save(userInfosEntity); @@ -276,31 +275,44 @@ private void updateProfile(boolean validParameters) throws Exception { UUID profileUuid = createProfile(PROFILE_1, ADMIN_USER, USER_ADMIN_ROLE, null, 0, HttpStatus.CREATED); // udpate the profile: change name and set its parameters, maxAllowedCases, maxAllowedBuilds and spreadsheet config collection - UserProfile userProfile = new UserProfile(profileUuid, PROFILE_2, loadFlowParametersUuid, securityAnalysisParametersUuid, - sensitivityAnalysisParametersUuid, shortcircuitParametersUuid, pccminParametersUuid, voltageInitParametersUuid, null, - 10, 11, spreadsheetConfigCollectionUuid, networkVisualizationParametersUuid, diagramConfigUuid); + Map maxAllowedValues = new HashMap<>(); + maxAllowedValues.put(MAX_ALLOWED_CASES, 10); + maxAllowedValues.put(MAX_ALLOWED_BUILD, 11); + UserProfile userProfile = UserProfile.builder() + .id(profileUuid).name(PROFILE_2) + .loadFlowParameterId(loadFlowParametersUuid) + .securityAnalysisParameterId(securityAnalysisParametersUuid) + .sensitivityAnalysisParameterId(sensitivityAnalysisParametersUuid) + .shortcircuitParameterId(shortcircuitParametersUuid) + .pccMinParameterId(pccminParametersUuid) + .voltageInitParameterId(voltageInitParametersUuid) + .maxAllowValuesMap(maxAllowedValues) + .spreadsheetConfigCollectionId(spreadsheetConfigCollectionUuid) + .networkVisualizationParameterId(networkVisualizationParametersUuid) + .workspaceId(diagramConfigUuid) + .build(); updateProfile(userProfile, ADMIN_USER, USER_ADMIN_ROLE, HttpStatus.OK); // profiles list (with validity flag) List userProfiles = getProfileList(true); wireMockUtils.verifyGetRequest(stubId, urlPath, handleQueryParams(elementsUuids), false, 1); assertEquals(1, userProfiles.size()); - assertEquals(loadFlowParametersUuid, userProfiles.get(0).loadFlowParameterId()); - assertEquals(securityAnalysisParametersUuid, userProfiles.get(0).securityAnalysisParameterId()); - assertEquals(sensitivityAnalysisParametersUuid, userProfiles.get(0).sensitivityAnalysisParameterId()); - assertEquals(shortcircuitParametersUuid, userProfiles.get(0).shortcircuitParameterId()); - assertEquals(pccminParametersUuid, userProfiles.get(0).pccMinParameterId()); - assertEquals(voltageInitParametersUuid, userProfiles.get(0).voltageInitParameterId()); - assertEquals(validParameters, userProfiles.get(0).allLinksValid()); - assertEquals(10, userProfiles.get(0).maxAllowedCases()); - assertEquals(11, userProfiles.get(0).maxAllowedBuilds()); - assertEquals(spreadsheetConfigCollectionUuid, userProfiles.get(0).spreadsheetConfigCollectionId()); - assertEquals(networkVisualizationParametersUuid, userProfiles.get(0).networkVisualizationParameterId()); + assertEquals(loadFlowParametersUuid, userProfiles.get(0).getLoadFlowParameterId()); + assertEquals(securityAnalysisParametersUuid, userProfiles.get(0).getSecurityAnalysisParameterId()); + assertEquals(sensitivityAnalysisParametersUuid, userProfiles.get(0).getSensitivityAnalysisParameterId()); + assertEquals(shortcircuitParametersUuid, userProfiles.get(0).getShortcircuitParameterId()); + assertEquals(pccminParametersUuid, userProfiles.get(0).getPccMinParameterId()); + assertEquals(voltageInitParametersUuid, userProfiles.get(0).getVoltageInitParameterId()); + assertEquals(validParameters, userProfiles.get(0).getAllLinksValid()); + assertEquals(10, userProfiles.get(0).getMaxAllowValuesMap().get(MAX_ALLOWED_CASES)); + assertEquals(11, userProfiles.get(0).getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)); + assertEquals(spreadsheetConfigCollectionUuid, userProfiles.get(0).getSpreadsheetConfigCollectionId()); + assertEquals(networkVisualizationParametersUuid, userProfiles.get(0).getNetworkVisualizationParameterId()); // profiles list (without validity flag) userProfiles = getProfileList(false); wireMockUtils.verifyGetRequest(stubId, urlPath, handleQueryParams(elementsUuids), false, 0); - assertNull(userProfiles.get(0).allLinksValid()); + assertNull(userProfiles.get(0).getAllLinksValid()); } private static Map handleQueryParams(List paramIds) { @@ -308,7 +320,10 @@ private static Map handleQueryParams(List para } private UUID createProfile(String profileName, String userName, String userRole, Integer maxAllowedCases, Integer maxAllowedBuilds, HttpStatusCode status) throws Exception { - UserProfile profileInfo = new UserProfile(null, profileName, null, null, null, null, null, null, false, maxAllowedCases, maxAllowedBuilds, null, null, null); + Map maxAllowedValues = new HashMap<>(); + maxAllowedValues.put(MAX_ALLOWED_CASES, maxAllowedCases); + maxAllowedValues.put(MAX_ALLOWED_BUILD, maxAllowedBuilds); + UserProfile profileInfo = UserProfile.builder().name(profileName).maxAllowValuesMap(maxAllowedValues).build(); mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/profiles") .content(objectWriter.writeValueAsString(profileInfo)) .contentType(MediaType.APPLICATION_JSON) @@ -365,7 +380,7 @@ private void removeProfile(String profileName, String userName, String userRole, } private void updateProfile(UserProfile newData, String userName, String userRole, HttpStatusCode status) throws Exception { - mockMvc.perform(put("/" + UserAdminApi.API_VERSION + "/profiles/{profileUuid}", newData.id()) + mockMvc.perform(put("/" + UserAdminApi.API_VERSION + "/profiles/{profileUuid}", newData.getId()) .content(objectWriter.writeValueAsString(newData)) .contentType(MediaType.APPLICATION_JSON) .header("userId", userName) @@ -375,7 +390,7 @@ private void updateProfile(UserProfile newData, String userName, String userRole if (status == HttpStatus.OK) { // check access to updated profile UserProfile updatedProfile = objectMapper.readValue( - mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/profiles/{profileUuid}", newData.id()) + mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/profiles/{profileUuid}", newData.getId()) .header("userId", userName) .header(ROLES_HEADER, userRole) .contentType(APPLICATION_JSON)) @@ -383,17 +398,17 @@ private void updateProfile(UserProfile newData, String userName, String userRole .andReturn().getResponse().getContentAsString(), new TypeReference<>() { }); assertNotNull(updatedProfile); - assertEquals(newData.name(), updatedProfile.name()); - assertEquals(newData.loadFlowParameterId(), updatedProfile.loadFlowParameterId()); - assertEquals(newData.securityAnalysisParameterId(), updatedProfile.securityAnalysisParameterId()); - assertEquals(newData.sensitivityAnalysisParameterId(), updatedProfile.sensitivityAnalysisParameterId()); - assertEquals(newData.shortcircuitParameterId(), updatedProfile.shortcircuitParameterId()); - assertEquals(newData.pccMinParameterId(), updatedProfile.pccMinParameterId()); - assertEquals(newData.voltageInitParameterId(), updatedProfile.voltageInitParameterId()); - assertEquals(newData.maxAllowedCases(), updatedProfile.maxAllowedCases()); - assertNull(updatedProfile.allLinksValid()); // validity not set in this case - assertEquals(newData.spreadsheetConfigCollectionId(), updatedProfile.spreadsheetConfigCollectionId()); - assertEquals(newData.networkVisualizationParameterId(), updatedProfile.networkVisualizationParameterId()); + assertEquals(newData.getName(), updatedProfile.getName()); + assertEquals(newData.getLoadFlowParameterId(), updatedProfile.getLoadFlowParameterId()); + assertEquals(newData.getSecurityAnalysisParameterId(), updatedProfile.getSecurityAnalysisParameterId()); + assertEquals(newData.getSensitivityAnalysisParameterId(), updatedProfile.getSensitivityAnalysisParameterId()); + assertEquals(newData.getShortcircuitParameterId(), updatedProfile.getShortcircuitParameterId()); + assertEquals(newData.getPccMinParameterId(), updatedProfile.getPccMinParameterId()); + assertEquals(newData.getVoltageInitParameterId(), updatedProfile.getVoltageInitParameterId()); + assertEquals(newData.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD), updatedProfile.getMaxAllowValuesMap().get(MAX_ALLOWED_BUILD)); + assertNull(updatedProfile.getAllLinksValid()); // validity not set in this case + assertEquals(newData.getSpreadsheetConfigCollectionId(), updatedProfile.getSpreadsheetConfigCollectionId()); + assertEquals(newData.getNetworkVisualizationParameterId(), updatedProfile.getNetworkVisualizationParameterId()); } } } diff --git a/src/test/java/org/gridsuite/useradmin/server/controller/UserInfosControllerTest.java b/src/test/java/org/gridsuite/useradmin/server/controller/UserInfosControllerTest.java index 4b68447..b3eb701 100644 --- a/src/test/java/org/gridsuite/useradmin/server/controller/UserInfosControllerTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/controller/UserInfosControllerTest.java @@ -63,7 +63,11 @@ void cleanDB() { @Test void getUserDetail() throws Exception { // Create a profile - UserProfileEntity profileEntity = new UserProfileEntity(UUID.randomUUID(), PROFILE_A, null, null, null, null, null, null, 10, 20, null, null, null); + UserProfileEntity profileEntity = new UserProfileEntity(UUID.randomUUID(), PROFILE_A, null, + null, null, null, null, null, + 10, 20, null, null, null, null, + null, null, null, null, null, + null, null, null, null, null); userProfileRepository.save(profileEntity); // Create a user UserInfosEntity userInfosEntity = new UserInfosEntity(UUID.randomUUID(), USER_A, profileEntity, null); diff --git a/src/test/java/org/gridsuite/useradmin/server/service/UserInfosServiceTest.java b/src/test/java/org/gridsuite/useradmin/server/service/UserInfosServiceTest.java index 583cea0..69ac6d4 100644 --- a/src/test/java/org/gridsuite/useradmin/server/service/UserInfosServiceTest.java +++ b/src/test/java/org/gridsuite/useradmin/server/service/UserInfosServiceTest.java @@ -50,7 +50,11 @@ void toDtoUserInfoTest() { // get number of cases used mock when(directoryServiceMock.getCasesCount("user_A")).thenReturn(3); // create user and profile - UserProfileEntity profile = new UserProfileEntity(UUID.randomUUID(), "profile_A", null, null, null, null, null, null, 5, 6, null, null, null); + UserProfileEntity profile = new UserProfileEntity(UUID.randomUUID(), "profile_A", null, null, + null, null, null, null, 5, 6, + null, null, null, null, null, + null, null, null, null, null, + null, null, null, null); UserInfosEntity user = new UserInfosEntity(UUID.randomUUID(), "user_A", profile, null); when(userInfosRepositoryMock.findBySub("user_A")).thenReturn(Optional.of(user)); diff --git a/src/test/resources/application-default.yml b/src/test/resources/application-default.yml index 2ba9c15..a08ed20 100644 --- a/src/test/resources/application-default.yml +++ b/src/test/resources/application-default.yml @@ -25,3 +25,14 @@ powsybl-ws: useradmin: defaultMaxAllowedCases: 20 defaultMaxAllowedBuilds: 10 + defaultMaxAllowedLoadflow: 2 + defaultMaxAllowedSecurity: 2 + defaultMaxAllowedSensitivity: 1 + defaultMaxAllowedShortCircuit: 1 + defaultMaxAllowedVoltageInit: 2 + defaultMaxAllowedPccMin: 1 + defaultMaxAllowedStateEstimation: 1 + defaultMaxAllowedBalanceAdjustement: 1 + defaultMaxAllowedDynamicSimulation: 1 + defaultMaxAllowedDynamicSecurity: 1 + defaultMaxAllowedDynamicMargin: 1 diff --git a/src/test/resources/application-noquota.yml b/src/test/resources/application-noquota.yml index 0f362c3..3ad5466 100644 --- a/src/test/resources/application-noquota.yml +++ b/src/test/resources/application-noquota.yml @@ -1,3 +1,14 @@ useradmin: defaultMaxAllowedCases: - defaultMaxAllowedBuilds: \ No newline at end of file + defaultMaxAllowedBuilds: + defaultMaxAllowedLoadflow: + defaultMaxAllowedSecurity: + defaultMaxAllowedSensitivity: + defaultMaxAllowedShortCircuit: + defaultMaxAllowedVoltageInit: + defaultMaxAllowedPccMin: + defaultMaxAllowedStateEstimation: + defaultMaxAllowedBalanceAdjustement: + defaultMaxAllowedDynamicSimulation: + defaultMaxAllowedDynamicSecurity: + defaultMaxAllowedDynamicMargin: \ No newline at end of file