WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus updateResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Map<String, Object>> propertyMaps = request.getProperties(); modifyResources(new Command<Void>() { @Override public Void invoke() throws AmbariException { for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new AmbariException("Widget should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new ObjectNotFoundException("There is no widget with id " + id); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_WIDGET_NAME_PROPERTY_ID)))) { entity.setWidgetName(propertyMap.get(WIDGET_WIDGET_NAME_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_WIDGET_TYPE_PROPERTY_ID)))) { entity.setWidgetType(propertyMap.get(WIDGET_WIDGET_TYPE_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_METRICS_PROPERTY_ID)))) { entity.setMetrics(gson.toJson(propertyMap.get(WIDGET_METRICS_PROPERTY_ID))); } entity.setAuthor(getAuthorName(propertyMap)); if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_DESCRIPTION_PROPERTY_ID)))) { entity.setDescription(propertyMap.get(WIDGET_DESCRIPTION_PROPERTY_ID).toString()); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_SCOPE_PROPERTY_ID)))) { String scope = propertyMap.get(WIDGET_SCOPE_PROPERTY_ID).toString(); if (!isScopeAllowedForUser(scope)) { throw new AmbariException("Only cluster operator can create widgets with cluster scope"); } entity.setScope(scope); } if (StringUtils.isNotBlank(ObjectUtils.toString(propertyMap.get(WIDGET_VALUES_PROPERTY_ID)))) { entity.setWidgetValues(gson.toJson(propertyMap.get(WIDGET_VALUES_PROPERTY_ID))); } Map<String, Object> widgetPropertiesMap = new HashMap<String, Object>(); for (Map.Entry<String, Object> entry : propertyMap.entrySet()) { if (PropertyHelper.getPropertyCategory(entry.getKey()).equals(WIDGET_PROPERTIES_PROPERTY_ID)) { widgetPropertiesMap.put(PropertyHelper.getPropertyName(entry.getKey()), entry.getValue()); } } if (!widgetPropertiesMap.isEmpty()) { entity.setProperties(gson.toJson(widgetPropertiesMap)); } widgetDAO.merge(entity); } return null; } }); return getRequestStatus(null); } WidgetResourceProvider(AmbariManagementController managementController); @Override RequestStatus createResources(final Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String WIDGET_ID_PROPERTY_ID; static final String WIDGET_CLUSTER_NAME_PROPERTY_ID; static final String WIDGET_WIDGET_NAME_PROPERTY_ID; static final String WIDGET_WIDGET_TYPE_PROPERTY_ID; static final String WIDGET_TIME_CREATED_PROPERTY_ID; static final String WIDGET_AUTHOR_PROPERTY_ID; static final String WIDGET_DESCRIPTION_PROPERTY_ID; static final String WIDGET_SCOPE_PROPERTY_ID; static final String WIDGET_METRICS_PROPERTY_ID; static final String WIDGET_VALUES_PROPERTY_ID; static final String WIDGET_PROPERTIES_PROPERTY_ID; @SuppressWarnings("serial")
  static Set<String> propertyIds; @SuppressWarnings("serial")
  static Map<Type, String> keyPropertyIds; }
@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(); 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()); 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); }