Skip to content

Commit

Permalink
feat: enabled searching assignments by list of appIds (#18)
Browse files Browse the repository at this point in the history
* feat: enabled searching assignments by list of appIds

* feat: improved dao

* fix: sonar issue
  • Loading branch information
JordenReuter authored Feb 14, 2024
1 parent 786e6d3 commit c0e9c28
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Setter
public class AssignmentSearchCriteria {

private String appId;
private String[] appId;
private Integer pageNumber;
private Integer pageSize;
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package org.tkit.onecx.permission.domain.daos;

import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate;

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
import jakarta.persistence.criteria.Predicate;
import jakarta.transaction.Transactional;

import org.tkit.onecx.permission.domain.criteria.AssignmentSearchCriteria;
Expand Down Expand Up @@ -47,13 +44,11 @@ public PageResult<Assignment> findByCriteria(AssignmentSearchCriteria criteria)
var cq = cb.createQuery(Assignment.class);
var root = cq.from(Assignment.class);

List<Predicate> predicates = new ArrayList<>();

addSearchStringPredicate(predicates, cb, root.get(Assignment_.permission).get(Permission_.APP_ID),
criteria.getAppId());

if (!predicates.isEmpty()) {
cq.where(predicates.toArray(new Predicate[] {}));
if (criteria.getAppId() != null) {
List<String> filteredAppIds = Arrays.stream(criteria.getAppId()).filter(s -> !s.isBlank()).toList();
if (!filteredAppIds.isEmpty()) {
cq.where(root.get(Assignment_.permission).get(Permission_.APP_ID).in(filteredAppIds));
}
}

cq.orderBy(cb.asc(root.get(AbstractTraceableEntity_.creationDate)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ public interface AssignmentMapper {
@Mapping(target = "permissionId", ignore = true)
Assignment create(Role role, Permission permission);

@Mapping(target = "appId", source = "permission.appId")
AssignmentDTO map(Assignment data);
}
10 changes: 7 additions & 3 deletions src/main/openapi/onecx-permission-internal-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ paths:
schema:
$ref: '#/components/schemas/UpdateRoleRequest'
responses:
204:
description: Theme updated
200:
description: Role updated
content:
application/json:
schema:
Expand Down Expand Up @@ -291,7 +291,9 @@ components:
type: object
properties:
appId:
type: string
type: array
items:
type: string
pageNumber:
format: int32
description: The number of page.
Expand Down Expand Up @@ -350,6 +352,8 @@ components:
type: string
permissionId:
type: string
appId:
type: string
UpdateRoleRequest:
type: object
required:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import static org.jboss.resteasy.reactive.RestResponse.Status.*;
import static org.jboss.resteasy.reactive.RestResponse.Status.BAD_REQUEST;

import java.util.List;

import jakarta.ws.rs.core.HttpHeaders;

import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -136,7 +138,7 @@ void searchAssignmentTest() {
assertThat(data.getTotalElements()).isEqualTo(3);
assertThat(data.getStream()).isNotNull().hasSize(3);

criteria.setAppId(" ");
criteria.setAppId(List.of(" "));

data = given()
.contentType(APPLICATION_JSON)
Expand All @@ -152,11 +154,13 @@ void searchAssignmentTest() {
assertThat(data.getTotalElements()).isEqualTo(3);
assertThat(data.getStream()).isNotNull().hasSize(3);

criteria.setAppId("app1");
var criteria2 = new AssignmentSearchCriteriaDTO();

criteria2.setAppId(List.of("app1"));

data = given()
.contentType(APPLICATION_JSON)
.body(criteria)
.body(criteria2)
.post("/search")
.then().log().all()
.statusCode(OK.getStatusCode())
Expand All @@ -167,6 +171,25 @@ void searchAssignmentTest() {
assertThat(data).isNotNull();
assertThat(data.getTotalElements()).isEqualTo(2);
assertThat(data.getStream()).isNotNull().hasSize(2);

//get by multiple appIds
var multipleAppIdsCriteria = new AssignmentSearchCriteriaDTO();
multipleAppIdsCriteria.appId(List.of("app1", "app2", ""));

var multipleAppIdsResult = given()
.contentType(APPLICATION_JSON)
.body(multipleAppIdsCriteria)
.post("/search")
.then()
.statusCode(OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.as(AssignmentPageResultDTO.class);

assertThat(multipleAppIdsResult).isNotNull();
assertThat(multipleAppIdsResult.getTotalElements()).isEqualTo(3);
assertThat(multipleAppIdsResult.getStream()).isNotNull().hasSize(3);

}

@Test
Expand Down

0 comments on commit c0e9c28

Please sign in to comment.