StackDependencyResourceProvider extends AbstractResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<Resource> resources = new HashSet<Resource>(); Set<Map<String, Object>> requestProps = getPropertyMaps(predicate); for (Map<String, Object> properties : requestProps) { try { resources.addAll(getDependencyResources(properties, getRequestPropertyIds(request, predicate))); } catch (NoSuchResourceException e) { if (requestProps.size() == 1) { throw e; } } catch (NoSuchParentResourceException e) { if (requestProps.size() == 1) { throw e; } } } return resources; } protected  StackDependencyResourceProvider(Set<String> propertyIds,
                                            Map<Resource.Type, String> keyPropertyIds); static void init(AmbariMetaInfo metaInfo); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus createResources(Request request); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate);  }
@Test public void testGetResources() throws SystemException, UnsupportedPropertyException, NoSuchParentResourceException, NoSuchResourceException, AmbariException { Request request = createNiceMock(Request.class); DependencyInfo dependencyInfo = new DependencyInfo(); dependencyInfo.setName("service_name/comp_name"); dependencyInfo.setScope("cluster"); Predicate namePredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.COMPONENT_NAME_ID, "comp_name"); Predicate depServicePredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.DEPENDENT_SERVICE_NAME_ID, "dep_service_name"); Predicate depCompPredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.DEPENDENT_COMPONENT_NAME_ID, "dep_comp_name"); Predicate stackNamePredicate = new EqualsPredicate<String> (StackDependencyResourceProvider.STACK_NAME_ID, "stack_name"); Predicate stackVersionPredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.STACK_VERSION_ID, "stack_version"); Predicate andPredicate = new AndPredicate(namePredicate, depServicePredicate, depCompPredicate, stackNamePredicate, stackVersionPredicate); expect(metaInfo.getComponentDependency("stack_name", "stack_version", "dep_service_name", "dep_comp_name", "comp_name")).andReturn(dependencyInfo); replay(metaInfo, request); ResourceProvider provider = createProvider(); Set<Resource> resources = provider.getResources(request, andPredicate); verify(metaInfo); assertEquals(1, resources.size()); Resource resource = resources.iterator().next(); assertEquals("cluster", resource.getPropertyValue(StackDependencyResourceProvider.SCOPE_ID)); assertEquals("comp_name", resource.getPropertyValue(StackDependencyResourceProvider.COMPONENT_NAME_ID)); assertEquals("service_name", resource.getPropertyValue(StackDependencyResourceProvider.SERVICE_NAME_ID)); assertEquals("dep_service_name", resource.getPropertyValue(StackDependencyResourceProvider.DEPENDENT_SERVICE_NAME_ID)); assertEquals("dep_comp_name", resource.getPropertyValue(StackDependencyResourceProvider.DEPENDENT_COMPONENT_NAME_ID)); assertEquals("stack_name", resource.getPropertyValue(StackDependencyResourceProvider.STACK_NAME_ID)); assertEquals("stack_version", resource.getPropertyValue(StackDependencyResourceProvider.STACK_VERSION_ID)); }
@Test public void testGetResources_Query() throws SystemException, UnsupportedPropertyException, NoSuchParentResourceException, NoSuchResourceException, AmbariException { Request request = createNiceMock(Request.class); DependencyInfo dependencyInfo = new DependencyInfo(); dependencyInfo.setName("service_name/comp_name"); dependencyInfo.setScope("cluster"); Predicate namePredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.COMPONENT_NAME_ID, "comp_name"); Predicate name2Predicate = new EqualsPredicate<String>( StackDependencyResourceProvider.COMPONENT_NAME_ID, "comp_name2"); Predicate depServicePredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.DEPENDENT_SERVICE_NAME_ID, "dep_service_name"); Predicate depCompPredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.DEPENDENT_COMPONENT_NAME_ID, "dep_comp_name"); Predicate stackNamePredicate = new EqualsPredicate<String> (StackDependencyResourceProvider.STACK_NAME_ID, "stack_name"); Predicate stackVersionPredicate = new EqualsPredicate<String>( StackDependencyResourceProvider.STACK_VERSION_ID, "stack_version"); Predicate andPredicate1 = new AndPredicate(namePredicate, depServicePredicate, depCompPredicate, stackNamePredicate, stackVersionPredicate); Predicate andPredicate2 = new AndPredicate(name2Predicate, depServicePredicate, depCompPredicate, stackNamePredicate, stackVersionPredicate); Predicate orPredicate = new OrPredicate(andPredicate1, andPredicate2); expect(metaInfo.getComponentDependency("stack_name", "stack_version", "dep_service_name", "dep_comp_name", "comp_name")).andReturn(dependencyInfo); expect(metaInfo.getComponentDependency("stack_name", "stack_version", "dep_service_name", "dep_comp_name", "comp_name2")).andThrow(new StackAccessException("test")); replay(metaInfo, request); ResourceProvider provider = createProvider(); Set<Resource> resources = provider.getResources(request, orPredicate); verify(metaInfo); assertEquals(1, resources.size()); Resource resource = resources.iterator().next(); assertEquals("cluster", resource.getPropertyValue(StackDependencyResourceProvider.SCOPE_ID)); assertEquals("comp_name", resource.getPropertyValue(StackDependencyResourceProvider.COMPONENT_NAME_ID)); assertEquals("service_name", resource.getPropertyValue(StackDependencyResourceProvider.SERVICE_NAME_ID)); assertEquals("dep_service_name", resource.getPropertyValue(StackDependencyResourceProvider.DEPENDENT_SERVICE_NAME_ID)); assertEquals("dep_comp_name", resource.getPropertyValue(StackDependencyResourceProvider.DEPENDENT_COMPONENT_NAME_ID)); assertEquals("stack_name", resource.getPropertyValue(StackDependencyResourceProvider.STACK_NAME_ID)); assertEquals("stack_version", resource.getPropertyValue(StackDependencyResourceProvider.STACK_VERSION_ID)); }