diff --git a/.idea/compiler.xml b/.idea/compiler.xml index 47634c5..2882170 100644 --- a/.idea/compiler.xml +++ b/.idea/compiler.xml @@ -5,7 +5,7 @@ - + diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java b/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java index 0d50334..0c19e9e 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/UserApiTest.java @@ -1,17 +1,23 @@ package com.digitalsanctuary.spring.user.api; import com.digitalsanctuary.spring.user.api.data.ApiTestData; +import com.digitalsanctuary.spring.user.api.data.DataStatus; import com.digitalsanctuary.spring.user.api.data.Response; -import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsHolder; +import com.digitalsanctuary.spring.user.api.helper.AssertionsHelper; +import com.digitalsanctuary.spring.user.api.provider.ApiTestUpdateUserArgumentsProvider; +import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder; import com.digitalsanctuary.spring.user.api.provider.ApiTestRegistrationArgumentsProvider; import com.digitalsanctuary.spring.user.dto.UserDto; -import com.digitalsanctuary.spring.user.json.JsonUtil; +import com.digitalsanctuary.spring.user.persistence.model.User; +import com.digitalsanctuary.spring.user.service.UserService; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ArgumentsSource; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; +import org.springframework.mock.web.MockHttpServletResponse; import org.springframework.test.web.servlet.ResultActions; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import com.digitalsanctuary.spring.user.jdbc.Jdbc; @@ -22,53 +28,75 @@ public class UserApiTest extends BaseApiTest { private static final String URL = "/user"; - private static final UserDto testUser = ApiTestData.getUserDto(); + @Autowired + private UserService userService; + + private static final UserDto baseTestUser = ApiTestData.getUserDto(); @AfterAll public static void deleteTestUser() { - Jdbc.deleteTestUser(testUser); + Jdbc.deleteTestUser(baseTestUser); } /** * * @param argumentsHolder * @throws Exception - * testing with three param: new user data, exist user data and invalid user data + * testing with three params: new user data, exist user data and invalid user data */ @ParameterizedTest @ArgumentsSource(ApiTestRegistrationArgumentsProvider.class) - public void registerUserAccount(ApiTestRegistrationArgumentsHolder argumentsHolder) throws Exception { + @Order(1) + public void registerUserAccount(ApiTestArgumentsHolder argumentsHolder) throws Exception { ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/registration") .contentType(MediaType.APPLICATION_FORM_URLENCODED) .content(buildUrlEncodedFormEntity(argumentsHolder.getUserDto()))); - if (argumentsHolder.getStatus() == ApiTestRegistrationArgumentsHolder.DataStatus.NEW) { + if (argumentsHolder.getStatus() == DataStatus.NEW) { action.andExpect(status().isOk()); } - if (argumentsHolder.getStatus() == ApiTestRegistrationArgumentsHolder.DataStatus.EXIST) { + if (argumentsHolder.getStatus() == DataStatus.EXIST) { action.andExpect(status().isConflict()); } - if (argumentsHolder.getStatus() == ApiTestRegistrationArgumentsHolder.DataStatus.INVALID) { + if (argumentsHolder.getStatus() == DataStatus.INVALID) { action.andExpect(status().is5xxServerError()); } - Response actual = JsonUtil.readValue(action.andReturn() - .getResponse().getContentAsString(), Response.class); + MockHttpServletResponse actual = action.andReturn().getResponse(); Response excepted = argumentsHolder.getResponse(); - Assertions.assertEquals(excepted, actual); + AssertionsHelper.compareResponses(actual, excepted); } @Test + @Order(2) public void resetPassword() throws Exception { ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/resetPassword") .contentType(MediaType.APPLICATION_FORM_URLENCODED) - .content(buildUrlEncodedFormEntity(testUser))) + .content(buildUrlEncodedFormEntity(baseTestUser))) + .andExpect(status().isOk()); + + MockHttpServletResponse actual = action.andReturn().getResponse(); + Response excepted = ApiTestData.resetPassword(); + AssertionsHelper.compareResponses(actual, excepted); + } + + @ParameterizedTest + @ArgumentsSource(ApiTestUpdateUserArgumentsProvider.class) + @Order(3) + public void updateUser(ApiTestArgumentsHolder argumentsHolder) throws Exception { + if(argumentsHolder.getStatus() == DataStatus.LOGGED) { + User user = userService.registerNewUserAccount(argumentsHolder.getUserDto()); + userService.authWithoutPassword(user); + } + + ResultActions action = perform(MockMvcRequestBuilders.post(URL + "/updateUser") + .contentType(MediaType.APPLICATION_FORM_URLENCODED) + .content(buildUrlEncodedFormEntity(argumentsHolder.getUserDto()))) .andExpect(status().isOk()); - Response actual = JsonUtil.readValue(action.andReturn().getResponse() - .getContentAsString(), Response.class); - Response expected = ApiTestData.resetPassword(); - Assertions.assertEquals(actual, expected); + MockHttpServletResponse actual = action.andReturn().getResponse(); + Response expected = argumentsHolder.getResponse(); + AssertionsHelper.compareResponses(actual, expected); } } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java b/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java index 8a8eb58..da82c1f 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/data/ApiTestData.java @@ -42,8 +42,21 @@ public static Response systemError() { public static Response resetPassword() { return new Response(true, null, "/user/forgot-password-pending-verification.html", - new String[]{"If account exists, password reset email has been sent!"}, null); + new String[]{"If account exists, password reset email has been sent!"}, null + ); } + public static Response userNotLogged() { + return new Response(false, null, null, + new String[]{"User Not Logged In!"}, null + ); + + } + public static Response userUpdateSuccess() { + return new Response(true, null, null, + new String[]{"Your Profile Was Successfully Updated.

"}, null + ); + + } } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java b/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java new file mode 100644 index 0000000..e131783 --- /dev/null +++ b/src/test/java/com/digitalsanctuary/spring/user/api/data/DataStatus.java @@ -0,0 +1,9 @@ +package com.digitalsanctuary.spring.user.api.data; + +public enum DataStatus { + NEW, + EXIST, + INVALID, + LOGGED, + NOT_LOGGED +} diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/data/Response.java b/src/test/java/com/digitalsanctuary/spring/user/api/data/Response.java index 711d1d9..ab551c4 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/data/Response.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/data/Response.java @@ -74,7 +74,7 @@ public boolean equals(Object obj) { @Override public String toString() { - return "RegistrationResponse{" + + return "ApiResponse{" + "success=" + success + ", redirectUrl='" + redirectUrl + '\'' + ", code=" + code + diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/helper/ApiTestHelper.java b/src/test/java/com/digitalsanctuary/spring/user/api/helper/ApiTestHelper.java index 6aa3364..3275093 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/helper/ApiTestHelper.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/helper/ApiTestHelper.java @@ -1,6 +1,7 @@ package com.digitalsanctuary.spring.user.api.helper; import com.digitalsanctuary.spring.user.dto.UserDto; +import com.digitalsanctuary.spring.user.persistence.model.User; import java.lang.reflect.Field; import java.net.URLEncoder; @@ -44,4 +45,11 @@ private static List buildParams(Object dto) { } return params; } + + public static User toUser(UserDto userDto) { + User user = new User(); + user.setEmail(userDto.getEmail()); + user.setPassword(userDto.getPassword()); + return user; + } } diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java b/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java new file mode 100644 index 0000000..d3270f4 --- /dev/null +++ b/src/test/java/com/digitalsanctuary/spring/user/api/helper/AssertionsHelper.java @@ -0,0 +1,15 @@ +package com.digitalsanctuary.spring.user.api.helper; + +import com.digitalsanctuary.spring.user.api.data.Response; +import com.digitalsanctuary.spring.user.json.JsonUtil; +import org.junit.jupiter.api.Assertions; +import org.springframework.mock.web.MockHttpServletResponse; + + +public class AssertionsHelper { + + public static void compareResponses(MockHttpServletResponse servletResponse, Response excepted) throws Exception{ + Response actual = JsonUtil.readValue(servletResponse.getContentAsString(), Response.class); + Assertions.assertEquals(actual, excepted); + } +} diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsProvider.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsProvider.java index ef110d9..c380b89 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsProvider.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsProvider.java @@ -1,6 +1,8 @@ package com.digitalsanctuary.spring.user.api.provider; import com.digitalsanctuary.spring.user.api.data.ApiTestData; +import com.digitalsanctuary.spring.user.api.data.DataStatus; +import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.ArgumentsProvider; @@ -12,21 +14,21 @@ public class ApiTestRegistrationArgumentsProvider implements ArgumentsProvider { @Override public Stream provideArguments(ExtensionContext extensionContext) throws Exception { return Stream.of( - new ApiTestRegistrationArgumentsHolder( + new ApiTestArgumentsHolder( ApiTestData.getEmptyUserDto(), - ApiTestRegistrationArgumentsHolder.DataStatus.INVALID, + DataStatus.INVALID, ApiTestData.systemError() ), - new ApiTestRegistrationArgumentsHolder( + new ApiTestArgumentsHolder( ApiTestData.getUserDto(), - ApiTestRegistrationArgumentsHolder.DataStatus.NEW, + DataStatus.NEW, ApiTestData.successRegistration() ), - new ApiTestRegistrationArgumentsHolder( + new ApiTestArgumentsHolder( ApiTestData.getUserDto(), - ApiTestRegistrationArgumentsHolder.DataStatus.EXIST, + DataStatus.EXIST, ApiTestData.userAlreadyExist() ) ).map(Arguments::of); diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java new file mode 100644 index 0000000..db73bb0 --- /dev/null +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestUpdateUserArgumentsProvider.java @@ -0,0 +1,29 @@ +package com.digitalsanctuary.spring.user.api.provider; + +import com.digitalsanctuary.spring.user.api.data.ApiTestData; +import com.digitalsanctuary.spring.user.api.data.DataStatus; +import com.digitalsanctuary.spring.user.api.provider.holder.ApiTestArgumentsHolder; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; + +import java.util.stream.Stream; + +public class ApiTestUpdateUserArgumentsProvider implements ArgumentsProvider +{ + @Override + public Stream provideArguments(ExtensionContext extensionContext) throws Exception { + return Stream.of( + new ApiTestArgumentsHolder( + ApiTestData.getUserDto(), + DataStatus.LOGGED, + ApiTestData.userUpdateSuccess() + ), + new ApiTestArgumentsHolder( + ApiTestData.getUserDto(), + DataStatus.NOT_LOGGED, + ApiTestData.userNotLogged() + ) + ).map(Arguments::of); + } +} diff --git a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsHolder.java b/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java similarity index 64% rename from src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsHolder.java rename to src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java index 97b035a..c26a462 100644 --- a/src/test/java/com/digitalsanctuary/spring/user/api/provider/ApiTestRegistrationArgumentsHolder.java +++ b/src/test/java/com/digitalsanctuary/spring/user/api/provider/holder/ApiTestArgumentsHolder.java @@ -1,22 +1,17 @@ -package com.digitalsanctuary.spring.user.api.provider; +package com.digitalsanctuary.spring.user.api.provider.holder; +import com.digitalsanctuary.spring.user.api.data.DataStatus; import com.digitalsanctuary.spring.user.api.data.Response; import com.digitalsanctuary.spring.user.dto.UserDto; -public class ApiTestRegistrationArgumentsHolder { - - public enum DataStatus { - NEW, - EXIST, - INVALID - } +public class ApiTestArgumentsHolder { private final UserDto userDto; private final DataStatus status; private final Response response; - public ApiTestRegistrationArgumentsHolder(UserDto userDto, DataStatus status, Response response) { + public ApiTestArgumentsHolder(UserDto userDto, DataStatus status, Response response) { this.userDto = userDto; this.status = status; this.response = response;