be195c89b153a04f7c1e2fcb01217ba7a12c8339
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AAIServiceTreeTest.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.Collections.emptyList;
24 import static java.util.Collections.emptyMap;
25 import static java.util.stream.Collectors.toList;
26 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.Matchers.any;
31 import static org.mockito.Mockito.mock;
32 import static org.mockito.Mockito.when;
33 import static org.onap.vid.services.AAITreeNodeBuilderTest.createExpectedVnfTreeNode;
34
35 import com.fasterxml.jackson.databind.JsonNode;
36 import com.google.common.collect.ImmutableList;
37 import com.google.common.collect.ImmutableMap;
38 import com.google.common.collect.Streams;
39 import com.google.common.util.concurrent.MoreExecutors;
40 import java.io.IOException;
41 import java.util.List;
42 import java.util.concurrent.ExecutorService;
43 import net.javacrumbs.jsonunit.core.Option;
44 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
45 import org.apache.commons.lang3.builder.ToStringStyle;
46 import org.jetbrains.annotations.NotNull;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.vid.aai.AaiClient;
51 import org.onap.vid.aai.util.AAITreeConverter;
52 import org.onap.vid.asdc.parser.ServiceModelInflator;
53 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
54 import org.onap.vid.model.ModelUtil;
55 import org.onap.vid.model.aaiTree.AAITreeNode;
56 import org.onap.vid.model.aaiTree.NodeType;
57 import org.onap.vid.mso.model.CloudConfiguration;
58 import org.onap.vid.testUtils.TestUtils;
59 import org.onap.vid.utils.Logging;
60 import org.onap.vid.utils.Unchecked;
61 import org.springframework.http.HttpMethod;
62 import org.testng.annotations.BeforeTest;
63 import org.testng.annotations.Test;
64
65 public class AAIServiceTreeTest {
66
67     @Mock
68     private VidService sdcService;
69     @Mock
70     private ServiceModelInflator serviceModelInflator;
71     @InjectMocks
72     private AAIServiceTree aaiServiceTree;
73
74     @BeforeTest
75     public void initMocks() {
76         MockitoAnnotations.initMocks(this);
77     }
78
79     private final static String nullString = "null placeholder";
80
81
82
83     @Test
84     public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {
85
86         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
87                 "version id a", new Names("name a", "key a"),
88                 "version id b", new Names("name b", "key b"),
89                 "version id c", new Names("name c", "key c")
90         ));
91
92         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
93         final ImmutableList<Names> expectedNames = ImmutableList.of(
94                 new Names("name a", "key a"),
95                 new Names("name b", "key b"),
96                 new Names("name c", "key c"));
97
98
99         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
100         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
101
102         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
103     }
104
105     @Test
106     public void enrichNodesWithModelCustomizationName_noNodes_noError() {
107
108         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
109                 "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
110         ));
111
112         aaiServiceTree.enrichNodesWithModelCustomizationName(emptyList(), null);
113     }
114
115     @Test
116     public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {
117
118         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());
119
120         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
121         final Names nullNames = new Names(nullString, nullString);
122         final ImmutableList<Names> expectedNames = ImmutableList.of(nullNames, nullNames, nullNames);
123
124         
125         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
126
127         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
128
129         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
130     }
131
132     @Test
133     public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {
134
135         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
136                 "version id Z", new Names("name Z", "key Z"),
137                 "version id d", new Names(null, "key d"),
138                 "version id c", new Names("name c", null),
139                 "version id a", new Names("name a", "key a")
140         ));
141
142         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString);
143         final ImmutableList<Names> expectedNames = ImmutableList.of(
144                 new Names("name a", "key a"),
145                 new Names(nullString, nullString),
146                 new Names("name c", nullString),
147                 new Names(nullString, "key d"),
148                 new Names(nullString, nullString)
149         );
150
151
152         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
153
154         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
155
156         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
157     }
158
159
160
161     @NotNull
162     private String[] toStringsArray(List<AAITreeNode> nodes) {
163         return toStrings(nodes).toArray(new String[] {});
164     }
165
166     @NotNull
167     private List<String> toStrings(List<AAITreeNode> nodes) {
168         return nodes.stream().map(n -> {
169             final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE);
170             reflectionToStringBuilder.setExcludeNullValues(true);
171             return reflectionToStringBuilder.toString();
172         }).collect(toList());
173     }
174
175     @NotNull
176     private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
177         return Streams
178                 .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
179                 .collect(toList());
180     }
181
182     @NotNull
183     private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
184         return versionIds.stream()
185                 .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
186                 .collect(toList());
187     }
188
189     private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) {
190         AAITreeNode newNode = new AAITreeNode();
191         newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId);
192         newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName());
193         newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey());
194         return newNode;
195     }
196
197     @Test
198     public void whenBuildTreeForOneResource_resultAsExpected() throws IOException {
199
200         AaiClient aaiClientMock = mock(AaiClient.class);
201         ExecutorService executorService = MoreExecutors.newDirectExecutorService();
202         AAIServiceTree aaiServiceTree = new AAIServiceTree(
203                 aaiClientMock,
204                 new AAITreeNodeBuilder(aaiClientMock, new Logging()),
205                 new AAITreeConverter(new ModelUtil()),
206                 null,
207                 null,
208                 executorService
209         );
210
211         String url = "anyUrl/vnf";
212
213         JsonNode mockedAaiGetVnfResponse = TestUtils.readJsonResourceFileAsObject("/getTopology/vnf.json", JsonNode.class);
214         when(aaiClientMock.typedAaiRest(Unchecked.toURI(url), JsonNode.class, null, HttpMethod.GET, false)).thenReturn(mockedAaiGetVnfResponse);
215
216         CloudConfiguration expectedCloudConfiguration = new CloudConfiguration("dyh3b", "c8035f5ee95d4c62bbc8074c044122b9", "irma-aic");
217         AAITreeNode expectedVnfNode = createExpectedVnfTreeNode(expectedCloudConfiguration);
218
219         List<AAITreeNode> aaiTreeNodes = aaiServiceTree.buildAAITreeForUniqueResource(url, NodeType.GENERIC_VNF);
220         assertEquals(1, aaiTreeNodes.size());
221         assertThat(aaiTreeNodes.get(0), jsonEquals(expectedVnfNode).when(Option.IGNORING_EXTRA_FIELDS).whenIgnoringPaths("relationshipList", "children[0].relationshipList"));
222     }
223 }