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