Skip to content

Commit

Permalink
Migrate tests to JUnit5 (#45)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tristan-WorkGH authored Oct 17, 2024
1 parent f512352 commit 6aa432a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 159 deletions.
15 changes: 0 additions & 15 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,6 @@
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/org/gridsuite/useradmin/server/NoAdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
*/
package org.gridsuite.useradmin.server;

import org.gridsuite.useradmin.server.entity.UserInfosEntity;
import org.gridsuite.useradmin.server.repository.ConnectionRepository;
import org.gridsuite.useradmin.server.repository.UserInfosRepository;
import org.gridsuite.useradmin.server.entity.UserInfosEntity;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -39,7 +39,7 @@ class NoAdminTest {
private ConnectionRepository connectionRepository;

@AfterEach
public void cleanDB() {
void cleanDB() {
userInfosRepository.deleteAll();
connectionRepository.deleteAll();
}
Expand Down
41 changes: 14 additions & 27 deletions src/test/java/org/gridsuite/useradmin/server/NoQuotaTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import lombok.SneakyThrows;
import org.gridsuite.useradmin.server.dto.UserInfos;
import org.gridsuite.useradmin.server.dto.UserProfile;
import org.gridsuite.useradmin.server.entity.UserProfileEntity;
Expand Down Expand Up @@ -65,36 +64,33 @@ class NoQuotaTest {
private ObjectWriter objectWriter;

@BeforeEach
public void setUp() {
void setUp() {
objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
}

@AfterEach
public void cleanDB() {
void cleanDB() {
userInfosRepository.deleteAll();
userProfileRepository.deleteAll();
}

@Test
@SneakyThrows
void testProfileCreation() {
void testProfileCreation() throws Exception {
createProfile(PROFILE_ONE, null, null);
// test with quotas
createProfile(PROFILE_TWO, 10, 20);
}

@Test
@SneakyThrows
void testUserCreationWithoutProfile() {
void testUserCreationWithoutProfile() throws Exception {
createUser(USER_SUB);

assertTrue(getMaxAllowedBuilds(USER_SUB).isEmpty());
assertTrue(getMaxAllowedCases(USER_SUB).isEmpty());
}

@Test
@SneakyThrows
void testUserCreationWithProfile() {
void testUserCreationWithProfile() throws Exception {
//profile with no quotas
createProfile(PROFILE_ONE, null, null);
createUser(USER_SUB);
Expand All @@ -112,8 +108,7 @@ void testUserCreationWithProfile() {
assertEquals("20", getMaxAllowedBuilds(USER_SUB_TWO));
}

@SneakyThrows
private void createProfile(String profileName, Integer maxAllowedCases, Integer maxAllowedBuilds) {
private void createProfile(String profileName, Integer maxAllowedCases, Integer maxAllowedBuilds) throws Exception {
UserProfile profileInfo = new UserProfile(null, profileName, null, false, maxAllowedCases, maxAllowedBuilds);
performPost(API_BASE_PATH + "/profiles", profileInfo);

Expand All @@ -124,8 +119,7 @@ private void createProfile(String profileName, Integer maxAllowedCases, Integer
assertEquals(maxAllowedBuilds, createdProfile.get().getMaxAllowedBuilds());
}

@SneakyThrows
private void createUser(String userSub) {
private void createUser(String userSub) throws Exception {
performPost(API_BASE_PATH + "/users/" + userSub, null);

// check user creation
Expand All @@ -135,50 +129,43 @@ private void createUser(String userSub) {
assertEquals(userSub, userInfos.sub());
}

@SneakyThrows
private UserInfos getUserInfos(String userSub) {
private UserInfos getUserInfos(String userSub) throws Exception {
MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub);
return objectMapper.readValue(result.getResponse().getContentAsString(), UserInfos.class);
}

@SneakyThrows
private void associateProfileToUser(String userSub, String profileName) {
private void associateProfileToUser(String userSub, String profileName) throws Exception {
UserInfos userInfos = new UserInfos(userSub, false, profileName, null, null, null);
performPut(API_BASE_PATH + "/users/" + userSub, userInfos);
}

@SneakyThrows
private String getMaxAllowedBuilds(String userSub) {
private String getMaxAllowedBuilds(String userSub) throws Exception {
MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub + "/profile/max-builds");
return result.getResponse().getContentAsString();
}

@SneakyThrows
private String getMaxAllowedCases(String userSub) {
private String getMaxAllowedCases(String userSub) throws Exception {
MvcResult result = performGet(API_BASE_PATH + "/users/" + userSub + "/profile/max-cases");
return result.getResponse().getContentAsString();
}

@SneakyThrows
private void performPost(String url, Object content) {
private void performPost(String url, Object content) throws Exception {
mockMvc.perform(post(url)
.content(content != null ? objectWriter.writeValueAsString(content) : "")
.contentType(APPLICATION_JSON)
.header("userId", ADMIN_USER))
.andExpect(status().isCreated());
}

@SneakyThrows
private void performPut(String url, Object content) {
private void performPut(String url, Object content) throws Exception {
mockMvc.perform(put(url)
.content(objectWriter.writeValueAsString(content))
.contentType(APPLICATION_JSON)
.header("userId", ADMIN_USER))
.andExpect(status().isOk());
}

@SneakyThrows
private MvcResult performGet(String url) {
private MvcResult performGet(String url) throws Exception {
return mockMvc.perform(get(url)
.header("userId", ADMIN_USER))
.andExpect(status().isOk())
Expand Down
64 changes: 23 additions & 41 deletions src/test/java/org/gridsuite/useradmin/server/UserAdminTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import lombok.SneakyThrows;
import org.gridsuite.useradmin.server.dto.UserInfos;
import org.gridsuite.useradmin.server.dto.UserProfile;
import org.gridsuite.useradmin.server.entity.ConnectionEntity;
Expand Down Expand Up @@ -67,14 +66,14 @@ class UserAdminTest {
@Autowired
private OutputDestination output;

private final String maintenanceMessageDestination = "config.message";
private static final String MAINTENANCE_MESSAGE_DESTINATION = "config.message";

private final String userMessageDestination = "directory.update";
private static final String USER_MESSAGE_DESTINATION = "directory.update";

private static final long TIMEOUT = 1000;

@AfterEach
public void cleanDB() {
void cleanDB() {
userInfosRepository.deleteAll();
userProfileRepository.deleteAll();
connectionRepository.deleteAll();
Expand Down Expand Up @@ -103,8 +102,7 @@ void testUserAdmin() throws Exception {
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
new TypeReference<>() { });

assertEquals(0, userInfos.size());

Expand All @@ -124,8 +122,7 @@ void testUserAdmin() throws Exception {
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
new TypeReference<>() { });

assertEquals(1, userInfos.size());

Expand Down Expand Up @@ -160,8 +157,7 @@ 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)
Expand Down Expand Up @@ -211,8 +207,7 @@ void testUserAdmin() throws Exception {
}

@Test
@SneakyThrows
void testUpdateUser() {
void testUpdateUser() throws Exception {
createUser(USER_SUB);
createProfile(PROFILE_1);

Expand All @@ -227,20 +222,17 @@ void testUpdateUser() {
}

@Test
@SneakyThrows
void testUpdateUserNotFound() {
void testUpdateUserNotFound() throws Exception {
updateUser("nofFound", new UserInfos("nofFound", false, "prof", null, null, null), HttpStatus.NOT_FOUND, ADMIN_USER);
}

@Test
@SneakyThrows
void testUpdateUserForbidden() {
void testUpdateUserForbidden() throws Exception {
updateUser("dummy", new UserInfos("dummy", false, "prof", null, null, null), HttpStatus.FORBIDDEN, NOT_ADMIN);
}

@Test
@SneakyThrows
void testGetUserProfileNotFound() {
void testGetUserProfileNotFound() throws Exception {
getUserProfile("BadUser", HttpStatus.NOT_FOUND);
}

Expand All @@ -264,8 +256,7 @@ void testGetConnections() throws Exception {
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
new TypeReference<>() { });

assertEquals(2, userInfos.size());

Expand All @@ -283,8 +274,7 @@ void testGetConnections() throws Exception {
.contentType(APPLICATION_JSON))
.andExpect(status().isOk())
.andReturn().getResponse().getContentAsString(),
new TypeReference<>() {
});
new TypeReference<>() { });

assertEquals(2, connectionEntities.size());

Expand All @@ -298,8 +288,7 @@ 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")
Expand Down Expand Up @@ -363,30 +352,29 @@ void testSendUserMessage() throws Exception {
}

private void assertUserMessageSent(String messageId, String sub) {
Message<byte[]> message = output.receive(TIMEOUT, userMessageDestination);
Message<byte[]> message = output.receive(TIMEOUT, USER_MESSAGE_DESTINATION);
MessageHeaders headers = message.getHeaders();
assertEquals(messageId, headers.get(HEADER_USER_MESSAGE));
assertEquals(sub, headers.get(HEADER_USER_ID));

}

private void assertMaintenanceMessageSent(String maintenanceMessage, Integer duration) {
Message<byte[]> message = output.receive(TIMEOUT, maintenanceMessageDestination);
Message<byte[]> message = output.receive(TIMEOUT, MAINTENANCE_MESSAGE_DESTINATION);
assertEquals(maintenanceMessage, new String(message.getPayload()));
MessageHeaders headers = message.getHeaders();
assertEquals(MESSAGE_TYPE_MAINTENANCE, headers.get(HEADER_MESSAGE_TYPE));
assertEquals(duration, headers.get(HEADER_DURATION));
}

private void assertCancelMaintenanceMessageSent() {
Message<byte[]> message = output.receive(TIMEOUT, maintenanceMessageDestination);
Message<byte[]> message = output.receive(TIMEOUT, MAINTENANCE_MESSAGE_DESTINATION);
assertEquals("", new String(message.getPayload()));
MessageHeaders headers = message.getHeaders();
assertEquals(MESSAGE_TYPE_CANCEL_MAINTENANCE, headers.get(HEADER_MESSAGE_TYPE));
}

@SneakyThrows
private void createUser(String userName) {
private void createUser(String userName) throws Exception {
mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/users/{sub}", userName)
.header("userId", ADMIN_USER)
)
Expand All @@ -398,16 +386,14 @@ private void createUser(String userName) {
.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());
assertEquals(userName, userInfos.sub());
}

@SneakyThrows
private void createProfile(String profileName) {
private void createProfile(String profileName) throws Exception {
ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
UserProfile profileInfo = new UserProfile(null, profileName, null, false, null, null);
mockMvc.perform(post("/" + UserAdminApi.API_VERSION + "/profiles")
Expand All @@ -419,8 +405,7 @@ private void createProfile(String profileName) {
.andReturn();
}

@SneakyThrows
private void updateUser(String updatedUserName, UserInfos userInfos, HttpStatusCode status, String userName) {
private void updateUser(String updatedUserName, UserInfos userInfos, HttpStatusCode status, String userName) throws Exception {
ObjectWriter objectWriter = objectMapper.writer().withDefaultPrettyPrinter();
mockMvc.perform(put("/" + UserAdminApi.API_VERSION + "/users/{sub}", updatedUserName)
.content(objectWriter.writeValueAsString(userInfos))
Expand All @@ -435,24 +420,21 @@ private void updateUser(String updatedUserName, UserInfos userInfos, HttpStatusC
.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());
assertEquals(userInfos.profileName(), updatedUserInfos.profileName());
}
}

@SneakyThrows
private UserProfile getUserProfile(String userName, HttpStatusCode status) {
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();
if (status == HttpStatus.OK) {
return objectMapper.readValue(response, new TypeReference<>() {
});
return objectMapper.readValue(response, new TypeReference<>() { });
}
return null;
}
Expand Down
Loading

0 comments on commit 6aa432a

Please sign in to comment.