WidgetLayoutResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { final Set<Resource> resources = new HashSet<Resource>(); final Set<String> requestedIds = getRequestPropertyIds(request, predicate); final Set<Map<String, Object>> propertyMaps = getPropertyMaps(predicate); List<WidgetEntity> widgetEntities = new ArrayList<WidgetEntity>(); List<WidgetLayoutEntity> layoutEntities = new ArrayList<WidgetLayoutEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { String userName = getUserName(propertyMap); if (propertyMap.get(WIDGETLAYOUT_ID_PROPERTY_ID) != null) { 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("WidgetLayout with id " + id + " does not exists"); } layoutEntities.add(entity); } else { layoutEntities.addAll(widgetLayoutDAO.findAll()); } } for (WidgetLayoutEntity layoutEntity : layoutEntities) { Resource resource = new ResourceImpl(Type.WidgetLayout); resource.setProperty(WIDGETLAYOUT_ID_PROPERTY_ID, layoutEntity.getId()); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(layoutEntity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } resource.setProperty(WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, clusterName); resource.setProperty(WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, layoutEntity.getLayoutName()); resource.setProperty(WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, layoutEntity.getSectionName()); resource.setProperty(WIDGETLAYOUT_SCOPE_PROPERTY_ID, layoutEntity.getScope()); resource.setProperty(WIDGETLAYOUT_USERNAME_PROPERTY_ID, layoutEntity.getUserName()); resource.setProperty(WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, layoutEntity.getDisplayName()); List<HashMap> widgets = new ArrayList<HashMap>(); List<WidgetLayoutUserWidgetEntity> widgetLayoutUserWidgetEntityList = layoutEntity.getListWidgetLayoutUserWidgetEntity(); for (WidgetLayoutUserWidgetEntity widgetLayoutUserWidgetEntity : widgetLayoutUserWidgetEntityList) { WidgetEntity widgetEntity = widgetLayoutUserWidgetEntity.getWidget(); HashMap<String, Object> widgetInfoMap = new HashMap<String, Object>(); widgetInfoMap.put("WidgetInfo",WidgetResponse.coerce(widgetEntity)); widgets.add(widgetInfoMap); } resource.setProperty(WIDGETLAYOUT_WIDGETS_PROPERTY_ID, widgets); resources.add(resource); } return resources; } 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; }
@Test public void testGetResourcesNoPredicate() throws Exception { WidgetLayoutResourceProvider provider = createProvider(null); Request request = PropertyHelper.getReadRequest("WidgetLayouts/id"); Set<Resource> results = provider.getResources(request, null); assertEquals(0, results.size()); }
@Test public void testGetSingleResource() throws Exception { Request request = PropertyHelper.getReadRequest( WidgetLayoutResourceProvider.WIDGETLAYOUT_CLUSTER_NAME_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_ID_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID, WidgetLayoutResourceProvider.WIDGETLAYOUT_WIDGETS_PROPERTY_ID); AmbariManagementController amc = createMock(AmbariManagementController.class); Clusters clusters = createMock(Clusters.class); Cluster cluster = createMock(Cluster.class); expect(amc.getClusters()).andReturn(clusters).atLeastOnce(); expect(clusters.getClusterById(1L)).andReturn(cluster).atLeastOnce(); expect(cluster.getClusterName()).andReturn("c1").anyTimes(); 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(); expect(dao.findById(1L)).andReturn(getMockEntities().get(0)); replay(amc, clusters, cluster, dao); WidgetLayoutResourceProvider provider = createProvider(amc); Set<Resource> results = provider.getResources(request, predicate); assertEquals(1, results.size()); Resource r = results.iterator().next(); Assert.assertEquals("section0", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_SECTION_NAME_PROPERTY_ID)); Assert.assertEquals("CLUSTER", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_SCOPE_PROPERTY_ID)); Assert.assertEquals("username", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_USERNAME_PROPERTY_ID)); Assert.assertEquals("displ_name", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_DISPLAY_NAME_PROPERTY_ID)); Assert.assertEquals("layout name0", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_LAYOUT_NAME_PROPERTY_ID)); Assert.assertEquals("[]", r.getPropertyValue(WidgetLayoutResourceProvider.WIDGETLAYOUT_WIDGETS_PROPERTY_ID).toString()); }