1 package org.onap.vid.services;
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;
18 import java.util.List;
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;
28 public class AAIServiceTreeTest {
31 private VidService sdcService;
33 private ServiceModelInflator serviceModelInflator;
35 private AAIServiceTree aaiServiceTree;
38 public void initMocks() {
39 MockitoAnnotations.initMocks(this);
42 private final static String nullString = "null placeholder";
47 public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {
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")
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"));
62 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
63 aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
65 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
69 public void enrichNodesWithModelCustomizationName_noNodes_noError() {
71 when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
72 "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
75 aaiServiceTree.enrichNodesWithModelCustomizationName(emptyList(), null);
79 public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {
81 when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());
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);
88 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
90 aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
92 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
96 public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {
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")
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)
115 final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
117 aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);
119 assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
125 private String[] toStringsArray(List<AAITreeNode> nodes) {
126 return toStrings(nodes).toArray(new String[] {});
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());
139 private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
141 .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
146 private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
147 return versionIds.stream()
148 .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
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());