TaskResourceProvider extends AbstractControllerResourceProvider { @Override public Set<Resource> getResources(Request request, Predicate predicate) throws SystemException, UnsupportedPropertyException, NoSuchResourceException, NoSuchParentResourceException { Set<Resource> results = new LinkedHashSet<Resource>(); Set<String> requestedIds = getRequestPropertyIds(request, predicate); List<HostRoleCommandEntity> entities = s_dao.findAll(request, predicate); String clusterName = null; Long requestId = null; for (Map<String, Object> propertyMap : getPropertyMaps(predicate)) { clusterName = (String) propertyMap.get(TASK_CLUSTER_NAME_PROPERTY_ID); String requestIdStr = (String) propertyMap.get(TASK_REQUEST_ID_PROPERTY_ID); requestId = Long.parseLong(requestIdStr); } Collection<HostRoleCommand> commands = new ArrayList<>(100); if (!entities.isEmpty()) { for (HostRoleCommandEntity entity : entities) { commands.add(s_hostRoleCommandFactory.createExisting(entity)); } } else { if (requestId != null) { commands.addAll(s_topologyManager.getTasks(requestId)); } } LOG.debug("Retrieved {} commands for request {}", commands.size(), request); for (HostRoleCommand hostRoleCommand : commands) { Resource resource = new ResourceImpl(Resource.Type.Task); if (null != clusterName) { setResourceProperty(resource, TASK_CLUSTER_NAME_PROPERTY_ID, clusterName, requestedIds); } setResourceProperty(resource, TASK_REQUEST_ID_PROPERTY_ID, hostRoleCommand.getRequestId(), requestedIds); setResourceProperty(resource, TASK_ID_PROPERTY_ID, hostRoleCommand.getTaskId(), requestedIds); setResourceProperty(resource, TASK_STAGE_ID_PROPERTY_ID, hostRoleCommand.getStageId(), requestedIds); setResourceProperty(resource, TASK_HOST_NAME_PROPERTY_ID, ensureHostname(hostRoleCommand.getHostName()), requestedIds); setResourceProperty(resource, TASK_ROLE_PROPERTY_ID, hostRoleCommand.getRole().toString(), requestedIds); setResourceProperty(resource, TASK_COMMAND_PROPERTY_ID, hostRoleCommand.getRoleCommand(), requestedIds); setResourceProperty(resource, TASK_STATUS_PROPERTY_ID, hostRoleCommand.getStatus(), requestedIds); setResourceProperty(resource, TASK_EXIT_CODE_PROPERTY_ID, hostRoleCommand.getExitCode(), requestedIds); setResourceProperty(resource, TASK_STDERR_PROPERTY_ID, hostRoleCommand.getStderr(), requestedIds); setResourceProperty(resource, TASK_STOUT_PROPERTY_ID, hostRoleCommand.getStdout(), requestedIds); setResourceProperty(resource, TASK_OUTPUTLOG_PROPERTY_ID, hostRoleCommand.getOutputLog(), requestedIds); setResourceProperty(resource, TASK_ERRORLOG_PROPERTY_ID, hostRoleCommand.getErrorLog(), requestedIds); setResourceProperty(resource, TASK_STRUCT_OUT_PROPERTY_ID, parseStructuredOutput(hostRoleCommand.getStructuredOut()), requestedIds); setResourceProperty(resource, TASK_START_TIME_PROPERTY_ID, hostRoleCommand.getStartTime(), requestedIds); setResourceProperty(resource, TASK_END_TIME_PROPERTY_ID, hostRoleCommand.getEndTime(), requestedIds); setResourceProperty(resource, TASK_ATTEMPT_CNT_PROPERTY_ID, hostRoleCommand.getAttemptCount(), requestedIds); if (hostRoleCommand.getCustomCommandName() != null) { setResourceProperty(resource, TASK_CUST_CMD_NAME_PROPERTY_ID, hostRoleCommand.getCustomCommandName(), requestedIds); } if (hostRoleCommand.getCommandDetail() == null) { setResourceProperty(resource, TASK_COMMAND_DET_PROPERTY_ID, String.format("%s %s", hostRoleCommand.getRole().toString(), hostRoleCommand.getRoleCommand()), requestedIds); } else { setResourceProperty(resource, TASK_COMMAND_DET_PROPERTY_ID, hostRoleCommand.getCommandDetail(), requestedIds); } results.add(resource); } return results; } TaskResourceProvider(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 request, Predicate predicate); @Override RequestStatus deleteResources(Request request, Predicate predicate); static final String TASK_CLUSTER_NAME_PROPERTY_ID; static final String TASK_REQUEST_ID_PROPERTY_ID; static final String TASK_ID_PROPERTY_ID; static final String TASK_STAGE_ID_PROPERTY_ID; static final String TASK_HOST_NAME_PROPERTY_ID; static final String TASK_ROLE_PROPERTY_ID; static final String TASK_COMMAND_PROPERTY_ID; static final String TASK_STATUS_PROPERTY_ID; static final String TASK_EXIT_CODE_PROPERTY_ID; static final String TASK_STDERR_PROPERTY_ID; static final String TASK_STOUT_PROPERTY_ID; static final String TASK_OUTPUTLOG_PROPERTY_ID; static final String TASK_ERRORLOG_PROPERTY_ID; static final String TASK_STRUCT_OUT_PROPERTY_ID; static final String TASK_START_TIME_PROPERTY_ID; static final String TASK_END_TIME_PROPERTY_ID; static final String TASK_ATTEMPT_CNT_PROPERTY_ID; static final String TASK_COMMAND_DET_PROPERTY_ID; static final String TASK_CUST_CMD_NAME_PROPERTY_ID; }
@Test public void testGetResources() throws Exception { Resource.Type type = Resource.Type.Task; AmbariManagementController amc = createMock(AmbariManagementController.class); HostRoleCommandDAO hostRoleCommandDAO = createMock(HostRoleCommandDAO.class); Injector m_injector = Guice.createInjector(new InMemoryDefaultTestModule()); TaskResourceProvider provider = (TaskResourceProvider) AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), amc); m_injector.injectMembers(provider); TaskResourceProvider.s_dao = hostRoleCommandDAO; List<HostRoleCommandEntity> entities = new ArrayList<>(); HostRoleCommandEntity hostRoleCommandEntity = new HostRoleCommandEntity(); hostRoleCommandEntity.setRequestId(100L); hostRoleCommandEntity.setTaskId(100L); hostRoleCommandEntity.setStageId(100L); hostRoleCommandEntity.setRole(Role.DATANODE); hostRoleCommandEntity.setCustomCommandName("customCommandName"); hostRoleCommandEntity.setCommandDetail("commandDetail"); entities.add(hostRoleCommandEntity); expect(hostRoleCommandDAO.findAll(EasyMock.anyObject(Request.class), EasyMock.anyObject(Predicate.class))).andReturn(entities).once(); replay(hostRoleCommandDAO); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(TaskResourceProvider.TASK_ID_PROPERTY_ID); propertyIds.add(TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID); propertyIds.add(TaskResourceProvider.TASK_COMMAND_DET_PROPERTY_ID); Predicate predicate = new PredicateBuilder().property(TaskResourceProvider.TASK_ID_PROPERTY_ID).equals("100"). and().property(TaskResourceProvider.TASK_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) { long taskId = (Long) resource.getPropertyValue(TaskResourceProvider.TASK_ID_PROPERTY_ID); Assert.assertEquals(100L, taskId); Assert.assertEquals(null, resource.getPropertyValue(TaskResourceProvider .TASK_CUST_CMD_NAME_PROPERTY_ID)); Assert.assertEquals("commandDetail", resource.getPropertyValue(TaskResourceProvider .TASK_COMMAND_DET_PROPERTY_ID)); } verify(hostRoleCommandDAO); }
@Test public void testGetResourcesForTopology() throws Exception { Resource.Type type = Resource.Type.Task; AmbariManagementController amc = createMock(AmbariManagementController.class); HostRoleCommandDAO hostRoleCommandDAO = createMock(HostRoleCommandDAO.class); TopologyManager topologyManager = createMock(TopologyManager.class); HostDAO hostDAO = createMock(HostDAO.class); ExecutionCommandDAO executionCommandDAO = createMock(ExecutionCommandDAO.class); ExecutionCommandWrapperFactory ecwFactory = createMock(ExecutionCommandWrapperFactory.class); Injector m_injector = Guice.createInjector(new InMemoryDefaultTestModule()); TaskResourceProvider provider = (TaskResourceProvider) AbstractControllerResourceProvider.getResourceProvider( type, PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), amc); m_injector.injectMembers(provider); TaskResourceProvider.s_dao = hostRoleCommandDAO; TaskResourceProvider.s_topologyManager = topologyManager; List<HostRoleCommandEntity> entities = new ArrayList<>(); List<HostRoleCommand> commands = new ArrayList<>(); HostRoleCommandEntity hostRoleCommandEntity = new HostRoleCommandEntity(); hostRoleCommandEntity.setRequestId(100L); hostRoleCommandEntity.setTaskId(100L); hostRoleCommandEntity.setStageId(100L); hostRoleCommandEntity.setRole(Role.DATANODE); hostRoleCommandEntity.setCustomCommandName("customCommandName"); hostRoleCommandEntity.setCommandDetail("commandDetail"); commands.add(new HostRoleCommand(hostRoleCommandEntity, hostDAO, executionCommandDAO, ecwFactory)); expect(hostRoleCommandDAO.findAll(EasyMock.anyObject(Request.class), EasyMock.anyObject(Predicate.class))).andReturn(entities).once(); expect(topologyManager.getTasks(EasyMock.anyLong())).andReturn(commands).once(); replay(hostRoleCommandDAO, topologyManager); Set<String> propertyIds = new HashSet<String>(); propertyIds.add(TaskResourceProvider.TASK_ID_PROPERTY_ID); propertyIds.add(TaskResourceProvider.TASK_REQUEST_ID_PROPERTY_ID); propertyIds.add(TaskResourceProvider.TASK_COMMAND_DET_PROPERTY_ID); Predicate predicate = new PredicateBuilder().property(TaskResourceProvider.TASK_ID_PROPERTY_ID).equals("100"). and().property(TaskResourceProvider.TASK_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) { long taskId = (Long) resource.getPropertyValue(TaskResourceProvider.TASK_ID_PROPERTY_ID); Assert.assertEquals(100L, taskId); Assert.assertEquals(null, resource.getPropertyValue(TaskResourceProvider .TASK_CUST_CMD_NAME_PROPERTY_ID)); Assert.assertEquals("commandDetail", resource.getPropertyValue(TaskResourceProvider .TASK_COMMAND_DET_PROPERTY_ID)); } verify(hostRoleCommandDAO, topologyManager); }