aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/PortDetailsTranslatorTest.java
blob: cb9eb93d0da7fb1324315389946b240ae7f60d80 (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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package org.onap.vid.services;

import com.google.common.collect.ImmutableList;
import org.codehaus.jackson.map.ObjectMapper;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.vid.aai.model.AaiGetPortMirroringSourcePorts;
import org.onap.vid.aai.model.PortDetailsTranslator;
import org.onap.vid.aai.model.RelatedTo;
import org.onap.vid.aai.model.SimpleResult;
import org.onap.vid.properties.Features;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.collection.IsEmptyCollection.empty;
import static org.mockito.Mockito.when;

public class PortDetailsTranslatorTest {

    private static final ObjectMapper om = new ObjectMapper();

    @InjectMocks
    private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();

    @Mock
    private FeatureManager featureManager;

    @BeforeMethod
    public void initMocks() throws Exception {
        MockitoAnnotations.initMocks(this);
        when(featureManager.isActive(Features.FLAG_ADVANCED_PORTS_FILTER)).thenReturn(true);
    }

    @Test
    public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {

        final String aaiResponse  = "{\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"id\": \"4876980240\",\n" +
                "            \"node-type\": \"l-interface\",\n" +
                "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "            \"properties\": {\n" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\",\n" +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\",\n" +
                "                \"is-port-mirrored\": false,\n" +
                "                \"resource-version\": \"1519383879190\",\n" +
                "                \"in-maint\": false,\n" +
                "                \"is-ip-unnumbered\": false\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);


        PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);

        assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsOk.class)));

        PortDetailsTranslator.PortDetailsOk portDetailsOk = (PortDetailsTranslator.PortDetailsOk) portDetails;
        assertThat(portDetailsOk.getInterfaceName(), is("zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib"));
        assertThat(portDetailsOk.getInterfaceId(), is("6de7bf87-6faa-4984-9492-18d1188b3d4a"));
        assertThat(portDetailsOk.getIsPortMirrored(), is(false));
    }

    @Test
    public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceNameNull_yieldException() throws IOException {
        final String aaiResponse  = "{\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"id\": \"4876980240\",\n" +
                "            \"node-type\": \"l-interface\",\n" +
                "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "            \"properties\": {\n" +
                "                \"interface-name\": null,\n" +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\",\n" +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\",\n" +
                "                \"is-port-mirrored\": false,\n" +
                "                \"resource-version\": \"1519383879190\",\n" +
                "                \"in-maint\": false,\n" +
                "                \"is-ip-unnumbered\": false\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
        PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);

        assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));

        PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
        assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is missing."));
        assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
    }

    @Test
    public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceIdNull_yieldException() throws IOException {
        final String aaiResponse  = "{\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"id\": \"4876980240\",\n" +
                "            \"node-type\": \"l-interface\",\n" +
                "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "            \"properties\": {\n" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"interface-id\": null,\n" +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\",\n" +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\",\n" +
                "                \"is-port-mirrored\": false,\n" +
                "                \"resource-version\": \"1519383879190\",\n" +
                "                \"in-maint\": false,\n" +
                "                \"is-ip-unnumbered\": false\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
        PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);

        assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));

        PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
        assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-id' is missing."));
        assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
    }

    @Test
    public void extractPortDetailsFromProperties_givenAaiResponseWithEmptyInstanceId_yieldException() throws IOException {
        final String aaiResponse  = "{\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"id\": \"4876980240\",\n" +
                "            \"node-type\": \"l-interface\",\n" +
                "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "            \"properties\": {\n" +
                "                \"interface-name\": \"\",\n" +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\",\n" +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\",\n" +
                "                \"is-port-mirrored\": false,\n" +
                "                \"resource-version\": \"1519383879190\",\n" +
                "                \"in-maint\": false,\n" +
                "                \"is-ip-unnumbered\": false\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
        PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);

        assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));

        PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
        assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is empty."));
        assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
    }

    @Test
    public void extractPortDetailsFromProperties_givenAaiResponseWithIsPortMirroredNull_yieldException() throws IOException {
        final String aaiResponse  = "{\n" +
                "    \"results\": [\n" +
                "        {\n" +
                "            \"id\": \"4876980240\",\n" +
                "            \"node-type\": \"l-interface\",\n" +
                "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/att-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "            \"properties\": {\n" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\",\n" +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\",\n" +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\",\n" +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\",\n" +
                "                \"is-port-mirrored\": null,\n" +
                "                \"resource-version\": \"1519383879190\",\n" +
                "                \"in-maint\": false,\n" +
                "                \"is-ip-unnumbered\": false\n" +
                "            }\n" +
                "        }\n" +
                "    ]\n" +
                "}";

        AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
        PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);

        assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));

        PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
        assertThat(portDetailsError.getErrorDescription(), is("Value of 'is-port-mirrored' is missing."));
        assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
    }

    @Test
    public void getFilteredPortList_givenEmptyList_ReturnEmptyList() {

        final ImmutableList<SimpleResult> input = ImmutableList.of();

        List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
        assertThat("List size if different than expected", result, is(empty()));
    }

    @DataProvider
    public static Object[][] trueAndFalse() {
        return new Object[][]{
                { Boolean.TRUE }, { Boolean.FALSE }
        };
    }

    @Test(dataProvider = "trueAndFalse")
    public void getFilteredPortList_givenFeatureFlagIsOff_returnAllLInterfaces(Boolean advancedPortsFilterFlag) throws IOException {
        when(featureManager.isActive(Features.FLAG_ADVANCED_PORTS_FILTER)).thenReturn(advancedPortsFilterFlag);

        final String relationshipLabelSource = "org.onap.relationships.inventory.Source";
        final String nodeTypeLInterface = "l-interface";

        SimpleResult lInterfaceWithSource =
                buildSimpleResult(nodeTypeLInterface, relationshipLabelSource);
        SimpleResult lInterfaceWithTwoSources =
                buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, relationshipLabelSource);
        SimpleResult lInterfaceWithSourceAndMore =
                buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, "not a source");
        SimpleResult lInterfaceWithoutSource =
                buildSimpleResult(nodeTypeLInterface, "not a source");
        SimpleResult lInterfaceWithoutRelations =
                buildSimpleResult(nodeTypeLInterface);
        SimpleResult fooTypeWithSource =
                buildSimpleResult("not an l-interface", relationshipLabelSource);
        SimpleResult fooTypeWithoutSource =
                buildSimpleResult("not an l-interface", "not a source");

        final ImmutableList<SimpleResult> input = ImmutableList.of(
                fooTypeWithSource, fooTypeWithoutSource,
                lInterfaceWithTwoSources, lInterfaceWithSourceAndMore,
                lInterfaceWithoutSource, lInterfaceWithSource,
                lInterfaceWithoutRelations);

        List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);

        if (advancedPortsFilterFlag) {
            assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
                    lInterfaceWithSource, lInterfaceWithSourceAndMore,
                    lInterfaceWithTwoSources));
        } else {
            assertThat("List should contain all l-interfaces", result, containsInAnyOrder(
                    lInterfaceWithSource, lInterfaceWithoutSource,
                    lInterfaceWithoutRelations, lInterfaceWithSourceAndMore,
                    lInterfaceWithTwoSources));
        }
    }

    private SimpleResult buildSimpleResult(String nodeType, String... relationshipLabels) {
        SimpleResult simpleResult = new SimpleResult();
        simpleResult.setNodeType(nodeType);
        simpleResult.setRelatedTo(Stream.of(relationshipLabels).map(label ->
                new RelatedTo("my foo id", label, "logical-link", "foo url"))
                .collect(Collectors.toList())
        );
        return simpleResult;
    }

}