@Test
public void testUpdateResources() throws Exception {

  Map<String, String> requestInfoProps = new HashMap<String, String>();
  requestInfoProps.put(Request.REQUEST_INFO_BODY_PROPERTY, bodyJson);
  Capture<ArtifactEntity> updateEntityCapture = newCapture();
  Capture<ArtifactEntity> updateEntityCapture2 = newCapture();
  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");
  // map with flattened properties
  Map<String, Object> properties = new HashMap<String, Object>();
  properties.put("Artifacts/artifact_name", "test-artifact");
  properties.put("Artifacts/cluster_name", "test-cluster");
  properties.put("artifact_data/foo", "bar");
  properties.put("artifact_data/child/childKey", "childValue");
  properties.put("artifact_data/child/child2/child2Key", "child2Value");
  properties.put("artifact_data/child/child2/child3/child4/child4Key", "child4Value");
  Set<Map<String, Object>> requestProperties = Collections.singleton(properties);
  properties.put("artifact_data/collection", Collections.emptySet());
  // expectations
  expect(request.getProperties()).andReturn(requestProperties).anyTimes();
  expect(request.getRequestInfoProperties()).andReturn(requestInfoProps).anyTimes();
  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();
  expect(dao.merge(capture(updateEntityCapture))).andReturn(entity).once();
  expect(dao.merge(capture(updateEntityCapture2))).andReturn(entity2).once();
  // end of expectation setting
  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.updateResources(request, predicate);
  ArtifactEntity updateEntity = updateEntityCapture.getValue();
  ArtifactEntity updateEntity2 = updateEntityCapture2.getValue();
  Gson serializer = new Gson();
  ArtifactEntity expected = new ArtifactEntity();
  expected.setArtifactData((Map<String, Object>) serializer.<Map<String, Object>>fromJson(
      bodyJson, Map.class).get("artifact_data"));
  assertEquals(expected.getArtifactData(), updateEntity.getArtifactData());
  assertEquals(expected.getArtifactData(), updateEntity2.getArtifactData());
  if (updateEntity.getArtifactName().equals("test-artifact")) {
    assertEquals("test-artifact2",updateEntity2.getArtifactName() );
  } else if (updateEntity.getArtifactName().equals("test-artifact2")) {
    assertEquals("test-artifact", updateEntity2.getArtifactName());
  } else {
    fail ("Unexpected artifact name: " + updateEntity.getArtifactName());
  }
  assertEquals(foreignKeys, updateEntity.getForeignKeys());
  assertEquals(foreignKeys, updateEntity2.getForeignKeys());
  assertEquals(RequestStatus.Status.Complete, response.getStatus());
  verify(dao, em, controller, request, clusters, cluster, entity, entity2);
}