Merge changes from topics "VID-45", "VID-44"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AAITreeNodeBuilderTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.services;
22
23 import static java.util.Comparator.comparing;
24 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
25 import static net.javacrumbs.jsonunit.core.Option.IGNORING_ARRAY_ORDER;
26 import static net.javacrumbs.jsonunit.core.Option.IGNORING_EXTRA_FIELDS;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.empty;
29 import static org.hamcrest.core.Is.is;
30 import static org.junit.Assert.assertEquals;
31 import static org.mockito.Mockito.when;
32 import static org.onap.vid.services.AAIServiceTree.AAI_TREE_PATHS;
33 import static org.onap.vid.utils.KotlinUtilsKt.JACKSON_OBJECT_MAPPER;
34 import static org.testng.Assert.assertNull;
35 import static org.testng.Assert.assertTrue;
36
37 import com.fasterxml.jackson.databind.JsonNode;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import com.fasterxml.jackson.databind.node.ArrayNode;
40 import com.fasterxml.jackson.databind.node.ObjectNode;
41 import com.google.common.collect.ImmutableList;
42 import com.google.common.collect.ImmutableMap;
43 import com.google.common.util.concurrent.MoreExecutors;
44 import java.io.IOException;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Map;
48 import java.util.Optional;
49 import java.util.concurrent.ConcurrentSkipListSet;
50 import java.util.concurrent.ExecutorService;
51 import java.util.concurrent.Executors;
52 import org.apache.commons.lang3.tuple.Pair;
53 import org.jetbrains.annotations.NotNull;
54 import org.mockito.Mock;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.stubbing.Answer;
57 import org.onap.vid.aai.AaiClientInterface;
58 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Relationship;
59 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.RelationshipList;
60 import org.onap.vid.exceptions.GenericUncheckedException;
61 import org.onap.vid.model.aaiTree.AAITreeNode;
62 import org.onap.vid.model.aaiTree.NodeType;
63 import org.onap.vid.mso.model.CloudConfiguration;
64 import org.onap.vid.testUtils.TestUtils;
65 import org.onap.vid.utils.Logging;
66 import org.onap.vid.utils.Tree;
67 import org.onap.vid.utils.Unchecked;
68 import org.springframework.http.HttpMethod;
69 import org.testng.annotations.BeforeClass;
70 import org.testng.annotations.DataProvider;
71 import org.testng.annotations.Test;
72
73 public class AAITreeNodeBuilderTest {
74
75     private AAITreeNodeBuilder aaiTreeNodeBuilder;
76
77     @Mock
78     private AaiClientInterface aaiClientMock;
79
80     private ExecutorService executorService;
81     private Logging logging = new Logging();
82
83     private static final ObjectMapper mapper = new ObjectMapper();
84
85     @BeforeClass
86     public void initMocks() {
87         MockitoAnnotations.initMocks(this);
88         aaiTreeNodeBuilder = new AAITreeNodeBuilder(aaiClientMock, logging);
89         executorService = MoreExecutors.newDirectExecutorService();
90     }
91
92     private void buildNodeAndAssert(JsonNode inputNode, AAITreeNode expectedNode, NodeType nodeType){
93         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
94         when(aaiClientMock.typedAaiRest(Unchecked.toURI("anyUrl"), JsonNode.class, null, HttpMethod.GET, false)).thenReturn(inputNode);
95         AAITreeNode actualNode = aaiTreeNodeBuilder.buildNode(
96                 nodeType,
97                 "anyUrl",
98                 null,
99                 HttpMethod.GET,
100                 nodesAccumulator,
101                 executorService,
102                 AAI_TREE_PATHS.getSubTree(new AAIServiceTree.AaiRelationship(nodeType))
103         ).get(0);
104         assertThat(actualNode, jsonEquals(expectedNode).when(IGNORING_ARRAY_ORDER, IGNORING_EXTRA_FIELDS).whenIgnoringPaths("relationshipList","children[0].relationshipList"));
105     }
106
107     @Test
108     public void buildNode_buildGroupNode_NodeIsAsExpected() {
109         buildNodeAndAssert(createGroupJson(), createExpectedGroupNode(), NodeType.INSTANCE_GROUP);
110     }
111
112     private AAITreeNode createExpectedGroupNode() {
113         AAITreeNode expectedNode = new AAITreeNode();
114         expectedNode.setId("c4fcf022-31a0-470a-b5b8-c18335b7af32");
115         expectedNode.setType(NodeType.INSTANCE_GROUP);
116         expectedNode.setName("Test vE-Flex");
117         expectedNode.setModelVersionId("Test vE-Flex");
118         expectedNode.setModelInvariantId("dd182d7d-6949-4b90-b3cc-5befe400742e");
119         expectedNode.setInMaint(false);
120         HashMap<String,Object> additionalProperties = new HashMap<>();
121         additionalProperties.put("inMaint","false");
122         additionalProperties.put("description","Test vE-Flex instance-group");
123         additionalProperties.put("instance-group-type","ha");
124         additionalProperties.put("instance-group-role","test-IG-role");
125         additionalProperties.put("resource-version","1533315433086");
126         additionalProperties.put("instance-group-function","vTSBC Customer Landing Network Collection");
127         expectedNode.setAdditionalProperties(additionalProperties);
128         return expectedNode;
129     }
130
131     private JsonNode createGroupJson() {
132         ObjectMapper objectMapper = new ObjectMapper();
133         JsonNode groupNode = null;
134         try {
135             groupNode = objectMapper.readTree("" +
136                     "{" +
137                     "      \"id\": \"c4fcf022-31a0-470a-b5b8-c18335b7af32\"," +
138                     "      \"instance-group-role\": \"test-IG-role\"," +
139                     "      \"description\": \"Test vE-Flex instance-group\"," +
140                     "      \"instance-group-type\": \"ha\"," +
141                     "      \"resource-version\": \"1533315433086\"," +
142                     "      \"instance-group-name\": \"Test vE-Flex\"," +
143                     "      \"model-invariant-id\": \"dd182d7d-6949-4b90-b3cc-5befe400742e\"," +
144                     "      \"model-version-id\": \"Test vE-Flex\"," +
145                     "      \"inMaint\": \"false\"," +
146                     "      \"instance-group-function\": \"vTSBC Customer Landing Network Collection\"," +
147                     "      \"relationship-list\": {" +
148                     "      \"relationship\": []" +
149                     "    }" +
150                     "    }");
151         } catch (IOException e) {
152             e.printStackTrace();
153         }
154         return groupNode;
155     }
156
157     @Test
158     public void whenReadNetworkNode_thenNodeIsAsExpected() throws IOException {
159         JsonNode mockedAaiResponse = TestUtils.readJsonResourceFileAsObject("/getTopology/network.json", JsonNode.class);
160
161         AAITreeNode expectedNetworkNode = new AAITreeNode();
162         expectedNetworkNode.setId("94c86b39-bbbf-4027-8120-ff37c6d2493a");
163         expectedNetworkNode.setName("AUK51a_oam_calea_net_1");
164         expectedNetworkNode.setOrchestrationStatus("Assigned");
165         expectedNetworkNode.setModelInvariantId("b9a9b549-0ee4-49fc-b4f2-5edc6701da68");
166         expectedNetworkNode.setModelVersionId("77010093-df36-4dcb-8428-c3d02bf3f88d");
167         expectedNetworkNode.setModelCustomizationId("e5f33853-f84c-4cdd-99f2-93846957aa18");
168         expectedNetworkNode.setType(NodeType.NETWORK);
169         expectedNetworkNode.setCloudConfiguration(new CloudConfiguration("auk51a", "b530fc990b6d4334bd45518bebca6a51", "att-nc"));
170
171         buildNodeAndAssert(mockedAaiResponse, expectedNetworkNode, NodeType.NETWORK);
172     }
173
174     @Test
175     public void whenCloudRegionMissing_otherPlacementFieldsReadAsExpected() throws IOException {
176
177         AAITreeNode node = new AAITreeNode();
178         Optional<Relationship> tenantRelationShip = Optional.of(
179                 JACKSON_OBJECT_MAPPER.readValue("{" +
180                         "      \"related-to\": \"tenant\"," +
181                         "      \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
182                         "      \"related-link\": \"/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/att-nc/auk51a/tenants/tenant/b530fc990b6d4334bd45518bebca6a51\"," +
183                         "      \"relationship-data\": [{" +
184                         "        \"relationship-key\": \"cloud-region.cloud-owner\"," +
185                         "        \"relationship-value\": \"att-nc\"" +
186                         "      }, {" +
187                         "        \"relationship-key\": \"tenant.tenant-id\"," +
188                         "        \"relationship-value\": \"b530fc990b6d4334bd45518bebca6a51\"" +
189                         "      }" +
190                         "      ]," +
191                         "      \"related-to-property\": [{" +
192                         "        \"property-key\": \"tenant.tenant-name\"," +
193                         "        \"property-value\": \"ecomp_ispt\"" +
194                         "      }" +
195                         "      ]" +
196                         "    }", Relationship.class)
197         );
198         aaiTreeNodeBuilder.enrichPlacementDataUsingTenantInfo(node, tenantRelationShip);
199         assertEquals(new CloudConfiguration(null, "b530fc990b6d4334bd45518bebca6a51", "att-nc"), node.getCloudConfiguration());
200     }
201
202     @Test
203     public void whenTenantMissing_otherPlacementFieldsReadAsExpected() throws IOException {
204
205         AAITreeNode node = new AAITreeNode();
206         Optional<Relationship> tenantRelationShip = Optional.of(
207                 JACKSON_OBJECT_MAPPER.readValue("{" +
208                         "      \"related-to\": \"tenant\"," +
209                         "      \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
210                         "      \"related-link\": \"/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/att-nc/auk51a/tenants/tenant/b530fc990b6d4334bd45518bebca6a51\"," +
211                         "      \"relationship-data\": [{" +
212                         "        \"relationship-key\": \"cloud-region.cloud-owner\"," +
213                         "        \"relationship-value\": \"att-nc\"" +
214                         "      }, {" +
215                         "        \"relationship-key\": \"cloud-region.cloud-region-id\"," +
216                         "        \"relationship-value\": \"auk51a\"" +
217                         "      }" +
218                         "      ]," +
219                         "      \"related-to-property\": [{" +
220                         "        \"property-key\": \"tenant.tenant-name\"," +
221                         "        \"property-value\": \"ecomp_ispt\"" +
222                         "      }" +
223                         "      ]" +
224                         "    }", Relationship.class)
225         );
226         aaiTreeNodeBuilder.enrichPlacementDataUsingTenantInfo(node, tenantRelationShip);
227         assertEquals(new CloudConfiguration("auk51a", null, "att-nc"), node.getCloudConfiguration());
228     }
229
230     @Test
231     public void whenCloudOwnerMissing_otherPlacementFieldsReadAsExpected() throws IOException {
232
233         AAITreeNode node = new AAITreeNode();
234         Optional<Relationship> tenantRelationShip = Optional.of(
235                 JACKSON_OBJECT_MAPPER.readValue("{" +
236                         "      \"related-to\": \"tenant\"," +
237                         "      \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
238                         "      \"related-link\": \"/aai/v14/cloud-infrastructure/cloud-regions/cloud-region/att-nc/auk51a/tenants/tenant/b530fc990b6d4334bd45518bebca6a51\"," +
239                         "      \"relationship-data\": [{" +
240                         "        \"relationship-key\": \"tenant.tenant-id\"," +
241                         "        \"relationship-value\": \"b530fc990b6d4334bd45518bebca6a51\"" +
242                         "      }, {" +
243                         "        \"relationship-key\": \"cloud-region.cloud-region-id\"," +
244                         "        \"relationship-value\": \"auk51a\"" +
245                         "      }" +
246                         "      ]," +
247                         "      \"related-to-property\": [{" +
248                         "        \"property-key\": \"tenant.tenant-name\"," +
249                         "        \"property-value\": \"ecomp_ispt\"" +
250                         "      }" +
251                         "      ]" +
252                         "    }", Relationship.class)
253         );
254         aaiTreeNodeBuilder.enrichPlacementDataUsingTenantInfo(node, tenantRelationShip);
255         assertEquals(new CloudConfiguration("auk51a", "b530fc990b6d4334bd45518bebca6a51",  null), node.getCloudConfiguration());
256     }
257
258     @Test
259     public void whenThereIsNoTenantRelationship_thenPlacementIsNull() throws IOException {
260         AAITreeNode node = new AAITreeNode();
261         aaiTreeNodeBuilder.enrichPlacementData(node);
262         assertNull(node.getCloudConfiguration());
263     }
264
265
266     @Test
267     public void whenReadVnfNodeWithVfModule_thenNodeIsAsExpected() throws IOException {
268         JsonNode mockedAaiGetVnfResponse = TestUtils.readJsonResourceFileAsObject("/getTopology/vnf.json", JsonNode.class);
269
270         //add mock for vfModule of the VNF
271         JsonNode mockedAaiGetVfModuleResponse = TestUtils.readJsonResourceFileAsObject("/getTopology/vfModule.json", JsonNode.class);
272         when(aaiClientMock.typedAaiGet(Unchecked.toURI("anyUrl/vf-modules"), JsonNode.class)).thenReturn(mockedAaiGetVfModuleResponse);
273
274         CloudConfiguration expectedCloudConfiguration = new CloudConfiguration("dyh3b", "c8035f5ee95d4c62bbc8074c044122b9", "irma-aic");
275
276         AAITreeNode expectedVnfNode = createExpectedVnfTreeNode(expectedCloudConfiguration);
277
278         AAITreeNode expectedVfModule = new AAITreeNode();
279         expectedVfModule.setId("2cb6d41e-2bef-4cb2-80ce-c7815bcdcf4e");
280         expectedVfModule.setName("dyh3brarf8000v_base");
281         expectedVfModule.setOrchestrationStatus("Active");
282         expectedVfModule.setModelInvariantId("3ecca473-b0c0-46ae-b0b7-bd2969d8b79f");
283         expectedVfModule.setModelVersionId("5c35b764-e266-4498-af87-a88c4ba92dc4");
284         expectedVfModule.setModelCustomizationId("06b4ece0-f6f8-4003-b445-653418292101");
285         expectedVfModule.setType(NodeType.VF_MODULE);
286         expectedVfModule.setInMaint(false);
287         expectedVfModule.setCloudConfiguration(expectedCloudConfiguration);
288
289         expectedVnfNode.addChildren(ImmutableList.of(expectedVfModule));
290
291         buildNodeAndAssert(mockedAaiGetVnfResponse, expectedVnfNode, NodeType.GENERIC_VNF);
292     }
293
294     @NotNull
295     public static AAITreeNode createExpectedVnfTreeNode(CloudConfiguration expectedCloudConfiguration) {
296         AAITreeNode expectedVnfNode = new AAITreeNode();
297         expectedVnfNode.setId("9a7a4dc1-8e5f-43fe-a360-7734c5f51382");
298         expectedVnfNode.setName("dyh3brarf8000v");
299         expectedVnfNode.setOrchestrationStatus("Active");
300         expectedVnfNode.setModelInvariantId("b711997f-36b3-4a9b-8b37-71a0fc2ebd6d");
301         expectedVnfNode.setModelVersionId("7f23e4f7-e44c-44df-b066-4cedc6950bfe");
302         expectedVnfNode.setModelCustomizationId("401350be-0f56-481c-86d8-f32d573fec26");
303         expectedVnfNode.setType(NodeType.GENERIC_VNF);
304         expectedVnfNode.setInMaint(true);
305         expectedVnfNode.setProvStatus("PREPROV");
306         expectedVnfNode.setCloudConfiguration(expectedCloudConfiguration);
307         return expectedVnfNode;
308     }
309
310     @DataProvider
311     public static Object[][] isArrayDataProvider() {
312         return new Object[][] {
313                 {"Json Array", buildArrayJson(NodeType.GENERIC_VNF), true},
314                 {"Json Object", buildOneLevelJson(NodeType.GENERIC_VNF), false},
315                 {"Json Array with another node type", buildArrayJson(NodeType.SERVICE_INSTANCE), false},
316                 {"null json", null, false}
317         };
318     }
319
320     @Test(dataProvider = "isArrayDataProvider")
321     public void IsArrayType(String description, JsonNode jsonNode, boolean expectedResult) {
322         boolean isArray = aaiTreeNodeBuilder.isArray(jsonNode, NodeType.GENERIC_VNF);
323         assertEquals(expectedResult, isArray);
324     }
325
326     @Test
327     public void jsonToAaiNodeTest() {
328         NodeType nodeType = NodeType.SERVICE_INSTANCE;
329         JsonNode node = buildOneLevelJson(nodeType);
330         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
331
332         AAITreeNode aaiTreeNode = aaiTreeNodeBuilder.createAaiNode(nodeType, node, nodesAccumulator);
333
334         assertEquals("any-instance-id", aaiTreeNode.getId());
335         assertEquals("any-instance-name", aaiTreeNode.getName());
336         assertTrue(nodesAccumulator.contains(aaiTreeNode));
337     }
338
339     @Test
340     public void getNextLevelInPathsTreeTest() {
341         Tree<AAIServiceTree.AaiRelationship> firstLevelTree = getPathsTree();
342
343         Tree<AAIServiceTree.AaiRelationship> secondLevelTree = aaiTreeNodeBuilder.getNextLevelInPathsTree(firstLevelTree, NodeType.GENERIC_VNF.getType());
344         assertEquals(NodeType.GENERIC_VNF.getType(), secondLevelTree.getRootValue().type);
345
346         Tree<AAIServiceTree.AaiRelationship> thirdLevelTree = aaiTreeNodeBuilder.getNextLevelInPathsTree(secondLevelTree, NodeType.INSTANCE_GROUP.getType());
347         assertEquals(NodeType.INSTANCE_GROUP.getType(), thirdLevelTree.getRootValue().type);
348     }
349
350     @Test
351     public void getNextLevelInPathsTreeTest_givenIrrelevantNode_expectedNull() {
352         Tree<AAIServiceTree.AaiRelationship> pathsTree = getPathsTree();
353
354         Tree<AAIServiceTree.AaiRelationship> subTree = aaiTreeNodeBuilder.getNextLevelInPathsTree(pathsTree, NodeType.INSTANCE_GROUP.getType());
355
356         assertNull(subTree);
357     }
358
359     @Test
360     public void getRelationships_given2Relationships_expect1filtered() {
361         NodeType firstRelationship = NodeType.GENERIC_VNF;
362         NodeType secondRelationship = NodeType.INSTANCE_GROUP;
363         JsonNode jsonNode = buildOneLevelJson(NodeType.SERVICE_INSTANCE, firstRelationship, secondRelationship);
364
365         List<Relationship> relationships = aaiTreeNodeBuilder.getFilteredRelationships(jsonNode, getPathsTree());
366
367         assertEquals(1, relationships.size());
368         assertEquals(firstRelationship.getType(), relationships.get(0).getRelatedTo());
369     }
370
371     @Test
372     public void getRelationships_givenNoRelationships_expectedEmptyListTest() {
373         JsonNode jsonNode = buildOneLevelJson(NodeType.SERVICE_INSTANCE);
374
375         List<Relationship> relationships = aaiTreeNodeBuilder.getFilteredRelationships(jsonNode, getPathsTree());
376
377         assertThat(relationships, is(empty()));
378     }
379
380     @Test
381     public void getRelationships_given2RelationshipsNotExistInTreePaths_expectAllFiltered() {
382         NodeType firstRelationship = NodeType.CONFIGURATION;
383         NodeType secondRelationship = NodeType.INSTANCE_GROUP;
384         JsonNode jsonNode = buildOneLevelJson(NodeType.SERVICE_INSTANCE, firstRelationship, secondRelationship);
385
386         List<Relationship> relationships = aaiTreeNodeBuilder.getFilteredRelationships(jsonNode, getPathsTree());
387
388         assertThat(relationships, is(empty()));
389     }
390
391     @Test
392     public void aggregateAllOtherPropertiesTest() {
393         NodeType nodeType = NodeType.SERVICE_INSTANCE;
394         JsonNode jsonNode = buildOneLevelJson(nodeType, NodeType.GENERIC_VNF, NodeType.GENERIC_VNF);
395         ((ObjectNode) jsonNode).put("nf-role", "any-value");
396
397         Map<String, Object> additionalProps = aaiTreeNodeBuilder.aggregateAllOtherProperties(jsonNode, nodeType);
398         assertThat(additionalProps, is(ImmutableMap.of(
399                 "nf-role", "any-value")));
400     }
401
402     @Test
403     public void parseNodeAndFilterRelationshipsTest() {
404         NodeType nodeType = NodeType.SERVICE_INSTANCE;
405         JsonNode jsonNode = buildOneLevelJson(NodeType.SERVICE_INSTANCE, NodeType.GENERIC_VNF, NodeType.NETWORK, NodeType.VF_MODULE);
406         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
407
408         Pair<AAITreeNode, List<Relationship>> resultNode = aaiTreeNodeBuilder.parseNodeAndFilterRelationships(jsonNode, nodeType,
409                 nodesAccumulator, getPathsTree());
410
411         assertEquals(nodeType, resultNode.getKey().getType());
412         assertEquals(2, resultNode.getValue().size());
413         assertEquals(NodeType.GENERIC_VNF.getType(), resultNode.getValue().get(0).getRelatedTo());
414         assertEquals(NodeType.NETWORK.getType(), resultNode.getValue().get(1).getRelatedTo());
415     }
416
417     @Test(expectedExceptions = GenericUncheckedException.class ,expectedExceptionsMessageRegExp = "AAI node fetching failed.")
418     public void fetchChildrenAsyncTest_given2children_expected1Ok1Timeout() {
419         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
420         ExecutorService threadPool = Executors.newFixedThreadPool(5);
421
422         AAITreeNode rootNode = createExpectedGroupNode();
423         JsonNode relationshipJson = getRelationships(NodeType.GENERIC_VNF, NodeType.NETWORK);
424         List<Relationship> relationships = mapper.convertValue(relationshipJson, RelationshipList.class).getRelationship();
425
426         when(aaiClientMock.typedAaiRest(Unchecked.toURI(relationships.get(0).getRelatedLink()), JsonNode.class, null, HttpMethod.GET, false))
427                 .thenReturn(buildOneLevelJson(NodeType.GENERIC_VNF));
428
429         when(aaiClientMock.typedAaiRest(Unchecked.toURI(relationships.get(1).getRelatedLink()), JsonNode.class, null, HttpMethod.GET, false))
430                 .thenAnswer((Answer<JsonNode>) invocation -> {
431                     Thread.sleep(2000);
432                     return buildOneLevelJson(NodeType.NETWORK);
433                 });
434
435         aaiTreeNodeBuilder.fetchChildrenAsync(threadPool, nodesAccumulator, rootNode, relationships, getPathsTree(), 1);
436
437         assertEquals(2, rootNode.getChildren().size());
438         assertEquals(NodeType.GENERIC_VNF, rootNode.getChildren().get(0).getType());
439         assertEquals(NodeType.NETWORK, rootNode.getChildren().get(1).getType());
440     }
441
442     @DataProvider
443     public Object[][] testIsListOfKeyResultsDataProvider() {
444         return new Object[][]{
445                 {"Node has results with several values",
446                         "{\"results\":[{\"l3-network\":{}},{\"l3-network\":{}},{\"l3-network\":{}}]}",
447                         true},
448                 {"Node has results with no values",
449                         "{\"results\":[]}",
450                         true},
451                 {"Node has results, but it isn't an array",
452                         "{\"results\":{\"some-field\":{}}}",
453                         false},
454                 {"Node doesn't have results",
455                         "{\"l3-network\":[{},{}]}",
456                         false},
457                 {"Node is null",
458                         "null",
459                         false},
460         };
461     }
462
463     @Test(dataProvider = "testIsListOfKeyResultsDataProvider")
464     public void testIsListOfKeyResults(String testCase, String input, boolean expectedResult) throws IOException {
465         assertEquals(testCase + ": " + input,
466                 expectedResult, aaiTreeNodeBuilder.isListOfKeyResults(new ObjectMapper().readTree(input)));
467     }
468
469     private Tree<AAIServiceTree.AaiRelationship> getPathsTree() {
470         Tree<AAIServiceTree.AaiRelationship> pathsTree = new Tree<>(new AAIServiceTree.AaiRelationship(NodeType.SERVICE_INSTANCE));
471         pathsTree.addPath(AAIServiceTree.toAaiRelationshipList(NodeType.GENERIC_VNF, NodeType.INSTANCE_GROUP));
472         pathsTree.addPath(AAIServiceTree.toAaiRelationshipList(NodeType.NETWORK));
473
474         return pathsTree;
475     }
476
477     private static JsonNode buildOneLevelJson(NodeType nodeType, NodeType...relationships) {
478         ObjectNode objectNode = mapper.createObjectNode();
479         objectNode.put(nodeType.getId(), "any-instance-id");
480         objectNode.put(nodeType.getName(), "any-instance-name");
481         if (relationships.length > 0 ) {
482             objectNode.putPOJO("relationship-list", getRelationships(relationships));
483         }
484         return objectNode;
485     }
486
487     private static JsonNode buildArrayJson(NodeType nodeType) {
488         ObjectNode objectNode = mapper.createObjectNode();
489         ArrayNode arrayNode = objectNode.putArray(nodeType.getType());
490         arrayNode.add(buildOneLevelJson(nodeType));
491         arrayNode.add(buildOneLevelJson(nodeType));
492
493         return objectNode;
494     }
495
496     private static JsonNode getRelationship(String nodeType) {
497         ObjectNode relationship = mapper.createObjectNode();
498         relationship.put("related-to", nodeType);
499         relationship.put("relationship-label", "org.onap.relationships.inventory.ComposedOf");
500         relationship.put("related-link", "/aai/v12/network/" + nodeType + "s/" + nodeType + "/cf6f60cd-808d-44e6-978b-c663e00dba8d");
501         return relationship;
502     }
503
504     private static JsonNode getRelationships(NodeType...nodeTypes) {
505         ObjectNode relationshipList = mapper.createObjectNode();
506         ArrayNode relationships = relationshipList.putArray("relationship");
507
508         for (NodeType nodeType: nodeTypes) {
509             relationships.add(getRelationship(nodeType.getType()));
510         }
511
512         return relationshipList;
513     }
514
515 }