From 18c0131dfa0f9109053abe7806f6c96f85b60a52 Mon Sep 17 00:00:00 2001 From: Andrej Petras Date: Wed, 6 Mar 2024 16:49:44 +0100 Subject: [PATCH] feat: create data import update request --- .../criteria/ApplicationSearchCriteria.java | 5 +- .../criteria/AssignmentSearchCriteria.java | 4 +- .../criteria/PermissionSearchCriteria.java | 7 +- .../domain/criteria/RoleSearchCriteria.java | 4 +- .../domain/daos/ApplicationDAO.java | 16 +- .../permission/domain/daos/AssignmentDAO.java | 34 +++- .../permission/domain/daos/PermissionDAO.java | 20 +- .../onecx/permission/domain/daos/RoleDAO.java | 13 ++ .../domain/di/PermissionDataImportV1.java | 186 +++++++++++++++--- .../domain/di/PermissionImportService.java | 47 +++-- .../domain/di/mappers/DataImportV1Mapper.java | 63 +++++- .../domain/models/PermissionAction.java | 4 + .../domain/services/AssignmentService.java | 23 +++ .../controllers/AssignmentRestController.java | 12 +- .../rs/internal/mappers/RoleMapper.java | 2 +- .../controllers/OperatorRestController.java | 2 +- src/main/resources/application.properties | 3 +- .../domain/daos/ApplicationDAOTest.java | 2 + .../domain/daos/AssignmentDAOTest.java | 2 + .../domain/daos/PermissionDAOTest.java | 8 +- .../permission/domain/daos/RoleDAOTest.java | 2 + .../AbstractPermissionDataImportV1Test.java | 96 +++++++++ .../di/PermissionDataImportServiceTest.java | 65 +++--- ...PermissionDataImportV1CleanInsertTest.java | 49 +++++ .../di/PermissionDataImportV1UpdateTest.java | 95 +++++++++ src/test/resources/data/test-di.xml | 36 ++++ 26 files changed, 669 insertions(+), 131 deletions(-) create mode 100644 src/main/java/org/tkit/onecx/permission/domain/models/PermissionAction.java create mode 100644 src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java create mode 100644 src/test/java/org/tkit/onecx/permission/domain/di/AbstractPermissionDataImportV1Test.java create mode 100644 src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1CleanInsertTest.java create mode 100644 src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1UpdateTest.java create mode 100644 src/test/resources/data/test-di.xml diff --git a/src/main/java/org/tkit/onecx/permission/domain/criteria/ApplicationSearchCriteria.java b/src/main/java/org/tkit/onecx/permission/domain/criteria/ApplicationSearchCriteria.java index ee510fc..ede7ca1 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/criteria/ApplicationSearchCriteria.java +++ b/src/main/java/org/tkit/onecx/permission/domain/criteria/ApplicationSearchCriteria.java @@ -10,7 +10,6 @@ public class ApplicationSearchCriteria { private String appId; private String name; - - private Integer pageNumber; - private Integer pageSize; + private Integer pageNumber = 0; + private Integer pageSize = 100; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/criteria/AssignmentSearchCriteria.java b/src/main/java/org/tkit/onecx/permission/domain/criteria/AssignmentSearchCriteria.java index 1701b83..7eaab89 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/criteria/AssignmentSearchCriteria.java +++ b/src/main/java/org/tkit/onecx/permission/domain/criteria/AssignmentSearchCriteria.java @@ -8,6 +8,6 @@ public class AssignmentSearchCriteria { private String[] appId; - private Integer pageNumber; - private Integer pageSize; + private Integer pageNumber = 0; + private Integer pageSize = 100; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/criteria/PermissionSearchCriteria.java b/src/main/java/org/tkit/onecx/permission/domain/criteria/PermissionSearchCriteria.java index bab905f..c6e2c2c 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/criteria/PermissionSearchCriteria.java +++ b/src/main/java/org/tkit/onecx/permission/domain/criteria/PermissionSearchCriteria.java @@ -1,7 +1,5 @@ package org.tkit.onecx.permission.domain.criteria; -import java.util.List; - import lombok.Getter; import lombok.Setter; @@ -10,7 +8,6 @@ public class PermissionSearchCriteria { private String appId; - private List productNames; - private Integer pageNumber; - private Integer pageSize; + private Integer pageNumber = 0; + private Integer pageSize = 100; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/criteria/RoleSearchCriteria.java b/src/main/java/org/tkit/onecx/permission/domain/criteria/RoleSearchCriteria.java index be8a760..f4cf274 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/criteria/RoleSearchCriteria.java +++ b/src/main/java/org/tkit/onecx/permission/domain/criteria/RoleSearchCriteria.java @@ -9,6 +9,6 @@ public class RoleSearchCriteria { private String name; private String description; - private Integer pageNumber; - private Integer pageSize; + private Integer pageNumber = 0; + private Integer pageSize = 100; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/daos/ApplicationDAO.java b/src/main/java/org/tkit/onecx/permission/domain/daos/ApplicationDAO.java index cb66ecd..7b4cfed 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/daos/ApplicationDAO.java +++ b/src/main/java/org/tkit/onecx/permission/domain/daos/ApplicationDAO.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; import jakarta.enterprise.context.ApplicationScoped; import jakarta.persistence.NoResultException; @@ -30,7 +31,6 @@ public PageResult findByCriteria(ApplicationSearchCriteria criteria List predicates = new ArrayList<>(); addSearchStringPredicate(predicates, cb, root.get(Application_.appId), criteria.getAppId()); addSearchStringPredicate(predicates, cb, root.get(Application_.name), criteria.getName()); - if (!predicates.isEmpty()) { cq.where(predicates.toArray(new Predicate[] {})); } @@ -57,8 +57,22 @@ public Application loadByAppId(String productName, String appId) { } } + public List findByProductNames(Set productNames) { + try { + var cb = this.getEntityManager().getCriteriaBuilder(); + var cq = cb.createQuery(Application.class); + var root = cq.from(Application.class); + cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames)); + return this.getEntityManager().createQuery(cq).getResultList(); + } catch (Exception ex) { + throw new DAOException(ErrorKeys.ERROR_FIND_APPLICATIONS_BY_PRODUCT_NAMES, ex); + } + } + public enum ErrorKeys { + ERROR_FIND_APPLICATIONS_BY_PRODUCT_NAMES, + ERROR_FIND_APPLICATIONS_BY_CRITERIA, ERROR_LOAD_BY_APP_ID; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java b/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java index 793707b..deafd80 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java +++ b/src/main/java/org/tkit/onecx/permission/domain/daos/AssignmentDAO.java @@ -3,6 +3,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.Set; import jakarta.enterprise.context.ApplicationScoped; import jakarta.persistence.NoResultException; @@ -10,9 +11,7 @@ import jakarta.transaction.Transactional; import org.tkit.onecx.permission.domain.criteria.AssignmentSearchCriteria; -import org.tkit.onecx.permission.domain.models.Assignment; -import org.tkit.onecx.permission.domain.models.Assignment_; -import org.tkit.onecx.permission.domain.models.Permission_; +import org.tkit.onecx.permission.domain.models.*; import org.tkit.quarkus.jpa.daos.AbstractDAO; import org.tkit.quarkus.jpa.daos.Page; import org.tkit.quarkus.jpa.daos.PageResult; @@ -46,13 +45,16 @@ public PageResult findByCriteria(AssignmentSearchCriteria criteria) var cq = cb.createQuery(Assignment.class); var root = cq.from(Assignment.class); + List predicates = new ArrayList<>(); if (criteria.getAppId() != null) { List 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)); + predicates.add(root.get(Assignment_.permission).get(Permission_.APP_ID).in(filteredAppIds)); } } - + if (!predicates.isEmpty()) { + cq.where(predicates.toArray(new Predicate[] {})); + } cq.orderBy(cb.asc(root.get(AbstractTraceableEntity_.creationDate))); return createPageQuery(cq, Page.of(criteria.getPageNumber(), criteria.getPageSize())).getPageResult(); @@ -84,8 +86,30 @@ public void deleteByCriteria(String roleId, List productNames, String pe this.getEntityManager().createQuery(dq).executeUpdate(); } + public List findPermissionActionForProducts(Set productNames) { + try { + var cb = this.getEntityManager().getCriteriaBuilder(); + var cq = cb.createQuery(PermissionAction.class); + var root = cq.from(Assignment.class); + + cq.select(cb.construct(PermissionAction.class, + root.get(Assignment_.ROLE).get(Role_.NAME), + root.get(Assignment_.permission).get(Permission_.PRODUCT_NAME), + root.get(Assignment_.permission).get(Permission_.APP_ID), + root.get(Assignment_.permission).get(Permission_.RESOURCE), + root.get(Assignment_.permission).get(Permission_.ACTION))); + cq.where(root.get(Assignment_.permission).get(Permission_.PRODUCT_NAME).in(productNames)); + + return this.getEntityManager().createQuery(cq).getResultList(); + } catch (Exception ex) { + throw new DAOException(ErrorKeys.ERROR_FIND_PERMISSION_ACTION_FOR_PRODUCTS, ex); + } + } + public enum ErrorKeys { + ERROR_FIND_PERMISSION_ACTION_FOR_PRODUCTS, + FIND_ENTITY_BY_ID_FAILED, ERROR_FIND_ASSIGNMENT_BY_CRITERIA; } diff --git a/src/main/java/org/tkit/onecx/permission/domain/daos/PermissionDAO.java b/src/main/java/org/tkit/onecx/permission/domain/daos/PermissionDAO.java index e578643..a9daae0 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/daos/PermissionDAO.java +++ b/src/main/java/org/tkit/onecx/permission/domain/daos/PermissionDAO.java @@ -3,6 +3,7 @@ import static org.tkit.quarkus.jpa.utils.QueryCriteriaUtil.addSearchStringPredicate; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import jakarta.enterprise.context.ApplicationScoped; @@ -30,9 +31,6 @@ public PageResult findByCriteria(PermissionSearchCriteria criteria) List predicates = new ArrayList<>(); addSearchStringPredicate(predicates, cb, root.get(Permission_.appId), criteria.getAppId()); - if (criteria.getProductNames() != null) { - predicates.add(root.get(Permission_.PRODUCT_NAME).in(criteria.getProductNames())); - } if (!predicates.isEmpty()) { cq.where(predicates.toArray(new Predicate[] {})); } @@ -43,19 +41,21 @@ public PageResult findByCriteria(PermissionSearchCriteria criteria) } } - public List loadByAppId(String appId) { + public List findByProductAndAppId(String productName, String appId) { try { var cb = this.getEntityManager().getCriteriaBuilder(); var cq = cb.createQuery(Permission.class); var root = cq.from(Permission.class); - cq.where(cb.equal(root.get(Permission_.APP_ID), appId)); + cq.where(cb.and( + cb.equal(root.get(Permission_.PRODUCT_NAME), productName), + cb.equal(root.get(Permission_.APP_ID), appId))); return this.getEntityManager().createQuery(cq).getResultList(); } catch (Exception ex) { - throw new DAOException(ErrorKeys.ERROR_LOAD_BY_APP_ID, ex); + throw new DAOException(ErrorKeys.ERROR_FIND_BY_PRODUCT_AND_APP_ID, ex); } } - public List loadByProductNames(List productNames) { + public List findByProductNames(Collection productNames) { try { var cb = this.getEntityManager().getCriteriaBuilder(); var cq = cb.createQuery(Permission.class); @@ -63,7 +63,7 @@ public List loadByProductNames(List productNames) { cq.where(root.get(Permission_.PRODUCT_NAME).in(productNames)); return this.getEntityManager().createQuery(cq).getResultList(); } catch (Exception ex) { - throw new DAOException(ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES, ex); + throw new DAOException(ErrorKeys.ERROR_FIND_BY_PRODUCT_NAMES, ex); } } @@ -91,8 +91,8 @@ public List findPermissionForUser(String appId, List roles) public enum ErrorKeys { ERROR_FIND_PERMISSION_FOR_USER, - ERROR_LOAD_BY_APP_ID, + ERROR_FIND_BY_PRODUCT_AND_APP_ID, ERROR_FIND_PERMISSION_BY_CRITERIA, - ERROR_LOAD_BY_PRODUCT_NAMES; + ERROR_FIND_BY_PRODUCT_NAMES; } } diff --git a/src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java b/src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java index 29200b3..e302710 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java +++ b/src/main/java/org/tkit/onecx/permission/domain/daos/RoleDAO.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; +import java.util.Set; import jakarta.enterprise.context.ApplicationScoped; import jakarta.persistence.NoResultException; @@ -59,6 +60,18 @@ public PageResult findByCriteria(RoleSearchCriteria criteria) { } } + public List findByNames(Set names) { + try { + var cb = this.getEntityManager().getCriteriaBuilder(); + var cq = cb.createQuery(Role.class); + var root = cq.from(Role.class); + cq.where(root.get(Role_.name).in(names)); + return this.getEntityManager().createQuery(cq).getResultList(); + } catch (Exception ex) { + throw new DAOException(ErrorKeys.ERROR_FIND_ROLE_BY_CRITERIA, ex); + } + } + public enum ErrorKeys { FIND_ENTITY_BY_ID_FAILED, diff --git a/src/main/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1.java b/src/main/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1.java index 4415370..e51f810 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1.java +++ b/src/main/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1.java @@ -1,16 +1,21 @@ package org.tkit.onecx.permission.domain.di; -import java.util.ArrayList; -import java.util.Map; +import static java.util.stream.Collectors.toMap; + +import java.util.*; import java.util.function.Consumer; -import java.util.stream.Collectors; import jakarta.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.tkit.onecx.permission.domain.daos.ApplicationDAO; +import org.tkit.onecx.permission.domain.daos.AssignmentDAO; +import org.tkit.onecx.permission.domain.daos.PermissionDAO; +import org.tkit.onecx.permission.domain.daos.RoleDAO; import org.tkit.onecx.permission.domain.di.mappers.DataImportV1Mapper; import org.tkit.onecx.permission.domain.models.Application; +import org.tkit.onecx.permission.domain.models.Assignment; import org.tkit.onecx.permission.domain.models.Permission; import org.tkit.onecx.permission.domain.models.Role; import org.tkit.quarkus.dataimport.DataImport; @@ -28,6 +33,8 @@ public class PermissionDataImportV1 implements DataImportService { private static final Logger log = LoggerFactory.getLogger(PermissionDataImportV1.class); + public static final String METADATA_OPERATION = "operation"; + @Inject ObjectMapper objectMapper; @@ -37,15 +44,28 @@ public class PermissionDataImportV1 implements DataImportService { @Inject DataImportV1Mapper mapper; + @Inject + ApplicationDAO applicationDAO; + + @Inject + PermissionDAO permissionDAO; + + @Inject + RoleDAO roleDAO; + + @Inject + AssignmentDAO assignmentDAO; + @Override public void importData(DataImportConfig config) { log.info("Import permissions from configuration {}", config); try { - var operation = config.getMetadata().getOrDefault("operation", "NONE"); + var operation = config.getMetadata().get(METADATA_OPERATION); + var op = Operation.valueOfMetadata(operation); - Consumer action = switch (operation) { - case "CLEAN_INSERT" -> this::cleanInsert; - case "UPDATE" -> this::update; + Consumer action = switch (op) { + case CLEAN_INSERT -> this::cleanInsert; + case UPDATE -> this::update; default -> null; }; @@ -87,21 +107,21 @@ public void cleanInsert(DataImportDTOV1 data) { // create application and permissions var applications = new ArrayList(); var permissions = new ArrayList(); + Map permissionMap = new HashMap<>(); - for (Map.Entry p : data.getProducts().entrySet()) { - String productName = p.getKey(); + for (Map.Entry product : data.getProducts().entrySet()) { + String productName = product.getKey(); - for (Map.Entry a : p.getValue().getApplications().entrySet()) { + for (Map.Entry a : product.getValue().getApplications().entrySet()) { var appId = a.getKey(); - var app = a.getValue(); - applications.add(mapper.createApp(appId, app.getName(), app.getDescription(), productName)); + applications.add(mapper.createApp(productName, appId, a.getValue().getName(), a.getValue().getDescription())); - for (Map.Entry> perm : app.getPermissions().entrySet()) { + for (Map.Entry> perm : a.getValue().getPermissions().entrySet()) { var resource = perm.getKey(); for (Map.Entry action : perm.getValue().entrySet()) { - var permission = mapper.createPermission(appId, resource, action.getKey(), productName); - permission.setDescription(action.getValue()); - permissions.add(permission); + var p = mapper.createPermission(productName, appId, resource, action.getKey(), action.getValue()); + permissions.add(p); + permissionMap.put(p.getProductName() + p.getAppId() + p.getResource() + p.getAction(), p); } } @@ -109,7 +129,7 @@ public void cleanInsert(DataImportDTOV1 data) { } // create all products, start Tx - Map permissionMap = service.createAllProducts(applications, permissions); + service.createAllProducts(applications, permissions); // create tenant data data.getTenants().forEach((tenantId, dto) -> { @@ -117,7 +137,7 @@ public void cleanInsert(DataImportDTOV1 data) { // mapping var roles = mapper.createRoles(dto.getRoles()); - var rolesMap = roles.stream().collect(Collectors.toMap(Role::getName, r -> r)); + var rolesMap = roles.stream().collect(toMap(Role::getName, r -> r)); var mapping = mapper.createMapping(dto.getRoles()); var assignments = mapper.createAssignments(mapping, rolesMap, permissionMap); @@ -135,10 +155,132 @@ public ImportException(String message, Throwable ex) { } public void update(DataImportDTOV1 data) { - // update applications - var permissionMap = service.updateApplicationsAndPermissions(data.getProducts()); - //update tenant data - // data.getTenants().forEach((tenantId, dto) -> service.createTenantData(tenantId, dto, permissionMap)); + // find apps in DB + var appList = applicationDAO.findByProductNames(data.getProducts().keySet()); + var appMap = appList.stream().collect(toMap(x -> x.getProductName() + x.getAppId(), x -> x)); + + // find permission in DB + var loadedProductPermissions = new HashSet<>(data.getProducts().keySet()); + var permList = permissionDAO.findByProductNames(data.getProducts().keySet()); + var permMap = permList.stream() + .collect(toMap(x -> x.getProductName() + x.getAppId() + x.getResource() + x.getAction(), x -> x)); + + // create or update apps + var createApplications = new ArrayList(); + var updateApplications = new ArrayList(); + // create or update permission + var createPermissions = new ArrayList(); + var updatePermissions = new ArrayList(); + + for (Map.Entry p : data.getProducts().entrySet()) { + String productName = p.getKey(); + + for (Map.Entry a : p.getValue().getApplications().entrySet()) { + var appId = a.getKey(); + var app = a.getValue(); + + var appDb = appMap.get(productName + appId); + if (appDb != null) { + updateApplications.add(mapper.updateApp(app.getName(), app.getDescription(), appDb)); + } else { + createApplications.add(mapper.createApp(productName, appId, app.getName(), app.getDescription())); + } + + for (Map.Entry> perm : app.getPermissions().entrySet()) { + var resource = perm.getKey(); + for (Map.Entry action : perm.getValue().entrySet()) { + + var id = productName + appId + resource + action.getKey(); + var permDb = permMap.get(id); + if (permDb != null) { + updatePermissions.add(mapper.updatePermission(action.getValue(), permDb)); + } else { + var x = mapper.createPermission(productName, appId, resource, action.getKey(), action.getValue()); + createPermissions.add(x); + permMap.put(x.getProductName() + x.getAppId() + x.getResource() + x.getAction(), x); + } + } + } + + } + } + + // create or update all products, start Tx + service.createAndUpdateAllProducts(createApplications, createPermissions, updateApplications, updatePermissions); + + // tenant data + data.getTenants().forEach((tenantId, dto) -> { + // check roles in DB + var roleList = roleDAO.findByNames(dto.getRoles().keySet()); + var roleMap = roleList.stream().collect(toMap(Role::getName, x -> x)); + + var productNames = new HashSet(); + for (var dr : dto.getRoles().entrySet()) { + productNames.addAll(dr.getValue().getAssignments().keySet()); + } + + // check assignments in DB + var assignmentList = assignmentDAO.findPermissionActionForProducts(productNames); + var assignmentMap = assignmentList.stream().collect( + toMap(x -> x.roleName() + x.productName() + x.applicationId() + x.resource() + x.action(), x -> x)); + + // check if we have all permission + productNames.removeAll(loadedProductPermissions); + if (!productNames.isEmpty()) { + var tmp = permissionDAO.findByProductNames(productNames); + tmp.forEach(x -> permMap.put(x.getProductName() + x.getAppId() + x.getResource() + x.getAction(), x)); + } + + // create or update roles + var createRoles = new ArrayList(); + var updateRoles = new ArrayList(); + var assignments = new ArrayList(); + + for (var dr : dto.getRoles().entrySet()) { + var role = roleMap.get(dr.getKey()); + if (role != null) { + updateRoles.add(mapper.updateRole(dr.getValue().getDescription(), role)); + } else { + role = mapper.createRole(dr.getKey(), dr.getValue().getDescription()); + createRoles.add(role); + roleMap.put(role.getName(), role); + } + + // check the assignments + for (var aProduct : dr.getValue().getAssignments().entrySet()) { + for (var aApp : aProduct.getValue().entrySet()) { + for (var aResource : aApp.getValue().entrySet()) { + for (var action : aResource.getValue()) { + var permId = aProduct.getKey() + aApp.getKey() + aResource.getKey() + action; + if (!assignmentMap.containsKey(role.getName() + permId)) { + var p = permMap.get(permId); + assignments.add(mapper.createAssignment(role, p)); + } + } + } + } + } + } + + // create data, start tenant Tx + service.createAndUpdateTenantData(tenantId, createRoles, assignments, updateRoles); + }); + } + + public enum Operation { + NONE, + CLEAN_INSERT, + + UPDATE; + + public static Operation valueOfMetadata(String operation) { + for (Operation value : values()) { + if (value.name().equalsIgnoreCase(operation)) { + return value; + } + } + return NONE; + } } } diff --git a/src/main/java/org/tkit/onecx/permission/domain/di/PermissionImportService.java b/src/main/java/org/tkit/onecx/permission/domain/di/PermissionImportService.java index f13ddcf..0997af1 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/di/PermissionImportService.java +++ b/src/main/java/org/tkit/onecx/permission/domain/di/PermissionImportService.java @@ -1,15 +1,12 @@ package org.tkit.onecx.permission.domain.di; import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.transaction.Transactional; import org.tkit.onecx.permission.domain.daos.*; -import org.tkit.onecx.permission.domain.di.mappers.DataImportV1Mapper; import org.tkit.onecx.permission.domain.models.Application; import org.tkit.onecx.permission.domain.models.Assignment; import org.tkit.onecx.permission.domain.models.Permission; @@ -17,9 +14,6 @@ import org.tkit.quarkus.context.ApplicationContext; import org.tkit.quarkus.context.Context; -import gen.org.tkit.onecx.permission.domain.di.v1.model.DataImportProductValueDTOV1; -import gen.org.tkit.onecx.permission.domain.di.v1.model.DataImportTenantValueDTOV1; - @ApplicationScoped public class PermissionImportService { @@ -54,20 +48,25 @@ public void deleteAllData(String tenantId) { } @Transactional(Transactional.TxType.REQUIRES_NEW) - public Map createAllProducts(List applications, List permissions) { + public void createAllProducts(List applications, List permissions) { permissionDAO.deleteQueryAll(); permissionDAO.create(permissions); applicationDAO.deleteQueryAll(); applicationDAO.create(applications); - - return permissions.stream() - .collect(Collectors.toMap(r -> r.getProductName() + r.getAppId() + r.getResource() + r.getAction(), r -> r)); } - @Inject - DataImportV1Mapper mapper; + @Transactional(Transactional.TxType.REQUIRES_NEW) + public void createAndUpdateAllProducts(List createApplications, List createPermissions, + List updateApplications, List updatePermissions) { + + permissionDAO.create(createPermissions); + permissionDAO.update(updatePermissions); + + applicationDAO.create(createApplications); + applicationDAO.update(updateApplications); + } @Transactional(Transactional.TxType.REQUIRES_NEW) public void createTenantData(String tenantId, List roles, List assignments) { @@ -80,7 +79,6 @@ public void createTenantData(String tenantId, List roles, List ApplicationContext.start(ctx); // create tenant roles - System.out.println("### " + tenantId + " roles " + roles); roleDAO.create(roles); // create tenant assignments @@ -92,13 +90,26 @@ public void createTenantData(String tenantId, List roles, List } @Transactional(Transactional.TxType.REQUIRES_NEW) - public Map updateApplicationsAndPermissions(Map products) { - return null; - } + public void createAndUpdateTenantData(String tenantId, List createRoles, List createAssignments, + List updateRoles) { + try { + var ctx = Context.builder() + .principal("data-import") + .tenantId(tenantId) + .build(); - @Transactional(Transactional.TxType.REQUIRES_NEW) - public void updateTenantData(String tenantId, DataImportTenantValueDTOV1 dto, Map permissionMap) { + ApplicationContext.start(ctx); + + // create tenant roles + roleDAO.create(createRoles); + roleDAO.update(updateRoles); + // create tenant assignments + assignmentDAO.create(createAssignments); + + } finally { + ApplicationContext.close(); + } } } diff --git a/src/main/java/org/tkit/onecx/permission/domain/di/mappers/DataImportV1Mapper.java b/src/main/java/org/tkit/onecx/permission/domain/di/mappers/DataImportV1Mapper.java index f0acafe..afdcfdf 100644 --- a/src/main/java/org/tkit/onecx/permission/domain/di/mappers/DataImportV1Mapper.java +++ b/src/main/java/org/tkit/onecx/permission/domain/di/mappers/DataImportV1Mapper.java @@ -8,9 +8,9 @@ import gen.org.tkit.onecx.permission.domain.di.v1.model.DataImportTenantRoleValueDTOV1; @Mapper -public interface DataImportV1Mapper { +public abstract class DataImportV1Mapper { - default List createAssignments(Map> mapping, Map roles, + public List createAssignments(Map> mapping, Map roles, Map permissions) { if (permissions == null || roles == null || mapping == null) { return List.of(); @@ -21,7 +21,7 @@ default List createAssignments(Map> mapping, Map return result; } - default Map> createMapping(Map dtoRoles) { + public Map> createMapping(Map dtoRoles) { if (dtoRoles == null || dtoRoles.isEmpty()) { return Map.of(); } @@ -49,13 +49,26 @@ default Map> createMapping(Map createRoles(Map dto) { + public List createRoles(Map dto) { if (dto == null) { return List.of(); } - return dto.entrySet().stream().map(entry -> create(entry.getKey(), entry.getValue().getDescription())).toList(); + return dto.entrySet().stream().map(entry -> createRole(entry.getKey(), entry.getValue().getDescription())).toList(); + } + + public Role updateRole(String description, Role role) { + if (description != null) { + role.setDescription(description); + } + return role; + } + + public Role createRole(String name, String description) { + var r = createRole(name); + r.setDescription(description); + return r; } @Mapping(target = "id", ignore = true) @@ -67,7 +80,21 @@ default List createRoles(Map dto) @Mapping(target = "modificationCount", ignore = true) @Mapping(target = "persisted", ignore = true) @Mapping(target = "tenantId", ignore = true) - Role create(String name, String description); + @Mapping(target = "description", ignore = true) + protected abstract Role createRole(String name); + + public Permission updatePermission(String description, Permission permission) { + if (description != null) { + permission.setDescription(description); + } + return permission; + } + + public Permission createPermission(String productName, String appId, String resource, String action, String description) { + var perm = createPermission(productName, appId, resource, action); + perm.setDescription(description); + return perm; + } @Mapping(target = "id", ignore = true) @Mapping(target = "creationDate", ignore = true) @@ -78,7 +105,14 @@ default List createRoles(Map dto) @Mapping(target = "modificationCount", ignore = true) @Mapping(target = "persisted", ignore = true) @Mapping(target = "description", ignore = true) - Application createApp(String appId, String name, String description, String productName); + protected abstract Permission createPermission(String productName, String appId, String resource, String action); + + public Application createApp(String productName, String appId, String name, String description) { + var app = createApp(appId, productName); + app.setName(name); + app.setDescription(description); + return app; + } @Mapping(target = "id", ignore = true) @Mapping(target = "creationDate", ignore = true) @@ -89,5 +123,16 @@ default List createRoles(Map dto) @Mapping(target = "modificationCount", ignore = true) @Mapping(target = "persisted", ignore = true) @Mapping(target = "description", ignore = true) - Permission createPermission(String appId, String resource, String action, String productName); + @Mapping(target = "name", ignore = true) + protected abstract Application createApp(String appId, String productName); + + public Application updateApp(String name, String description, @MappingTarget Application application) { + if (name != null) { + application.setName(name); + } + if (description != null) { + application.setDescription(description); + } + return application; + } } diff --git a/src/main/java/org/tkit/onecx/permission/domain/models/PermissionAction.java b/src/main/java/org/tkit/onecx/permission/domain/models/PermissionAction.java new file mode 100644 index 0000000..3fbe083 --- /dev/null +++ b/src/main/java/org/tkit/onecx/permission/domain/models/PermissionAction.java @@ -0,0 +1,4 @@ +package org.tkit.onecx.permission.domain.models; + +public record PermissionAction(String roleName, String productName, String applicationId, String resource, String action) { +} diff --git a/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java b/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java new file mode 100644 index 0000000..33a7106 --- /dev/null +++ b/src/main/java/org/tkit/onecx/permission/domain/services/AssignmentService.java @@ -0,0 +1,23 @@ +package org.tkit.onecx.permission.domain.services; + +import java.util.List; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import jakarta.transaction.Transactional; + +import org.tkit.onecx.permission.domain.daos.AssignmentDAO; +import org.tkit.onecx.permission.domain.models.Assignment; + +@ApplicationScoped +public class AssignmentService { + + @Inject + AssignmentDAO dao; + + @Transactional + public void createProductAssignment(List assignments, String roleId, List productName) { + dao.deleteByCriteria(roleId, productName, null); + dao.create(assignments); + } +} diff --git a/src/main/java/org/tkit/onecx/permission/rs/internal/controllers/AssignmentRestController.java b/src/main/java/org/tkit/onecx/permission/rs/internal/controllers/AssignmentRestController.java index 0695939..8c064d2 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/internal/controllers/AssignmentRestController.java +++ b/src/main/java/org/tkit/onecx/permission/rs/internal/controllers/AssignmentRestController.java @@ -2,7 +2,6 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; -import jakarta.transaction.Transactional; import jakarta.validation.ConstraintViolationException; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; @@ -13,6 +12,7 @@ import org.tkit.onecx.permission.domain.daos.AssignmentDAO; import org.tkit.onecx.permission.domain.daos.PermissionDAO; import org.tkit.onecx.permission.domain.daos.RoleDAO; +import org.tkit.onecx.permission.domain.services.AssignmentService; import org.tkit.onecx.permission.rs.internal.mappers.AssignmentMapper; import org.tkit.onecx.permission.rs.internal.mappers.ExceptionMapper; import org.tkit.quarkus.jpa.exceptions.ConstraintException; @@ -43,6 +43,9 @@ public class AssignmentRestController implements AssignmentInternalApi { @Inject PermissionDAO permissionDAO; + @Inject + AssignmentService service; + @Override public Response getAssignment(String id) { var data = dao.findById(id); @@ -88,21 +91,20 @@ public Response createAssignment(CreateAssignmentRequestDTO createAssignmentRequ } @Override - @Transactional public Response createProductAssignment(CreateProductAssignmentRequestDTO createAssignmentRequestDTO) { var role = roleDAO.findById(createAssignmentRequestDTO.getRoleId()); if (role == null) { return Response.status(Response.Status.NOT_FOUND).build(); } - var permissions = permissionDAO.loadByProductNames(createAssignmentRequestDTO.getProductNames()); + var permissions = permissionDAO.findByProductNames(createAssignmentRequestDTO.getProductNames()); if (permissions.isEmpty()) { return Response.status(Response.Status.NOT_FOUND).build(); } var data = mapper.createList(role, permissions); - dao.deleteByCriteria(role.getId(), createAssignmentRequestDTO.getProductNames(), null); - dao.create(data); + service.createProductAssignment(data, role.getId(), createAssignmentRequestDTO.getProductNames()); + return Response.status(Response.Status.CREATED).build(); } diff --git a/src/main/java/org/tkit/onecx/permission/rs/internal/mappers/RoleMapper.java b/src/main/java/org/tkit/onecx/permission/rs/internal/mappers/RoleMapper.java index 4e5aeed..c2873b9 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/internal/mappers/RoleMapper.java +++ b/src/main/java/org/tkit/onecx/permission/rs/internal/mappers/RoleMapper.java @@ -14,7 +14,7 @@ public interface RoleMapper { @Mapping(target = "removeStreamItem", ignore = true) - public abstract RolePageResultDTO mapPage(PageResult page); + RolePageResultDTO mapPage(PageResult page); RoleSearchCriteria map(RoleSearchCriteriaDTO dto); diff --git a/src/main/java/org/tkit/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java b/src/main/java/org/tkit/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java index 3d7e894..8df9fe8 100644 --- a/src/main/java/org/tkit/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java +++ b/src/main/java/org/tkit/onecx/permission/rs/operator/v1/controllers/OperatorRestController.java @@ -56,7 +56,7 @@ public Response createOrUpdatePermission(String productName, String appId, Permi if (data.isEmpty()) { return Response.ok().build(); } - var permissions = dao.loadByAppId(appId); + var permissions = dao.findByProductAndAppId(productName, appId); var map = permissions.stream().collect(Collectors.toMap(x -> x.getResource() + x.getAction(), x -> x)); for (Permission item : data) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 8861ecd..682a184 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -56,7 +56,8 @@ quarkus.test.integration-test-profile=test %test.tkit.dataimport.enabled=true %test.tkit.dataimport.configurations.permission.enabled=true %test.tkit.dataimport.configurations.permission.file=./src/test/resources/import/permission-import.json -%test.tkit.dataimport.configurations.permission.metadata.operation=CLEAN_INSERT +# CLEAN_INSERT, UPDATE +%test.tkit.dataimport.configurations.permission.metadata.operation=UPDATE %test.tkit.dataimport.configurations.permission.stop-at-error=true # PIPE CONFIG diff --git a/src/test/java/org/tkit/onecx/permission/domain/daos/ApplicationDAOTest.java b/src/test/java/org/tkit/onecx/permission/domain/daos/ApplicationDAOTest.java index 68f8a9b..b22a80b 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/daos/ApplicationDAOTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/daos/ApplicationDAOTest.java @@ -15,6 +15,8 @@ class ApplicationDAOTest extends AbstractDAOTest { @Test @SuppressWarnings("java:S2699") void methodExceptionTests() { + methodExceptionTests(() -> dao.findByProductNames(null), + ApplicationDAO.ErrorKeys.ERROR_FIND_APPLICATIONS_BY_PRODUCT_NAMES); methodExceptionTests(() -> dao.loadByAppId(null, null), ApplicationDAO.ErrorKeys.ERROR_LOAD_BY_APP_ID); methodExceptionTests(() -> dao.findByCriteria(null), diff --git a/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java b/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java index 687751a..d347f33 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/daos/AssignmentDAOTest.java @@ -19,6 +19,8 @@ void methodExceptionTests() { AssignmentDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED); methodExceptionTests(() -> dao.findByCriteria(null), AssignmentDAO.ErrorKeys.ERROR_FIND_ASSIGNMENT_BY_CRITERIA); + methodExceptionTests(() -> dao.findPermissionActionForProducts(null), + AssignmentDAO.ErrorKeys.ERROR_FIND_PERMISSION_ACTION_FOR_PRODUCTS); } } diff --git a/src/test/java/org/tkit/onecx/permission/domain/daos/PermissionDAOTest.java b/src/test/java/org/tkit/onecx/permission/domain/daos/PermissionDAOTest.java index 370290d..c96fee0 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/daos/PermissionDAOTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/daos/PermissionDAOTest.java @@ -17,10 +17,10 @@ class PermissionDAOTest extends AbstractDAOTest { void methodExceptionTests() { methodExceptionTests(() -> dao.findPermissionForUser(null, null), PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_FOR_USER); - methodExceptionTests(() -> dao.loadByAppId(null), - PermissionDAO.ErrorKeys.ERROR_LOAD_BY_APP_ID); - methodExceptionTests(() -> dao.loadByProductNames(null), - PermissionDAO.ErrorKeys.ERROR_LOAD_BY_PRODUCT_NAMES); + methodExceptionTests(() -> dao.findByProductAndAppId(null, null), + PermissionDAO.ErrorKeys.ERROR_FIND_BY_PRODUCT_AND_APP_ID); + methodExceptionTests(() -> dao.findByProductNames(null), + PermissionDAO.ErrorKeys.ERROR_FIND_BY_PRODUCT_NAMES); methodExceptionTests(() -> dao.findByCriteria(null), PermissionDAO.ErrorKeys.ERROR_FIND_PERMISSION_BY_CRITERIA); } diff --git a/src/test/java/org/tkit/onecx/permission/domain/daos/RoleDAOTest.java b/src/test/java/org/tkit/onecx/permission/domain/daos/RoleDAOTest.java index fcad5be..b66e801 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/daos/RoleDAOTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/daos/RoleDAOTest.java @@ -15,6 +15,8 @@ class RoleDAOTest extends AbstractDAOTest { @Test @SuppressWarnings("java:S2699") void methodExceptionTests() { + methodExceptionTests(() -> dao.findByNames(null), + RoleDAO.ErrorKeys.ERROR_FIND_ROLE_BY_CRITERIA); methodExceptionTests(() -> dao.findById(null), RoleDAO.ErrorKeys.FIND_ENTITY_BY_ID_FAILED); methodExceptionTests(() -> dao.findByCriteria(null), diff --git a/src/test/java/org/tkit/onecx/permission/domain/di/AbstractPermissionDataImportV1Test.java b/src/test/java/org/tkit/onecx/permission/domain/di/AbstractPermissionDataImportV1Test.java new file mode 100644 index 0000000..e62e950 --- /dev/null +++ b/src/test/java/org/tkit/onecx/permission/domain/di/AbstractPermissionDataImportV1Test.java @@ -0,0 +1,96 @@ +package org.tkit.onecx.permission.domain.di; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.tkit.onecx.permission.domain.di.PermissionDataImportV1.METADATA_OPERATION; + +import java.util.Map; + +import jakarta.inject.Inject; + +import org.tkit.onecx.permission.domain.daos.ApplicationDAO; +import org.tkit.onecx.permission.domain.daos.AssignmentDAO; +import org.tkit.onecx.permission.domain.daos.PermissionDAO; +import org.tkit.onecx.permission.domain.daos.RoleDAO; +import org.tkit.onecx.permission.test.AbstractTest; +import org.tkit.quarkus.context.ApplicationContext; +import org.tkit.quarkus.context.Context; +import org.tkit.quarkus.dataimport.DataImportConfig; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import gen.org.tkit.onecx.permission.domain.di.v1.model.DataImportDTOV1; + +public class AbstractPermissionDataImportV1Test extends AbstractTest { + + @Inject + PermissionDataImportV1 service; + + @Inject + ObjectMapper objectMapper; + + @Inject + PermissionDAO permissionDAO; + + @Inject + RoleDAO roleDAO; + + @Inject + AssignmentDAO assignmentDAO; + + @Inject + ApplicationDAO applicationDAO; + + protected DataImportConfig createConfig(String key, PermissionDataImportV1.Operation operation, DataImportDTOV1 dto) { + + byte[] data; + try { + data = objectMapper.writerFor(DataImportDTOV1.class).writeValueAsBytes(dto); + } catch (Exception ex) { + throw new RuntimeException("Error serialize data import data", ex); + } + + return new DataImportConfig() { + + @Override + public Map getMetadata() { + return Map.of(METADATA_OPERATION, operation.name()); + } + + @Override + public String getKey() { + return key; + } + + @Override + public byte[] getData() { + return data; + } + }; + } + + protected void checkResult(String tenantId, int applicationCount, int permissionCount, int roleCount, int assignmentCount) { + try { + var ctx = Context.builder() + .principal("data-import") + .tenantId(tenantId) + .build(); + + ApplicationContext.start(ctx); + + var applications = applicationDAO.findAll().toList(); + assertThat(applications).hasSize(applicationCount); + + var permissions = permissionDAO.findAll().toList(); + assertThat(permissions).hasSize(permissionCount); + + var roles = roleDAO.findAll().toList(); + assertThat(roles).hasSize(roleCount); + + var assignments = assignmentDAO.findAll(); + assertThat(assignments).hasSize(assignmentCount); + + } finally { + ApplicationContext.close(); + } + } +} diff --git a/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportServiceTest.java b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportServiceTest.java index 41cf757..eb94de6 100644 --- a/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportServiceTest.java +++ b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportServiceTest.java @@ -1,6 +1,7 @@ package org.tkit.onecx.permission.domain.di; import static org.assertj.core.api.Assertions.assertThat; +import static org.tkit.onecx.permission.domain.di.PermissionDataImportV1.METADATA_OPERATION; import java.util.HashMap; import java.util.List; @@ -24,7 +25,7 @@ import io.quarkus.test.junit.QuarkusTest; @QuarkusTest -@WithDBData(value = "data/test-internal.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true) +@WithDBData(value = "data/test-di.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true) class PermissionDataImportServiceTest extends AbstractTest { @Inject @@ -70,7 +71,7 @@ void checkTest() { void importDataNotSupportedActionTest() { Map metadata = new HashMap<>(); - metadata.put("operation", "CUSTOM_NOT_SUPPORTED"); + metadata.put(METADATA_OPERATION, "CUSTOM_NOT_SUPPORTED"); DataImportConfig config = new DataImportConfig() { @Override public Map getMetadata() { @@ -81,7 +82,7 @@ public Map getMetadata() { service.importData(config); List data = permissionDAO.findAll().toList(); - assertThat(data).isNotNull().hasSize(7); + assertThat(data).isNotNull().hasSize(8); } @@ -91,14 +92,14 @@ void importEmptyDataTest() { service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } }); service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, "NOT_SUPPORTED"); } @Override @@ -110,7 +111,19 @@ public byte[] getData() { service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); + } + + @Override + public byte[] getData() { + return new byte[] {}; + } + }); + + service.importData(new DataImportConfig() { + @Override + public Map getMetadata() { + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } @Override @@ -126,7 +139,7 @@ public byte[] getData() { service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } @Override @@ -145,7 +158,7 @@ public byte[] getData() { service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } @Override @@ -166,7 +179,7 @@ public byte[] getData() { var config = new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } @Override @@ -184,7 +197,7 @@ void importEmptyProductsDataTest() { service.importData(new DataImportConfig() { @Override public Map getMetadata() { - return Map.of("operation", "CLEAN_INSERT"); + return Map.of(METADATA_OPERATION, PermissionDataImportV1.Operation.CLEAN_INSERT.name()); } @Override @@ -204,36 +217,4 @@ public byte[] getData() { }); } - // @Test - // void importProductEmptyApplicationsDataTest() { - // Assertions.assertDoesNotThrow(() -> { - // service.importData(new DataImportConfig() { - // @Override - // public Map getMetadata() { - // return Map.of("operation", "CLEAN_INSERT"); - // } - // - // @Override - // public byte[] getData() { - // try { - // var products = new HashMap(); - // products.put("p", new DataImportProductValueDTOV1().applications( - // Map.of("a", new DataImportApplicationValueDTOV1() - // .permissions(Map.of( - // - // ))) - // )); - // var data = new DataImportDTOV1(); - // data.setProducts(products); - // data.setTenants(Map.of( - // "default", new DataImportTenantValueDTOV1(), - // "100", new DataImportTenantValueDTOV1())); - // return mapper.writeValueAsBytes(data); - // } catch (Exception ex) { - // throw new RuntimeException(ex); - // } - // } - // }); - // }); - // } } diff --git a/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1CleanInsertTest.java b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1CleanInsertTest.java new file mode 100644 index 0000000..e1972d5 --- /dev/null +++ b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1CleanInsertTest.java @@ -0,0 +1,49 @@ +package org.tkit.onecx.permission.domain.di; + +import java.util.List; +import java.util.Map; + +import org.junit.jupiter.api.Test; +import org.tkit.quarkus.test.WithDBData; + +import gen.org.tkit.onecx.permission.domain.di.v1.model.*; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithDBData(value = "data/test-di.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true) +public class PermissionDataImportV1CleanInsertTest extends AbstractPermissionDataImportV1Test { + + @Test + public void cleanInsertTest() { + + var dto = new DataImportDTOV1(); + dto.products(Map.of( + "product1", new DataImportProductValueDTOV1() + .applications(Map.of( + "app1", new DataImportApplicationValueDTOV1() + .description("app1") + .name("app1") + .permissions(Map.of( + "resource1", Map.of( + "action1", "action description", + "action2", "action description"))) + + )))) + .tenants(Map.of( + "default", new DataImportTenantValueDTOV1() + .roles(Map.of("role1", new DataImportTenantRoleValueDTOV1() + .description("role description") + .assignments( + Map.of("product1", + Map.of("app1", + Map.of("resource1", List.of("action1", "action2"))))) + + )))); + + var config = createConfig("test-clean-insert-1", PermissionDataImportV1.Operation.CLEAN_INSERT, dto); + service.importData(config); + + checkResult("default", 1, 2, 1, 2); + } + +} diff --git a/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1UpdateTest.java b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1UpdateTest.java new file mode 100644 index 0000000..caa19de --- /dev/null +++ b/src/test/java/org/tkit/onecx/permission/domain/di/PermissionDataImportV1UpdateTest.java @@ -0,0 +1,95 @@ +package org.tkit.onecx.permission.domain.di; + +import java.util.List; +import java.util.Map; + +import jakarta.inject.Inject; + +import org.junit.jupiter.api.Test; +import org.tkit.quarkus.test.WithDBData; + +import gen.org.tkit.onecx.permission.domain.di.v1.model.*; +import io.quarkus.test.junit.QuarkusTest; + +@QuarkusTest +@WithDBData(value = "data/test-di.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true) +public class PermissionDataImportV1UpdateTest extends AbstractPermissionDataImportV1Test { + + @Inject + PermissionDataImportV1 service; + + @Test + public void updateNewDataTest() { + + var dto = new DataImportDTOV1(); + dto.products(Map.of( + "product1", new DataImportProductValueDTOV1() + .applications(Map.of( + "app1", new DataImportApplicationValueDTOV1() + .description("app1") + .name("app1") + .permissions(Map.of( + "resource1", Map.of( + "action1", "action description", + "action2", "action description"))) + + )))) + .tenants(Map.of( + "default", new DataImportTenantValueDTOV1() + .roles(Map.of("role1", new DataImportTenantRoleValueDTOV1() + .description("role description") + .assignments( + Map.of("product1", + Map.of("app1", + Map.of("resource1", List.of("action1", "action2"))))) + + )))); + + var config = createConfig("test-update-1", PermissionDataImportV1.Operation.UPDATE, dto); + + service.importData(config); + checkResult("default", 4, 10, 5, 5); + } + + @Test + public void updateExistingDataTest() { + + var dto = new DataImportDTOV1(); + dto.products(Map.of( + "test1", new DataImportProductValueDTOV1() + .applications(Map.of( + "app1", new DataImportApplicationValueDTOV1() + .description("app1") + .name("app1") + .permissions(Map.of( + "o2", Map.of( + "a2", "change permission description"), + "resource1", Map.of( + "action1", "action description", + "action2", "action description"))) + + )))) + .tenants(Map.of( + "default", new DataImportTenantValueDTOV1() + .roles(Map.of( + "n3", new DataImportTenantRoleValueDTOV1() + .description("desc") + .assignments( + Map.of("test1", Map.of("app1", + Map.of("o1", List.of("a3"))))), + "n1", new DataImportTenantRoleValueDTOV1() + .description("role description") + .assignments( + Map.of("test2", Map.of(), + "test1", Map.of("app1", + Map.of("resource1", List.of("action1", "action2"), + "o1", List.of("a3"))))) + + )))); + + var config = createConfig("test-update-1", PermissionDataImportV1.Operation.UPDATE, dto); + + service.importData(config); + checkResult("default", 3, 10, 4, 6); + } +} diff --git a/src/test/resources/data/test-di.xml b/src/test/resources/data/test-di.xml new file mode 100644 index 0000000..614f646 --- /dev/null +++ b/src/test/resources/data/test-di.xml @@ -0,0 +1,36 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file