Skip to content

Commit

Permalink
feat: creating multiple roles at once internal
Browse files Browse the repository at this point in the history
  • Loading branch information
JordenReuter committed Feb 27, 2024
1 parent c3e6431 commit 8a25ca3
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tkit.onecx.permission.rs.internal.RoleInternalApi;
import gen.org.tkit.onecx.permission.rs.internal.model.CreateRoleRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.CreateRolesRequestDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.RoleSearchCriteriaDTO;
import gen.org.tkit.onecx.permission.rs.internal.model.UpdateRoleRequestDTO;
Expand All @@ -39,12 +39,11 @@ public class RoleRestController implements RoleInternalApi {
UriInfo uriInfo;

@Override
public Response createRole(CreateRoleRequestDTO createRoleRequestDTO) {
var role = mapper.create(createRoleRequestDTO);
role = dao.create(role);
public Response createRole(CreateRolesRequestDTO createRolesRequestDTO) {
var roles = mapper.create(createRolesRequestDTO);
roles = dao.create(roles).toList();
return Response
.created(uriInfo.getAbsolutePathBuilder().path(role.getId()).build())
.entity(mapper.map(role))
.status(Response.Status.CREATED).entity(mapper.map(roles))
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public List<Item> getClasses() {
return CreateAssignmentRequestDTO.class.getSimpleName() + ":r=" + d.getRoleId() + ",p="
+ d.getPermissionId();
}),
item(10, CreateRoleRequestDTO.class,
x -> x.getClass().getSimpleName() + ":" + ((CreateRoleRequestDTO) x).getName()),
item(10, CreateRolesRequestDTO.class,
x -> x.getClass().getSimpleName() + ": size: " + ((CreateRolesRequestDTO) x).getRoles().size()),
item(10, UpdateRoleRequestDTO.class,
x -> x.getClass().getSimpleName() + ":" + ((UpdateRoleRequestDTO) x).getName()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package org.tkit.onecx.permission.rs.internal.mappers;

import java.util.ArrayList;
import java.util.List;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;
Expand All @@ -18,6 +21,24 @@ public interface RoleMapper {

RoleSearchCriteria map(RoleSearchCriteriaDTO dto);

default List<Role> create(CreateRolesRequestDTO dto) {
List<Role> roles = new ArrayList<>();
roles = mapList(dto.getRoles());
return roles;
}

List<Role> mapList(List<CreateRoleDTO> roles);

default CreateRolesResponseDTO map(List<Role> roles) {
CreateRolesResponseDTO responseDTO = new CreateRolesResponseDTO();
responseDTO.setRoles(mapListResponse(roles));
return responseDTO;
}

List<RoleDTO> mapListResponse(List<Role> roles);

CreateRoleDTO create(Role role);

@Mapping(target = "id", ignore = true)
@Mapping(target = "creationDate", ignore = true)
@Mapping(target = "creationUser", ignore = true)
Expand All @@ -27,7 +48,7 @@ public interface RoleMapper {
@Mapping(target = "modificationCount", ignore = true)
@Mapping(target = "persisted", ignore = true)
@Mapping(target = "tenantId", ignore = true)
Role create(CreateRoleRequestDTO dto);
Role map(CreateRoleDTO dto);

RoleDTO map(Role data);

Expand Down
24 changes: 17 additions & 7 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/CreateRoleRequest'
$ref: '#/components/schemas/CreateRolesRequest'
responses:
201:
description: New role created
Expand All @@ -130,7 +130,7 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/Role'
$ref: '#/components/schemas/CreateRolesResponse'
400:
description: Bad request
content:
Expand Down Expand Up @@ -358,19 +358,29 @@ components:
type: integer
name:
type: string
shortDescription:
type: string
description:
type: string
CreateRoleRequest:
CreateRolesRequest:
type: object
properties:
roles:
type: array
items:
$ref: '#/components/schemas/CreateRole'
CreateRole:
type: object
properties:
name:
type: string
shortDescription:
type: string
description:
type: string
CreateRolesResponse:
type: object
properties:
roles:
type: array
items:
$ref: '#/components/schemas/Role'
Role:
type: object
properties:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import static org.assertj.core.api.Assertions.from;
import static org.jboss.resteasy.reactive.RestResponse.Status.*;

import jakarta.ws.rs.core.HttpHeaders;
import java.util.List;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -27,29 +27,31 @@ class RoleRestControllerTest extends AbstractTest {
void createNewRoleTest() {

// create Role
var requestDTO = new CreateRoleRequestDTO();
requestDTO.setName("test01");
requestDTO.setDescription("description");
var requestDTO = new CreateRolesRequestDTO();
CreateRoleDTO role = new CreateRoleDTO();
role.setName("test01");
role.setDescription("description");
requestDTO.setRoles(List.of(role));

var uri = given()
.when()
.contentType(APPLICATION_JSON)
.body(requestDTO)
.post()
.then().statusCode(CREATED.getStatusCode())
.extract().header(HttpHeaders.LOCATION);
.extract().as(CreateRolesResponseDTO.class);

var dto = given()
.contentType(APPLICATION_JSON)
.get(uri)
.get(uri.getRoles().get(0).getId())
.then()
.statusCode(OK.getStatusCode())
.extract()
.body().as(RoleDTO.class);

assertThat(dto).isNotNull()
.returns(requestDTO.getName(), from(RoleDTO::getName))
.returns(requestDTO.getDescription(), from(RoleDTO::getDescription));
.returns(requestDTO.getRoles().get(0).getName(), from(RoleDTO::getName))
.returns(requestDTO.getRoles().get(0).getDescription(), from(RoleDTO::getDescription));

// create Role without body
var exception = given()
Expand All @@ -61,23 +63,7 @@ void createNewRoleTest() {
.extract().as(ProblemDetailResponseDTO.class);

assertThat(exception.getErrorCode()).isEqualTo(ExceptionMapper.ErrorKeys.CONSTRAINT_VIOLATIONS.name());
assertThat(exception.getDetail()).isEqualTo("createRole.createRoleRequestDTO: must not be null");

// create Role with existing name
requestDTO = new CreateRoleRequestDTO();
requestDTO.setName("n1");

exception = given().when()
.contentType(APPLICATION_JSON)
.body(requestDTO)
.post()
.then()
.statusCode(BAD_REQUEST.getStatusCode())
.extract().as(ProblemDetailResponseDTO.class);

assertThat(exception.getErrorCode()).isEqualTo("PERSIST_ENTITY_FAILED");
assertThat(exception.getDetail()).isEqualTo(
"could not execute statement [ERROR: duplicate key value violates unique constraint 'role_name' Detail: Key (tenant_id, name)=(default, n1) already exists.]");
assertThat(exception.getDetail()).isEqualTo("createRole.createRolesRequestDTO: must not be null");
}

@Test
Expand Down

0 comments on commit 8a25ca3

Please sign in to comment.