ArtifactResourceProvider extends AbstractResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<Map<String, Object>> requestProps = getPropertyMaps(predicate); Set<Resource> resources = new LinkedHashSet<Resource>(); for (Map<String, Object> props : requestProps) { resources.addAll(getResources(getGetCommand(request, predicate, props))); } if (resources.isEmpty() && isInstanceRequest(requestProps)) { throw new NoSuchResourceException( "The requested resource doesn't exist: Artifact not found, " + predicate); } return resources; } @Inject protected  ArtifactResourceProvider(AmbariManagementController controller); @Override RequestStatus createResources(Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(final Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String ARTIFACT_NAME_PROPERTY; static final String ARTIFACT_DATA_PROPERTY; }
@Test public void testGetResources_instance() throws Exception { Set<String> propertyIds = new HashSet<String>(); TreeMap<String, String> foreignKeys = new TreeMap<String, String>(); foreignKeys.put("cluster", "500"); Map<String, Object> childMap = new TreeMap<String, Object>(); childMap.put("childKey", "childValue"); Map<String, Object> child2Map = new TreeMap<String, Object>(); childMap.put("child2", child2Map); child2Map.put("child2Key", "child2Value"); Map<String, Object> child3Map = new TreeMap<String, Object>(); child2Map.put("child3", child3Map); Map<String, Object> child4Map = new TreeMap<String, Object>(); child3Map.put("child4", child4Map); child4Map.put("child4Key", "child4Value"); Map<String, Object> artifact_data = new TreeMap<String, Object>(); artifact_data.put("foo", "bar"); artifact_data.put("child", childMap); Map<String, String> responseForeignKeys = new HashMap<String, String>(); responseForeignKeys.put("cluster", "500"); expect(controller.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("test-cluster")).andReturn(cluster).anyTimes(); expect(clusters.getClusterById(500L)).andReturn(cluster).anyTimes(); expect(cluster.getClusterId()).andReturn(500L).anyTimes(); expect(cluster.getClusterName()).andReturn("test-cluster").anyTimes(); expect(request.getPropertyIds()).andReturn(propertyIds).anyTimes(); expect(dao.findByNameAndForeignKeys(eq("test-artifact"), eq(foreignKeys))).andReturn(entity).once(); expect(entity.getArtifactName()).andReturn("test-artifact").anyTimes(); expect(entity.getForeignKeys()).andReturn(responseForeignKeys).anyTimes(); expect(entity.getArtifactData()).andReturn(artifact_data).anyTimes(); replay(dao, em, controller, request, clusters, cluster, entity, entity2); PredicateBuilder pb = new PredicateBuilder(); Predicate predicate = pb.begin().property("Artifacts/cluster_name").equals("test-cluster").and(). property("Artifacts/artifact_name").equals("test-artifact").end().toPredicate(); Set<Resource> response = resourceProvider.getResources(request, predicate); assertEquals(1, response.size()); Resource resource = response.iterator().next(); Map<String, Map<String, Object>> responseProperties = resource.getPropertiesMap(); assertEquals(5, responseProperties.size()); Map<String, Object> artifactDataMap = responseProperties.get("artifact_data"); assertEquals("bar", artifactDataMap.get("foo")); assertEquals("test-artifact", resource.getPropertyValue("Artifacts/artifact_name")); assertEquals("test-cluster", resource.getPropertyValue("Artifacts/cluster_name")); assertEquals("bar", resource.getPropertyValue("artifact_data/foo")); assertEquals("childValue", resource.getPropertyValue("artifact_data/child/childKey")); assertEquals("child2Value", resource.getPropertyValue("artifact_data/child/child2/child2Key")); assertEquals("child4Value", resource.getPropertyValue("artifact_data/child/child2/child3/child4/child4Key")); }
@Test public void testGetResources_collection() throws Exception { Set<String> propertyIds = new HashSet<String>(); TreeMap<String, String> foreignKeys = new TreeMap<String, String>(); foreignKeys.put("cluster", "500"); List<ArtifactEntity> entities = new ArrayList<ArtifactEntity>(); entities.add(entity); entities.add(entity2); Map<String, Object> artifact_data = Collections.<String, Object>singletonMap("foo", "bar"); Map<String, Object> artifact_data2 = Collections.<String, Object>singletonMap("foo2", "bar2"); Map<String, String> responseForeignKeys = new HashMap<String, String>(); responseForeignKeys.put("cluster", "500"); expect(controller.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("test-cluster")).andReturn(cluster).anyTimes(); expect(clusters.getClusterById(500L)).andReturn(cluster).anyTimes(); expect(cluster.getClusterId()).andReturn(500L).anyTimes(); expect(cluster.getClusterName()).andReturn("test-cluster").anyTimes(); expect(request.getPropertyIds()).andReturn(propertyIds).anyTimes(); expect(dao.findByForeignKeys(eq(foreignKeys))).andReturn(entities).anyTimes(); expect(entity.getArtifactName()).andReturn("test-artifact").anyTimes(); expect(entity.getForeignKeys()).andReturn(responseForeignKeys).anyTimes(); expect(entity.getArtifactData()).andReturn(artifact_data).anyTimes(); expect(entity2.getArtifactName()).andReturn("test-artifact2").anyTimes(); expect(entity2.getForeignKeys()).andReturn(responseForeignKeys).anyTimes(); expect(entity2.getArtifactData()).andReturn(artifact_data2).anyTimes(); replay(dao, em, controller, request, clusters, cluster, entity, entity2); PredicateBuilder pb = new PredicateBuilder(); Predicate predicate = pb.begin().property("Artifacts/cluster_name").equals("test-cluster").end().toPredicate(); Set<Resource> response = resourceProvider.getResources(request, predicate); assertEquals(2, response.size()); boolean artifact1Returned = false; boolean artifact2Returned = false; for (Resource resource : response) { if (resource.getPropertyValue("Artifacts/artifact_name").equals("test-artifact")) { artifact1Returned = true; assertEquals("bar", resource.getPropertyValue("artifact_data/foo")); assertEquals("test-cluster", resource.getPropertyValue("Artifacts/cluster_name")); } else if (resource.getPropertyValue("Artifacts/artifact_name").equals("test-artifact2")) { artifact2Returned = true; assertEquals("bar2", resource.getPropertyValue("artifact_data/foo2")); assertEquals("test-cluster", resource.getPropertyValue("Artifacts/cluster_name")); } else { fail("unexpected artifact name"); } } assertTrue(artifact1Returned); assertTrue(artifact2Returned); }