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.springframework.http.HttpMethod;
51 import org.testng.annotations.BeforeMethod;
52 import org.testng.annotations.DataProvider;
53 import org.testng.annotations.Test;
55 public class AAIServiceIntegrativeTest extends TestWithAaiClient {
57 private AAIRestInterface aaiRestInterface;
58 private AaiServiceImpl aaiServiceWithoutMocks;
60 private AaiServiceImpl createAaiServiceWithoutMocks(AAIRestInterface aaiRestInterface, CacheProvider cacheProvider) {
61 AaiClient aaiClient = new AaiClient(aaiRestInterface, null, cacheProvider);
62 ExecutorService executorService = MoreExecutors.newDirectExecutorService();
63 AAIServiceTree aaiServiceTree = new AAIServiceTree(
65 new AAITreeNodeBuilder(aaiClient),
66 new AAITreeConverter(),
71 return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService);
76 aaiRestInterface = mock(AAIRestInterface.class);
77 CacheProvider cacheProvider = mock(CacheProvider.class);
78 CacheProvider.Cache cache = mock(CacheProvider.Cache.class);
79 when(cache.get(any())).thenReturn("ddd");
80 when(cacheProvider.aaiClientCacheFor(eq("getCloudOwnerByCloudRegionId"), any())).thenReturn(cache);
81 aaiServiceWithoutMocks = createAaiServiceWithoutMocks(aaiRestInterface, cacheProvider);
85 public void getVpnListTest_successResponse() {
87 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
89 " \"vpn-binding\": [" +
91 " \"vpn-id\": \"7a7b327d9-287aa00-82c4b0-100001\"," +
92 " \"vpn-name\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"," +
93 " \"resource-version\": \"1494001848224\"," +
94 " \"relationship-list\": {" +
95 " \"relationship\": [" +
97 " \"related-to\": \"l3-network\"," +
98 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
99 " \"related-link\": \"/aai/v15/network/l3-networks/l3-network/148b7892-c654-411d-9bb1-0d31fa6f9cc0\"," +
100 " \"relationship-data\": [" +
102 " \"relationship-key\": \"l3-network.network-id\"," +
103 " \"relationship-value\": \"148b7892-c654-411d-9bb1-0d31fa6f9cc0\"" +
106 " \"related-to-property\": [" +
108 " \"property-key\": \"l3-network.network-name\"," +
109 " \"property-value\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"" +
119 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
121 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
122 assertThat(vpnList, hasSize(1));
126 public void getVpnListTest_notFoundResponse() {
128 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
130 " \"requestError\": {" +
131 " \"serviceException\": {" +
132 " \"messageId\": \"SVC3001\"," +
133 " \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\"," +
134 " \"variables\": [" +
136 " \"network/vpn-bindings\"," +
137 " \"Node Not Found:No Node of type vpn-binding found at: network/vpn-bindings\"," +
138 " \"ERR.5.4.6114\"" +
145 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
147 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
148 assertThat(vpnList, empty());
151 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
152 public void getVpnListTest_errorResponse() {
154 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
158 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
160 aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
164 public void getNetworkListTest_successResponse() {
165 String rawResponse = TestUtils.readFileAsString("/responses/aai/l3-networks-by-cloud-region-and-tenantId.json");
166 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
170 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
171 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
172 assertThat(networkList, hasSize(4));
173 verify(aaiRestInterface).doRest(anyString(), anyString(), eq(URI.create("query?format=resource")),
174 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());
178 public static Object[][] networkRoles() {
179 return new Object[][]{
180 {"", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
181 {null, "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
182 {"ccc", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb&networkRole=ccc\"}"}
186 @Test(dataProvider = "networkRoles")
187 public void testBuildPayloadForL3NetworksByCloudRegion(String networkRole, String expected) {
188 String payload = aaiServiceWithoutMocks.buildPayloadForL3NetworksByCloudRegion("aaa", "bbb", networkRole);
189 assertEquals(expected, payload);
194 public void getNetworkListTest_notFoundResponse() {
196 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
198 " \"requestError\": {" +
199 " \"serviceException\": {" +
200 " \"messageId\": \"SVC3001\"," +
201 " \"text\": \"Some text\"," +
202 " \"variables\": []" +
209 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
211 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
212 assertThat(networkList, empty());
215 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
216 public void getNetworkListTest_errorResponse() {
218 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
223 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
225 aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");