Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#150 | Fix response type of save user endpoint #153

Merged
merged 3 commits into from
Aug 23, 2023
Merged
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
10 changes: 3 additions & 7 deletions src/main/java/com/ays/user/controller/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
import com.ays.user.model.dto.request.UserSaveRequest;
import com.ays.user.model.dto.request.UserUpdateRequest;
import com.ays.user.model.dto.response.UserResponse;
import com.ays.user.model.dto.response.UserSavedResponse;
import com.ays.user.model.dto.response.UsersResponse;
import com.ays.user.model.mapper.UserToUserResponseMapper;
import com.ays.user.model.mapper.UserToUserSavedResponseMapper;
import com.ays.user.model.mapper.UserToUsersResponseMapper;
import com.ays.user.service.UserSaveService;
import com.ays.user.service.UserService;
Expand All @@ -36,7 +34,6 @@ class UserController {
private final UserService userService;
private final UserSaveService userSaveService;

private final UserToUserSavedResponseMapper userToUserSavedResponseMapper = UserToUserSavedResponseMapper.initialize();
private final UserToUserResponseMapper userToUserResponseMapper = UserToUserResponseMapper.initialize();
private final UserToUsersResponseMapper userToUsersResponseMapper = UserToUsersResponseMapper.initialize();

Expand Down Expand Up @@ -83,10 +80,9 @@ public AysResponse<UserResponse> getUserById(@PathVariable @UUID String id) {
*/
@PostMapping("/user")
@PreAuthorize("hasAnyAuthority('ADMIN')")
public AysResponse<UserSavedResponse> saveUser(@RequestBody @Valid UserSaveRequest saveRequest) {
User user = userSaveService.saveUser(saveRequest);
UserSavedResponse userSavedResponse = userToUserSavedResponseMapper.map(user);
return AysResponse.successOf(userSavedResponse);
public AysResponse<Void> saveUser(@RequestBody @Valid UserSaveRequest saveRequest) {
userSaveService.saveUser(saveRequest);
return AysResponse.SUCCESS;
}

/**
Expand Down

This file was deleted.

This file was deleted.

3 changes: 1 addition & 2 deletions src/main/java/com/ays/user/service/UserSaveService.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ public interface UserSaveService {
* Saves a saveRequest to the database.
*
* @param saveRequest the UserSaveRequest entity
* @return User
*/
User saveUser(UserSaveRequest saveRequest);
void saveUser(UserSaveRequest saveRequest);

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ class UserSaveServiceImpl implements UserSaveService {
* Saves a new user based on the provided save request.
*
* @param saveRequest the request object containing user data to be saved
* @return the saved user
* @throws AysUserAlreadyExistsByPhoneNumberException if a user with the same phone number already exists
*/
public User saveUser(final UserSaveRequest saveRequest) {
public void saveUser(final UserSaveRequest saveRequest) {

final List<UserEntity> usersFromDatabase = userRepository.findAll();

Expand All @@ -58,11 +57,6 @@ public User saveUser(final UserSaveRequest saveRequest) {
);

userRepository.save(userEntity);

return User.builder()
.username(username)
.password(password)
.build();
}

/**
Expand Down
22 changes: 3 additions & 19 deletions src/test/java/com/ays/user/controller/UserControllerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import com.ays.user.model.UserBuilder;
import com.ays.user.model.dto.request.*;
import com.ays.user.model.dto.response.UserResponse;
import com.ays.user.model.dto.response.UserSavedResponse;
import com.ays.user.model.dto.response.UserSavedResponseBuilder;
import com.ays.user.model.dto.response.UsersResponse;
import com.ays.user.model.entity.UserEntity;
import com.ays.user.model.entity.UserEntityBuilder;
Expand All @@ -35,7 +33,6 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.List;

Expand All @@ -62,20 +59,11 @@ void givenValidUserSaveRequest_whenUserSaved_thenReturnUserSavedResponse() throw
.build();

// When
User mockUser = new UserBuilder()
.withUsername("123456")
.withPassword("987654")
.withStatus(UserStatus.ACTIVE).build();
Mockito.when(userSaveService.saveUser(Mockito.any(UserSaveRequest.class)))
.thenReturn(mockUser);
Mockito.doNothing().when(userSaveService).saveUser(Mockito.any(UserSaveRequest.class));

// Then
String endpoint = BASE_PATH.concat("/user");
UserSavedResponse mockUserSavedResponse = new UserSavedResponseBuilder()
.withUsername(mockUser.getUsername())
.withPassword(mockUser.getPassword())
.build();
AysResponse<UserSavedResponse> mockResponse = AysResponseBuilder.successOf(mockUserSavedResponse);
AysResponse<Void> mockResponse = AysResponseBuilder.SUCCESS;
mockMvc.perform(AysMockMvcRequestBuilders
.post(endpoint, mockAdminUserToken.getAccessToken(), mockUserSaveRequest))
.andDo(MockMvcResultHandlers.print())
Expand All @@ -87,11 +75,7 @@ void givenValidUserSaveRequest_whenUserSaved_thenReturnUserSavedResponse() throw
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(mockResponse.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.isNotEmpty())
.andExpect(MockMvcResultMatchers.jsonPath("$.response.username")
.value(mockResponse.getResponse().getUsername()))
.andExpect(MockMvcResultMatchers.jsonPath("$.response.password")
.value(mockResponse.getResponse().getPassword()));
.doesNotExist());

Mockito.verify(userSaveService, Mockito.times(1))
.saveUser(Mockito.any(UserSaveRequest.class));
Expand Down
12 changes: 2 additions & 10 deletions src/test/java/com/ays/user/controller/UserSystemTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import com.ays.user.model.UserBuilder;
import com.ays.user.model.dto.request.*;
import com.ays.user.model.dto.response.UserResponse;
import com.ays.user.model.dto.response.UserSavedResponse;
import com.ays.user.model.dto.response.UserSavedResponseBuilder;
import com.ays.user.model.dto.response.UsersResponse;
import com.ays.user.model.entity.UserEntity;
import com.ays.user.model.entity.UserEntityBuilder;
Expand All @@ -31,7 +29,6 @@
import org.springframework.data.domain.PageImpl;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

import java.util.List;

Expand All @@ -53,8 +50,7 @@ void givenValidUserSaveRequest_whenUserSaved_thenReturnUserSavedResponse() throw

// Then
String endpoint = BASE_PATH.concat("/user");
UserSavedResponse userSavedResponse = new UserSavedResponseBuilder().build();
AysResponse<UserSavedResponse> response = AysResponseBuilder.successOf(userSavedResponse);
AysResponse<Void> response = AysResponseBuilder.SUCCESS;
mockMvc.perform(AysMockMvcRequestBuilders
.post(endpoint, adminUserTokenOne.getAccessToken(), userSaveRequest))
.andDo(MockMvcResultHandlers.print())
Expand All @@ -66,11 +62,7 @@ void givenValidUserSaveRequest_whenUserSaved_thenReturnUserSavedResponse() throw
.andExpect(AysMockResultMatchersBuilders.isSuccess()
.value(response.getIsSuccess()))
.andExpect(AysMockResultMatchersBuilders.response()
.isNotEmpty())
.andExpect(MockMvcResultMatchers.jsonPath("$.response.username")
.isNotEmpty())
.andExpect(MockMvcResultMatchers.jsonPath("$.response.password")
.isNotEmpty());
.doesNotExist());
}

@Test
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,8 @@ void givenValidUserSaveRequest_whenUserSaved_thenReturnUser() {
.thenReturn(AysRandomUtil.generateUUID());

// Then
User user = userSaveService.saveUser(mockUserSaveRequest);

Assertions.assertNotNull(user.getUsername());
Assertions.assertEquals(6, user.getUsername().length());
Assertions.assertNotNull(user.getPassword());
Assertions.assertEquals(6, user.getPassword().length());

userSaveService.saveUser(mockUserSaveRequest);

Mockito.verify(userRepository, Mockito.times(1)).findAll();
Mockito.verify(identity, Mockito.times(1)).getInstitutionId();
Mockito.verify(passwordEncoder, Mockito.times(1)).encode(Mockito.anyString());
Expand Down