@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<WidgetLayoutEntity> entityCapture = EasyMock.newCapture(); dao.create(capture(entityCapture)); expectLastCall(); replay(amc, clusters, cluster, dao); WidgetLayoutResourceProvider provider = createProvider(amc); Map<String, Object> requestProps = new HashMap<String, Object>(); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, "c1"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, "layout_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, "display_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, "section_name"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, "admin"); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, "CLUSTER"); Set widgetsInfo = new LinkedHashSet(); requestProps.put(WidgetLayoutResourceProvider.WIDGETLAYOUT_WIDGETS_PROPERTY_ID, widgetsInfo); Request request = PropertyHelper.getCreateRequest(Collections.singleton(requestProps), null); provider.createResources(request); Assert.assertTrue(entityCapture.hasCaptured()); WidgetLayoutEntity entity = entityCapture.getValue(); Assert.assertNotNull(entity); Predicate predicate = new PredicateBuilder().property( WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID).equals("c1") .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID).equals("1") .and().property(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_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); WidgetLayoutEntity entity1 = entityCapture.getValue(); Assert.assertEquals(Long.valueOf(1), entity1.getId()); verify(amc, clusters, cluster, dao); }
@Override public RequestStatus deleteResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate); final List<WidgetLayoutEntity> entitiesToBeRemoved = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetLayoutEntity entity = widgetLayoutDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget layout with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetLayoutEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetLayoutDAO.remove(entity); } return getRequestStatus(null); }
WidgetLayoutResourceProvider 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<WidgetLayoutEntity> entitiesToBeRemoved = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetLayoutEntity entity = widgetLayoutDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget layout with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetLayoutEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetLayoutDAO.remove(entity); } return getRequestStatus(null); } }
WidgetLayoutResourceProvider 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<WidgetLayoutEntity> entitiesToBeRemoved = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetLayoutEntity entity = widgetLayoutDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget layout with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetLayoutEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetLayoutDAO.remove(entity); } return getRequestStatus(null); } WidgetLayoutResourceProvider(AmbariManagementController managementController); }
WidgetLayoutResourceProvider 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<WidgetLayoutEntity> entitiesToBeRemoved = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetLayoutEntity entity = widgetLayoutDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget layout with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetLayoutEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetLayoutDAO.remove(entity); } return getRequestStatus(null); } WidgetLayoutResourceProvider(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); }
WidgetLayoutResourceProvider 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<WidgetLayoutEntity> entitiesToBeRemoved = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap : propertyMaps) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetLayoutEntity entity = widgetLayoutDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("There is no widget layout with id " + id); } entitiesToBeRemoved.add(entity); } for (WidgetLayoutEntity entity: entitiesToBeRemoved) { if (entity.getListWidgetLayoutUserWidgetEntity() != null) { for (WidgetLayoutUserWidgetEntity layoutUserWidgetEntity : entity.getListWidgetLayoutUserWidgetEntity()) { if (layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity() != null) { layoutUserWidgetEntity.getWidget().getListWidgetLayoutUserWidgetEntity().remove(layoutUserWidgetEntity); } } } widgetLayoutDAO.remove(entity); } return getRequestStatus(null); } WidgetLayoutResourceProvider(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 WIDGETLAYOUT_ID_PROPERTY_ID; static final String WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID; static final String WIDGETLAYOUT_SCOPE_PROPERTY_ID; static final String WIDGETLAYOUT_WIDGETS_PROPERTY_ID; static final String WIDGETLAYOUT_USERNAME_PROPERTY_ID; static final String WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID; @SuppressWarnings("serial")
  static Set<String> propertyIds; @SuppressWarnings("serial")
  static Map<Type, String> keyPropertyIds; }