@Test public void testGetResources() throws Exception { Resource.Type type = Resource.Type.Stack; AmbariManagementController managementController = createMock(AmbariManagementController.class); Set<StackResponse> allResponse = new HashSet<StackResponse>(); allResponse.add(new StackResponse("Stack1")); allResponse.add(new StackResponse("Stack2")); expect(managementController.getStacks(EasyMock.<Set<StackRequest>>anyObject())).andReturn(allResponse).once(); replay(managementController); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(StackResourceProvider.STACK_NAME_PROPERTY_ID); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, null); Assert.assertEquals(2, resources.size()); Set<String> stackNames = new HashSet<String>(); stackNames.add("Stack1"); stackNames.add("Stack2"); for (Resource resource : resources) { String stackName = (String) resource.getPropertyValue(StackResourceProvider.STACK_NAME_PROPERTY_ID); Assert.assertTrue(stackNames.contains(stackName)); } verify(managementController); }
@Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<StackRequest> requests = new HashSet<StackRequest>(); if (predicate == null) { requests.add(getRequest(Collections.<String, Object>emptyMap())); } else { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { requests.add(getRequest(propertyMap)); } } Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<StackResponse> responses = getResources(new Command<Set<StackResponse>>() { @Override public Set<StackResponse> invoke() throws AmbariException { return getManagementController().getStacks(requests); } }); Set<Resource> resources = new HashSet<Resource>(); for (StackResponse response : responses) { Resource resource = new ResourceImpl(Resource.Type.Stack); setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getStackName(), requestedIds); resource.setProperty(STACK_NAME_PROPERTY_ID, response.getStackName()); resources.add(resource); } return resources; }
StackResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<StackRequest> requests = new HashSet<StackRequest>(); if (predicate == null) { requests.add(getRequest(Collections.<String, Object>emptyMap())); } else { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { requests.add(getRequest(propertyMap)); } } Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<StackResponse> responses = getResources(new Command<Set<StackResponse>>() { @Override public Set<StackResponse> invoke() throws AmbariException { return getManagementController().getStacks(requests); } }); Set<Resource> resources = new HashSet<Resource>(); for (StackResponse response : responses) { Resource resource = new ResourceImpl(Resource.Type.Stack); setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getStackName(), requestedIds); resource.setProperty(STACK_NAME_PROPERTY_ID, response.getStackName()); resources.add(resource); } return resources; } }
StackResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<StackRequest> requests = new HashSet<StackRequest>(); if (predicate == null) { requests.add(getRequest(Collections.<String, Object>emptyMap())); } else { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { requests.add(getRequest(propertyMap)); } } Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<StackResponse> responses = getResources(new Command<Set<StackResponse>>() { @Override public Set<StackResponse> invoke() throws AmbariException { return getManagementController().getStacks(requests); } }); Set<Resource> resources = new HashSet<Resource>(); for (StackResponse response : responses) { Resource resource = new ResourceImpl(Resource.Type.Stack); setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getStackName(), requestedIds); resource.setProperty(STACK_NAME_PROPERTY_ID, response.getStackName()); resources.add(resource); } return resources; } protected  StackResourceProvider(Set<String> propertyIds,
      Map<Type, String> keyPropertyIds,
      AmbariManagementController managementController); }
StackResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<StackRequest> requests = new HashSet<StackRequest>(); if (predicate == null) { requests.add(getRequest(Collections.<String, Object>emptyMap())); } else { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { requests.add(getRequest(propertyMap)); } } Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<StackResponse> responses = getResources(new Command<Set<StackResponse>>() { @Override public Set<StackResponse> invoke() throws AmbariException { return getManagementController().getStacks(requests); } }); Set<Resource> resources = new HashSet<Resource>(); for (StackResponse response : responses) { Resource resource = new ResourceImpl(Resource.Type.Stack); setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getStackName(), requestedIds); resource.setProperty(STACK_NAME_PROPERTY_ID, response.getStackName()); resources.add(resource); } return resources; } protected  StackResourceProvider(Set<String> propertyIds,
      Map<Type, String> keyPropertyIds,
      AmbariManagementController managementController); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); }
StackResourceProvider extends ReadOnlyResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<StackRequest> requests = new HashSet<StackRequest>(); if (predicate == null) { requests.add(getRequest(Collections.<String, Object>emptyMap())); } else { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { requests.add(getRequest(propertyMap)); } } Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<StackResponse> responses = getResources(new Command<Set<StackResponse>>() { @Override public Set<StackResponse> invoke() throws AmbariException { return getManagementController().getStacks(requests); } }); Set<Resource> resources = new HashSet<Resource>(); for (StackResponse response : responses) { Resource resource = new ResourceImpl(Resource.Type.Stack); setResourceProperty(resource, STACK_NAME_PROPERTY_ID, response.getStackName(), requestedIds); resource.setProperty(STACK_NAME_PROPERTY_ID, response.getStackName()); resources.add(resource); } return resources; } protected  StackResourceProvider(Set<String> propertyIds,
      Map<Type, String> keyPropertyIds,
      AmbariManagementController managementController); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); static final String STACK_NAME_PROPERTY_ID; }