Skip to content

Commit

Permalink
tests: fix sonar issues
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Feb 28, 2024
1 parent 32a6ae4 commit f3ead41
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.tkit.quarkus.jpa.daos.Page;
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.jpa.exceptions.DAOException;
import org.tkit.quarkus.jpa.models.AbstractTraceableEntity_;
import org.tkit.quarkus.jpa.models.TraceableEntity_;

@ApplicationScoped
Expand All @@ -31,7 +32,7 @@ public void deleteAllMenuItemsByWorkspaceAndAppId(String workspaceId, String app
var root = cq.from(MenuItem.class);

cq.where(cb.and(
cb.equal(root.get(MenuItem_.WORKSPACE).get(Workspace_.ID), workspaceId),
cb.equal(root.get(MenuItem_.WORKSPACE).get(TraceableEntity_.ID), workspaceId),
cb.equal(root.get(MenuItem_.APPLICATION_ID), appId)));

var items = getEntityManager().createQuery(cq).getResultList();
Expand Down Expand Up @@ -127,7 +128,7 @@ public MenuItem loadAllChildren(String id) {
var cq = cb.createQuery(MenuItem.class);
var root = cq.from(MenuItem.class);

cq.where(cb.equal(root.get(MenuItem_.ID), id));
cq.where(cb.equal(root.get(TraceableEntity_.ID), id));

return getEntityManager()
.createQuery(cq)
Expand Down Expand Up @@ -209,15 +210,16 @@ private void updatePosition(String menuId, String parentId, int position, int su

List<Predicate> predicates = new ArrayList<>();
predicates.add(cb.greaterThanOrEqualTo(root.get(MenuItem_.POSITION), position));
predicates.add(cb.notEqual(root.get(MenuItem_.ID), menuId));
predicates.add(cb.notEqual(root.get(TraceableEntity_.ID), menuId));
if (parentId == null) {
predicates.add(cb.isNull(root.get(MenuItem_.PARENT_ID)));
} else {
predicates.add(cb.equal(root.get(MenuItem_.PARENT_ID), parentId));
}

uq.set(MenuItem_.POSITION, cb.sum(root.get(MenuItem_.POSITION), sum))
.set(MenuItem_.MODIFICATION_COUNT, cb.sum(root.get(MenuItem_.MODIFICATION_COUNT), 1))
.set(AbstractTraceableEntity_.MODIFICATION_COUNT,
cb.sum(root.get(AbstractTraceableEntity_.MODIFICATION_COUNT), 1))
.where(cb.and(predicates.toArray(new Predicate[0])));

this.getEntityManager().createQuery(uq).executeUpdate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ default EximMenuStructureDTOV1 map(List<MenuItem> menuItems) {

default List<EximWorkspaceMenuItemDTOV1> children(Set<MenuItem> set, Map<String, Set<String>> roles) {
if (set == null) {
return null;
return List.of();
}
List<EximWorkspaceMenuItemDTOV1> list = new ArrayList<>(set.size());
for (MenuItem item : set) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ default void updateMenu(MenuItem menuItem, int position, Workspace workspace,
MenuItem.Scope map(ScopeDTO scope);

default List<TkitMenuItemStructureDTO> mapToEmptyTree() {
return new ArrayList<TkitMenuItemStructureDTO>();
return new ArrayList<>();
}

default List<TkitMenuItemStructureDTO> mapToTree(List<MenuItem> menuItems, String portalId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ void importDataFromFileTest() {
assertThat(w.getRoles()).isNotNull().isNotEmpty().hasSize(4);
var map = w.getRoles().stream().collect(Collectors.toMap(Role::getName, x -> x));

assertThat(map.keySet()).containsOnly("role1", "role2", "roleA", "roleB");
assertThat(map).containsOnlyKeys("role1", "role2", "roleA", "roleB");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@QuarkusTest
@TestHTTPEndpoint(AssignmentInternalRestController.class)
@WithDBData(value = "data/testdata-internal.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true)
public class AssignmentRestControllerTest extends AbstractTest {
class AssignmentRestControllerTest extends AbstractTest {

@Test
void createAssignment() {
Expand Down Expand Up @@ -179,8 +179,8 @@ void searchAssignmentTest() {
.as(AssignmentPageResultDTO.class);

assertThat(data).isNotNull();
assertThat(data.getTotalElements()).isEqualTo(0);
assertThat(data.getStream()).isNotNull().hasSize(0);
assertThat(data.getTotalElements()).isZero();
assertThat(data.getStream()).isNotNull().isEmpty();

var criteria2 = new AssignmentSearchCriteriaDTO();

Expand Down Expand Up @@ -215,8 +215,8 @@ void searchAssignmentTest() {
.as(AssignmentPageResultDTO.class);

assertThat(multipleAppIdsResult).isNotNull();
assertThat(multipleAppIdsResult.getTotalElements()).isEqualTo(0);
assertThat(multipleAppIdsResult.getStream()).isNotNull().hasSize(0);
assertThat(multipleAppIdsResult.getTotalElements()).isZero();
assertThat(multipleAppIdsResult.getStream()).isNotNull().isEmpty();

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class AssignmentRestControllerTestIT extends AssignmentRestControllerTest {
class AssignmentRestControllerTestIT extends AssignmentRestControllerTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static jakarta.ws.rs.core.Response.Status.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.params.provider.Arguments.arguments;

import java.util.List;
import java.util.stream.Stream;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.tkit.onecx.workspace.test.AbstractTest;
import org.tkit.quarkus.test.WithDBData;

Expand All @@ -20,77 +25,23 @@
@WithDBData(value = "data/testdata-parent-change.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true)
class MenuInternalRestControllerParentChangeTest extends AbstractTest {

@Test
void updateMenuItemLastPositionTest() {

printWorkspace();

// 4-2-3 pos 2 -> change parent to 4-1 pos 1
var request = new UpdateMenuItemParentRequestDTO()
.parentItemId("4-1")
.position(3)
.modificationCount(0);

given()
.when()
.contentType(APPLICATION_JSON)
.body(request)
.pathParam("menuItemId", "4-2-2")
.put("{menuItemId}/parentItemId")
.then().statusCode(OK.getStatusCode());

var dto = printWorkspace();

// 4-1 -> 4-1-2 pos 2
var parent = dto.getMenuItems().stream().filter(x -> x.getId().equals("4-1")).findFirst();
assertThat(parent).isPresent();
assertThat(parent.get().getPosition()).isEqualTo(0);

var item = parent.get().getChildren().stream().filter(x -> x.getId().equals("4-2-2")).findFirst();
assertThat(item).isPresent();
assertThat(item.get().getPosition()).isEqualTo(3);
}

@Test
void updateMenuItemMaxPositionTest() {

printWorkspace();

// 4-2-3 pos 2 -> change parent to 4-1 pos 1
var request = new UpdateMenuItemParentRequestDTO()
.parentItemId("4-1")
.position(30)
.modificationCount(0);

given()
.when()
.contentType(APPLICATION_JSON)
.body(request)
.pathParam("menuItemId", "4-2-2")
.put("{menuItemId}/parentItemId")
.then().statusCode(OK.getStatusCode());

var dto = printWorkspace();

// 4-1 -> 4-1-2 pos 2
var parent = dto.getMenuItems().stream().filter(x -> x.getId().equals("4-1")).findFirst();
assertThat(parent).isPresent();
assertThat(parent.get().getPosition()).isEqualTo(0);

var item = parent.get().getChildren().stream().filter(x -> x.getId().equals("4-2-2")).findFirst();
assertThat(item).isPresent();
assertThat(item.get().getPosition()).isEqualTo(3);
private static Stream<Arguments> menuPosition() {
return Stream.of(
arguments(3, 3),
arguments(30, 3),
arguments(1, 1));
}

@Test
void updateMenuItemParentTest() {
@ParameterizedTest
@MethodSource("menuPosition")
void updateMenuItemLastPositionTest(int position, int result) {

printWorkspace();

// 4-2-3 pos 2 -> change parent to 4-1 pos 1
var request = new UpdateMenuItemParentRequestDTO()
.parentItemId("4-1")
.position(1)
.position(position)
.modificationCount(0);

given()
Expand All @@ -106,11 +57,11 @@ void updateMenuItemParentTest() {
// 4-1 -> 4-1-2 pos 2
var parent = dto.getMenuItems().stream().filter(x -> x.getId().equals("4-1")).findFirst();
assertThat(parent).isPresent();
assertThat(parent.get().getPosition()).isEqualTo(0);
assertThat(parent.get().getPosition()).isZero();

var item = parent.get().getChildren().stream().filter(x -> x.getId().equals("4-2-2")).findFirst();
assertThat(item).isPresent();
assertThat(item.get().getPosition()).isEqualTo(1);
assertThat(item.get().getPosition()).isEqualTo(result);
}

@Test
Expand Down Expand Up @@ -173,7 +124,7 @@ void updateMenuItemParentToNullTest() {
// null<root> -> 4-1-2 pos 2
var item = dto.getMenuItems().stream().filter(x -> x.getId().equals("4-2-2")).findFirst();
assertThat(item).isPresent();
assertThat(item.get().getPosition()).isEqualTo(0);
assertThat(item.get().getPosition()).isZero();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
@QuarkusTest
@TestHTTPEndpoint(RoleInternalRestController.class)
@WithDBData(value = "data/testdata-internal.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true)
public class RoleRestControllerTest extends AbstractTest {
class RoleRestControllerTest extends AbstractTest {

@Test
void createNewRoleTest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
import io.quarkus.test.junit.QuarkusIntegrationTest;

@QuarkusIntegrationTest
public class RoleRestControllerTestIT extends RoleRestControllerTest {
class RoleRestControllerTestIT extends RoleRestControllerTest {
}

0 comments on commit f3ead41

Please sign in to comment.