1 package org.onap.vid.services;
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;
18 import java.io.IOException;
19 import java.util.List;
20 import java.util.stream.Collectors;
21 import java.util.stream.Stream;
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;
30 public class PortDetailsTranslatorTest {
32 private static final ObjectMapper om = new ObjectMapper();
35 private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();
38 private FeatureManager featureManager;
41 public void initMocks() throws Exception {
42 MockitoAnnotations.initMocks(this);
43 when(featureManager.isActive(Features.FLAG_ADVANCED_PORTS_FILTER)).thenReturn(true);
47 public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {
49 final String aaiResponse = "{\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" +
70 AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
73 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
75 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsOk.class)));
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));
84 public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceNameNull_yieldException() throws IOException {
85 final String aaiResponse = "{\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" +
106 AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
107 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);
109 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
111 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
112 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is missing."));
113 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
117 public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceIdNull_yieldException() throws IOException {
118 final String aaiResponse = "{\n" +
119 " \"results\": [\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" +
139 AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
140 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);
142 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
144 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
145 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-id' is missing."));
146 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
150 public void extractPortDetailsFromProperties_givenAaiResponseWithEmptyInstanceId_yieldException() throws IOException {
151 final String aaiResponse = "{\n" +
152 " \"results\": [\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" +
172 AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
173 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);
175 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
177 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
178 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is empty."));
179 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
183 public void extractPortDetailsFromProperties_givenAaiResponseWithIsPortMirroredNull_yieldException() throws IOException {
184 final String aaiResponse = "{\n" +
185 " \"results\": [\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" +
205 AaiGetPortMirroringSourcePorts aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, AaiGetPortMirroringSourcePorts.class);
206 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(),aaiResponse);
208 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
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));
216 public void getFilteredPortList_givenEmptyList_ReturnEmptyList() {
218 final ImmutableList<SimpleResult> input = ImmutableList.of();
220 List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
221 assertThat("List size if different than expected", result, is(empty()));
225 public static Object[][] trueAndFalse() {
226 return new Object[][]{
227 { Boolean.TRUE }, { Boolean.FALSE }
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);
235 final String relationshipLabelSource = "org.onap.relationships.inventory.Source";
236 final String nodeTypeLInterface = "l-interface";
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");
253 final ImmutableList<SimpleResult> input = ImmutableList.of(
254 fooTypeWithSource, fooTypeWithoutSource,
255 lInterfaceWithTwoSources, lInterfaceWithSourceAndMore,
256 lInterfaceWithoutSource, lInterfaceWithSource,
257 lInterfaceWithoutRelations);
259 List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
261 if (advancedPortsFilterFlag) {
262 assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
263 lInterfaceWithSource, lInterfaceWithSourceAndMore,
264 lInterfaceWithTwoSources));
266 assertThat("List should contain all l-interfaces", result, containsInAnyOrder(
267 lInterfaceWithSource, lInterfaceWithoutSource,
268 lInterfaceWithoutRelations, lInterfaceWithSourceAndMore,
269 lInterfaceWithTwoSources));
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())