Fix for Penetration test _ Session and cookie management
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / util / ServiceInstanceStandardQueryTest.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.aai.util;
22
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.collect.ImmutableList;
26 import org.apache.log4j.LogManager;
27 import org.apache.log4j.Logger;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.vid.aai.AaiClientInterface;
32 import org.onap.vid.aai.ExceptionWithRequestInfo;
33 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Network;
34 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.ServiceInstance;
35 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vlan;
36 import org.onap.vid.aai.model.AaiGetNetworkCollectionDetails.Vnf;
37 import org.onap.vid.utils.Multival;
38 import org.springframework.http.HttpMethod;
39 import org.springframework.http.HttpStatus;
40 import org.springframework.web.client.HttpClientErrorException;
41 import org.testng.annotations.BeforeMethod;
42 import org.testng.annotations.Test;
43
44 import java.io.IOException;
45 import java.net.URI;
46 import java.util.List;
47 import java.util.stream.Collectors;
48
49 import static org.hamcrest.CoreMatchers.everyItem;
50 import static org.hamcrest.CoreMatchers.is;
51 import static org.hamcrest.MatcherAssert.assertThat;
52 import static org.hamcrest.Matchers.*;
53 import static org.mockito.Matchers.any;
54 import static org.mockito.Mockito.when;
55 import static org.onap.vid.utils.Unchecked.toURI;
56
57
58 public class ServiceInstanceStandardQueryTest {
59     private static final Logger logger = LogManager.getLogger(ServiceInstanceStandardQueryTest.class);
60     private static final ObjectMapper MAPPER = new ObjectMapper();
61     private final String serviceInstanceId = "9cdd1b2a-43a7-47bc-a88e-759ba2399f0b";
62     private final String vnfInstanceId_1 = "c015cc0f-0f37-4488-aabf-53795fd93cd3";
63     private final String vnfInstanceId_2 = "0846287b-65bf-45a6-88f6-6a1af4149fac";
64     private final String networkInstanceId_1 = "7989a6d2-ba10-4a5d-8f15-4520bc833090";
65     private final String networkInstanceId_2 = "82373aaa-c561-4e2b-96f1-7ef6f7f7b0e9";
66     private final String vlanTagInstanceId_1 = "701edbb2-37e4-4473-a2a6-405cb3ab2e37";
67     private final String vlanTagInstanceId_2 = "531571f4-e133-4780-8ba8-d79e63804084";
68     private final String vlanTagInstanceId_3 = "df674e8c-1773-4d39-a9e9-bddd2b339d0a";
69     private final String SERVICE_TYPE = "service";
70     private final String VNF_TYPE = "vnf";
71
72     @Mock
73     AaiClientInterface aaiClient;
74     @InjectMocks
75     private ServiceInstanceStandardQuery serviceInstanceStandardQuery;
76
77     @BeforeMethod
78     public void initMocks() {
79         MockitoAnnotations.initMocks(this);
80
81         when(aaiClient.typedAaiGet(any(URI.class), any(Class.class)))
82                 .thenAnswer(invocationOnMock -> {
83                     final URI uri = (URI) invocationOnMock.getArguments()[0];
84                     final String lastPart = uri.toString().replaceAll(".*/", "");
85                     switch (lastPart) {
86                         case serviceInstanceId:
87                             return getAaiObject(ServiceInstance.class);
88                         case vnfInstanceId_1:
89                         case vnfInstanceId_2:
90                             return getAaiObject(Vnf.class);
91                         case networkInstanceId_1:
92                         case networkInstanceId_2:
93                             return getAaiObject(Network.class);
94                         case vlanTagInstanceId_1:
95                         case vlanTagInstanceId_2:
96                         case vlanTagInstanceId_3:
97                             return getAaiObject(Vlan.class);
98                         default:
99                             throw new ExceptionWithRequestInfo(HttpMethod.GET, uri.toASCIIString(), getAaiObjectString(true), 404,
100                                     new HttpClientErrorException(HttpStatus.NOT_FOUND));
101                     }
102                 });
103     }
104
105     @Test
106     public void pathToObject_serviceInstanceUri_yieldsAaiObject() {
107         final ServiceInstance serviceInstance = serviceInstanceStandardQuery.objectByUri(ServiceInstance.class, toURI("/aai/v12/business/customers/customer/a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb/service-subscriptions/service-subscription/vMOG/service-instances/service-instance/" + serviceInstanceId));
108         assertThat(serviceInstance.getServiceInstanceId(), is(serviceInstanceId));
109         assertThat(serviceInstance.getServiceInstanceName(), is("Network_repl_vMOG_rk"));
110     }
111
112     @Test
113     public void pathToObject_networkUri_yieldsAaiObject() {
114         final Network network = serviceInstanceStandardQuery.objectByUri(Network.class, toURI("/aai/v12/network/l3-networks/l3-network/" + networkInstanceId_1 + ""));
115         assertThat(network.getNetworkId(), is(networkInstanceId_1));
116         assertThat(network.getNetworkName(), is("APPC-24595-T-IST-02AShared_cor_direct_net_1"));
117     }
118
119     @Test
120     public void pathToObject_vlanTagUri_yieldsAaiObject() {
121         final Vlan vlan = serviceInstanceStandardQuery.objectByUri(Vlan.class, toURI("/this is an invented link/aai/v12/tag/vlan-tags/vlan-tag/" + vlanTagInstanceId_1 + ""));
122         assertThat(vlan.getVlanInterface(), is("US-10688-genvnf-vlan-interface1"));
123         assertThat(vlan.getVlanIdInner(), is("917"));
124     }
125
126     @Test(expectedExceptions = Exception.class)
127     public void pathToObject_oneUnknownUri_throwsException() {
128         serviceInstanceStandardQuery.objectByUri(ServiceInstance.class, toURI("/aai/v12/non existing path"));
129     }
130
131     @Test
132     public void vnfsForServiceInstance_noRelatedButManyOthers_emptyResult() {
133         final Multival<ServiceInstance, Vnf> vnfs =
134                 serviceInstanceStandardQuery.fetchRelatedVnfs(getAaiObject(false, ServiceInstance.class));
135         assertThat(vnfs.getValues(), is(empty()));
136     }
137
138     @Test
139     public void vnfsForServiceInstance_2RelatedAndManyOthers_Result2CorrectPath2() {
140         final Multival<ServiceInstance, Vnf> vnfs =
141                 serviceInstanceStandardQuery.fetchRelatedVnfs(getAaiObject(ServiceInstance.class));
142
143         assertThat(vnfs.getValues(), hasSize(2));
144     }
145
146     @Test
147     public void serviceInstanceToL3Networks_noRelatedButManyOthers_emptyResult() {
148         final Multival<ServiceInstance, Network> l3Networks =
149                 serviceInstanceStandardQuery.fetchRelatedL3Networks(SERVICE_TYPE, getAaiObject(false, ServiceInstance.class));
150         assertThat(l3Networks.getValues(), is(empty()));
151     }
152
153     @Test
154     public void serviceInstanceToL3Networks_2RelatedAndManyOthers_Result2CorrectPath2() {
155         final Multival<ServiceInstance, Network> l3Networks =
156                 serviceInstanceStandardQuery.fetchRelatedL3Networks(SERVICE_TYPE, getAaiObject(ServiceInstance.class));
157
158         assertThat(l3Networks.getValues(), hasSize(2));
159     }
160
161     @Test
162     public void l3NetworkToVlanTags_noRelatedButManyOthers_emptyResult() {
163         final Multival<Network, Vlan> vlanTags =
164                 serviceInstanceStandardQuery.fetchRelatedVlanTags(getAaiObject(false, Network.class));
165         assertThat(vlanTags.getValues(), is(empty()));
166     }
167
168     @Test
169     public void l3NetworkToVlanTags__2RelatedAndManyOthers_Result2CorrectPath() {
170         final Multival<Network, Vlan> vlanTags =
171                 serviceInstanceStandardQuery.fetchRelatedVlanTags(getAaiObject(Network.class));
172
173         assertThat(vlanTags.getValues(), hasSize(3));
174     }
175
176     private <T> T getAaiObject(Class<T> valueType) {
177         return getAaiObject(true, valueType);
178     }
179
180     private <T> T getAaiObject(boolean withRelated, Class<T> valueType) {
181         try {
182             return MAPPER.readValue(getAaiObjectString(withRelated), valueType);
183         } catch (IOException e) {
184             throw new RuntimeException(e);
185         }
186     }
187
188     @Test
189     public void integrativeUsageWithLinearInvocation() throws JsonProcessingException {
190         final ServiceInstanceStandardQuery srv = this.serviceInstanceStandardQuery;
191         final ServiceInstance service = srv.fetchServiceInstance(toURI("path of service/" + serviceInstanceId + ""));
192         final Network l3Network = ImmutableList.copyOf(srv.fetchRelatedL3Networks(SERVICE_TYPE, service).getValues()).get(0);
193         final Vlan vlanTag = ImmutableList.copyOf(srv.fetchRelatedVlanTags(l3Network).getValues()).get(0);
194
195         assertThat(vlanTag.getVlanInterface(), is("US-10688-genvnf-vlan-interface1"));
196         assertThat(vlanTag.getVlanIdInner(), is("917"));
197     }
198
199     @Test
200     public void integrativeUsageWithGenericAccessors() throws JsonProcessingException {
201         final ServiceInstanceStandardQuery srv = this.serviceInstanceStandardQuery;
202
203         final ServiceInstance serviceInstance = srv.fetchServiceInstance(toURI("path of service/" + serviceInstanceId + ""));
204
205         final Multival<ServiceInstance, Network> serviceToNetworks =
206                 srv.fetchRelatedL3Networks(SERVICE_TYPE, serviceInstance);
207
208         final Multival<ServiceInstance, Multival<Network, Vlan>> serviceToNetworksToVlans =
209                 serviceToNetworks.mapEachVal(srv::fetchRelatedVlanTags);
210
211         logger.info(MAPPER.writeValueAsString(serviceToNetworksToVlans));
212
213         // check all tags are in place
214         final List<Vlan> vlanTags = serviceToNetworksToVlans
215                 .getValues().stream()
216                 .flatMap(networkVlanMultival -> networkVlanMultival.getValues().stream())
217                 .collect(Collectors.toList());
218
219         assertThat(vlanTags, hasSize(6)); // 2 networks, with 3 vlans each
220         assertThat(vlanTags, everyItem(hasProperty("vlanInterface", is("US-10688-genvnf-vlan-interface1"))));
221         assertThat(vlanTags, everyItem(hasProperty("vlanIdInner", is("917"))));
222     }
223
224     private String getAaiObjectString(boolean withRelated) {
225         final String relatedToVnfs = "" +
226                 "      { " +
227                 "        \"related-to\": \"generic-vnf\", " +
228                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
229                 "        \"related-link\": \"/aai/v12/network/generic-vnfs/generic-vnf/" + vnfInstanceId_1 + "\", " +
230                 "        \"relationship-data\": [{ " +
231                 "            \"relationship-key\": \"generic-vnf.vnf-id\", " +
232                 "            \"relationship-value\": \"" + vlanTagInstanceId_1 + "\" " +
233                 "          } " +
234                 "        ], " +
235                 "        \"related-to-property\": [{ " +
236                 "            \"property-key\": \"generic-vnf.vnf-name\", " +
237                 "            \"property-value\": \"\" " +
238                 "          } " +
239                 "        ] " +
240                 "      }, { " +
241                 "        \"related-to\": \"generic-vnf\", " +
242                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
243                 "        \"related-link\": \"/aai/v12/network/generic-vnfs/generic-vnf/" + vnfInstanceId_2 + "\", " +
244                 "        \"relationship-data\": [{ " +
245                 "            \"relationship-key\": \"generic-vnf.vnf-id\", " +
246                 "            \"relationship-value\": \"" + vlanTagInstanceId_2 + "\" " +
247                 "          } " +
248                 "        ], " +
249                 "        \"related-to-property\": [{ " +
250                 "            \"property-key\": \"generic-vnf.vnf-name\", " +
251                 "            \"property-value\": \"\" " +
252                 "          } " +
253                 "        ] " +
254                 "      }, ";
255
256
257         final String relatedToL3Networks = "" +
258                 "      { " +
259                 "        \"related-to\": \"l3-network\", " +
260                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
261                 "        \"related-link\": \"/aai/v12/network/l3-networks/l3-network/" + networkInstanceId_1 + "\", " +
262                 "        \"relationship-data\": [{ " +
263                 "            \"relationship-key\": \"l3-network.network-id\", " +
264                 "            \"relationship-value\": \"" + networkInstanceId_1 + "\" " +
265                 "          } " +
266                 "        ], " +
267                 "        \"related-to-property\": [{ " +
268                 "            \"property-key\": \"l3-network.network-name\", " +
269                 "            \"property-value\": \"APPC-24595-T-IST-02AShared_cor_direct_net_1\" " +
270                 "          } " +
271                 "        ] " +
272                 "      }, { " +
273                 "        \"related-to\": \"l3-network\", " +
274                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
275                 "        \"related-link\": \"/aai/v12/network/l3-networks/l3-network/" + networkInstanceId_2 + "\", " +
276                 "        \"relationship-data\": [{ " +
277                 "            \"relationship-key\": \"l3-network.network-id\", " +
278                 "            \"relationship-value\": \"" + networkInstanceId_2 + "\" " +
279                 "          } " +
280                 "        ], " +
281                 "        \"related-to-property\": [{ " +
282                 "            \"property-key\": \"l3-network.network-name\", " +
283                 "            \"property-value\": \"APPC-24595-T-IST-02AShared_cor_direct_net_1\" " +
284                 "          } " +
285                 "        ] " +
286                 "      }, ";
287
288         final String relatedToVlanTags = "" +
289                 "      { " +
290                 "        \"related-to\": \"vlan-tag\", " +
291                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
292                 "        \"related-link\": \"/this is an invented link/aai/v12/tag/vlan-tags/vlan-tag/" + vlanTagInstanceId_2 + "\", " +
293                 "        \"relationship-data\": [{ " +
294                 "            \"relationship-key\": \"vlan-tag.vlan-tag-id\", " +
295                 "            \"relationship-value\": \"" + vlanTagInstanceId_2 + "\" " +
296                 "          } " +
297                 "        ], " +
298                 "        \"related-to-property\": [{ " +
299                 "            \"property-key\": \"vlan-tag.vlan-tag-name\", " +
300                 "            \"property-value\": \"Behram_smeralda_56\" " +
301                 "          } " +
302                 "        ] " +
303                 "      }, { " +
304                 "        \"related-to\": \"vlan-tag\", " +
305                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
306                 "        \"related-link\": \"/this is an invented link/aai/v12/tag/vlan-tags/vlan-tag/" + vlanTagInstanceId_3 + "\" " +
307                 "      }, { " +
308                 "        \"related-to\": \"vlan-tag\", " +
309                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
310                 "        \"related-link\": \"/this is an invented link/aai/v12/tag/vlan-tags/vlan-tag/" + vlanTagInstanceId_1 + "\", " +
311                 "        \"relationship-data\": [{ " +
312                 "            \"relationship-key\": \"vlan-tag.vlan-tag-id\", " +
313                 "            \"relationship-value\": \"" + vlanTagInstanceId_1 + "\" " +
314                 "          } " +
315                 "        ], " +
316                 "        \"related-to-property\": [{ " +
317                 "            \"property-key\": \"vlan-tag.vlan-tag-name\", " +
318                 "            \"property-value\": \"Alexandra_Liron_3\" " +
319                 "          } " +
320                 "        ] " +
321                 "      }, ";
322
323         return "" +
324                 "{ " +
325
326                 // vlan props
327                 "  \"vlan-interface\": \"US-10688-genvnf-vlan-interface1\", " +
328                 "  \"vlan-id-inner\": 917, " +
329                 "  \"resource-version\": \"1518934744675\", " +
330                 "  \"in-maint\": false, " +
331                 "  \"is-ip-unnumbered\": false, " +
332                 // imaginary vlan props
333                 "  \"vlan-tag-id\": \"" + vlanTagInstanceId_2 + "\", " +
334                 "  \"vlan-tag-name\": \"Alexandra_Liron_3\", " +
335                 "  \"vlan-tag-type\": \"SUNIWOBA\", " +
336
337                 // service-instance props
338                 "  \"service-instance-name\": \"Network_repl_vMOG_rk\", " +
339                 "  \"service-instance-id\": \"" + serviceInstanceId + "\", " +
340                 "  \"environment-context\": \"General_Revenue-Bearing\", " +
341                 "  \"workload-context\": \"Production\", " +
342                 "  \"model-invariant-id\": \"92a72881-0a97-4d16-8c29-4831062e7e9b\", " +
343                 "  \"model-version-id\": \"5a3ad576-c01d-4bed-8194-0e72b4a3d020\", " +
344                 "  \"resource-version\": \"1516045827731\", " +
345                 "  \"orchestration-status\": \"Active\", " +
346
347                 // network props
348                 "  \"network-id\": \"" + networkInstanceId_1 + "\", " +
349                 "  \"network-name\": \"APPC-24595-T-IST-02AShared_cor_direct_net_1\", " +
350                 "  \"network-type\": \"CONTRAIL30_BASIC\", " +
351                 "  \"network-role\": \"repl\", " +
352                 "  \"network-technology\": \"contrail\", " +
353                 "  \"neutron-network-id\": \"66ee6123-1c45-4e71-b6c0-a748ae0fee88\", " +
354                 "  \"is-bound-to-vpn\": true, " +
355                 "  \"service-id\": \"db171b8f-115c-4992-a2e3-ee04cae357e0\", " +
356                 "  \"network-role-instance\": 0, " +
357                 "  \"resource-version\": \"1516046029762\", " +
358                 "  \"heat-stack-id\": \"APPC-24595-T-IST-02AShared_cor_direct_net_1/e8b256aa-8ce1-4384-9d99-6606eaca9eac\", " +
359                 "  \"contrail-network-fqdn\": \"default-domain:APPC-24595-T-IST-02C:APPC-24595-T-IST-02AShared_cor_direct_net_1\", " +
360                 "  \"physical-network-name\": \"FALSE\", " +
361                 "  \"is-provider-network\": false, " +
362                 "  \"is-shared-network\": true, " +
363                 "  \"is-external-network\": true, " +
364
365                 "  \"relationship-list\": { " +
366                 "    \"relationship\": [{ " +
367                 "        \"related-to\": \"service-instance\", " +
368                 "        \"relationship-label\": \"org.onap.relationships.inventory.ComposedOf\", " +
369                 "        \"related-link\": \"/aai/v12/business/customers/customer/a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb/service-subscriptions/service-subscription/vMOG/service-instances/service-instance/" + serviceInstanceId + "\", " +
370                 "        \"relationship-data\": [{ " +
371                 "            \"relationship-key\": \"customer.global-customer-id\", " +
372                 "            \"relationship-value\": \"a9a77d5a-123e-4ca2-9eb9-0b015d2ee0fb\" " +
373                 "          }, { " +
374                 "            \"relationship-key\": \"service-subscription.service-type\", " +
375                 "            \"relationship-value\": \"vMOG\" " +
376                 "          }, { " +
377                 "            \"relationship-key\": \"service-instance.service-instance-id\", " +
378                 "            \"relationship-value\": \"" + serviceInstanceId + "\" " +
379                 "          } " +
380                 "        ], " +
381                 "        \"related-to-property\": [{ " +
382                 "            \"property-key\": \"service-instance.service-instance-name\", " +
383                 "            \"property-value\": \"Network_repl_vMOG_rk\" " +
384                 "          } " +
385                 "        ] " +
386                 "      }, { " +
387                 "        \"related-to\": \"vpn-binding\", " +
388                 "        \"relationship-label\": \"org.onap.relationships.inventory.Uses\", " +
389                 "        \"related-link\": \"/aai/v12/network/vpn-bindings/vpn-binding/13e94b71-3ce1-4988-ab0e-61208fc91f1c\", " +
390                 "        \"relationship-data\": [{ " +
391                 "            \"relationship-key\": \"vpn-binding.vpn-id\", " +
392                 "            \"relationship-value\": \"13e94b71-3ce1-4988-ab0e-61208fc91f1c\" " +
393                 "          } " +
394                 "        ], " +
395                 "        \"related-to-property\": [{ " +
396                 "            \"property-key\": \"vpn-binding.vpn-name\", " +
397                 "            \"property-value\": \"vMDNS\" " +
398                 "          }, { " +
399                 "            \"property-key\": \"vpn-binding.vpn-type\" " +
400                 "          } " +
401                 "        ] " +
402                 "      }, " + (withRelated ? relatedToVlanTags : "") + "{ " +
403                 "        \"related-to\": \"project\", " +
404                 "        \"relationship-label\": \"org.onap.relationships.inventory.Uses\", " +
405                 "        \"related-link\": \"/aai/v12/business/projects/project/project1\", " +
406                 "        \"relationship-data\": [{ " +
407                 "            \"relationship-key\": \"project.project-name\", " +
408                 "            \"relationship-value\": \"project1\" " +
409                 "          } " +
410                 "        ] " +
411                 "      }, " + (withRelated ? relatedToL3Networks + relatedToVnfs : "") + "{ " +
412                 "        \"related-to\": \"owning-entity\", " +
413                 "        \"relationship-label\": \"org.onap.relationships.inventory.BelongsTo\", " +
414                 "        \"related-link\": \"/aai/v12/business/owning-entities/owning-entity/589fe0db-26c4-45e5-9f4e-a246c74fce76\", " +
415                 "        \"relationship-data\": [{ " +
416                 "            \"relationship-key\": \"owning-entity.owning-entity-id\", " +
417                 "            \"relationship-value\": \"589fe0db-26c4-45e5-9f4e-a246c74fce76\" " +
418                 "          } " +
419                 "        ] " +
420                 "      } " +
421                 "    ] " +
422                 "  } " +
423                 "} ";
424     }
425 }