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; }
@Test public void testGetResources() throws Exception { RoleAuthorizationEntity roleAuthorizationEntity = createNiceMock(RoleAuthorizationEntity.class); expect(roleAuthorizationEntity.getAuthorizationId()).andReturn("TEST.DO_SOMETHING"); expect(roleAuthorizationEntity.getAuthorizationName()).andReturn("Do Something"); List<RoleAuthorizationEntity> authorizationEntities = new ArrayList<RoleAuthorizationEntity>(); authorizationEntities.add(roleAuthorizationEntity); RoleAuthorizationDAO roleAuthorizationDAO = injector.getInstance(RoleAuthorizationDAO.class); expect(roleAuthorizationDAO.findAll()).andReturn(authorizationEntities); replayAll(); AmbariManagementController managementController = injector.getInstance(AmbariManagementController.class); RoleAuthorizationResourceProvider provider = new RoleAuthorizationResourceProvider(managementController); injector.injectMembers(provider); Set<Resource> resources = provider.getResources(PropertyHelper.getReadRequest(), null); 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.assertNull(resource.getPropertyValue(RoleAuthorizationResourceProvider.PERMISSION_ID_PROPERTY_ID)); verifyAll(); }
@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(); }
@Test public void testGetResource() throws Exception { RoleAuthorizationEntity roleAuthorizationEntity1 = createNiceMock(RoleAuthorizationEntity.class); expect(roleAuthorizationEntity1.getAuthorizationId()).andReturn("TEST.DO_SOMETHING").anyTimes(); expect(roleAuthorizationEntity1.getAuthorizationName()).andReturn("Do Something").anyTimes(); RoleAuthorizationEntity roleAuthorizationEntity2 = createNiceMock(RoleAuthorizationEntity.class); expect(roleAuthorizationEntity2.getAuthorizationId()).andReturn("TEST.DO_SOMETHING_ELSE").anyTimes(); expect(roleAuthorizationEntity2.getAuthorizationName()).andReturn("Do Something Else").anyTimes(); List<RoleAuthorizationEntity> authorizationEntities = new ArrayList<RoleAuthorizationEntity>(); authorizationEntities.add(roleAuthorizationEntity1); authorizationEntities.add(roleAuthorizationEntity2); 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().begin() .property(RoleAuthorizationResourceProvider.AUTHORIZATION_ID_PROPERTY_ID).equals("TEST.DO_SOMETHING") .and() .property(RoleAuthorizationResourceProvider.PERMISSION_ID_PROPERTY_ID).equals("1") .end() .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(); }