Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / PortDetailsTranslatorTest.java
1 package org.onap.vid.services;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.google.common.collect.ImmutableList;
5 import org.onap.vid.aai.model.CustomQuerySimpleResult;
6 import org.onap.vid.aai.model.PortDetailsTranslator;
7 import org.onap.vid.aai.model.RelatedTo;
8 import org.onap.vid.aai.model.SimpleResult;
9 import org.testng.annotations.Test;
10
11 import java.io.IOException;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
15
16 import static org.hamcrest.CoreMatchers.instanceOf;
17 import static org.hamcrest.CoreMatchers.is;
18 import static org.hamcrest.MatcherAssert.assertThat;
19 import static org.hamcrest.Matchers.containsInAnyOrder;
20 import static org.hamcrest.collection.IsEmptyCollection.empty;
21
22 public class PortDetailsTranslatorTest {
23
24     private static final ObjectMapper om = new ObjectMapper();
25
26     private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();
27
28     @Test
29     public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {
30
31         final String aaiResponse = "{" +
32                 "    \"results\": [" +
33                 "        {" +
34                 "            \"id\": \"4876980240\"," +
35                 "            \"node-type\": \"l-interface\"," +
36                 "            \"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\"," +
37                 "            \"properties\": {" +
38                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
39                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
40                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
41                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
42                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
43                 "                \"is-port-mirrored\": false," +
44                 "                \"resource-version\": \"1519383879190\"," +
45                 "                \"in-maint\": false," +
46                 "                \"is-ip-unnumbered\": false" +
47                 "            }" +
48                 "        }" +
49                 "    ]" +
50                 "}";
51
52         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
53
54
55         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
56
57         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsOk.class)));
58
59         PortDetailsTranslator.PortDetailsOk portDetailsOk = (PortDetailsTranslator.PortDetailsOk) portDetails;
60         assertThat(portDetailsOk.getInterfaceName(), is("zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib"));
61         assertThat(portDetailsOk.getInterfaceId(), is("6de7bf87-6faa-4984-9492-18d1188b3d4a"));
62         assertThat(portDetailsOk.getIsPortMirrored(), is(false));
63     }
64
65     @Test
66     public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceNameNull_yieldException() throws IOException {
67         final String aaiResponse = "{" +
68                 "    \"results\": [" +
69                 "        {" +
70                 "            \"id\": \"4876980240\"," +
71                 "            \"node-type\": \"l-interface\"," +
72                 "            \"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\"," +
73                 "            \"properties\": {" +
74                 "                \"interface-name\": null," +
75                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
76                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
77                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
78                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
79                 "                \"is-port-mirrored\": false," +
80                 "                \"resource-version\": \"1519383879190\"," +
81                 "                \"in-maint\": false," +
82                 "                \"is-ip-unnumbered\": false" +
83                 "            }" +
84                 "        }" +
85                 "    ]" +
86                 "}";
87
88         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
89         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
90
91         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
92
93         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
94         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is missing."));
95         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
96     }
97
98     @Test
99     public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceIdNull_yieldException() throws IOException {
100         final String aaiResponse = "{" +
101                 "    \"results\": [" +
102                 "        {" +
103                 "            \"id\": \"4876980240\"," +
104                 "            \"node-type\": \"l-interface\"," +
105                 "            \"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\"," +
106                 "            \"properties\": {" +
107                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
108                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
109                 "                \"interface-id\": null," +
110                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
111                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
112                 "                \"is-port-mirrored\": false," +
113                 "                \"resource-version\": \"1519383879190\"," +
114                 "                \"in-maint\": false," +
115                 "                \"is-ip-unnumbered\": false" +
116                 "            }" +
117                 "        }" +
118                 "    ]" +
119                 "}";
120
121         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
122         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
123
124         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
125
126         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
127         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-id' is missing."));
128         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
129     }
130
131     @Test
132     public void extractPortDetailsFromProperties_givenAaiResponseWithEmptyInstanceId_yieldException() throws IOException {
133         final String aaiResponse = "{" +
134                 "    \"results\": [" +
135                 "        {" +
136                 "            \"id\": \"4876980240\"," +
137                 "            \"node-type\": \"l-interface\"," +
138                 "            \"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\"," +
139                 "            \"properties\": {" +
140                 "                \"interface-name\": \"\"," +
141                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
142                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
143                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
144                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
145                 "                \"is-port-mirrored\": false," +
146                 "                \"resource-version\": \"1519383879190\"," +
147                 "                \"in-maint\": false," +
148                 "                \"is-ip-unnumbered\": false" +
149                 "            }" +
150                 "        }" +
151                 "    ]" +
152                 "}";
153
154         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
155         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
156
157         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
158
159         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
160         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is empty."));
161         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
162     }
163
164     @Test
165     public void extractPortDetailsFromProperties_givenAaiResponseWithIsPortMirroredNull_yieldException() throws IOException {
166         final String aaiResponse = "{" +
167                 "    \"results\": [" +
168                 "        {" +
169                 "            \"id\": \"4876980240\"," +
170                 "            \"node-type\": \"l-interface\"," +
171                 "            \"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\"," +
172                 "            \"properties\": {" +
173                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
174                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
175                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
176                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
177                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
178                 "                \"is-port-mirrored\": null," +
179                 "                \"resource-version\": \"1519383879190\"," +
180                 "                \"in-maint\": false," +
181                 "                \"is-ip-unnumbered\": false" +
182                 "            }" +
183                 "        }" +
184                 "    ]" +
185                 "}";
186
187         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
188         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
189
190         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
191
192         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
193         assertThat(portDetailsError.getErrorDescription(), is("Value of 'is-port-mirrored' is missing."));
194         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
195     }
196
197     @Test
198     public void getFilteredPortList_givenEmptyList_ReturnEmptyList() {
199
200         final ImmutableList<SimpleResult> input = ImmutableList.of();
201
202         List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
203         assertThat("List size if different than expected", result, is(empty()));
204     }
205
206     @Test
207     public void getFilteredPortList_givenFeatureFlagIsOff_returnAllLInterfaces() {
208         final String relationshipLabelSource = "org.onap.relationships.inventory.Source";
209         final String nodeTypeLInterface = "l-interface";
210
211         SimpleResult lInterfaceWithSource =
212                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource);
213         SimpleResult lInterfaceWithTwoSources =
214                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, relationshipLabelSource);
215         SimpleResult lInterfaceWithSourceAndMore =
216                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, "not a source");
217         SimpleResult lInterfaceWithoutSource =
218                 buildSimpleResult(nodeTypeLInterface, "not a source");
219         SimpleResult lInterfaceWithoutRelations =
220                 buildSimpleResult(nodeTypeLInterface);
221         SimpleResult fooTypeWithSource =
222                 buildSimpleResult("not an l-interface", relationshipLabelSource);
223         SimpleResult fooTypeWithoutSource =
224                 buildSimpleResult("not an l-interface", "not a source");
225
226         final ImmutableList<SimpleResult> input = ImmutableList.of(
227                 fooTypeWithSource, fooTypeWithoutSource,
228                 lInterfaceWithTwoSources, lInterfaceWithSourceAndMore,
229                 lInterfaceWithoutSource, lInterfaceWithSource,
230                 lInterfaceWithoutRelations);
231
232         List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
233
234         assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
235                 lInterfaceWithSource, lInterfaceWithSourceAndMore,
236                 lInterfaceWithTwoSources));
237
238     }
239
240     private SimpleResult buildSimpleResult(String nodeType, String... relationshipLabels) {
241         SimpleResult simpleResult = new SimpleResult();
242         simpleResult.setJsonNodeType(nodeType);
243         simpleResult.setJsonRelatedTo(Stream.of(relationshipLabels).map(label ->
244                 new RelatedTo("my foo id", label, "logical-link", "foo url"))
245                 .collect(Collectors.toList())
246         );
247         return simpleResult;
248     }
249
250 }