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
13 changes: 13 additions & 0 deletions src/main/java/org/gridsuite/explore/server/dto/QuotaState.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/**
* Copyright (c) 2026, 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.explore.server.dto;

/**
* @author Ghiles Abdellah {@literal <ghiles.abdellah at rte-france.com>}
*/
public record QuotaState(Integer current, Integer max) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package org.gridsuite.explore.server.services;

import lombok.Setter;
import org.gridsuite.explore.server.dto.QuotaState;
import org.gridsuite.explore.server.dto.QuotaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
Expand All @@ -25,7 +26,7 @@ public class UserAdminService {

private static final String USER_ADMIN_API_VERSION = "v1";
private static final String USERS_QUOTA_URI = "/users/{sub}/quota";
private static final String USERS_MAX_QUOTA_URI = USERS_QUOTA_URI + "/max";
private static final String USERS_QUOTA_STATE_URI = USERS_QUOTA_URI + "/state";
private static final String CASES_ALERT_THRESHOLD_URI = "/cases-alert-threshold";
private static final String DELIMITER = "/";
private final RestTemplate restTemplate;
Expand All @@ -38,21 +39,25 @@ public UserAdminService(RestTemplate restTemplate, RemoteServicesProperties remo
this.restTemplate = restTemplate;
}

public Map<QuotaType, Integer> getUserMaxQuota(String sub) {
String path = UriComponentsBuilder.fromPath(DELIMITER + USER_ADMIN_API_VERSION + USERS_MAX_QUOTA_URI)
public Map<QuotaType, QuotaState> getUserQuotaState(String sub) {
String path = UriComponentsBuilder.fromPath(DELIMITER + USER_ADMIN_API_VERSION + USERS_QUOTA_STATE_URI)
.buildAndExpand(sub).toUriString();
return restTemplate.exchange(
userAdminServerBaseUri + path,
HttpMethod.GET,
null,
new ParameterizedTypeReference<Map<QuotaType, Integer>>() {
new ParameterizedTypeReference<Map<QuotaType, QuotaState>>() {
}).getBody();
}

public Integer getUserMaxAllowedCases(String sub) {
Map<QuotaType, Integer> userMaxQuotas = getUserMaxQuota(sub);
Map<QuotaType, QuotaState> userMaxQuotas = getUserQuotaState(sub);

return userMaxQuotas.getOrDefault(QuotaType.CASES, null);
QuotaState stateCases = userMaxQuotas.getOrDefault(QuotaType.CASES, null);
if (stateCases != null) {
return stateCases.max();
}
return null;
}

public Integer getCasesAlertThreshold() {
Expand Down
20 changes: 10 additions & 10 deletions src/test/java/org/gridsuite/explore/server/ExploreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -424,17 +424,17 @@ public MockResponse dispatch(RecordedRequest request) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), listOfPrivateStudyAttributesAsString.replace("elementUuid", "id"));
} else if (path.matches("/v1/process-configs/metadata[?]ids=" + PROCESS_CONFIG_UUID)) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(List.of(processConfigSprecificMetadata)));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_EXCEEDED + "/quota/max")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, 3)));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_NOT_EXCEEDED + "/quota/max")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, 5)));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_NOT_EXCEEDED_2 + "/quota/max")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, 5)));
} else if (path.matches("/v1/users/" + USER_NOT_FOUND + "/quota/max")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, 5)));
} else if (path.matches("/v1/users/" + USER_UNEXPECTED_ERROR + "/quota/max")) {
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_EXCEEDED + "/quota/state")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, new QuotaState(4, 3))));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_NOT_EXCEEDED + "/quota/state")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, new QuotaState(2, 5))));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_NOT_EXCEEDED_2 + "/quota/state")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, new QuotaState(1, 5))));
} else if (path.matches("/v1/users/" + USER_NOT_FOUND + "/quota/state")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Map.of(QuotaType.CASES, new QuotaState(0, 5))));
} else if (path.matches("/v1/users/" + USER_UNEXPECTED_ERROR + "/quota/state")) {
return new MockResponse(500);
} else if (path.matches("/v1/users/.*/quota/max")) {
} else if (path.matches("/v1/users/.*/quota/state")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), mapper.writeValueAsString(Collections.emptyMap()));
} else if (path.matches("/v1/users/" + USER_WITH_CASE_LIMIT_EXCEEDED + "/cases/count")) {
return new MockResponse(200, Headers.of(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE), "4");
Expand Down
Loading