Skip to content

Commit

Permalink
tests: update tests for DAOs
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejpetras committed Feb 21, 2024
1 parent cb952f3 commit 17588e6
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,24 +138,7 @@ public MenuItem loadAllChildren(String id) {
} catch (NoResultException nex) {
return null;
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE, ex);
}
}

public List<MenuItem> findAllMenuItemsByWorkspace(String id) {

try {
var cb = getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(MenuItem.class);
var root = cq.from(MenuItem.class);

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

return getEntityManager()
.createQuery(cq)
.getResultList();
} catch (Exception ex) {
throw new DAOException(ErrorKeys.ERROR_FIND_ALL_MENU_ITEMS_BY_WORKSPACE, ex);
throw new DAOException(ErrorKeys.ERROR_LOAD_ALL_CHILDREN, ex);
}
}

Expand All @@ -182,7 +165,7 @@ public enum ErrorKeys {
ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE,
ERROR_LOAD_ALL_MENU_ITEMS_BY_WORKSPACE,

ERROR_FIND_ALL_MENU_ITEMS_BY_WORKSPACE,
ERROR_LOAD_ALL_CHILDREN,

ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_NAME_AND_APP_ID,
}
Expand Down
15 changes: 0 additions & 15 deletions src/main/java/org/tkit/onecx/workspace/domain/daos/ProductDAO.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.tkit.onecx.workspace.domain.daos;

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

import jakarta.enterprise.context.ApplicationScoped;
import jakarta.persistence.NoResultException;
Expand Down Expand Up @@ -74,18 +73,6 @@ public Product loadById(Object id) throws DAOException {
}
}

public Stream<Product> findByName(String name) throws DAOException {
try {
var cb = this.getEntityManager().getCriteriaBuilder();
var cq = cb.createQuery(Product.class);
var root = cq.from(Product.class);
cq.where(cb.equal(root.get(Product_.PRODUCT_NAME), name));
return this.getEntityManager().createQuery(cq).getResultStream();
} catch (Exception e) {
throw this.handleConstraint(e, ProductDAO.ErrorKeys.FIND_ENTITY_BY_NAME_FAILED);
}
}

public enum ErrorKeys {

LOAD_ENTITY_BY_ID_FAILED,
Expand All @@ -94,7 +81,5 @@ public enum ErrorKeys {

ERROR_FIND_PRODUCTS_BY_WORKSPACE_ID,

FIND_ENTITY_BY_NAME_FAILED,

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ public Response importMenu(String name, MenuSnapshotDTOV1 menuSnapshotDTOV1) {
@Override
@Transactional
public Response importWorkspaces(WorkspaceSnapshotDTOV1 request) {
var keys = request.getWorkspaces().keySet();
var workspaceNames = request.getWorkspaces().keySet();

var criteria = new WorkspaceSearchCriteria();
criteria.setNames(keys);
criteria.setNames(workspaceNames);
var workspaces = dao.findBySearchCriteria(criteria);

var map = workspaces.getStream().collect(Collectors.toMap(Workspace::getName, workspace -> workspace));
Expand Down
2 changes: 0 additions & 2 deletions src/main/openapi/onecx-workspace-v1-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ servers:
- url: "http://onecx-workspace:8080"
tags:
- name: workspaceExternal
- name: productExternal

paths:
/v1/workspaces/search:
post:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.tkit.onecx.workspace.domain.daos;

import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito;
import org.tkit.quarkus.jpa.exceptions.DAOException;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class AssignmentDAOTest {
@Inject
AssignmentDAO dao;

@InjectMock
EntityManager em;

@BeforeEach
void beforeAll() {
Mockito.when(em.getCriteriaBuilder()).thenThrow(new RuntimeException("Test technical error exception"));
}

@Test
void methodExceptionTests() {
methodExceptionTests(() -> dao.findByCriteria(null),
AssignmentDAO.ErrorKeys.ERROR_FIND_ASSIGNMENT_BY_CRITERIA);
methodExceptionTests(() -> dao.findById(null),
AssignmentDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED);

}

void methodExceptionTests(Executable fn, Enum<?> key) {
var exc = Assertions.assertThrows(DAOException.class, fn);
Assertions.assertEquals(key, exc.key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ void beforeAll() {

@Test
void methodExceptionTests() {
methodExceptionTests(() -> dao.loadAllChildren(null),
MenuItemDAO.ErrorKeys.ERROR_LOAD_ALL_CHILDREN);
methodExceptionTests(() -> dao.deleteAllMenuItemsByWorkspaceId(null),
MenuItemDAO.ErrorKeys.ERROR_DELETE_ALL_MENU_ITEMS_BY_WORKSPACE_ID);
methodExceptionTests(() -> dao.deleteAllMenuItemsByWorkspace(null),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ void methodExceptionTests() {
ProductDAO.ErrorKeys.LOAD_ENTITY_BY_ID_FAILED);
methodExceptionTests(() -> dao.deleteProduct(null),
ProductDAO.ErrorKeys.ERROR_DELETE_PRODUCT_ID);
methodExceptionTests(() -> dao.findByName(null),
ProductDAO.ErrorKeys.FIND_ENTITY_BY_NAME_FAILED);
}

void methodExceptionTests(Executable fn, Enum<?> key) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.tkit.onecx.workspace.domain.daos;

import jakarta.inject.Inject;
import jakarta.persistence.EntityManager;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito;
import org.tkit.quarkus.jpa.exceptions.DAOException;

import io.quarkus.test.InjectMock;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
class RoleDAOTest {
@Inject
RoleDAO dao;

@InjectMock
EntityManager em;

@BeforeEach
void beforeAll() {
Mockito.when(em.getCriteriaBuilder()).thenThrow(new RuntimeException("Test technical error exception"));
}

@Test
void methodExceptionTests() {
methodExceptionTests(() -> dao.findByCriteria(null),
RoleDAO.ErrorKeys.ERROR_FIND_ROLE_BY_CRITERIA);
methodExceptionTests(() -> dao.findById(null),
RoleDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED);

}

void methodExceptionTests(Executable fn, Enum<?> key) {
var exc = Assertions.assertThrows(DAOException.class, fn);
Assertions.assertEquals(key, exc.key);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ void beforeAll() {

@Test
void methodExceptionTests() {
methodExceptionTests(() -> dao.loadById(null),
WorkspaceDAO.ErrorKeys.ERROR_LOAD_WORKSPACE);
methodExceptionTests(() -> dao.findBySearchCriteria(null),
WorkspaceDAO.ErrorKeys.ERROR_FIND_BY_CRITERIA);
methodExceptionTests(() -> dao.findById(null),
WorkspaceDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED);
methodExceptionTests(() -> dao.findByName(null),
WorkspaceDAO.ErrorKeys.ERROR_FIND_WORKSPACE_BY_NAME);
}

void methodExceptionTests(Executable fn, Enum<?> key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import static jakarta.ws.rs.core.Response.Status.*;
import static org.assertj.core.api.Assertions.assertThat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;

import org.junit.jupiter.api.Test;
import org.tkit.onecx.workspace.test.AbstractTest;
Expand Down Expand Up @@ -40,6 +37,21 @@ void exportWorkspaceTest() {
assertThat(dto.getWorkspaces().get("test01").getName()).isEqualTo("test01");
}

@Test
void exportAllWorkspaceTest() {
var dto = given()
.when()
.contentType(APPLICATION_JSON)
.body(new ExportWorkspacesRequestDTOV1().names(new HashSet<>()))
.post("/export")
.then()
.statusCode(OK.getStatusCode())
.extract().as(WorkspaceSnapshotDTOV1.class);

assertThat(dto).isNotNull();
assertThat(dto.getWorkspaces()).hasSize(3);
}

@Test
void exportWorkspaceNotFoundTest() {
ExportWorkspacesRequestDTOV1 request = new ExportWorkspacesRequestDTOV1();
Expand Down

0 comments on commit 17588e6

Please sign in to comment.