WidgetResourceProvider extends AbstractControllerResourceProvider { @Override public RequestStatus deleteResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate); final List<WidgetEntity> entitiesToBeRemoved = new ArrayList<WidgetEntity>(); 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 SystemException("Widget should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidgetLayout().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidgetLayout().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetDAO.remove(entity); } 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 testDeleteResources() 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)).anyTimes(); Capture<WidgetEntity> entityCapture = EasyMock.newCapture(); dao.create(capture(entityCapture)); expectLastCall(); replay(amc, clusters, cluster, dao); WidgetResourceProvider provider = createProvider(amc); 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); 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)); resetToStrict(dao); expect(dao.findById(1L)).andReturn(entity).anyTimes(); dao.remove(capture(entityCapture)); expectLastCall(); replay(dao); provider.deleteResources(request, predicate); WidgetEntity entity1 = entityCapture.getValue(); Assert.assertEquals(Long.valueOf(1), entity1.getId()); verify(amc, clusters, cluster, dao); }