RequestResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<String> requestedIds = getRequestPropertyIds(request, predicate); Set<Resource> resources = new HashSet<Resource>(); String maxResultsRaw = request.getRequestInfoProperties().get(BaseRequest.PAGE_SIZE_PROPERTY_KEY); String ascOrderRaw = request.getRequestInfoProperties().get(BaseRequest.ASC_ORDER_PROPERTY_KEY); Integer maxResults = (maxResultsRaw == null ? null : Integer.parseInt(maxResultsRaw)); Boolean ascOrder = (ascOrderRaw == null ? null : Boolean.parseBoolean(ascOrderRaw)); if (null == predicate) { resources.addAll( getRequestResources(null, null, null, maxResults, ascOrder, requestedIds)); } else { for (Map<String, Object> properties : getPropertyMaps(predicate)) { String clusterName = (String) properties.get(REQUEST_CLUSTER_NAME_PROPERTY_ID); Long requestId = null; if (properties.get(REQUEST_ID_PROPERTY_ID) != null) { requestId = Long.valueOf((String) properties.get(REQUEST_ID_PROPERTY_ID)); } String requestStatus = null; if (properties.get(REQUEST_STATUS_PROPERTY_ID) != null) { requestStatus = (String) properties.get(REQUEST_STATUS_PROPERTY_ID); } resources.addAll(getRequestResources(clusterName, requestId, requestStatus, maxResults, ascOrder, requestedIds)); } } return resources; } RequestResourceProvider(Set<String> propertyIds,
                          Map<Resource.Type, String> keyPropertyIds,
                          AmbariManagementController managementController); @Override RequestStatus createResources(Request request); @Override Set<Resource> getResources(Request request, Predicate predicate); @Override RequestStatus updateResources(Request requestInfo, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String REQUEST_CLUSTER_NAME_PROPERTY_ID; static final String REQUEST_CLUSTER_ID_PROPERTY_ID; static final String REQUEST_ID_PROPERTY_ID; static final String REQUEST_SOURCE_SCHEDULE; static final String REQUEST_SOURCE_SCHEDULE_ID; static final String REQUEST_SOURCE_SCHEDULE_HREF; }
@Test public void testGetResourcesWithRequestInfo() throws Exception { Resource.Type type = Resource.Type.Request; expect(requestDAO.findByPks(Collections.<Long> emptyList(), true)).andReturn(Collections.<RequestEntity>emptyList()).anyTimes(); ActionManager actionManager = createNiceMock(ActionManager.class); Cluster cluster = createNiceMock(Cluster.class); expect(cluster.getClusterId()).andReturn(1L).anyTimes(); expect(cluster.getResourceId()).andReturn(4L).anyTimes(); Clusters clusters = createNiceMock(Clusters.class); expect(clusters.getCluster("foo_cluster")).andReturn(cluster).anyTimes(); AmbariManagementController managementController = createMock(AmbariManagementController.class); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); replay(managementController, clusters, cluster); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider(type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Map<String, String> requestInfoProperties = new HashMap<String, String>(); Request request; Predicate predicate = new PredicateBuilder() .property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID) .equals("foo_cluster") .and().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID) .equals(null) .and().property(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID) .equals(null) .toPredicate(); request = PropertyHelper.getReadRequest(new HashSet<String>(), requestInfoProperties, null, null, null); expect(requestDAO.findAllRequestIds(BaseRequest.DEFAULT_PAGE_SIZE, false, 1L)).andReturn(Collections.<Long> emptyList()).anyTimes(); replay(requestDAO); provider.getResources(request, predicate); verify(requestDAO); requestInfoProperties.put(BaseRequest.PAGE_SIZE_PROPERTY_KEY, "20"); request = PropertyHelper.getReadRequest(new HashSet<String>(), requestInfoProperties, null, null, null); provider.getResources(request, predicate); verify(requestDAO); reset(requestDAO); requestInfoProperties.put(BaseRequest.ASC_ORDER_PROPERTY_KEY, "true"); request = PropertyHelper.getReadRequest(new HashSet<String>(), requestInfoProperties, null, null, null); expect(requestDAO.findByPks(Collections.<Long> emptyList(), true)).andReturn(Collections.<RequestEntity>emptyList()).anyTimes(); expect(requestDAO.findAllRequestIds(BaseRequest.DEFAULT_PAGE_SIZE, true, 1L)).andReturn(Collections.<Long> emptyList()).anyTimes(); replay(requestDAO); provider.getResources(request, predicate); verify(requestDAO); }
@Test public void testGetResources() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)).anyTimes(); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, requestDAO, hrcDAO, requestMock); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); } verify(managementController, actionManager, requestDAO, hrcDAO); }
@Test public void testGetResourcesWithRequestSchedule() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); expect(requestMock.getRequestScheduleId()).andReturn(11L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)).anyTimes(); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, requestDAO, hrcDAO, requestMock); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_SOURCE_SCHEDULE); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(11L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_SOURCE_SCHEDULE_ID)); } verify(managementController, actionManager, requestDAO, hrcDAO); }
@Test public void testGetResourcesWithoutRequestSchedule() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); expect(requestMock.getRequestScheduleId()).andReturn(null).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)).anyTimes(); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, requestMock, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_SOURCE_SCHEDULE); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(null, resource.getPropertyValue(RequestResourceProvider.REQUEST_SOURCE_SCHEDULE)); } verify(managementController, actionManager, requestMock, requestDAO, hrcDAO); }
@Test public void testGetResourcesWithCluster() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); Clusters clusters = createNiceMock(Clusters.class); Cluster cluster = createNiceMock(Cluster.class); expect(cluster.getClusterId()).andReturn(50L).anyTimes(); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getClusterId()).andReturn(50L).anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster("c1")).andReturn(cluster).anyTimes(); expect(clusters.getCluster("bad-cluster")).andThrow(new AmbariException("bad cluster!")).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, clusters, cluster, requestMock, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); Predicate predicate = new PredicateBuilder(). property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("c1").and(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); } predicate = new PredicateBuilder(). property(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID).equals("bad-cluster").and(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); try { provider.getResources(request, predicate); } catch (NoSuchParentResourceException e) { e.printStackTrace(); } verify(managementController, actionManager, clusters, cluster, requestMock, requestDAO, hrcDAO); }
@Test public void testGetResourcesOrPredicate() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); RequestEntity requestMock1 = createNiceMock(RequestEntity.class); expect(requestMock1.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock1.getRequestId()).andReturn(101L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))). andReturn(Arrays.asList(requestMock, requestMock1)).anyTimes(); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, requestMock, requestMock1, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). or().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("101"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); for (Resource resource : resources) { long id = (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); Assert.assertTrue(id == 100L || id == 101L); } verify(managementController, actionManager, requestMock, requestMock1, requestDAO, hrcDAO); }
@Test public void testGetResourcesCompleted() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock0 = createNiceMock(RequestEntity.class); expect(requestMock0.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock0.getRequestId()).andReturn(100L).anyTimes(); RequestEntity requestMock1 = createNiceMock(RequestEntity.class); expect(requestMock1.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock1.getRequestId()).andReturn(101L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock0)); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock1)); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().completed(2)); }}).anyTimes(); replay(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").or(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("101").toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); for (Resource resource : resources) { long id = (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); Assert.assertTrue(id == 100L || id == 101L); Assert.assertEquals("COMPLETED", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(2, resource.getPropertyValue(RequestResourceProvider.REQUEST_TASK_CNT_ID)); Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID)); Assert.assertEquals(2, resource.getPropertyValue(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID)); Assert.assertEquals(100.0, resource.getPropertyValue(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID)); } verify(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); }
@Test public void testGetResourcesInProgress() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock0 = createNiceMock(RequestEntity.class); expect(requestMock0.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock0.getRequestId()).andReturn(100L).anyTimes(); RequestEntity requestMock1 = createNiceMock(RequestEntity.class); expect(requestMock1.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock1.getRequestId()).andReturn(101L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = EasyMock.newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock0)); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock1)); expect(hrcDAO.findAggregateCounts(100L)).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>() {{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1).pending(1)); }}).once(); expect(hrcDAO.findAggregateCounts(101L)).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1).queued(1)); }}).once(); replay(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_QUEUED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").or(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("101").toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); for (Resource resource : resources) { long id = (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); Assert.assertTrue(id == 100L || id == 101L); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(2, resource.getPropertyValue(RequestResourceProvider.REQUEST_TASK_CNT_ID)); Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID)); if (id == 100L) { Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_QUEUED_TASK_CNT_ID)); int progressPercent = ((Double) resource.getPropertyValue(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID)).intValue(); Assert.assertEquals(17, progressPercent); } else { Assert.assertEquals(1, resource.getPropertyValue(RequestResourceProvider.REQUEST_QUEUED_TASK_CNT_ID)); int progressPercent = ((Double) resource.getPropertyValue(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID)).intValue(); Assert.assertEquals(21, progressPercent); } Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID)); } verify(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); }
@Test public void testGetResourcesFailed() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); RequestEntity requestMock0 = createNiceMock(RequestEntity.class); expect(requestMock0.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock0.getRequestId()).andReturn(100L).anyTimes(); RequestEntity requestMock1 = createNiceMock(RequestEntity.class); expect(requestMock1.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock1.getRequestId()).andReturn(101L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = EasyMock.newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock0)); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Arrays.asList(requestMock1)); expect(hrcDAO.findAggregateCounts(100L)).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().failed(1).completed(1)); }}).once(); expect(hrcDAO.findAggregateCounts(101L)).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().aborted(1).timedout(1)); }}).once(); replay(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_ABORTED_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_TIMED_OUT_TASK_CNT_ID); propertyIds.add(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID); Predicate predicate = new PredicateBuilder().property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100").or(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("101").toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(2, resources.size()); for (Resource resource : resources) { long id = (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); Assert.assertTrue(id == 100L || id == 101L); Assert.assertEquals(2, resource.getPropertyValue(RequestResourceProvider.REQUEST_TASK_CNT_ID)); if (id == 100L) { Assert.assertEquals("FAILED", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(1, resource.getPropertyValue(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID)); Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_ABORTED_TASK_CNT_ID)); Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_TIMED_OUT_TASK_CNT_ID)); } else { Assert.assertEquals("TIMEDOUT", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(0, resource.getPropertyValue(RequestResourceProvider.REQUEST_FAILED_TASK_CNT_ID)); Assert.assertEquals(1, resource.getPropertyValue(RequestResourceProvider.REQUEST_ABORTED_TASK_CNT_ID)); Assert.assertEquals(1, resource.getPropertyValue(RequestResourceProvider.REQUEST_TIMED_OUT_TASK_CNT_ID)); } Assert.assertEquals(2, resource.getPropertyValue(RequestResourceProvider.REQUEST_COMPLETED_TASK_CNT_ID)); Assert.assertEquals(100.0, resource.getPropertyValue(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID)); } verify(managementController, actionManager, requestMock0, requestMock1, requestDAO, hrcDAO); }
@Test public void testGetResourcesWithoutCluster() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); Clusters clusters = createNiceMock(Clusters.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster(anyObject(String.class))).andReturn(null).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn(new HashMap<Long, HostRoleCommandStatusSummaryDTO>(){{ put(1L, HostRoleCommandStatusSummaryDTO.create().inProgress(1)); }}).anyTimes(); replay(managementController, actionManager, clusters, requestMock, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); Predicate predicate = new PredicateBuilder(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("IN_PROGRESS", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertNull(resource.getPropertyValue(RequestResourceProvider.REQUEST_CLUSTER_NAME_PROPERTY_ID)); } verify(managementController, actionManager, clusters, requestMock, requestDAO, hrcDAO); }
@Test public void testRequestStatusWithNoTasks() throws Exception { Resource.Type type = Resource.Type.Request; AmbariManagementController managementController = createMock(AmbariManagementController.class); ActionManager actionManager = createNiceMock(ActionManager.class); Clusters clusters = createNiceMock(Clusters.class); RequestEntity requestMock = createNiceMock(RequestEntity.class); expect(requestMock.getRequestContext()).andReturn("this is a context").anyTimes(); expect(requestMock.getRequestId()).andReturn(100L).anyTimes(); Capture<Collection<Long>> requestIdsCapture = newCapture(); expect(managementController.getActionManager()).andReturn(actionManager).anyTimes(); expect(managementController.getClusters()).andReturn(clusters).anyTimes(); expect(clusters.getCluster(anyObject(String.class))).andReturn(null).anyTimes(); expect(requestDAO.findByPks(capture(requestIdsCapture), eq(true))).andReturn(Collections.singletonList(requestMock)); expect(hrcDAO.findAggregateCounts((Long) anyObject())).andReturn( Collections.<Long, HostRoleCommandStatusSummaryDTO>emptyMap()).anyTimes(); replay(managementController, actionManager, clusters, requestMock, requestDAO, hrcDAO); ResourceProvider provider = AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(RequestResourceProvider.REQUEST_ID_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID); propertyIds.add(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID); Predicate predicate = new PredicateBuilder(). property(RequestResourceProvider.REQUEST_ID_PROPERTY_ID).equals("100"). toPredicate(); Request request = PropertyHelper.getReadRequest(propertyIds); Set<Resource> resources = provider.getResources(request, predicate); Assert.assertEquals(1, resources.size()); for (Resource resource : resources) { Assert.assertEquals(100L, (long) (Long) resource.getPropertyValue(RequestResourceProvider.REQUEST_ID_PROPERTY_ID)); Assert.assertEquals("COMPLETED", resource.getPropertyValue(RequestResourceProvider.REQUEST_STATUS_PROPERTY_ID)); Assert.assertEquals(100.0, resource.getPropertyValue(RequestResourceProvider.REQUEST_PROGRESS_PERCENT_ID)); } verify(managementController, actionManager, clusters, requestMock, requestDAO, hrcDAO); }