1 package org.onap.vid.services;
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;
11 import java.io.IOException;
12 import java.util.List;
13 import java.util.stream.Collectors;
14 import java.util.stream.Stream;
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;
22 public class PortDetailsTranslatorTest {
24 private static final ObjectMapper om = new ObjectMapper();
26 private PortDetailsTranslator portDetailsTranslator = new PortDetailsTranslator();
29 public void extractPortDetailsFromProperties_givenValidAaiResponse() throws IOException {
31 final String aaiResponse = "{" +
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" +
52 CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
55 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
57 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsOk.class)));
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));
66 public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceNameNull_yieldException() throws IOException {
67 final String aaiResponse = "{" +
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" +
88 CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
89 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
91 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
93 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
94 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is missing."));
95 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
99 public void extractPortDetailsFromProperties_givenAaiResponseWithInstanceIdNull_yieldException() throws IOException {
100 final String aaiResponse = "{" +
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" +
121 CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
122 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
124 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
126 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
127 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-id' is missing."));
128 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
132 public void extractPortDetailsFromProperties_givenAaiResponseWithEmptyInstanceId_yieldException() throws IOException {
133 final String aaiResponse = "{" +
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" +
154 CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
155 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
157 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
159 PortDetailsTranslator.PortDetailsError portDetailsError = (PortDetailsTranslator.PortDetailsError) portDetails;
160 assertThat(portDetailsError.getErrorDescription(), is("Value of 'interface-name' is empty."));
161 assertThat(portDetailsError.getRawAaiResponse(), is(aaiResponse));
165 public void extractPortDetailsFromProperties_givenAaiResponseWithIsPortMirroredNull_yieldException() throws IOException {
166 final String aaiResponse = "{" +
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" +
187 CustomQuerySimpleResult aaiGetPortMirroringSourcePorts = om.readValue(aaiResponse, CustomQuerySimpleResult.class);
188 PortDetailsTranslator.PortDetails portDetails = PortDetailsTranslator.extractPortDetailsFromProperties(aaiGetPortMirroringSourcePorts.getResults().get(0).getProperties(), aaiResponse);
190 assertThat(portDetails, is(instanceOf(PortDetailsTranslator.PortDetailsError.class)));
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));
198 public void getFilteredPortList_givenEmptyList_ReturnEmptyList() {
200 final ImmutableList<SimpleResult> input = ImmutableList.of();
202 List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
203 assertThat("List size if different than expected", result, is(empty()));
207 public void getFilteredPortList_givenFeatureFlagIsOff_returnAllLInterfaces() {
208 final String relationshipLabelSource = "org.onap.relationships.inventory.Source";
209 final String nodeTypeLInterface = "l-interface";
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");
226 final ImmutableList<SimpleResult> input = ImmutableList.of(
227 fooTypeWithSource, fooTypeWithoutSource,
228 lInterfaceWithTwoSources, lInterfaceWithSourceAndMore,
229 lInterfaceWithoutSource, lInterfaceWithSource,
230 lInterfaceWithoutRelations);
232 List<SimpleResult> result = portDetailsTranslator.getFilteredPortList(input);
234 assertThat("List should contain all l-interfaces with a related source", result, containsInAnyOrder(
235 lInterfaceWithSource, lInterfaceWithSourceAndMore,
236 lInterfaceWithTwoSources));
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())