ArtifactResourceProvider extends AbstractResourceProvider { @Override public RequestStatus deleteResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<Resource> setResources = getResources( new RequestImpl(null, null, null, null), predicate); for (final Resource resource : setResources) { modifyResources(getDeleteCommand(resource)); } notifyDelete(Resource.Type.Artifact, predicate); return getRequestStatus(null); } @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 testDeleteResources() throws Exception { Capture<ArtifactEntity> deleteEntityCapture = newCapture(); Capture<ArtifactEntity> deleteEntityCapture2 = newCapture(); 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(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(); IAnswer<ArtifactEntity> findByNameAndForeignKeys = new IAnswer<ArtifactEntity>() { @Override public ArtifactEntity answer() throws Throwable { String artifactName = (String) getCurrentArguments()[0]; if ("test-artifact".equals(artifactName)) { return entity; } else if ("test-artifact2".equals(artifactName)) { return entity2; } else { return null; } } }; expect(dao.findByForeignKeys(eq(foreignKeys))).andReturn(entities).once(); expect(dao.findByNameAndForeignKeys(anyString(), eq(foreignKeys))).andAnswer(findByNameAndForeignKeys).once(); dao.remove(capture(deleteEntityCapture)); expect(dao.findByNameAndForeignKeys(anyString(), eq(foreignKeys))).andAnswer(findByNameAndForeignKeys).once(); dao.remove(capture(deleteEntityCapture2)); 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(); RequestStatus response = resourceProvider.deleteResources(new RequestImpl(null, null, null, null), predicate); ArtifactEntity deleteEntity = deleteEntityCapture.getValue(); ArtifactEntity deleteEntity2 = deleteEntityCapture2.getValue(); if (deleteEntity.getArtifactName().equals("test-artifact")) { assertEquals("test-artifact2", deleteEntity2.getArtifactName()); } else if (deleteEntity.getArtifactName().equals("test-artifact2")) { assertEquals("test-artifact", deleteEntity2.getArtifactName()); } else { fail ("Unexpected artifact name: " + deleteEntity.getArtifactName()); } assertEquals(foreignKeys, deleteEntity.getForeignKeys()); assertEquals(foreignKeys, deleteEntity2.getForeignKeys()); assertEquals(RequestStatus.Status.Complete, response.getStatus()); verify(dao, em, controller, request, clusters, cluster, entity, entity2); }