Skip to content

Commit

Permalink
refactor after fe changes
Browse files Browse the repository at this point in the history
  • Loading branch information
john-tco committed Jul 13, 2023
1 parent 4ae4877 commit c2afe3a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -25,15 +25,13 @@ public class SpadminService {
private final RoleRepository roleRepository;
private final RoleMapper roleMapper;

public User updateRolesForUser(String userSub, Collection<List<String>> roleCollection) {
public User updateRolesForUser(String userSub, Object roles) {
User user = userRepository.findBySub(userSub).orElseThrow(()-> new RuntimeException("User not found"));
user.removeAllRoles();
for (List<String> roles : roleCollection) {
for (String id : roles) {
for (String id : (ArrayList<String>) roles) {
Role fullRole = roleRepository.findById(Integer.valueOf(id)).orElseThrow();
user.addRole(fullRole);
}
}
userRepository.save(user);
return user;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.view.RedirectView;

import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -35,9 +34,8 @@ public class SpadminController {
private String redirectUrl;

@PostMapping("/edit-role/{users_sub}")
public RedirectView updateRolesForUserId(@RequestParam MultiValueMap<String, String> roles, @PathVariable String users_sub) {
Collection<List<String>> newRoles = roles.values();
spadminService.updateRolesForUser(users_sub, newRoles);
public RedirectView updateRolesForUserId(@RequestBody Object body, @PathVariable String users_sub) {
spadminService.updateRolesForUser(users_sub, ((LinkedHashMap) body).get("newUserRoles"));
return new RedirectView(redirectUrl + "/edit-role/" + users_sub + "?success");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ public class SpadminServiceTest {
@Test
public void testUpdateRolesForUser() {
String userSub = "user123";
Collection<List<String>> newRoles = new ArrayList<>();
newRoles.add(List.of("2", "1"));
ArrayList<String> newRoles = new ArrayList<String>();
newRoles.add("1");
newRoles.add("2");

List<Role> currentUserRoles = spy(List.of(Role.builder().name(RoleEnum.FIND).id(1).build()));
User user = spy(User.builder().id(1).sub(userSub).roles(currentUserRoles).build());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import gov.cabinetofice.gapuserservice.dto.RoleDto;
import gov.cabinetofice.gapuserservice.dto.UserDto;
import gov.cabinetofice.gapuserservice.mappers.RoleMapper;
import gov.cabinetofice.gapuserservice.model.Role;
import gov.cabinetofice.gapuserservice.model.RoleEnum;
import gov.cabinetofice.gapuserservice.model.User;
import gov.cabinetofice.gapuserservice.repository.RoleRepository;
import gov.cabinetofice.gapuserservice.repository.UserRepository;
Expand All @@ -10,12 +13,12 @@
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.servlet.view.RedirectView;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Optional;

Expand All @@ -36,10 +39,13 @@ class SpadminControllerTest {
@Mock
private RoleRepository roleRepository;

@Mock
private RoleMapper roleMapper;

@Test
void updateRolesForUserId() {
MultiValueMap<String, String> roles = new LinkedMultiValueMap<String, String>();
roles.add("roles", "4");
LinkedHashMap<String, String> roles = new LinkedHashMap<String, String>();
roles.put("roles", "4");
final RedirectView methodResponse = controller.updateRolesForUserId(roles, "1");

assertThat(methodResponse.getUrl()).contains("edit-role/1?success");
Expand All @@ -57,10 +63,16 @@ void getAllRoles_returnsArrayOfRoles() {

@Test
void getUserData() {
User mockUser = User.builder().sub("1").build();
User mockUser = User.builder().sub("1").id(1)
.roles(List.of(Role.builder()
.name(RoleEnum.FIND)
.description("desc").build()))
.emailAddress("john").build();

when(userRepository.findBySub("1")).thenReturn(Optional.of(mockUser));
when(roleMapper.roleToRoleDto(Mockito.any())).thenReturn(RoleDto.builder().name("FIND").id("1").description("desc").build());
final ResponseEntity<UserDto> methodResponse = controller.getUserData("1");

assertThat(methodResponse.getBody()).isEqualTo(UserDto.builder().sub("1").roles(List.of()).build());
assertThat(methodResponse.getBody()).isEqualTo(UserDto.builder().roles(List.of(RoleDto.builder().name("FIND").id("1").description("desc").build())).emailAddress("john").sub("1").build());
}
}

0 comments on commit c2afe3a

Please sign in to comment.