2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
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;
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;
49 public class AAITreeNodesEnricherTest {
52 private AaiClientInterface aaiClient;
54 private VidService sdcService;
56 private ServiceModelInflator serviceModelInflator;
58 private FeatureManager featureManager;
60 private AAITreeNodesEnricher aaiTreeNodesEnricher;
63 public void initMocks() {
64 MockitoAnnotations.initMocks(this);
67 private final static String nullString = "null placeholder";
70 public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {
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")
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"));
85 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
86 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
88 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
92 public void enrichNodesWithModelCustomizationName_noNodes_noError() {
94 when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
95 "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
98 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(emptyList(), null);
102 public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {
104 when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());
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);
111 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
113 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
115 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
119 public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {
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")
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)
138 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
140 aaiTreeNodesEnricher.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
142 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
148 private String[] toStringsArray(List<AAITreeNode> nodes) {
149 return toStrings(nodes).toArray(new String[] {});
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());
162 private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
164 .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
169 private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
170 return versionIds.stream()
171 .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
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());