-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Display user information * add get user information controller * Address PR feedback * improve getting profileName --------- Signed-off-by: TOURI ANIS <[email protected]>
- Loading branch information
Showing
11 changed files
with
307 additions
and
15 deletions.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
src/main/java/org/gridsuite/useradmin/server/controller/UserInfoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* 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 | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.gridsuite.useradmin.server.controller; | ||
|
||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import org.gridsuite.useradmin.server.UserAdminApi; | ||
import org.gridsuite.useradmin.server.dto.UserInfos; | ||
import org.gridsuite.useradmin.server.service.UserInfosService; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
/** | ||
* @author Anis TOURI <anis.touri at rte-france.com> | ||
*/ | ||
@RestController | ||
@RequestMapping(value = "/" + UserAdminApi.API_VERSION + "/users") | ||
@Tag(name = "UserInfoController") | ||
public class UserInfoController { | ||
|
||
private final UserInfosService userInfosService; | ||
|
||
public UserInfoController(UserInfosService userInfosService) { | ||
this.userInfosService = userInfosService; | ||
} | ||
|
||
@GetMapping(value = "/{sub}/detail", produces = "application/json") | ||
@Operation(summary = "get detailed user information") | ||
@ApiResponse(responseCode = "200", description = "The user exist") | ||
@ApiResponse(responseCode = "404", description = "The user doesn't exist") | ||
public ResponseEntity<UserInfos> getUserDetail(@PathVariable("sub") String sub) { | ||
return ResponseEntity.of(userInfosService.getUserInfo(sub)); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
src/main/java/org/gridsuite/useradmin/server/service/UserInfosService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package org.gridsuite.useradmin.server.service; | ||
|
||
import org.gridsuite.useradmin.server.UserAdminApplicationProps; | ||
import org.gridsuite.useradmin.server.dto.UserInfos; | ||
import org.gridsuite.useradmin.server.entity.UserInfosEntity; | ||
import org.gridsuite.useradmin.server.entity.UserProfileEntity; | ||
import org.gridsuite.useradmin.server.repository.UserInfosRepository; | ||
import org.springframework.context.annotation.Lazy; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
@Service | ||
public class UserInfosService { | ||
|
||
private final UserInfosService self; | ||
private final UserInfosRepository userInfosRepository; | ||
private final DirectoryService directoryService; | ||
private final UserAdminApplicationProps applicationProps; | ||
private final AdminRightService adminRightService; | ||
|
||
public UserInfosService(@Lazy final UserInfosService self, | ||
final UserInfosRepository userInfosRepository, | ||
final DirectoryService directoryService, | ||
final UserAdminApplicationProps applicationProps, | ||
final AdminRightService adminRightService) { | ||
this.self = Objects.requireNonNull(self); | ||
this.userInfosRepository = Objects.requireNonNull(userInfosRepository); | ||
this.directoryService = Objects.requireNonNull(directoryService); | ||
this.applicationProps = Objects.requireNonNull(applicationProps); | ||
this.adminRightService = Objects.requireNonNull(adminRightService); | ||
} | ||
|
||
public UserInfos toDtoUserInfo(final UserInfosEntity userInfosEntity, Integer casesUsed) { | ||
// get max allowed cases | ||
Integer maxAllowedCases = Optional.ofNullable(userInfosEntity.getProfile()) | ||
.map(UserProfileEntity::getMaxAllowedCases) | ||
.orElse(applicationProps.getDefaultMaxAllowedCases()); | ||
// get max allowed builds | ||
Integer maxAllowedBuilds = Optional.ofNullable(userInfosEntity.getProfile()) | ||
.map(UserProfileEntity::getMaxAllowedBuilds) | ||
.orElse(applicationProps.getDefaultMaxAllowedBuilds()); | ||
return UserInfosEntity.toDtoWithDetail(userInfosEntity, adminRightService::isAdmin, maxAllowedCases, casesUsed, maxAllowedBuilds); | ||
} | ||
|
||
public Optional<UserInfos> getUserInfo(String sub) { | ||
Optional<UserInfosEntity> userInfosEntity = self.getUserInfosEntity(sub); | ||
if (userInfosEntity.isPresent()) { | ||
// get number of cases used | ||
Integer casesUsed = directoryService.getCasesCount(userInfosEntity.get().getSub()); | ||
|
||
return Optional.of(toDtoUserInfo(userInfosEntity.get(), casesUsed)); | ||
} | ||
return Optional.empty(); | ||
} | ||
|
||
@Transactional(readOnly = true) | ||
public Optional<UserInfosEntity> getUserInfosEntity(String sub) { | ||
return userInfosRepository.findBySub(sub); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/test/java/org/gridsuite/useradmin/server/controller/UserInfosControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/** | ||
* 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 | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
|
||
package org.gridsuite.useradmin.server.controller; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.gridsuite.useradmin.server.UserAdminApi; | ||
import org.gridsuite.useradmin.server.dto.UserInfos; | ||
import org.gridsuite.useradmin.server.entity.UserInfosEntity; | ||
import org.gridsuite.useradmin.server.entity.UserProfileEntity; | ||
import org.gridsuite.useradmin.server.repository.UserInfosRepository; | ||
import org.gridsuite.useradmin.server.repository.UserProfileRepository; | ||
import org.gridsuite.useradmin.server.service.DirectoryService; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.MockBean; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
|
||
import java.util.UUID; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.head; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
/** | ||
* @author Anis Touri <anis.touri at rte-france.com> | ||
*/ | ||
@AutoConfigureMockMvc | ||
@SpringBootTest | ||
class UserInfosControllerTest { | ||
|
||
private static final String PROFILE_A = "profile_A"; | ||
private static final String USER_A = "user_A"; | ||
|
||
private static final String API_BASE_PATH = "/" + UserAdminApi.API_VERSION; | ||
|
||
@Autowired | ||
UserProfileRepository userProfileRepository; | ||
@Autowired | ||
private UserInfosRepository userInfosRepository; | ||
|
||
@MockBean | ||
private DirectoryService directoryService; | ||
@Autowired | ||
private MockMvc mockMvc; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@AfterEach | ||
public void cleanDB() { | ||
userInfosRepository.deleteAll(); | ||
} | ||
|
||
@Test | ||
void getUserDetail() throws Exception { | ||
// Create a profile | ||
UserProfileEntity profileEntity = new UserProfileEntity(UUID.randomUUID(), PROFILE_A, null, 10, 20); | ||
userProfileRepository.save(profileEntity); | ||
// Create a user | ||
UserInfosEntity userInfosEntity = new UserInfosEntity(UUID.randomUUID(), USER_A, profileEntity); | ||
userInfosRepository.save(userInfosEntity); | ||
|
||
// Mock the calls to the directory service and the database | ||
when(directoryService.getCasesCount(USER_A)).thenReturn(5); | ||
|
||
UserInfos userInfos = objectMapper.readValue( | ||
mockMvc.perform(head(API_BASE_PATH + "/users/{sub}/detail", USER_A)) | ||
.andExpect(status().isOk()) | ||
.andReturn().getResponse().getContentAsString(), UserInfos.class); | ||
assertNotNull(userInfos); | ||
assertEquals(USER_A, userInfos.sub()); | ||
assertFalse(userInfos.isAdmin()); | ||
assertEquals(PROFILE_A, userInfos.profileName()); | ||
assertEquals(10, userInfos.maxAllowedCases()); | ||
assertEquals(5, userInfos.numberCasesUsed()); | ||
assertEquals(20, userInfos.maxAllowedBuilds()); | ||
|
||
} | ||
} |
Oops, something went wrong.