Skip to content

Commit

Permalink
Merge pull request #1345 from hmcts/CCD-267-Delete-Role-functionality…
Browse files Browse the repository at this point in the history
…-not-working

CCD-267: Implement back-end Delete Role functionality
  • Loading branch information
muhammad-umerji01 authored Mar 19, 2024
2 parents 8b8f3b2 + 944e093 commit df77194
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ public interface AccessProfileService {
List<UserRole> getRoles(List<String> roles);

List<UserRole> getRoles();

void deleteRole(String role);
}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,16 @@ public List<UserRole> getRoles() {
.map(UserRoleModelMapper::toModel)
.collect(toList());
}

@Transactional
@Override
public void deleteRole(final String role) {
final Optional<AccessProfileEntity> searchResult = repository.findTopByReference(role);
if (searchResult.isPresent()) {
final AccessProfileEntity entity = searchResult.get();
repository.delete(entity);
} else {
throw new NotFoundException("Role '" + role + "' is not found");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,37 @@ void shouldThrowExceptionwhenCreateRole() {
}
}

@Nested
@DisplayName("Delete Role Tests")
class DeleteTests {
@Test
@DisplayName("should delete role")
void shouldDeleteRole() {
final String role = "delete";
givenUserRole(role, PUBLIC);
givenEntityWithRole(role);

doReturn(Optional.of(mockAccessProfileEntity)).when(repository).findTopByReference(role);

service.deleteRole(role);

verify(repository).delete(any(AccessProfileEntity.class));
}

@Test
@DisplayName("should throw exception when role not found")
void shouldThrowException_whenRoleNotFound() {
final String role = "delete";
givenUserRole(role, PUBLIC);
givenEntityWithRole(role);

doReturn(Optional.empty()).when(repository).findTopByReference(role);

Throwable thrown = assertThrows(NotFoundException.class, () -> service.deleteRole(role));
assertEquals("Role 'delete' is not found", thrown.getMessage());
}
}

@Nested
@DisplayName("GetRoles Tests")
class GetRolesTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
Expand All @@ -27,6 +28,7 @@
import java.util.List;

import static org.springframework.http.HttpStatus.CREATED;
import static org.springframework.http.HttpStatus.NO_CONTENT;
import static org.springframework.http.HttpStatus.RESET_CONTENT;
import static uk.gov.hmcts.ccd.definition.store.domain.service.response.SaveOperationEnum.CREATE;

Expand Down Expand Up @@ -78,6 +80,18 @@ public ResponseEntity<UserRole> userRoleCreate(
return responseEntityBuilder.body(serviceResponse.getResponseBody());
}

@DeleteMapping(URI_USER_ROLE)
@ResponseStatus(NO_CONTENT)
@ApiOperation(
value = "Delete a user role",
notes = "a user role is deleted if it exists"
)
@ApiResponse(code = 204, message = "User role is deleted")
public void userRoleDelete(
@ApiParam(value = "user role", required = true) @RequestParam("role") @NotNull String role) {
accessProfileService.deleteRole(role);
}

@GetMapping(value = URI_USER_ROLE, produces = {"application/json"})
@ResponseStatus(HttpStatus.OK)
@ApiOperation(value = "Get a user profile")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
import static org.hamcrest.core.Is.is;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.mockito.ArgumentMatchers.isA;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
Expand Down Expand Up @@ -310,6 +312,34 @@ void shouldHaveStatusResetContent_whenPutSuccessfully() throws Exception {
}
}

@Nested
@DisplayName("Delete Role Tests")
class DeleteRoleTests {

@Test
@DisplayName("Should delete role")
void shouldDeleteRole() throws Exception {
uriVariables.put("role", Base64.getEncoder().encode(ROLE_DEFINED.getBytes()));

mockMvc.perform(
delete(URL_TEMPLATE.expand(uriVariables)))
.andExpect(status().isNoContent());
}

@Test
@DisplayName("Should throw exception when role not found")
void shouldThrowException_whenRoleNotFound() throws Exception {
uriVariables.put("role", ROLE_DEFINED);

doThrow(new NotFoundException("Role is not found"))
.when(accessProfileService).deleteRole(ROLE_DEFINED);

mockMvc.perform(
delete(URL_TEMPLATE.expand(uriVariables)))
.andExpect(status().isNotFound());
}
}

private UserRole buildUserRole(final String role) {
return buildUserRole(role, null);
}
Expand Down

0 comments on commit df77194

Please sign in to comment.