@Test public void shouldDoNothingForNotExistingResources() { when(applicationContext.getResource("roles.json")).thenReturn(resource); when(resource.exists()).thenReturn(false); securityRoleLoader.loadRoles(applicationContext); verify(roleService, never()).updateRole(any(RoleDto.class)); verify(roleService, never()).createRole(any(RoleDto.class)); verify(permissionService, never()).addPermission(any(PermissionDto.class)); }
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);  }