9cd9ebae82cf1eed52f6207b8cc916b7369b4189
[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 com.fasterxml.jackson.databind.JsonNode;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.onap.vid.aai.AaiClientInterface;
28 import org.onap.vid.model.aaiTree.AAITreeNode;
29 import org.onap.vid.utils.Unchecked;
30 import org.testng.Assert;
31 import org.testng.annotations.BeforeTest;
32 import org.testng.annotations.Test;
33
34 import java.io.IOException;
35 import java.util.HashMap;
36 import java.util.concurrent.ConcurrentLinkedQueue;
37 import java.util.concurrent.ConcurrentSkipListSet;
38 import java.util.concurrent.ThreadPoolExecutor;
39 import java.util.concurrent.atomic.AtomicInteger;
40
41 import static java.util.Comparator.comparing;
42 import static org.mockito.Mockito.when;
43 import static org.onap.vid.services.AAIServiceTree.AAI_TREE_PATHS;
44
45 public class AAITreeNodeBuilderTest {
46
47     AAITreeNodeBuilder aaiTreeNodeBuilder;
48
49     @Mock
50     AaiClientInterface aaiClientMock;
51
52     @Mock
53     ThreadPoolExecutor threadPoolMock;
54
55
56     @BeforeTest
57     public void initMocks() {
58         MockitoAnnotations.initMocks(this);
59         aaiTreeNodeBuilder = new AAITreeNodeBuilder(aaiClientMock);
60     }
61
62     @Test
63     public void buildNode_buildGroupNode_NodeIsAsExpected(){
64         ConcurrentSkipListSet<AAITreeNode> nodesAccumulator = new ConcurrentSkipListSet<>(comparing(AAITreeNode::getUniqueNodeKey));
65         ConcurrentLinkedQueue<String> visitedNodes = new ConcurrentLinkedQueue<>();
66         when(aaiClientMock.typedAaiGet(Unchecked.toURI("anyUrl"), JsonNode.class)).thenReturn(createGroupJson());
67
68         AAITreeNode groupNode = aaiTreeNodeBuilder.buildNode("instance-group",
69                 "anyUrl",
70                 nodesAccumulator,
71                 threadPoolMock,
72                 visitedNodes,
73                 new AtomicInteger(0),
74                 AAI_TREE_PATHS).get(0);
75
76         AAITreeNode expectedGroupNode = createExpectedGroupNode();
77         assertNodeIsAsExpected(expectedGroupNode,groupNode);
78     }
79
80     private void assertNodeIsAsExpected(AAITreeNode expectedGroupNode, AAITreeNode groupNode) {
81         Assert.assertEquals(groupNode.getId(), expectedGroupNode.getId());
82         Assert.assertEquals(groupNode.getType(), expectedGroupNode.getType());
83         Assert.assertEquals(groupNode.getName(), expectedGroupNode.getName());
84         Assert.assertEquals(groupNode.getModelVersionId(), expectedGroupNode.getModelVersionId());
85         Assert.assertEquals(groupNode.getModelInvariantId(), expectedGroupNode.getModelInvariantId());
86         Assert.assertEquals(groupNode.getInMaint(), expectedGroupNode.getInMaint());
87         Assert.assertEquals(groupNode.getAdditionalProperties(), expectedGroupNode.getAdditionalProperties());
88     }
89
90     private AAITreeNode createExpectedGroupNode() {
91         AAITreeNode expectedNode = new AAITreeNode();
92         expectedNode.setId("c4fcf022-31a0-470a-b5b8-c18335b7af32");
93         expectedNode.setType("instance-group");
94         expectedNode.setName("Test vE-Flex");
95         expectedNode.setModelVersionId("Test vE-Flex");
96         expectedNode.setModelInvariantId("dd182d7d-6949-4b90-b3cc-5befe400742e");
97         expectedNode.setInMaint(false);
98         HashMap<String,Object> additionalProperties = new HashMap<>();
99         additionalProperties.put("inMaint","false");
100         additionalProperties.put("description","Test vE-Flex instance-group");
101         additionalProperties.put("instance-group-type","ha");
102         additionalProperties.put("instance-group-role","test-IG-role");
103         additionalProperties.put("resource-version","1533315433086");
104         additionalProperties.put("instance-group-function","vTSBC Customer Landing Network Collection");
105         expectedNode.setAdditionalProperties(additionalProperties);
106
107
108         return expectedNode;
109     }
110
111     private JsonNode createGroupJson() {
112         ObjectMapper objectMapper = new ObjectMapper();
113         JsonNode groupNode = null;
114         try {
115             groupNode = objectMapper.readTree("" +
116                     "{" +
117                     "      \"id\": \"c4fcf022-31a0-470a-b5b8-c18335b7af32\"," +
118                     "      \"instance-group-role\": \"test-IG-role\"," +
119                     "      \"description\": \"Test vE-Flex instance-group\"," +
120                     "      \"instance-group-type\": \"ha\"," +
121                     "      \"resource-version\": \"1533315433086\"," +
122                     "      \"instance-group-name\": \"Test vE-Flex\"," +
123                     "      \"model-invariant-id\": \"dd182d7d-6949-4b90-b3cc-5befe400742e\"," +
124                     "      \"model-version-id\": \"Test vE-Flex\"," +
125                     "      \"inMaint\": \"false\"," +
126                     "      \"instance-group-function\": \"vTSBC Customer Landing Network Collection\"," +
127                     "      \"relationship-list\": {" +
128                     "      \"relationship\": [{" +
129                     "       \"related-to\": \"generic-vnf\"," +
130                     "       \"relationship-label\": \"org.onap.relationships.inventory.MemberOf\" ," +
131                     "       \"related-link\": \"/aai/v14/network/generic-vnfs/generic-vnf/8c54c369-2876-4423-9b33-80f783f29082\" ," +
132                     "       \"relationship-data\": [{" +
133                     "        \"relationship-key\": \"generic-vnf.vnf-id\"," +
134                     "        \"relationship-value\": \"8c54c369-2876-4423-9b33-80f783f29082\"" +
135                     "      }" +
136                     "    ]," +
137                     "      \"related-to-property\": [{" +
138                     "      \"property-key\": \"generic-vnf.vnf-name\"," +
139                     "      \"property-value\": \"zrdm5bffad01\"" +
140                     "    }" +
141                     "    ]" +
142                     "    }" +
143                     "    ]" +
144                     "    }" +
145                     "    }");
146         } catch (IOException e) {
147             e.printStackTrace();
148         }
149         return groupNode;
150     }
151 }