@Test public void testGetResourcesForPermission() throws Exception { RoleAuthorizationEntity roleAuthorizationEntity = createNiceMock(RoleAuthorizationEntity.class); expect(roleAuthorizationEntity.getAuthorizationId()).andReturn("TEST.DO_SOMETHING").once(); expect(roleAuthorizationEntity.getAuthorizationName()).andReturn("Do Something").once(); List<RoleAuthorizationEntity> authorizationEntities = new ArrayList<RoleAuthorizationEntity>(); authorizationEntities.add(roleAuthorizationEntity); PermissionEntity permissionEntry = createStrictMock(PermissionEntity.class); expect(permissionEntry.getAuthorizations()).andReturn(authorizationEntities).once(); PermissionDAO permissionDAO = injector.getInstance(PermissionDAO.class); expect(permissionDAO.findById(1)).andReturn(permissionEntry).once(); replayAll(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); RoleAuthorizationResourceProvider provider = new RoleAuthorizationResourceProvider(managementController); Predicate predicate = new PredicateBuilder() .property(RoleAuthorizationResourceProvider.PERMISSION_ID_PROPERTY_ID).equals("1") .toPredicate(); Set<Resource> resources = provider.getResources(PropertyHelper.getReadRequest(), predicate); Assert.assertEquals(1, resources.size()); Resource resource = resources.iterator().next(); Assert.assertEquals("TEST.DO_SOMETHING", resource.getPropertyValue(RoleAuthorizationResourceProvider.AUTHORIZATION_ID_PROPERTY_ID)); Assert.assertEquals("Do Something", resource.getPropertyValue(RoleAuthorizationResourceProvider.AUTHORIZATION_NAME_PROPERTY_ID)); Assert.assertEquals(1, resource.getPropertyValue(RoleAuthorizationResourceProvider.PERMISSION_ID_PROPERTY_ID)); verifyAll(); }
@Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> propertyMaps; if (predicate == null) { propertyMaps = Collections.singleton(Collections.<String, Object>emptyMap()); } else { propertyMaps = getPropertyMaps(predicate); } if (propertyMaps != null) { for (Map<String, Object> propertyMap : propertyMaps) { Object object = propertyMap.get(PERMISSION_ID_PROPERTY_ID); Collection<RoleAuthorizationEntity> authorizationEntities; Integer permissionId; if (object instanceof String) { try { permissionId = Integer.valueOf((String) object); } catch (NumberFormatException e) { LOG.warn(PERMISSION_ID_PROPERTY_ID + " is not a valid integer value", e); throw new NoSuchResourceException("The requested resource doesn't exist: Authorization not found, " + predicate, e); } } else if (object instanceof Number) { permissionId = ((Number) object).intValue(); } else { permissionId = null; } if (permissionId == null) { authorizationEntities = roleAuthorizationDAO.findAll(); } else { PermissionEntity permissionEntity = permissionDAO.findById(permissionId); if (permissionEntity == null) { authorizationEntities = null; } else { authorizationEntities = permissionEntity.getAuthorizations(); } } if (authorizationEntities != null) { String authorizationId = (String) propertyMap.get(AUTHORIZATION_ID_PROPERTY_ID); if (!StringUtils.isEmpty(authorizationId)) { Iterator<RoleAuthorizationEntity> iterator = authorizationEntities.iterator(); while (iterator.hasNext()) { if (!authorizationId.equals(iterator.next().getAuthorizationId())) { iterator.remove(); } } } for (RoleAuthorizationEntity entity : authorizationEntities) { resources.add(toResource(permissionId, entity, requestedIds)); } } } } return resources; }
RoleAuthorizationResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> propertyMaps; if (predicate == null) { propertyMaps = Collections.singleton(Collections.<String, Object>emptyMap()); } else { propertyMaps = getPropertyMaps(predicate); } if (propertyMaps != null) { for (Map<String, Object> propertyMap : propertyMaps) { Object object = propertyMap.get(PERMISSION_ID_PROPERTY_ID); Collection<RoleAuthorizationEntity> authorizationEntities; Integer permissionId; if (object instanceof String) { try { permissionId = Integer.valueOf((String) object); } catch (NumberFormatException e) { LOG.warn(PERMISSION_ID_PROPERTY_ID + " is not a valid integer value", e); throw new NoSuchResourceException("The requested resource doesn't exist: Authorization not found, " + predicate, e); } } else if (object instanceof Number) { permissionId = ((Number) object).intValue(); } else { permissionId = null; } if (permissionId == null) { authorizationEntities = roleAuthorizationDAO.findAll(); } else { PermissionEntity permissionEntity = permissionDAO.findById(permissionId); if (permissionEntity == null) { authorizationEntities = null; } else { authorizationEntities = permissionEntity.getAuthorizations(); } } if (authorizationEntities != null) { String authorizationId = (String) propertyMap.get(AUTHORIZATION_ID_PROPERTY_ID); if (!StringUtils.isEmpty(authorizationId)) { Iterator<RoleAuthorizationEntity> iterator = authorizationEntities.iterator(); while (iterator.hasNext()) { if (!authorizationId.equals(iterator.next().getAuthorizationId())) { iterator.remove(); } } } for (RoleAuthorizationEntity entity : authorizationEntities) { resources.add(toResource(permissionId, entity, requestedIds)); } } } } return resources; } }
RoleAuthorizationResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> propertyMaps; if (predicate == null) { propertyMaps = Collections.singleton(Collections.<String, Object>emptyMap()); } else { propertyMaps = getPropertyMaps(predicate); } if (propertyMaps != null) { for (Map<String, Object> propertyMap : propertyMaps) { Object object = propertyMap.get(PERMISSION_ID_PROPERTY_ID); Collection<RoleAuthorizationEntity> authorizationEntities; Integer permissionId; if (object instanceof String) { try { permissionId = Integer.valueOf((String) object); } catch (NumberFormatException e) { LOG.warn(PERMISSION_ID_PROPERTY_ID + " is not a valid integer value", e); throw new NoSuchResourceException("The requested resource doesn't exist: Authorization not found, " + predicate, e); } } else if (object instanceof Number) { permissionId = ((Number) object).intValue(); } else { permissionId = null; } if (permissionId == null) { authorizationEntities = roleAuthorizationDAO.findAll(); } else { PermissionEntity permissionEntity = permissionDAO.findById(permissionId); if (permissionEntity == null) { authorizationEntities = null; } else { authorizationEntities = permissionEntity.getAuthorizations(); } } if (authorizationEntities != null) { String authorizationId = (String) propertyMap.get(AUTHORIZATION_ID_PROPERTY_ID); if (!StringUtils.isEmpty(authorizationId)) { Iterator<RoleAuthorizationEntity> iterator = authorizationEntities.iterator(); while (iterator.hasNext()) { if (!authorizationId.equals(iterator.next().getAuthorizationId())) { iterator.remove(); } } } for (RoleAuthorizationEntity entity : authorizationEntities) { resources.add(toResource(permissionId, entity, requestedIds)); } } } } return resources; } RoleAuthorizationResourceProvider(AmbariManagementController managementController); }
RoleAuthorizationResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> propertyMaps; if (predicate == null) { propertyMaps = Collections.singleton(Collections.<String, Object>emptyMap()); } else { propertyMaps = getPropertyMaps(predicate); } if (propertyMaps != null) { for (Map<String, Object> propertyMap : propertyMaps) { Object object = propertyMap.get(PERMISSION_ID_PROPERTY_ID); Collection<RoleAuthorizationEntity> authorizationEntities; Integer permissionId; if (object instanceof String) { try { permissionId = Integer.valueOf((String) object); } catch (NumberFormatException e) { LOG.warn(PERMISSION_ID_PROPERTY_ID + " is not a valid integer value", e); throw new NoSuchResourceException("The requested resource doesn't exist: Authorization not found, " + predicate, e); } } else if (object instanceof Number) { permissionId = ((Number) object).intValue(); } else { permissionId = null; } if (permissionId == null) { authorizationEntities = roleAuthorizationDAO.findAll(); } else { PermissionEntity permissionEntity = permissionDAO.findById(permissionId); if (permissionEntity == null) { authorizationEntities = null; } else { authorizationEntities = permissionEntity.getAuthorizations(); } } if (authorizationEntities != null) { String authorizationId = (String) propertyMap.get(AUTHORIZATION_ID_PROPERTY_ID); if (!StringUtils.isEmpty(authorizationId)) { Iterator<RoleAuthorizationEntity> iterator = authorizationEntities.iterator(); while (iterator.hasNext()) { if (!authorizationId.equals(iterator.next().getAuthorizationId())) { iterator.remove(); } } } for (RoleAuthorizationEntity entity : authorizationEntities) { resources.add(toResource(permissionId, entity, requestedIds)); } } } } return resources; } RoleAuthorizationResourceProvider(AmbariManagementController managementController); @Override Set<Resource> getResources(Request request, Predicate predicate); }
RoleAuthorizationResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> propertyMaps; if (predicate == null) { propertyMaps = Collections.singleton(Collections.<String, Object>emptyMap()); } else { propertyMaps = getPropertyMaps(predicate); } if (propertyMaps != null) { for (Map<String, Object> propertyMap : propertyMaps) { Object object = propertyMap.get(PERMISSION_ID_PROPERTY_ID); Collection<RoleAuthorizationEntity> authorizationEntities; Integer permissionId; if (object instanceof String) { try { permissionId = Integer.valueOf((String) object); } catch (NumberFormatException e) { LOG.warn(PERMISSION_ID_PROPERTY_ID + " is not a valid integer value", e); throw new NoSuchResourceException("The requested resource doesn't exist: Authorization not found, " + predicate, e); } } else if (object instanceof Number) { permissionId = ((Number) object).intValue(); } else { permissionId = null; } if (permissionId == null) { authorizationEntities = roleAuthorizationDAO.findAll(); } else { PermissionEntity permissionEntity = permissionDAO.findById(permissionId); if (permissionEntity == null) { authorizationEntities = null; } else { authorizationEntities = permissionEntity.getAuthorizations(); } } if (authorizationEntities != null) { String authorizationId = (String) propertyMap.get(AUTHORIZATION_ID_PROPERTY_ID); if (!StringUtils.isEmpty(authorizationId)) { Iterator<RoleAuthorizationEntity> iterator = authorizationEntities.iterator(); while (iterator.hasNext()) { if (!authorizationId.equals(iterator.next().getAuthorizationId())) { iterator.remove(); } } } for (RoleAuthorizationEntity entity : authorizationEntities) { resources.add(toResource(permissionId, entity, requestedIds)); } } } } return resources; } RoleAuthorizationResourceProvider(AmbariManagementController managementController); @Override Set<Resource> getResources(Request request, Predicate predicate); static final String AUTHORIZATION_ID_PROPERTY_ID; static final String PERMISSION_ID_PROPERTY_ID; static final String AUTHORIZATION_NAME_PROPERTY_ID; }