aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/services/PortDetailsTranslatorTest.java
blob: 00497734784a81a3d3c05da3b74c4d4eff1c26d0 (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
package org.onap.vid.services;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
import org.onap.vid.aai.model.CustomQuerySimpleResult;
import org.onap.vid.aai.model.PortDetailsTranslator;
import org.onap.vid.aai.model.RelatedTo;
import org.onap.vid.aai.model.SimpleResult;
import org.testng.annotations.Test;

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;

public class PortDetailsTranslatorTest {

    private static final ObjectMapper om = new ObjectMapper();

    private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();

    @Test
    public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {

        final String aaiResponse = "{" +
                "    \"results\": [" +
                "        {" +
                "            \"id\": \"4876980240\"," +
                "            \"node-type\": \"l-interface\"," +
                "            \"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\"," +
                "            \"properties\": {" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
                "                \"is-port-mirrored\": false," +
                "                \"resource-version\": \"1519383879190\"," +
                "                \"in-maint\": false," +
                "                \"is-ip-unnumbered\": false" +
                "            }" +
                "        }" +
                "    ]" +
                "}";

        CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.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 = "{" +
                "    \"results\": [" +
                "        {" +
                "            \"id\": \"4876980240\"," +
                "            \"node-type\": \"l-interface\"," +
                "            \"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\"," +
                "            \"properties\": {" +
                "                \"interface-name\": null," +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
                "                \"is-port-mirrored\": false," +
                "                \"resource-version\": \"1519383879190\"," +
                "                \"in-maint\": false," +
                "                \"is-ip-unnumbered\": false" +
                "            }" +
                "        }" +
                "    ]" +
                "}";

        CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.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 = "{" +
                "    \"results\": [" +
                "        {" +
                "            \"id\": \"4876980240\"," +
                "            \"node-type\": \"l-interface\"," +
                "            \"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\"," +
                "            \"properties\": {" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"interface-id\": null," +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
                "                \"is-port-mirrored\": false," +
                "                \"resource-version\": \"1519383879190\"," +
                "                \"in-maint\": false," +
                "                \"is-ip-unnumbered\": false" +
                "            }" +
                "        }" +
                "    ]" +
                "}";

        CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.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 = "{" +
                "    \"results\": [" +
                "        {" +
                "            \"id\": \"4876980240\"," +
                "            \"node-type\": \"l-interface\"," +
                "            \"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\"," +
                "            \"properties\": {" +
                "                \"interface-name\": \"\"," +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
                "                \"is-port-mirrored\": false," +
                "                \"resource-version\": \"1519383879190\"," +
                "                \"in-maint\": false," +
                "                \"is-ip-unnumbered\": false" +
                "            }" +
                "        }" +
                "    ]" +
                "}";

        CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.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 = "{" +
                "    \"results\": [" +
                "        {" +
                "            \"id\": \"4876980240\"," +
                "            \"node-type\": \"l-interface\"," +
                "            \"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\"," +
                "            \"properties\": {" +
                "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
                "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
                "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
                "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
                "                \"is-port-mirrored\": null," +
                "                \"resource-version\": \"1519383879190\"," +
                "                \"in-maint\": false," +
                "                \"is-ip-unnumbered\": false" +
                "            }" +
                "        }" +
                "    ]" +
                "}";

        CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.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()));
    }

    @Test
    public void getFilteredPortList_givenFeatureFlagIsOff_returnAllLInterfaces() {
        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);

        assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
                lInterfaceWithSource, lInterfaceWithSourceAndMore,
                lInterfaceWithTwoSources));

    }

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

}