TaskResourceProvider extends AbstractControllerResourceProvider { Map<?, ?> parseStructuredOutput(String structuredOutput) { if (null == structuredOutput || structuredOutput.isEmpty()) { return null; } Map<?, ?> result = null; try { result = mapper.readValue(structuredOutput, Map.class); } catch (Exception excepton) { LOG.warn("Unable to parse task structured output: {}", structuredOutput); } return result; } 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 testParseStructuredOutput() { Resource.Type type = Resource.Type.Task; AmbariManagementController managementController = createMock(AmbariManagementController.class); TaskResourceProvider taskResourceProvider = new TaskResourceProvider( PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); replay(managementController); Map<?, ?> result = taskResourceProvider .parseStructuredOutput("{\"a\":\"b\", \"c\": {\"d\":\"e\",\"f\": [\"g\",\"h\"],\"i\": {\"k\":\"l\"}}}"); assertEquals(result.size(), 2); Map<?, ?> submap = (Map<?, ?>) result.get("c"); assertEquals(submap.size(), 3); List sublist = (List) submap.get("f"); assertEquals(sublist.size(), 2); Map<?, ?> subsubmap = (Map<?, ?>) submap.get("i"); assertEquals(subsubmap.size(), 1); assertEquals(subsubmap.get("k"), "l"); result = taskResourceProvider.parseStructuredOutput("{\"a\": invalid JSON}"); assertNull(result); result = taskResourceProvider.parseStructuredOutput("{\"a\": 5}"); assertEquals(result.get("a"), 5); verify(managementController); }
@Test public void testParseStructuredOutputForHostCheck() { Resource.Type type = Resource.Type.Task; AmbariManagementController managementController = createMock(AmbariManagementController.class); TaskResourceProvider taskResourceProvider = new TaskResourceProvider( PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); replay(managementController); Map<?, ?> result = taskResourceProvider.parseStructuredOutput("{\"host_resolution_check\": {\"failures\": [{\"cause\": [-2, \"Name or service not known\"], \"host\": \"foobar\", \"type\": \"FORWARD_LOOKUP\"}], \"message\": \"There were 1 host(s) that could not resolve to an IP address.\", \"failed_count\": 1, \"success_count\": 3, \"exit_code\": 0}}"); Assert.assertNotNull(result); Map<?,?> host_resolution_check = (Map<?,?>)result.get("host_resolution_check"); assertEquals(host_resolution_check.get("success_count"), 3); assertEquals(host_resolution_check.get("failed_count"), 1); verify(managementController); }
@Test public void testInvalidStructuredOutput() { Resource.Type type = Resource.Type.Task; AmbariManagementController managementController = createMock(AmbariManagementController.class); TaskResourceProvider taskResourceProvider = new TaskResourceProvider( PropertyHelper.getPropertyIds(type), PropertyHelper.getKeyPropertyIds(type), managementController); replay(managementController); Map<?, ?> result = taskResourceProvider.parseStructuredOutput(null); Assert.assertNull(result); result = taskResourceProvider.parseStructuredOutput("This is some bad JSON"); Assert.assertNull(result); verify(managementController); }