2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
23 import static org.hamcrest.MatcherAssert.assertThat;
24 import static org.hamcrest.Matchers.empty;
25 import static org.hamcrest.Matchers.hasSize;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyString;
29 import static org.mockito.ArgumentMatchers.eq;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33 import static org.testng.AssertJUnit.assertEquals;
35 import com.google.common.util.concurrent.MoreExecutors;
37 import java.util.List;
38 import java.util.concurrent.ExecutorService;
39 import javax.ws.rs.core.Response;
40 import org.onap.vid.aai.AaiClient;
41 import org.onap.vid.aai.ExceptionWithRequestInfo;
42 import org.onap.vid.aai.ResponseWithRequestInfo;
43 import org.onap.vid.aai.util.AAIRestInterface;
44 import org.onap.vid.aai.util.AAITreeConverter;
45 import org.onap.vid.aai.util.CacheProvider;
46 import org.onap.vid.aai.util.TestWithAaiClient;
47 import org.onap.vid.model.aaiTree.Network;
48 import org.onap.vid.model.aaiTree.VpnBinding;
49 import org.onap.vid.testUtils.TestUtils;
50 import org.onap.vid.utils.Logging;
51 import org.springframework.http.HttpMethod;
52 import org.testng.annotations.BeforeMethod;
53 import org.testng.annotations.DataProvider;
54 import org.testng.annotations.Test;
56 public class AAIServiceIntegrativeTest extends TestWithAaiClient {
58 private AAIRestInterface aaiRestInterface;
59 private AaiServiceImpl aaiServiceWithoutMocks;
60 private Logging logging = new Logging();
62 private AaiServiceImpl createAaiServiceWithoutMocks(AAIRestInterface aaiRestInterface, CacheProvider cacheProvider) {
63 AaiClient aaiClient = new AaiClient(aaiRestInterface, null, cacheProvider);
64 ExecutorService executorService = MoreExecutors.newDirectExecutorService();
65 AAIServiceTree aaiServiceTree = new AAIServiceTree(
67 new AAITreeNodeBuilder(aaiClient, logging),
68 new AAITreeConverter(),
73 return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService, logging);
78 aaiRestInterface = mock(AAIRestInterface.class);
79 CacheProvider cacheProvider = mock(CacheProvider.class);
80 CacheProvider.Cache cache = mock(CacheProvider.Cache.class);
81 when(cache.get(any())).thenReturn("ddd");
82 when(cacheProvider.aaiClientCacheFor(eq("getCloudOwnerByCloudRegionId"), any())).thenReturn(cache);
83 aaiServiceWithoutMocks = createAaiServiceWithoutMocks(aaiRestInterface, cacheProvider);
87 public void getVpnListTest_successResponse() {
89 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
91 " \"vpn-binding\": [" +
93 " \"vpn-id\": \"7a7b327d9-287aa00-82c4b0-100001\"," +
94 " \"vpn-name\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"," +
95 " \"resource-version\": \"1494001848224\"," +
96 " \"relationship-list\": {" +
97 " \"relationship\": [" +
99 " \"related-to\": \"l3-network\"," +
100 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
101 " \"related-link\": \"/aai/v15/network/l3-networks/l3-network/148b7892-c654-411d-9bb1-0d31fa6f9cc0\"," +
102 " \"relationship-data\": [" +
104 " \"relationship-key\": \"l3-network.network-id\"," +
105 " \"relationship-value\": \"148b7892-c654-411d-9bb1-0d31fa6f9cc0\"" +
108 " \"related-to-property\": [" +
110 " \"property-key\": \"l3-network.network-name\"," +
111 " \"property-value\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"" +
121 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
123 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
124 assertThat(vpnList, hasSize(1));
128 public void getVpnListTest_notFoundResponse() {
130 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
132 " \"requestError\": {" +
133 " \"serviceException\": {" +
134 " \"messageId\": \"SVC3001\"," +
135 " \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\"," +
136 " \"variables\": [" +
138 " \"network/vpn-bindings\"," +
139 " \"Node Not Found:No Node of type vpn-binding found at: network/vpn-bindings\"," +
140 " \"ERR.5.4.6114\"" +
147 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
149 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
150 assertThat(vpnList, empty());
153 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
154 public void getVpnListTest_errorResponse() {
156 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
160 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
162 aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
166 public void getNetworkListTest_successResponse() {
167 String rawResponse = TestUtils.readFileAsString("/responses/aai/l3-networks-by-cloud-region-and-tenantId.json");
168 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
172 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
173 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
174 assertThat(networkList, hasSize(4));
175 verify(aaiRestInterface).doRest(anyString(), anyString(), eq(URI.create("query?format=resource")),
176 eq("{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb&networkRole=ccc\"}"), eq(HttpMethod.PUT), anyBoolean(), anyBoolean());
180 public static Object[][] networkRoles() {
181 return new Object[][]{
182 {"", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
183 {null, "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
184 {"ccc", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb&networkRole=ccc\"}"}
188 @Test(dataProvider = "networkRoles")
189 public void testBuildPayloadForL3NetworksByCloudRegion(String networkRole, String expected) {
190 String payload = aaiServiceWithoutMocks.buildPayloadForL3NetworksByCloudRegion("aaa", "bbb", networkRole);
191 assertEquals(expected, payload);
196 public void getNetworkListTest_notFoundResponse() {
198 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
200 " \"requestError\": {" +
201 " \"serviceException\": {" +
202 " \"messageId\": \"SVC3001\"," +
203 " \"text\": \"Some text\"," +
204 " \"variables\": []" +
211 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
213 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
214 assertThat(networkList, empty());
217 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
218 public void getNetworkListTest_errorResponse() {
220 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
225 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
227 aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");