@Test
public void testUpdateResources() throws Exception {

  AmbariManagementController amc = createMock(AmbariManagementController.class);
  Clusters clusters = createMock(Clusters.class);
  Cluster cluster = createMock(Cluster.class);
  expect(amc.getClusters()).andReturn(clusters).atLeastOnce();
  expect(clusters.getCluster((String) anyObject())).andReturn(cluster).atLeastOnce();
  expect(cluster.getClusterId()).andReturn(Long.valueOf(1)).atLeastOnce();
  Capture<WidgetEntity> entityCapture = EasyMock.newCapture();
  dao.create(capture(entityCapture));
  expectLastCall();
  replay(amc, clusters, cluster, dao);
  Map<String, Object> requestProps = new HashMap<>();
  requestProps.put(WidgetResourceProvider.WIDGET_CLUSTER_NAME_PROPERTY_ID, "c1");
  requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_NAME_PROPERTY_ID, "widget name");
  requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_TYPE_PROPERTY_ID, "GAUGE");
  requestProps.put(WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID, "admin");
  requestProps.put(WidgetResourceProvider.WIDGET_SCOPE_PROPERTY_ID, "USER");
  Set<Map<String, String>> testSet = new LinkedHashSet<>();
  Map<String, String> testMap = new HashMap<>();
  testMap.put("name","value");
  testMap.put("name2","value2");
  testSet.add(testMap);
  requestProps.put(WidgetResourceProvider.WIDGET_METRICS_PROPERTY_ID, testSet);
  requestProps.put(WidgetResourceProvider.WIDGET_VALUES_PROPERTY_ID, testSet);
  requestProps.put(WidgetResourceProvider.WIDGET_PROPERTIES_PROPERTY_ID+"/property1", "value1");
  requestProps.put(WidgetResourceProvider.WIDGET_PROPERTIES_PROPERTY_ID+"/property2", "value2");
  Request request = PropertyHelper.getCreateRequest(
      Collections.singleton(requestProps), null);
  WidgetResourceProvider provider = createProvider(amc);
  provider.createResources(request);
  Assert.assertTrue(entityCapture.hasCaptured());
  WidgetEntity entity = entityCapture.getValue();
  Assert.assertNotNull(entity);
  Predicate predicate = new PredicateBuilder().property(
          WidgetResourceProvider.WIDGET_CLUSTER_NAME_PROPERTY_ID).equals("c1")
          .and().property(WidgetResourceProvider.WIDGET_ID_PROPERTY_ID).equals("1")
          .and().property(WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID).equals("username").toPredicate();
  // everything is mocked, there is no DB
  entity.setId(Long.valueOf(1));
  String oldMetrics = entity.getMetrics();
  String oldProperties = entity.getProperties();
  String oldName = entity.getWidgetName();
  resetToStrict(dao);
  expect(dao.findById(1L)).andReturn(entity).anyTimes();
  expect(dao.merge((WidgetEntity) anyObject())).andReturn(entity).anyTimes();
  replay(dao);
  requestProps = new HashMap<>();
  requestProps.put(WidgetResourceProvider.WIDGET_ID_PROPERTY_ID, "1");
  requestProps.put(WidgetResourceProvider.WIDGET_CLUSTER_NAME_PROPERTY_ID, "c1");
  requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_NAME_PROPERTY_ID, "widget name2");
  requestProps.put(WidgetResourceProvider.WIDGET_WIDGET_TYPE_PROPERTY_ID, "GAUGE");
  requestProps.put(WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID, "admin");
  requestProps.put(WidgetResourceProvider.WIDGET_SCOPE_PROPERTY_ID, "USER");
  testSet = new LinkedHashSet<>();
  testMap = new HashMap<>();
  testMap.put("name","new_value");
  testMap.put("new_name","new_value2");
  testSet.add(testMap);
  requestProps.put(WidgetResourceProvider.WIDGET_METRICS_PROPERTY_ID, testSet);
  requestProps.put(WidgetResourceProvider.WIDGET_VALUES_PROPERTY_ID, testSet);
  requestProps.put(WidgetResourceProvider.WIDGET_PROPERTIES_PROPERTY_ID+"/property1", "new_value1");
  requestProps.put(WidgetResourceProvider.WIDGET_PROPERTIES_PROPERTY_ID+"/new_property", "new_value2");
  request = PropertyHelper.getUpdateRequest(requestProps, null);
  provider.updateResources(request, predicate);
  Assert.assertFalse(oldName.equals(entity.getWidgetName()));
  Assert.assertFalse(oldMetrics.equals(entity.getMetrics()));
  Assert.assertFalse(oldProperties.equals(entity.getProperties()));
  Assert.assertEquals("[{\"name\":\"new_value\",\"new_name\":\"new_value2\"}]",entity.getMetrics());
  // Depends on hashing, string representation can be different
  Assert.assertTrue(CollectionPresentationUtils.isJsonsEquals("{\"new_property\":\"new_value2\",\"property1\":\"new_value1\"}", entity.getProperties()));
  Assert.assertEquals("widget name2",entity.getWidgetName());
  Assert.assertEquals(null,entity.getDefaultSectionName());
  verify(amc, clusters, cluster, dao);
}