@Test public void testGetResources() throws Exception { Resource.Type type = Resource.Type.Action; AmbariMetaInfo am = createNiceMock(AmbariMetaInfo.class); AmbariManagementController managementController = createNiceMock(AmbariManagementController.class); expect(managementController.getAmbariMetaInfo()).andReturn(am).anyTimes(); List<ActionDefinition> allDefinition = new ArrayList<ActionDefinition>(); allDefinition.add(new ActionDefinition( "a1", ActionType.SYSTEM, "fileName", "HDFS", "DATANODE", "Does file exist", TargetHostType.ANY, Short.valueOf("100"), null)); allDefinition.add(new ActionDefinition( "a2", ActionType.SYSTEM, "fileName", "HDFS", "DATANODE", "Does file exist", TargetHostType.ANY, Short.valueOf("100"), null)); allDefinition.add(new ActionDefinition( "a3", ActionType.SYSTEM, "fileName", "HDFS", "DATANODE", "Does file exist", TargetHostType.ANY, Short.valueOf("100"), null)); Set<ActionResponse> allResponse = new HashSet<ActionResponse>(); for (ActionDefinition definition : allDefinition) { allResponse.add(definition.convertToResponse()); } ActionDefinition namedDefinition = new ActionDefinition( "a1", ActionType.SYSTEM, "fileName", "HDFS", "DATANODE", "Does file exist", TargetHostType.ANY, Short.valueOf("100"), null); Set<ActionResponse> nameResponse = new HashSet<ActionResponse>(); nameResponse.add(namedDefinition.convertToResponse()); expect(am.getAllActionDefinition()).andReturn(allDefinition).once(); expect(am.getActionDefinition("a1")).andReturn(namedDefinition).once(); replay(managementController, am); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(ActionResourceProvider.ACTION_NAME_PROPERTY_ID); propertyIds.add(ActionResourceProvider.ACTION_TYPE_PROPERTY_ID); propertyIds.add(ActionResourceProvider.DEFAULT_TIMEOUT_PROPERTY_ID); propertyIds.add(ActionResourceProvider.DESCRIPTION_PROPERTY_ID); propertyIds.add(ActionResourceProvider.INPUTS_PROPERTY_ID); propertyIds.add(ActionResourceProvider.TARGET_COMPONENT_PROPERTY_ID); propertyIds.add(ActionResourceProvider.TARGET_HOST_PROPERTY_ID); propertyIds.add(ActionResourceProvider.TARGET_SERVICE_PROPERTY_ID); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, null); Assert.assertEquals(allResponse.size(), resources.size()); for (Resource resource : resources) { String actionName = (String) resource.getPropertyValue(ActionResourceProvider.ACTION_NAME_PROPERTY_ID); String actionType = (String) resource.getPropertyValue(ActionResourceProvider.ACTION_TYPE_PROPERTY_ID); String defaultTimeout = (String) resource.getPropertyValue(ActionResourceProvider.DEFAULT_TIMEOUT_PROPERTY_ID); String description = (String) resource.getPropertyValue(ActionResourceProvider.DESCRIPTION_PROPERTY_ID); String inputs = (String) resource.getPropertyValue(ActionResourceProvider.INPUTS_PROPERTY_ID); String comp = (String) resource.getPropertyValue(ActionResourceProvider.TARGET_COMPONENT_PROPERTY_ID); String svc = (String) resource.getPropertyValue(ActionResourceProvider.TARGET_SERVICE_PROPERTY_ID); String host = (String) resource.getPropertyValue(ActionResourceProvider.TARGET_HOST_PROPERTY_ID); Assert.assertTrue(allResponse.contains(new ActionResponse(actionName, actionType, inputs, svc, comp, description, host, defaultTimeout))); } Predicate predicate = new PredicateBuilder().property(ActionResourceProvider.ACTION_NAME_PROPERTY_ID). equals("a1").toPredicate(); resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); Assert.assertEquals("a1", resources.iterator().next(). getPropertyValue(ActionResourceProvider.ACTION_NAME_PROPERTY_ID)); verify(managementController); }
@Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<ActionRequest> requests = new HashSet<ActionRequest>(); if (predicate != null) { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { ActionRequest actionReq = getRequest(propertyMap); LOG.debug("Received a get request for Action with" + ", actionName = " + actionReq.getActionName()); requests.add(actionReq); } } else { LOG.debug("Received a get request for all Actions"); requests.add(ActionRequest.getAllRequest()); } Set<ActionResponse> responses = getResources(new Command<Set<ActionResponse>>() { @Override public Set<ActionResponse> invoke() throws AmbariException { return getActionDefinitions(requests); } }); Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); for (ActionResponse response : responses) { Resource resource = new ResourceImpl(Type.Action); setResourceProperty(resource, ACTION_NAME_PROPERTY_ID, response.getActionName(), requestedIds); setResourceProperty(resource, ACTION_TYPE_PROPERTY_ID, response.getActionType(), requestedIds); setResourceProperty(resource, INPUTS_PROPERTY_ID, response.getInputs(), requestedIds); setResourceProperty(resource, TARGET_SERVICE_PROPERTY_ID, response.getTargetService(), requestedIds); setResourceProperty(resource, TARGET_COMPONENT_PROPERTY_ID, response.getTargetComponent(), requestedIds); setResourceProperty(resource, DESCRIPTION_PROPERTY_ID, response.getDescription(), requestedIds); setResourceProperty(resource, TARGET_HOST_PROPERTY_ID, response.getTargetType(), requestedIds); setResourceProperty(resource, DEFAULT_TIMEOUT_PROPERTY_ID, response.getDefaultTimeout(), requestedIds); resources.add(resource); } return resources; }
ActionResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<ActionRequest> requests = new HashSet<ActionRequest>(); if (predicate != null) { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { ActionRequest actionReq = getRequest(propertyMap); LOG.debug("Received a get request for Action with" + ", actionName = " + actionReq.getActionName()); requests.add(actionReq); } } else { LOG.debug("Received a get request for all Actions"); requests.add(ActionRequest.getAllRequest()); } Set<ActionResponse> responses = getResources(new Command<Set<ActionResponse>>() { @Override public Set<ActionResponse> invoke() throws AmbariException { return getActionDefinitions(requests); } }); Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); for (ActionResponse response : responses) { Resource resource = new ResourceImpl(Type.Action); setResourceProperty(resource, ACTION_NAME_PROPERTY_ID, response.getActionName(), requestedIds); setResourceProperty(resource, ACTION_TYPE_PROPERTY_ID, response.getActionType(), requestedIds); setResourceProperty(resource, INPUTS_PROPERTY_ID, response.getInputs(), requestedIds); setResourceProperty(resource, TARGET_SERVICE_PROPERTY_ID, response.getTargetService(), requestedIds); setResourceProperty(resource, TARGET_COMPONENT_PROPERTY_ID, response.getTargetComponent(), requestedIds); setResourceProperty(resource, DESCRIPTION_PROPERTY_ID, response.getDescription(), requestedIds); setResourceProperty(resource, TARGET_HOST_PROPERTY_ID, response.getTargetType(), requestedIds); setResourceProperty(resource, DEFAULT_TIMEOUT_PROPERTY_ID, response.getDefaultTimeout(), requestedIds); resources.add(resource); } return resources; } }
ActionResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<ActionRequest> requests = new HashSet<ActionRequest>(); if (predicate != null) { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { ActionRequest actionReq = getRequest(propertyMap); LOG.debug("Received a get request for Action with" + ", actionName = " + actionReq.getActionName()); requests.add(actionReq); } } else { LOG.debug("Received a get request for all Actions"); requests.add(ActionRequest.getAllRequest()); } Set<ActionResponse> responses = getResources(new Command<Set<ActionResponse>>() { @Override public Set<ActionResponse> invoke() throws AmbariException { return getActionDefinitions(requests); } }); Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); for (ActionResponse response : responses) { Resource resource = new ResourceImpl(Type.Action); setResourceProperty(resource, ACTION_NAME_PROPERTY_ID, response.getActionName(), requestedIds); setResourceProperty(resource, ACTION_TYPE_PROPERTY_ID, response.getActionType(), requestedIds); setResourceProperty(resource, INPUTS_PROPERTY_ID, response.getInputs(), requestedIds); setResourceProperty(resource, TARGET_SERVICE_PROPERTY_ID, response.getTargetService(), requestedIds); setResourceProperty(resource, TARGET_COMPONENT_PROPERTY_ID, response.getTargetComponent(), requestedIds); setResourceProperty(resource, DESCRIPTION_PROPERTY_ID, response.getDescription(), requestedIds); setResourceProperty(resource, TARGET_HOST_PROPERTY_ID, response.getTargetType(), requestedIds); setResourceProperty(resource, DEFAULT_TIMEOUT_PROPERTY_ID, response.getDefaultTimeout(), requestedIds); resources.add(resource); } return resources; } ActionResourceProvider(Set<String> propertyIds,
                                Map<Type, String> keyPropertyIds,
                                AmbariManagementController managementController); }
ActionResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<ActionRequest> requests = new HashSet<ActionRequest>(); if (predicate != null) { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { ActionRequest actionReq = getRequest(propertyMap); LOG.debug("Received a get request for Action with" + ", actionName = " + actionReq.getActionName()); requests.add(actionReq); } } else { LOG.debug("Received a get request for all Actions"); requests.add(ActionRequest.getAllRequest()); } Set<ActionResponse> responses = getResources(new Command<Set<ActionResponse>>() { @Override public Set<ActionResponse> invoke() throws AmbariException { return getActionDefinitions(requests); } }); Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); for (ActionResponse response : responses) { Resource resource = new ResourceImpl(Type.Action); setResourceProperty(resource, ACTION_NAME_PROPERTY_ID, response.getActionName(), requestedIds); setResourceProperty(resource, ACTION_TYPE_PROPERTY_ID, response.getActionType(), requestedIds); setResourceProperty(resource, INPUTS_PROPERTY_ID, response.getInputs(), requestedIds); setResourceProperty(resource, TARGET_SERVICE_PROPERTY_ID, response.getTargetService(), requestedIds); setResourceProperty(resource, TARGET_COMPONENT_PROPERTY_ID, response.getTargetComponent(), requestedIds); setResourceProperty(resource, DESCRIPTION_PROPERTY_ID, response.getDescription(), requestedIds); setResourceProperty(resource, TARGET_HOST_PROPERTY_ID, response.getTargetType(), requestedIds); setResourceProperty(resource, DEFAULT_TIMEOUT_PROPERTY_ID, response.getDefaultTimeout(), requestedIds); resources.add(resource); } return resources; } ActionResourceProvider(Set<String> propertyIds,
                                Map<Type, String> keyPropertyIds,
                                AmbariManagementController managementController); @Override RequestStatus createResources(Request request); @Override RequestStatus updateResources(final Request request, Predicate predicate); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); @Override Set<String> getPKPropertyIds(); }
ActionResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<ActionRequest> requests = new HashSet<ActionRequest>(); if (predicate != null) { for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { ActionRequest actionReq = getRequest(propertyMap); LOG.debug("Received a get request for Action with" + ", actionName = " + actionReq.getActionName()); requests.add(actionReq); } } else { LOG.debug("Received a get request for all Actions"); requests.add(ActionRequest.getAllRequest()); } Set<ActionResponse> responses = getResources(new Command<Set<ActionResponse>>() { @Override public Set<ActionResponse> invoke() throws AmbariException { return getActionDefinitions(requests); } }); Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); for (ActionResponse response : responses) { Resource resource = new ResourceImpl(Type.Action); setResourceProperty(resource, ACTION_NAME_PROPERTY_ID, response.getActionName(), requestedIds); setResourceProperty(resource, ACTION_TYPE_PROPERTY_ID, response.getActionType(), requestedIds); setResourceProperty(resource, INPUTS_PROPERTY_ID, response.getInputs(), requestedIds); setResourceProperty(resource, TARGET_SERVICE_PROPERTY_ID, response.getTargetService(), requestedIds); setResourceProperty(resource, TARGET_COMPONENT_PROPERTY_ID, response.getTargetComponent(), requestedIds); setResourceProperty(resource, DESCRIPTION_PROPERTY_ID, response.getDescription(), requestedIds); setResourceProperty(resource, TARGET_HOST_PROPERTY_ID, response.getTargetType(), requestedIds); setResourceProperty(resource, DEFAULT_TIMEOUT_PROPERTY_ID, response.getDefaultTimeout(), requestedIds); resources.add(resource); } return resources; } ActionResourceProvider(Set<String> propertyIds,
                                Map<Type, String> keyPropertyIds,
                                AmbariManagementController managementController); @Override RequestStatus createResources(Request request); @Override RequestStatus updateResources(final Request request, Predicate predicate); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); @Override Set<String> getPKPropertyIds(); static final String ACTION_NAME_PROPERTY_ID; static final String ACTION_TYPE_PROPERTY_ID; static final String INPUTS_PROPERTY_ID; static final String TARGET_SERVICE_PROPERTY_ID; static final String TARGET_COMPONENT_PROPERTY_ID; static final String DESCRIPTION_PROPERTY_ID; static final String TARGET_HOST_PROPERTY_ID; static final String DEFAULT_TIMEOUT_PROPERTY_ID; }