@Test public void createRoleWithPermissions() throws Exception { final String roleName = "roleName"; final String contextString = "context-test"; final String descriptorName = "descriptorName"; RoleEntity roleEntity = new RoleEntity(roleName, true); roleEntity.setId(1L); ConfigContextEntity configContextEntity = new ConfigContextEntity(contextString); configContextEntity.setId(1L); RegisteredDescriptorEntity registeredDescriptorEntity = new RegisteredDescriptorEntity(descriptorName, 1L); registeredDescriptorEntity.setId(1L); PermissionKey permissionKey = new PermissionKey(contextString, descriptorName); PermissionMatrixModel permissionMatrixModel = new PermissionMatrixModel(Map.of(permissionKey, AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit())); PermissionMatrixRelation permissionMatrixRelation = new PermissionMatrixRelation(roleEntity.getId(), configContextEntity.getId(), registeredDescriptorEntity.getId(), AccessOperation.READ.getBit() + AccessOperation.WRITE.getBit()); Mockito.when(roleRepository.save(Mockito.any())).thenReturn(new RoleEntity(roleName, true)); mockUpdateRoleOperations(permissionMatrixRelation, configContextEntity, registeredDescriptorEntity); mockCreateModelFromPermission(configContextEntity, registeredDescriptorEntity); DefaultRoleAccessor authorizationUtility = new DefaultRoleAccessor(roleRepository, userRoleRepository, permissionMatrixRepository, registeredDescriptorRepository, configContextRepository); UserRoleModel userRoleModel = authorizationUtility.createRoleWithPermissions(roleName, permissionMatrixModel); Mockito.verify(permissionMatrixRepository).deleteAll(Mockito.any()); assertEquals(roleName, userRoleModel.getName()); assertTrue(userRoleModel.isCustom()); assertEquals(permissionMatrixModel, userRoleModel.getPermissions()); }
@Override public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) throws AlertDatabaseConstraintException { RoleEntity roleEntity = createRole(roleName, true); List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix); return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions)); }
DefaultRoleAccessor implements RoleAccessor { @Override public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) throws AlertDatabaseConstraintException { RoleEntity roleEntity = createRole(roleName, true); List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix); return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions)); } }
DefaultRoleAccessor implements RoleAccessor { @Override public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) throws AlertDatabaseConstraintException { RoleEntity roleEntity = createRole(roleName, true); List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix); return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions)); } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); }
DefaultRoleAccessor implements RoleAccessor { @Override public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) throws AlertDatabaseConstraintException { RoleEntity roleEntity = createRole(roleName, true); List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix); return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions)); } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); @Override Set<UserRoleModel> getRoles(); @Override Set<UserRoleModel> getRoles(Collection<Long> roleIds); @Override boolean doesRoleNameExist(String name); @Override UserRoleModel createRole(String roleName); @Override UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix); @Override void updateRoleName(Long roleId, String roleName); @Override PermissionMatrixModel updatePermissionsForRole(String roleName, PermissionMatrixModel permissionMatrix); @Override void deleteRole(Long roleId); @Override void updateUserRoles(Long userId, Collection<UserRoleModel> roles); @Override PermissionMatrixModel mergePermissionsForRoles(Collection<String> roleNames); @Override PermissionMatrixModel readPermissionsForRole(Long roleId); }
DefaultRoleAccessor implements RoleAccessor { @Override public UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix) throws AlertDatabaseConstraintException { RoleEntity roleEntity = createRole(roleName, true); List<PermissionMatrixRelation> permissions = updateRoleOperations(roleEntity, permissionMatrix); return new UserRoleModel(roleEntity.getId(), roleEntity.getRoleName(), roleEntity.getCustom(), createModelFromPermission(permissions)); } @Autowired  DefaultRoleAccessor(RoleRepository roleRepository, UserRoleRepository userRoleRepository, PermissionMatrixRepository permissionMatrixRepository,
        RegisteredDescriptorRepository registeredDescriptorRepository, ConfigContextRepository configContextRepository); @Override Set<UserRoleModel> getRoles(); @Override Set<UserRoleModel> getRoles(Collection<Long> roleIds); @Override boolean doesRoleNameExist(String name); @Override UserRoleModel createRole(String roleName); @Override UserRoleModel createRoleWithPermissions(String roleName, PermissionMatrixModel permissionMatrix); @Override void updateRoleName(Long roleId, String roleName); @Override PermissionMatrixModel updatePermissionsForRole(String roleName, PermissionMatrixModel permissionMatrix); @Override void deleteRole(Long roleId); @Override void updateUserRoles(Long userId, Collection<UserRoleModel> roles); @Override PermissionMatrixModel mergePermissionsForRoles(Collection<String> roleNames); @Override PermissionMatrixModel readPermissionsForRole(Long roleId);  }