Implant vid-app-common org.onap.vid.job (main and test)
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / PortDetailsTranslatorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.services;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.google.common.collect.ImmutableList;
25 import org.onap.vid.aai.model.CustomQuerySimpleResult;
26 import org.onap.vid.aai.model.PortDetailsTranslator;
27 import org.onap.vid.aai.model.RelatedTo;
28 import org.onap.vid.aai.model.SimpleResult;
29 import org.testng.annotations.Test;
30
31 import java.io.IOException;
32 import java.util.List;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35
36 import static org.hamcrest.CoreMatchers.instanceOf;
37 import static org.hamcrest.CoreMatchers.is;
38 import static org.hamcrest.MatcherAssert.assertThat;
39 import static org.hamcrest.Matchers.containsInAnyOrder;
40 import static org.hamcrest.collection.IsEmptyCollection.empty;
41
42 public class PortDetailsTranslatorTest {
43
44     private static final ObjectMapper om = new ObjectMapper();
45
46     private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();
47
48     @Test
49     public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {
50
51         final String aaiResponse = "{" +
52                 "    \"results\": [" +
53                 "        {" +
54                 "            \"id\": \"4876980240\"," +
55                 "            \"node-type\": \"l-interface\"," +
56                 "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
57                 "            \"properties\": {" +
58                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
59                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
60                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
61                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
62                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
63                 "                \"is-port-mirrored\": false," +
64                 "                \"resource-version\": \"1519383879190\"," +
65                 "                \"in-maint\": false," +
66                 "                \"is-ip-unnumbered\": false" +
67                 "            }" +
68                 "        }" +
69                 "    ]" +
70                 "}";
71
72         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
73
74
75         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
76
77         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsOk.class)));
78
79         PortDetailsTranslator.PortDetailsOk portDetailsOk = (PortDetailsTranslator.PortDetailsOk) portDetails;
80         assertThat(portDetailsOk.getInterfaceName(), is("zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib"));
81         assertThat(portDetailsOk.getInterfaceId(), is("6de7bf87-6faa-4984-9492-18d1188b3d4a"));
82         assertThat(portDetailsOk.getIsPortMirrored(), is(false));
83     }
84
85     @Test
86     public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceNameNull_yieldException() throws IOException {
87         final String aaiResponse = "{" +
88                 "    \"results\": [" +
89                 "        {" +
90                 "            \"id\": \"4876980240\"," +
91                 "            \"node-type\": \"l-interface\"," +
92                 "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
93                 "            \"properties\": {" +
94                 "                \"interface-name\": null," +
95                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
96                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
97                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
98                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
99                 "                \"is-port-mirrored\": false," +
100                 "                \"resource-version\": \"1519383879190\"," +
101                 "                \"in-maint\": false," +
102                 "                \"is-ip-unnumbered\": false" +
103                 "            }" +
104                 "        }" +
105                 "    ]" +
106                 "}";
107
108         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
109         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
110
111         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
112
113         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
114         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is missing."));
115         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
116     }
117
118     @Test
119     public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceIdNull_yieldException() throws IOException {
120         final String aaiResponse = "{" +
121                 "    \"results\": [" +
122                 "        {" +
123                 "            \"id\": \"4876980240\"," +
124                 "            \"node-type\": \"l-interface\"," +
125                 "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
126                 "            \"properties\": {" +
127                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
128                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
129                 "                \"interface-id\": null," +
130                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
131                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
132                 "                \"is-port-mirrored\": false," +
133                 "                \"resource-version\": \"1519383879190\"," +
134                 "                \"in-maint\": false," +
135                 "                \"is-ip-unnumbered\": false" +
136                 "            }" +
137                 "        }" +
138                 "    ]" +
139                 "}";
140
141         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
142         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
143
144         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
145
146         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
147         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-id' is missing."));
148         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
149     }
150
151     @Test
152     public void extractPortDetailsFromProperties_givenAaiResponseWithEmptyInstanceId_yieldException() throws IOException {
153         final String aaiResponse = "{" +
154                 "    \"results\": [" +
155                 "        {" +
156                 "            \"id\": \"4876980240\"," +
157                 "            \"node-type\": \"l-interface\"," +
158                 "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
159                 "            \"properties\": {" +
160                 "                \"interface-name\": \"\"," +
161                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
162                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
163                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
164                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
165                 "                \"is-port-mirrored\": false," +
166                 "                \"resource-version\": \"1519383879190\"," +
167                 "                \"in-maint\": false," +
168                 "                \"is-ip-unnumbered\": false" +
169                 "            }" +
170                 "        }" +
171                 "    ]" +
172                 "}";
173
174         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
175         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
176
177         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
178
179         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
180         assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is empty."));
181         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
182     }
183
184     @Test
185     public void extractPortDetailsFromProperties_givenAaiResponseWithIsPortMirroredNull_yieldException() throws IOException {
186         final String aaiResponse = "{" +
187                 "    \"results\": [" +
188                 "        {" +
189                 "            \"id\": \"4876980240\"," +
190                 "            \"node-type\": \"l-interface\"," +
191                 "            \"url\": \"/aai/v12/cloud-infrastructure/cloud-regions/cloud-region/irma-aic/rdm5b/tenants/tenant/460f35aeb53542dc9f77105066483e83/vservers/vserver/15e46e2f-4b98-4e06-9644-f0e6e35cc79a/l-interfaces/l-interface/zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
192                 "            \"properties\": {" +
193                 "                \"interface-name\": \"zrdm5bfprbVLBA005-vlbagent_aff_int_pktmirror_1_port-dr5jhyxva5ib\"," +
194                 "                \"selflink\": \"https://network-aic.rdm5b.cci.att.com:9696/v2.0/ports/6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
195                 "                \"interface-id\": \"6de7bf87-6faa-4984-9492-18d1188b3d4a\"," +
196                 "                \"macaddr\": \"02:6d:e7:bf:87:6f\"," +
197                 "                \"network-name\": \"APP-C-24595-D-T001-vprobe_int_pktmirror_net_1\"," +
198                 "                \"is-port-mirrored\": null," +
199                 "                \"resource-version\": \"1519383879190\"," +
200                 "                \"in-maint\": false," +
201                 "                \"is-ip-unnumbered\": false" +
202                 "            }" +
203                 "        }" +
204                 "    ]" +
205                 "}";
206
207         CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
208         PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
209
210         assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
211
212         PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
213         assertThat(portDetailsError.getErrorDescription(), is("Value of 'is-port-mirrored' is missing."));
214         assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
215     }
216
217     @Test
218     public void getFilteredPortList_givenEmptyList_ReturnEmptyList() {
219
220         final ImmutableList<SimpleResult> input = ImmutableList.of();
221
222         List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
223         assertThat("List size if different than expected", result, is(empty()));
224     }
225
226     @Test
227     public void getFilteredPortList_givenFeatureFlagIsOff_returnAllLInterfaces() {
228         final String relationshipLabelSource = "org.onap.relationships.inventory.Source";
229         final String nodeTypeLInterface = "l-interface";
230
231         SimpleResult lInterfaceWithSource =
232                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource);
233         SimpleResult lInterfaceWithTwoSources =
234                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, relationshipLabelSource);
235         SimpleResult lInterfaceWithSourceAndMore =
236                 buildSimpleResult(nodeTypeLInterface, relationshipLabelSource, "not a source");
237         SimpleResult lInterfaceWithoutSource =
238                 buildSimpleResult(nodeTypeLInterface, "not a source");
239         SimpleResult lInterfaceWithoutRelations =
240                 buildSimpleResult(nodeTypeLInterface);
241         SimpleResult fooTypeWithSource =
242                 buildSimpleResult("not an l-interface", relationshipLabelSource);
243         SimpleResult fooTypeWithoutSource =
244                 buildSimpleResult("not an l-interface", "not a source");
245
246         final ImmutableList<SimpleResult> input = ImmutableList.of(
247                 fooTypeWithSource, fooTypeWithoutSource,
248                 lInterfaceWithTwoSources, lInterfaceWithSourceAndMore,
249                 lInterfaceWithoutSource, lInterfaceWithSource,
250                 lInterfaceWithoutRelations);
251
252         List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
253
254         assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
255                 lInterfaceWithSource, lInterfaceWithSourceAndMore,
256                 lInterfaceWithTwoSources));
257
258     }
259
260     private SimpleResult buildSimpleResult(String nodeType, String... relationshipLabels) {
261         SimpleResult simpleResult = new SimpleResult();
262         simpleResult.setJsonNodeType(nodeType);
263         simpleResult.setJsonRelatedTo(Stream.of(relationshipLabels).map(label ->
264                 new RelatedTo("my foo id", label, "logical-link", "foo url"))
265                 .collect(Collectors.toList())
266         );
267         return simpleResult;
268     }
269
270 }