@Test public void testGetSingleResource() throws Exception { Request request = PropertyHelper.getReadRequest( WidgetResourceProvider.WIDGET_ID_PROPERTY_ID, WidgetResourceProvider.WIDGET_WIDGET_NAME_PROPERTY_ID, WidgetResourceProvider.WIDGET_WIDGET_TYPE_PROPERTY_ID, WidgetResourceProvider.WIDGET_TIME_CREATED_PROPERTY_ID, WidgetResourceProvider.WIDGET_CLUSTER_NAME_PROPERTY_ID, WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID, WidgetResourceProvider.WIDGET_DESCRIPTION_PROPERTY_ID, WidgetResourceProvider.WIDGET_SCOPE_PROPERTY_ID, WidgetResourceProvider.WIDGET_METRICS_PROPERTY_ID, WidgetResourceProvider.WIDGET_VALUES_PROPERTY_ID, WidgetResourceProvider.WIDGET_PROPERTIES_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( 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(); expect(dao.findById(1L)).andReturn(getMockEntities("CLUSTER").get(0)); replay(amc, clusters, cluster, dao); WidgetResourceProvider provider = createProvider(amc); Set<Resource> results = provider.getResources(request, predicate); assertEquals(1, results.size()); Resource r = results.iterator().next(); Assert.assertEquals("GAUGE", r.getPropertyValue(WidgetResourceProvider.WIDGET_WIDGET_TYPE_PROPERTY_ID)); Assert.assertEquals("CLUSTER", r.getPropertyValue(WidgetResourceProvider.WIDGET_SCOPE_PROPERTY_ID)); Assert.assertEquals("username", r.getPropertyValue(WidgetResourceProvider.WIDGET_AUTHOR_PROPERTY_ID)); Assert.assertEquals("widget name", r.getPropertyValue(WidgetResourceProvider.WIDGET_WIDGET_NAME_PROPERTY_ID)); Object metrics = r.getPropertyValue(WidgetResourceProvider.WIDGET_METRICS_PROPERTY_ID); Assert.assertEquals("[{\"widget_id\":\"metrics/jvm/HeapMemoryUsed\"," + "\"host_component_criteria\":\"host_components/metrics/dfs/FSNamesystem/HAState\\u003dactive\"," + "\"service_name\":\"HDFS\",\"component_name\":\"NAMENODE\"," + "\"name\":\"java.lang:type\\u003dMemory.HeapMemoryUsage[used]\",\"category\":\"\"}," + "{\"widget_id\":\"metrics/jvm/HeapMemoryMax\"," + "\"host_component_criteria\":\"host_components/metrics/dfs/FSNamesystem/HAState\\u003dactive\"," + "\"service_name\":\"HDFS\",\"component_name\":\"NAMENODE\"," + "\"name\":\"java.lang:type\\u003dMemory.HeapMemoryUsage[max]\"," + "\"category\":\"\"}]", r.getPropertyValue(WidgetResourceProvider.WIDGET_METRICS_PROPERTY_ID)); Assert.assertEquals("[{\"name\":\"NameNode Heap\"," + "\"value\":\"${java.lang:type\\u003dMemory.HeapMemoryUsage[used] / " + "java.lang:type\\u003dMemory.HeapMemoryUsage[max]}\"}]", r.getPropertyValue(WidgetResourceProvider.WIDGET_VALUES_PROPERTY_ID)); Assert.assertEquals("{\"name\":\"value\"}", r.getPropertyValue(WidgetResourceProvider.WIDGET_PROPERTIES_PROPERTY_ID)); }
@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); String author = AuthorizationHelper.getAuthenticatedName(); List<WidgetEntity> requestedEntities = new ArrayList<WidgetEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { if (propertyMap.get(WIDGET_ID_PROPERTY_ID) != null) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("WidgetLayout with id " + id + " does not exists"); } if (!(entity.getAuthor().equals(author) || entity.getScope().equals(SCOPE.CLUSTER.name()))) { throw new AccessDeniedException("User must be author of the widget or widget must have cluster scope"); } requestedEntities.add(entity); } else { requestedEntities.addAll(widgetDAO.findByScopeOrAuthor(author, SCOPE.CLUSTER.name())); } } for (WidgetEntity entity: requestedEntities) { final Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, entity.getId()); resource.setProperty(WIDGET_WIDGET_NAME_PROPERTY_ID, entity.getWidgetName()); resource.setProperty(WIDGET_WIDGET_TYPE_PROPERTY_ID, entity.getWidgetType()); setResourceProperty(resource, WIDGET_METRICS_PROPERTY_ID, entity.getMetrics(), requestedIds); setResourceProperty(resource, WIDGET_TIME_CREATED_PROPERTY_ID, entity.getTimeCreated(), requestedIds); resource.setProperty(WIDGET_AUTHOR_PROPERTY_ID, entity.getAuthor()); setResourceProperty(resource, WIDGET_DESCRIPTION_PROPERTY_ID, entity.getDescription(), requestedIds); resource.setProperty(WIDGET_SCOPE_PROPERTY_ID, entity.getScope()); setResourceProperty(resource, WIDGET_VALUES_PROPERTY_ID, entity.getWidgetValues(), requestedIds); setResourceProperty(resource, WIDGET_PROPERTIES_PROPERTY_ID, entity.getProperties(), requestedIds); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(entity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } setResourceProperty(resource, WIDGET_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); resources.add(resource); } return resources; }
WidgetResourceProvider 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); String author = AuthorizationHelper.getAuthenticatedName(); List<WidgetEntity> requestedEntities = new ArrayList<WidgetEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { if (propertyMap.get(WIDGET_ID_PROPERTY_ID) != null) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("WidgetLayout with id " + id + " does not exists"); } if (!(entity.getAuthor().equals(author) || entity.getScope().equals(SCOPE.CLUSTER.name()))) { throw new AccessDeniedException("User must be author of the widget or widget must have cluster scope"); } requestedEntities.add(entity); } else { requestedEntities.addAll(widgetDAO.findByScopeOrAuthor(author, SCOPE.CLUSTER.name())); } } for (WidgetEntity entity: requestedEntities) { final Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, entity.getId()); resource.setProperty(WIDGET_WIDGET_NAME_PROPERTY_ID, entity.getWidgetName()); resource.setProperty(WIDGET_WIDGET_TYPE_PROPERTY_ID, entity.getWidgetType()); setResourceProperty(resource, WIDGET_METRICS_PROPERTY_ID, entity.getMetrics(), requestedIds); setResourceProperty(resource, WIDGET_TIME_CREATED_PROPERTY_ID, entity.getTimeCreated(), requestedIds); resource.setProperty(WIDGET_AUTHOR_PROPERTY_ID, entity.getAuthor()); setResourceProperty(resource, WIDGET_DESCRIPTION_PROPERTY_ID, entity.getDescription(), requestedIds); resource.setProperty(WIDGET_SCOPE_PROPERTY_ID, entity.getScope()); setResourceProperty(resource, WIDGET_VALUES_PROPERTY_ID, entity.getWidgetValues(), requestedIds); setResourceProperty(resource, WIDGET_PROPERTIES_PROPERTY_ID, entity.getProperties(), requestedIds); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(entity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } setResourceProperty(resource, WIDGET_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); resources.add(resource); } return resources; } }
WidgetResourceProvider 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); String author = AuthorizationHelper.getAuthenticatedName(); List<WidgetEntity> requestedEntities = new ArrayList<WidgetEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { if (propertyMap.get(WIDGET_ID_PROPERTY_ID) != null) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("WidgetLayout with id " + id + " does not exists"); } if (!(entity.getAuthor().equals(author) || entity.getScope().equals(SCOPE.CLUSTER.name()))) { throw new AccessDeniedException("User must be author of the widget or widget must have cluster scope"); } requestedEntities.add(entity); } else { requestedEntities.addAll(widgetDAO.findByScopeOrAuthor(author, SCOPE.CLUSTER.name())); } } for (WidgetEntity entity: requestedEntities) { final Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, entity.getId()); resource.setProperty(WIDGET_WIDGET_NAME_PROPERTY_ID, entity.getWidgetName()); resource.setProperty(WIDGET_WIDGET_TYPE_PROPERTY_ID, entity.getWidgetType()); setResourceProperty(resource, WIDGET_METRICS_PROPERTY_ID, entity.getMetrics(), requestedIds); setResourceProperty(resource, WIDGET_TIME_CREATED_PROPERTY_ID, entity.getTimeCreated(), requestedIds); resource.setProperty(WIDGET_AUTHOR_PROPERTY_ID, entity.getAuthor()); setResourceProperty(resource, WIDGET_DESCRIPTION_PROPERTY_ID, entity.getDescription(), requestedIds); resource.setProperty(WIDGET_SCOPE_PROPERTY_ID, entity.getScope()); setResourceProperty(resource, WIDGET_VALUES_PROPERTY_ID, entity.getWidgetValues(), requestedIds); setResourceProperty(resource, WIDGET_PROPERTIES_PROPERTY_ID, entity.getProperties(), requestedIds); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(entity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } setResourceProperty(resource, WIDGET_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); resources.add(resource); } return resources; } WidgetResourceProvider(AmbariManagementController managementController); }
WidgetResourceProvider 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); String author = AuthorizationHelper.getAuthenticatedName(); List<WidgetEntity> requestedEntities = new ArrayList<WidgetEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { if (propertyMap.get(WIDGET_ID_PROPERTY_ID) != null) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("WidgetLayout with id " + id + " does not exists"); } if (!(entity.getAuthor().equals(author) || entity.getScope().equals(SCOPE.CLUSTER.name()))) { throw new AccessDeniedException("User must be author of the widget or widget must have cluster scope"); } requestedEntities.add(entity); } else { requestedEntities.addAll(widgetDAO.findByScopeOrAuthor(author, SCOPE.CLUSTER.name())); } } for (WidgetEntity entity: requestedEntities) { final Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, entity.getId()); resource.setProperty(WIDGET_WIDGET_NAME_PROPERTY_ID, entity.getWidgetName()); resource.setProperty(WIDGET_WIDGET_TYPE_PROPERTY_ID, entity.getWidgetType()); setResourceProperty(resource, WIDGET_METRICS_PROPERTY_ID, entity.getMetrics(), requestedIds); setResourceProperty(resource, WIDGET_TIME_CREATED_PROPERTY_ID, entity.getTimeCreated(), requestedIds); resource.setProperty(WIDGET_AUTHOR_PROPERTY_ID, entity.getAuthor()); setResourceProperty(resource, WIDGET_DESCRIPTION_PROPERTY_ID, entity.getDescription(), requestedIds); resource.setProperty(WIDGET_SCOPE_PROPERTY_ID, entity.getScope()); setResourceProperty(resource, WIDGET_VALUES_PROPERTY_ID, entity.getWidgetValues(), requestedIds); setResourceProperty(resource, WIDGET_PROPERTIES_PROPERTY_ID, entity.getProperties(), requestedIds); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(entity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } setResourceProperty(resource, WIDGET_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); resources.add(resource); } return resources; } 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); }
WidgetResourceProvider 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); String author = AuthorizationHelper.getAuthenticatedName(); List<WidgetEntity> requestedEntities = new ArrayList<WidgetEntity>(); for (Map<String, Object> propertyMap: propertyMaps) { if (propertyMap.get(WIDGET_ID_PROPERTY_ID) != null) { final Long id; try { id = Long.parseLong(propertyMap.get(WIDGET_ID_PROPERTY_ID).toString()); } catch (Exception ex) { throw new SystemException("WidgetLayout should have numerical id"); } final WidgetEntity entity = widgetDAO.findById(id); if (entity == null) { throw new NoSuchResourceException("WidgetLayout with id " + id + " does not exists"); } if (!(entity.getAuthor().equals(author) || entity.getScope().equals(SCOPE.CLUSTER.name()))) { throw new AccessDeniedException("User must be author of the widget or widget must have cluster scope"); } requestedEntities.add(entity); } else { requestedEntities.addAll(widgetDAO.findByScopeOrAuthor(author, SCOPE.CLUSTER.name())); } } for (WidgetEntity entity: requestedEntities) { final Resource resource = new ResourceImpl(Type.Widget); resource.setProperty(WIDGET_ID_PROPERTY_ID, entity.getId()); resource.setProperty(WIDGET_WIDGET_NAME_PROPERTY_ID, entity.getWidgetName()); resource.setProperty(WIDGET_WIDGET_TYPE_PROPERTY_ID, entity.getWidgetType()); setResourceProperty(resource, WIDGET_METRICS_PROPERTY_ID, entity.getMetrics(), requestedIds); setResourceProperty(resource, WIDGET_TIME_CREATED_PROPERTY_ID, entity.getTimeCreated(), requestedIds); resource.setProperty(WIDGET_AUTHOR_PROPERTY_ID, entity.getAuthor()); setResourceProperty(resource, WIDGET_DESCRIPTION_PROPERTY_ID, entity.getDescription(), requestedIds); resource.setProperty(WIDGET_SCOPE_PROPERTY_ID, entity.getScope()); setResourceProperty(resource, WIDGET_VALUES_PROPERTY_ID, entity.getWidgetValues(), requestedIds); setResourceProperty(resource, WIDGET_PROPERTIES_PROPERTY_ID, entity.getProperties(), requestedIds); String clusterName = null; try { clusterName = getManagementController().getClusters().getClusterById(entity.getClusterId()).getClusterName(); } catch (AmbariException e) { throw new SystemException(e.getMessage()); } setResourceProperty(resource, WIDGET_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); resources.add(resource); } return resources; } 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; }