### Question:
StackAdvisorCommand extends BaseService { protected void populateStackHierarchy(ObjectNode root) { ObjectNode version = (ObjectNode) root.get("Versions"); TextNode stackName = (TextNode) version.get("stack_name"); TextNode stackVersion = (TextNode) version.get("stack_version"); ObjectNode stackHierarchy = version.putObject("stack_hierarchy"); stackHierarchy.put("stack_name", stackName); ArrayNode parents = stackHierarchy.putArray("stack_versions"); for (String parentVersion : metaInfo.getStackParentVersions(stackName.asText(), stackVersion.asText())) { parents.add(parentVersion); } } @SuppressWarnings("unchecked")  StackAdvisorCommand(File recommendationsDir, String recommendationsArtifactsLifetime, ServiceInfo.ServiceAdvisorType serviceAdvisorType, int requestId,
                             StackAdvisorRunner saRunner, AmbariMetaInfo metaInfo, AmbariServerConfigurationHandler ambariServerConfigurationHandler); synchronized T invoke(StackAdvisorRequest request, ServiceInfo.ServiceAdvisorType serviceAdvisorType);  }### Answer:
@Test public void testPopulateStackHierarchy() throws Exception { File file = mock(File.class); String recommendationsArtifactsLifetime = "1w"; StackAdvisorRunner stackAdvisorRunner = mock(StackAdvisorRunner.class); AmbariMetaInfo ambariMetaInfo = mock(AmbariMetaInfo.class); StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, recommendationsArtifactsLifetime, ServiceInfo.ServiceAdvisorType.PYTHON, 1, stackAdvisorRunner, ambariMetaInfo); ObjectNode objectNode = (ObjectNode) cmd.mapper.readTree("{\"Versions\": " + "{\"stack_name\": \"stack\", \"stack_version\":\"1.0.0\"}}"); doReturn(Arrays.asList("0.9", "0.8")).when(ambariMetaInfo).getStackParentVersions("stack", "1.0.0"); cmd.populateStackHierarchy(objectNode); JsonNode stackHierarchy = objectNode.get("Versions").get("stack_hierarchy"); assertNotNull(stackHierarchy); JsonNode stackName = stackHierarchy.get("stack_name"); assertNotNull(stackName); assertEquals("stack", stackName.asText()); ArrayNode stackVersions = (ArrayNode) stackHierarchy.get("stack_versions"); assertNotNull(stackVersions); assertEquals(2, stackVersions.size()); Iterator<JsonNode> stackVersionsElements = stackVersions.getElements(); assertEquals("0.9", stackVersionsElements.next().asText()); assertEquals("0.8", stackVersionsElements.next().asText()); }
@Test public void testPopulateStackHierarchy_noParents() throws Exception { File file = mock(File.class); String recommendationsArtifactsLifetime = "1w"; StackAdvisorRunner stackAdvisorRunner = mock(StackAdvisorRunner.class); AmbariMetaInfo ambariMetaInfo = mock(AmbariMetaInfo.class); StackAdvisorCommand<TestResource> cmd = new TestStackAdvisorCommand(file, recommendationsArtifactsLifetime, ServiceInfo.ServiceAdvisorType.PYTHON, 1, stackAdvisorRunner, ambariMetaInfo); ObjectNode objectNode = (ObjectNode) cmd.mapper.readTree("{\"Versions\": " + "{\"stack_name\": \"stack\", \"stack_version\":\"1.0.0\"}}"); doReturn(Collections.emptyList()).when(ambariMetaInfo).getStackParentVersions("stack", "1.0.0"); cmd.populateStackHierarchy(objectNode); JsonNode stackHierarchy = objectNode.get("Versions").get("stack_hierarchy"); assertNotNull(stackHierarchy); JsonNode stackName = stackHierarchy.get("stack_name"); assertNotNull(stackName); assertEquals("stack", stackName.asText()); ArrayNode stackVersions = (ArrayNode) stackHierarchy.get("stack_versions"); assertNotNull(stackVersions); assertEquals(0, stackVersions.size()); }