Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AAIServiceTreeTest.java
1 package org.onap.vid.services;
2
3 import com.google.common.collect.ImmutableList;
4 import com.google.common.collect.ImmutableMap;
5 import com.google.common.collect.Streams;
6 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
7 import org.apache.commons.lang3.builder.ToStringStyle;
8 import org.jetbrains.annotations.NotNull;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.MockitoAnnotations;
12 import org.onap.vid.asdc.parser.ServiceModelInflator;
13 import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
14 import org.onap.vid.model.aaiTree.AAITreeNode;
15 import org.testng.annotations.BeforeTest;
16 import org.testng.annotations.Test;
17
18 import java.util.List;
19
20 import static java.util.Collections.emptyList;
21 import static java.util.Collections.emptyMap;
22 import static java.util.stream.Collectors.toList;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Mockito.when;
27
28 public class AAIServiceTreeTest {
29
30     @Mock
31     private VidService sdcService;
32     @Mock
33     private ServiceModelInflator serviceModelInflator;
34     @InjectMocks
35     private AAIServiceTree aaiServiceTree;
36
37     @BeforeTest
38     public void initMocks() {
39         MockitoAnnotations.initMocks(this);
40     }
41
42     private final static String nullString = "null placeholder";
43
44
45
46     @Test
47     public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {
48
49         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
50                 "version id a", new Names("name a", "key a"),
51                 "version id b", new Names("name b", "key b"),
52                 "version id c", new Names("name c", "key c")
53         ));
54
55         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
56         final ImmutableList<Names> expectedNames = ImmutableList.of(
57                 new Names("name a", "key a"),
58                 new Names("name b", "key b"),
59                 new Names("name c", "key c"));
60
61
62         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
63         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
64
65         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
66     }
67
68     @Test
69     public void enrichNodesWithModelCustomizationName_noNodes_noError() {
70
71         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
72                 "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
73         ));
74
75         aaiServiceTree.enrichNodesWithModelCustomizationName(emptyList(), null);
76     }
77
78     @Test
79     public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {
80
81         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());
82
83         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
84         final Names nullNames = new Names(nullString, nullString);
85         final ImmutableList<Names> expectedNames = ImmutableList.of(nullNames, nullNames, nullNames);
86
87         
88         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
89
90         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
91
92         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
93     }
94
95     @Test
96     public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {
97
98         when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
99                 "version id Z", new Names("name Z", "key Z"),
100                 "version id d", new Names(null, "key d"),
101                 "version id c", new Names("name c", null),
102                 "version id a", new Names("name a", "key a")
103         ));
104
105         final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString);
106         final ImmutableList<Names> expectedNames = ImmutableList.of(
107                 new Names("name a", "key a"),
108                 new Names(nullString, nullString),
109                 new Names("name c", nullString),
110                 new Names(nullString, "key d"),
111                 new Names(nullString, nullString)
112         );
113
114
115         final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
116
117         aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
118
119         assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
120     }
121
122
123
124     @NotNull
125     private String[] toStringsArray(List<AAITreeNode> nodes) {
126         return toStrings(nodes).toArray(new String[] {});
127     }
128
129     @NotNull
130     private List<String> toStrings(List<AAITreeNode> nodes) {
131         return nodes.stream().map(n -> {
132             final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE);
133             reflectionToStringBuilder.setExcludeNullValues(true);
134             return reflectionToStringBuilder.toString();
135         }).collect(toList());
136     }
137
138     @NotNull
139     private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
140         return Streams
141                 .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
142                 .collect(toList());
143     }
144
145     @NotNull
146     private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
147         return versionIds.stream()
148                 .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
149                 .collect(toList());
150     }
151
152     private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) {
153         AAITreeNode newNode = new AAITreeNode();
154         newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId);
155         newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName());
156         newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey());
157         return newNode;
158     }
159
160 }