8aba2793262eabb84b980477ba699a57a2333619
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AAITreeNodesEnricherTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2020 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 org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
28 import static org.mockito.Matchers.any;
29 import static org.mockito.Mockito.when;
30
31 import com.google.common.collect.ImmutableList;
32 import com.google.common.collect.ImmutableMap;
33 import com.google.common.collect.Streams;
34 import java.util.List;
35 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
36 import org.apache.commons.lang3.builder.ToStringStyle;
37 import org.jetbrains.annotations.NotNull;
38 import org.mockito.InjectMocks;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.onap.vid.aai.AaiClientInterface;
42 import org.onap.vid.asdc.parser.ServiceModelInflator;
43 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
44 import org.onap.vid.model.aaiTree.AAITreeNode;
45 import org.testng.annotations.BeforeTest;
46 import org.testng.annotations.Test;
47 import org.togglz.core.manager.FeatureManager;
48
49 public class AAITreeNodesEnricherTest {
50
51     @Mock
52     private AaiClientInterface aaiClient;
53     @Mock
54     private VidService sdcService;
55     @Mock
56     private ServiceModelInflator serviceModelInflator;
57     @Mock
58     private FeatureManager featureManager;
59     @InjectMocks
60     private AAITreeNodesEnricher aaiTreeNodesEnricher;
61
62     @BeforeTest
63     public void initMocks() {
64         MockitoAnnotations.initMocks(this);
65     }
66
67     private final static String nullString = "null placeholder";
68
69     @Test
70     public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {
71
72         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
73                 "version id a", new Names("name a", "key a"),
74                 "version id b", new Names("name b", "key b"),
75                 "version id c", new Names("name c", "key c")
76         ));
77
78         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
79         final ImmutableList<Names> expectedNames = ImmutableList.of(
80                 new Names("name a", "key a"),
81                 new Names("name b", "key b"),
82                 new Names("name c", "key c"));
83
84
85         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
86         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
87
88         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
89     }
90
91     @Test
92     public void enrichNodesWithModelCustomizationName_noNodes_noError() {
93
94         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
95                 "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
96         ));
97
98         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(emptyList(), null);
99     }
100
101     @Test
102     public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {
103
104         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());
105
106         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
107         final Names nullNames = new Names(nullString, nullString);
108         final ImmutableList<Names> expectedNames = ImmutableList.of(nullNames, nullNames, nullNames);
109
110         
111         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
112
113         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
114
115         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
116     }
117
118     @Test
119     public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {
120
121         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
122                 "version id Z", new Names("name Z", "key Z"),
123                 "version id d", new Names(null, "key d"),
124                 "version id c", new Names("name c", null),
125                 "version id a", new Names("name a", "key a")
126         ));
127
128         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString);
129         final ImmutableList<Names> expectedNames = ImmutableList.of(
130                 new Names("name a", "key a"),
131                 new Names(nullString, nullString),
132                 new Names("name c", nullString),
133                 new Names(nullString, "key d"),
134                 new Names(nullString, nullString)
135         );
136
137
138         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
139
140         aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
141
142         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
143     }
144
145
146
147     @NotNull
148     private String[] toStringsArray(List<AAITreeNode> nodes) {
149         return toStrings(nodes).toArray(new String[] {});
150     }
151
152     @NotNull
153     private List<String> toStrings(List<AAITreeNode> nodes) {
154         return nodes.stream().map(n -> {
155             final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE);
156             reflectionToStringBuilder.setExcludeNullValues(true);
157             return reflectionToStringBuilder.toString();
158         }).collect(toList());
159     }
160
161     @NotNull
162     private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
163         return Streams
164                 .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
165                 .collect(toList());
166     }
167
168     @NotNull
169     private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
170         return versionIds.stream()
171                 .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
172                 .collect(toList());
173     }
174
175     private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) {
176         AAITreeNode newNode = new AAITreeNode();
177         newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId);
178         newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName());
179         newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey());
180         return newNode;
181     }
182
183 }