aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/AAIServiceTreeTest.java
blob: 815c6b9eecec9838003380737fb8610abe712a58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package org.onap.vid.services;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Streams;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.jetbrains.annotations.NotNull;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.vid.asdc.parser.ServiceModelInflator;
import org.onap.vid.asdc.parser.ServiceModelInflator.Names;
import org.onap.vid.model.aaiTree.AAITreeNode;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.util.List;

import static java.util.Collections.emptyList;
import static java.util.Collections.emptyMap;
import static java.util.stream.Collectors.toList;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.when;

public class AAIServiceTreeTest {

    @Mock
    private VidService sdcService;
    @Mock
    private ServiceModelInflator serviceModelInflator;
    @InjectMocks
    private AAIServiceTree aaiServiceTree;

    @BeforeTest
    public void initMocks() {
        MockitoAnnotations.initMocks(this);
    }

    private final static String nullString = "null placeholder";



    @Test
    public void enrichNodesWithModelCustomizationName_simple3NodesCase_nodesEnriched() {

        when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
                "version id a", new Names("name a", "key a"),
                "version id b", new Names("name b", "key b"),
                "version id c", new Names("name c", "key c")
        ));

        final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
        final ImmutableList<Names> expectedNames = ImmutableList.of(
                new Names("name a", "key a"),
                new Names("name b", "key b"),
                new Names("name c", "key c"));


        final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);
        aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);

        assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
    }

    @Test
    public void enrichNodesWithModelCustomizationName_noNodes_noError() {

        when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
                "11c6dc3e-cd6a-41b3-a50e-b5a10f7157d0", new Names("my model cust name", "my key")
        ));

        aaiServiceTree.enrichNodesWithModelCustomizationName(emptyList(), null);
    }

    @Test
    public void enrichNodesWithModelCustomizationName_nothingInModel_nodesUnchanged() {

        when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(emptyMap());

        final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c");
        final Names nullNames = new Names(nullString, nullString);
        final ImmutableList<Names> expectedNames = ImmutableList.of(nullNames, nullNames, nullNames);

        
        final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);

        aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);

        assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
    }

    @Test
    public void enrichNodesWithModelCustomizationName_staggered4NodesAndNull_3nodesEnriched2isNull() {

        when(serviceModelInflator.toNamesByVersionId(any())).thenReturn(ImmutableMap.of(
                "version id Z", new Names("name Z", "key Z"),
                "version id d", new Names(null, "key d"),
                "version id c", new Names("name c", null),
                "version id a", new Names("name a", "key a")
        ));

        final ImmutableList<String> versionIds = ImmutableList.of("version id a", "version id b", "version id c", "version id d", nullString);
        final ImmutableList<Names> expectedNames = ImmutableList.of(
                new Names("name a", "key a"),
                new Names(nullString, nullString),
                new Names("name c", nullString),
                new Names(nullString, "key d"),
                new Names(nullString, nullString)
        );


        final List<AAITreeNode> nodesUnderTest = nodesWithVersionIds(versionIds);

        aaiServiceTree.enrichNodesWithModelCustomizationName(nodesUnderTest, null);

        assertThat(toStrings(nodesUnderTest), containsInAnyOrder(toStringsArray(nodesWithVersionIdsAndCustomizationNames(versionIds, expectedNames))));
    }



    @NotNull
    private String[] toStringsArray(List<AAITreeNode> nodes) {
        return toStrings(nodes).toArray(new String[] {});
    }

    @NotNull
    private List<String> toStrings(List<AAITreeNode> nodes) {
        return nodes.stream().map(n -> {
            final ReflectionToStringBuilder reflectionToStringBuilder = new ReflectionToStringBuilder(n, ToStringStyle.SHORT_PREFIX_STYLE);
            reflectionToStringBuilder.setExcludeNullValues(true);
            return reflectionToStringBuilder.toString();
        }).collect(toList());
    }

    @NotNull
    private List<AAITreeNode> nodesWithVersionIdsAndCustomizationNames(List<String> versionIds, List<Names> customizationNames) {
        return Streams
                .zip(versionIds.stream(), customizationNames.stream(), this::nodeWithVersionIdAndCustomizationName)
                .collect(toList());
    }

    @NotNull
    private List<AAITreeNode> nodesWithVersionIds(List<String> versionIds) {
        return versionIds.stream()
                .map(versionId -> nodeWithVersionIdAndCustomizationName(versionId, new Names(nullString, nullString)))
                .collect(toList());
    }

    private AAITreeNode nodeWithVersionIdAndCustomizationName(String versionId, Names names) {
        AAITreeNode newNode = new AAITreeNode();
        newNode.setModelVersionId(versionId.equals(nullString) ? null : versionId);
        newNode.setModelCustomizationName(names.getModelCustomizationName().equals(nullString) ? null : names.getModelCustomizationName());
        newNode.setKeyInModel(names.getModelKey().equals(nullString) ? null : names.getModelKey());
        return newNode;
    }

}