Skip to content

Commit

Permalink
add 1 test
Browse files Browse the repository at this point in the history
Signed-off-by: David BRAQUART <[email protected]>
  • Loading branch information
dbraquart committed Mar 28, 2024
1 parent f9078b8 commit a3ad1ec
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 3 deletions.
35 changes: 35 additions & 0 deletions src/test/java/org/gridsuite/useradmin/server/DtoConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.assertj.core.api.WithAssertions;
import org.gridsuite.useradmin.server.dto.UserConnection;
import org.gridsuite.useradmin.server.dto.UserInfos;
import org.gridsuite.useradmin.server.dto.UserProfile;
import org.gridsuite.useradmin.server.entity.ConnectionEntity;
import org.gridsuite.useradmin.server.entity.UserInfosEntity;
import org.gridsuite.useradmin.server.entity.UserProfileEntity;
Expand All @@ -12,6 +13,7 @@
import org.mockito.Mockito;

import java.time.*;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;

Expand Down Expand Up @@ -51,6 +53,39 @@ void testConversionToDtoOfUserInfosNull() {
}
}

@Nested
class ConverterOfUserProfile {
@Test
void testConversionToDtoOfUserProfile() {
final UUID uuid = UUID.randomUUID();
final UUID parameterUuid = UUID.randomUUID();
// no parameters
assertThat(UserProfileEntity.toDto(new UserProfileEntity(uuid, "profil", null), Set.of(UUID.randomUUID())))
.as("dto result")
.isEqualTo(new UserProfile(uuid, "profil", null, null));
assertThat(UserProfileEntity.toDto(new UserProfileEntity(uuid, "profil", null)))
.as("dto result")
.isEqualTo(new UserProfile(uuid, "profil", null, null));
// with parameters, no validity check
assertThat(UserProfileEntity.toDto(new UserProfileEntity(uuid, "profil", parameterUuid)))
.as("dto result")
.isEqualTo(new UserProfile(uuid, "profil", parameterUuid, null));
// with parameters and validity ok
assertThat(UserProfileEntity.toDto(new UserProfileEntity(uuid, "profil", parameterUuid), Set.of(UUID.randomUUID())))
.as("dto result")
.isEqualTo(new UserProfile(uuid, "profil", parameterUuid, Boolean.TRUE));
// with parameters and validity ko
assertThat(UserProfileEntity.toDto(new UserProfileEntity(uuid, "profil", parameterUuid), Set.of(parameterUuid)))
.as("dto result")
.isEqualTo(new UserProfile(uuid, "profil", parameterUuid, Boolean.FALSE));
}

@Test
void testConversionToDtoOfUserProfileNull() {
assertThat(UserProfileEntity.toDto(null, null)).isNull();
}
}

@Nested
class ConverterOfUserConnection {
@Test
Expand Down
59 changes: 56 additions & 3 deletions src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.gridsuite.useradmin.server.dto.UserInfos;
import org.gridsuite.useradmin.server.entity.ConnectionEntity;
import org.gridsuite.useradmin.server.repository.ConnectionRepository;
import org.gridsuite.useradmin.server.repository.UserAdminRepository;
import org.gridsuite.useradmin.server.entity.UserInfosEntity;
import org.gridsuite.useradmin.server.service.NotificationService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -56,10 +57,10 @@ class UserAdminTest {
private ConnectionRepository connectionRepository;

@Autowired
private NotificationService notificationService;
private OutputDestination output;

@Autowired
private OutputDestination output;
private ObjectMapper mapper;

private final String maintenanceMessageDestination = "config.message";

Expand All @@ -76,6 +77,7 @@ public void cleanDB() {
private static final String USER_UNKNOWN = "UNKNOWN";
private static final String ADMIN_USER = "admin1";
private static final String NOT_ADMIN = "notAdmin";
private static final String PROFILE_1 = "profile_1";

@Test
void testUserAdmin() throws Exception {
Expand Down Expand Up @@ -187,6 +189,57 @@ void testUserAdmin() throws Exception {
.andReturn();
}

@Test
void testUpdateUser() throws Exception {
ObjectWriter objectWriter = mapper.writer().withDefaultPrettyPrinter();
// add a user
mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB)
.header("userId", ADMIN_USER)
)
.andExpect(status().isCreated())
.andReturn();
UserInfos userInfos = objectMapper.readValue(
mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB)
.header("userId", ADMIN_USER)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
// the new user has no profile by default
assertNotNull(userInfos);
assertNull(userInfos.profileName());
assertEquals(USER_SUB, userInfos.sub());

// Create a profile
mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/profiles/{profileName}", PROFILE_1)
.header("userId", ADMIN_USER)
)
.andExpect(status().isCreated())
.andReturn();

// udpate the user: change its name and link it to the profile
UserInfos userInfo = new UserInfos(USER_SUB2, false, PROFILE_1);
mockMvc.perform(put("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB)
.content(objectWriter.writeValueAsString(userInfo))
.contentType(MediaType.APPLICATION_JSON)
.header("userId", ADMIN_USER))
.andExpect(status().isOk());

userInfos = objectMapper.readValue(
mockMvc.perform(get("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB2)
.header("userId", ADMIN_USER)
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
// the new user has the new name and profile
assertNotNull(userInfos);
assertEquals(USER_SUB2, userInfos.sub());
assertEquals(PROFILE_1, userInfos.profileName());
}

@Test
void testGetConnections() throws Exception {
mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/users/{sub}", USER_SUB)
Expand Down

0 comments on commit a3ad1ec

Please sign in to comment.