@Test public void shouldCreateNewRoles() throws IOException { when(roleService.getRole("Test Role")).thenReturn(null); when(applicationContext.getResource("roles.json")).thenReturn(resource); when(resource.exists()).thenReturn(true); try (InputStream in = getClass().getClassLoader().getResourceAsStream("roles.json")) { when(resource.getInputStream()).thenReturn(new ByteArrayInputStream(IOUtils.toByteArray(in))); } securityRoleLoader.loadRoles(applicationContext); verify(roleService).getRole("Test Role"); ArgumentCaptor<RoleDto> captor = ArgumentCaptor.forClass(RoleDto.class); verify(roleService).createRole(captor.capture()); assertEquals("Test Role", captor.getValue().getRoleName()); assertEquals(asList("perm1", "perm2"), captor.getValue().getPermissionNames()); ArgumentCaptor<PermissionDto> permissionCaptor = ArgumentCaptor.forClass(PermissionDto.class); verify(permissionService, times(2)).addPermission(permissionCaptor.capture()); verifyPermission("perm1", null, permissionCaptor.getAllValues().get(0)); verifyPermission("perm2", null, permissionCaptor.getAllValues().get(1)); }
public void loadRoles(ApplicationContext applicationContext) { LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName()); Resource rolesResource = applicationContext.getResource("roles.json"); if (rolesResource.exists()) { LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName()); try (InputStream in = rolesResource.getInputStream()) { List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() { }.getType()); for (RoleDto role : roles) { RoleDto existingRole = roleService.getRole(role.getRoleName()); if (existingRole == null) { roleService.createRole(role); } else if (roleNeedUpdate(existingRole, role)) { existingRole.setPermissionNames(role.getPermissionNames()); roleService.updateRole(existingRole); } savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext)); } } catch (IOException e) { LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e); } } LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName()); }
SecurityRoleLoader { public void loadRoles(ApplicationContext applicationContext) { LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName()); Resource rolesResource = applicationContext.getResource("roles.json"); if (rolesResource.exists()) { LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName()); try (InputStream in = rolesResource.getInputStream()) { List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() { }.getType()); for (RoleDto role : roles) { RoleDto existingRole = roleService.getRole(role.getRoleName()); if (existingRole == null) { roleService.createRole(role); } else if (roleNeedUpdate(existingRole, role)) { existingRole.setPermissionNames(role.getPermissionNames()); roleService.updateRole(existingRole); } savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext)); } } catch (IOException e) { LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e); } } LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName()); } }
SecurityRoleLoader { public void loadRoles(ApplicationContext applicationContext) { LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName()); Resource rolesResource = applicationContext.getResource("roles.json"); if (rolesResource.exists()) { LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName()); try (InputStream in = rolesResource.getInputStream()) { List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() { }.getType()); for (RoleDto role : roles) { RoleDto existingRole = roleService.getRole(role.getRoleName()); if (existingRole == null) { roleService.createRole(role); } else if (roleNeedUpdate(existingRole, role)) { existingRole.setPermissionNames(role.getPermissionNames()); roleService.updateRole(existingRole); } savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext)); } } catch (IOException e) { LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e); } } LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName()); } SecurityRoleLoader(MotechRoleService roleService, MotechPermissionService permissionService); }
SecurityRoleLoader { public void loadRoles(ApplicationContext applicationContext) { LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName()); Resource rolesResource = applicationContext.getResource("roles.json"); if (rolesResource.exists()) { LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName()); try (InputStream in = rolesResource.getInputStream()) { List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() { }.getType()); for (RoleDto role : roles) { RoleDto existingRole = roleService.getRole(role.getRoleName()); if (existingRole == null) { roleService.createRole(role); } else if (roleNeedUpdate(existingRole, role)) { existingRole.setPermissionNames(role.getPermissionNames()); roleService.updateRole(existingRole); } savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext)); } } catch (IOException e) { LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e); } } LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName()); } SecurityRoleLoader(MotechRoleService roleService, MotechPermissionService permissionService); void loadRoles(ApplicationContext applicationContext); }
SecurityRoleLoader { public void loadRoles(ApplicationContext applicationContext) { LOGGER.info("Loading roles from: {}", applicationContext.getDisplayName()); Resource rolesResource = applicationContext.getResource("roles.json"); if (rolesResource.exists()) { LOGGER.debug("File roles.json exists in {}", applicationContext.getDisplayName()); try (InputStream in = rolesResource.getInputStream()) { List<RoleDto> roles = (List<RoleDto>) motechJsonReader.readFromStream(in, new TypeToken<List<RoleDto>>() { }.getType()); for (RoleDto role : roles) { RoleDto existingRole = roleService.getRole(role.getRoleName()); if (existingRole == null) { roleService.createRole(role); } else if (roleNeedUpdate(existingRole, role)) { existingRole.setPermissionNames(role.getPermissionNames()); roleService.updateRole(existingRole); } savePermissions(role.getPermissionNames(), getSymbolicName(applicationContext)); } } catch (IOException e) { LOGGER.error("Unable to read roles in " + applicationContext.getDisplayName(), e); } } LOGGER.info("Loaded roles from: {}", applicationContext.getDisplayName()); } SecurityRoleLoader(MotechRoleService roleService, MotechPermissionService permissionService); void loadRoles(ApplicationContext applicationContext);  }