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.ModelUtil;
48 import org.onap.vid.model.aaiTree.Network;
49 import org.onap.vid.model.aaiTree.VpnBinding;
50 import org.onap.vid.testUtils.TestUtils;
51 import org.onap.vid.utils.Logging;
52 import org.springframework.http.HttpMethod;
53 import org.testng.annotations.BeforeMethod;
54 import org.testng.annotations.DataProvider;
55 import org.testng.annotations.Test;
57 public class AAIServiceIntegrativeTest extends TestWithAaiClient {
59 private AAIRestInterface aaiRestInterface;
60 private AaiServiceImpl aaiServiceWithoutMocks;
61 private Logging logging = new Logging();
63 private AaiServiceImpl createAaiServiceWithoutMocks(AAIRestInterface aaiRestInterface, CacheProvider cacheProvider) {
64 AaiClient aaiClient = new AaiClient(aaiRestInterface, null, cacheProvider);
65 ExecutorService executorService = MoreExecutors.newDirectExecutorService();
66 AAIServiceTree aaiServiceTree = new AAIServiceTree(
68 new AAITreeNodeBuilder(aaiClient, logging),
69 new AAITreeConverter(new ModelUtil()),
74 return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService, logging);
79 aaiRestInterface = mock(AAIRestInterface.class);
80 CacheProvider cacheProvider = mock(CacheProvider.class);
81 CacheProvider.Cache cache = mock(CacheProvider.Cache.class);
82 when(cache.get(any())).thenReturn("ddd");
83 when(cacheProvider.aaiClientCacheFor(eq("getCloudOwnerByCloudRegionId"), any())).thenReturn(cache);
84 aaiServiceWithoutMocks = createAaiServiceWithoutMocks(aaiRestInterface, cacheProvider);
88 public void getVpnListTest_successResponse() {
90 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
92 " \"vpn-binding\": [" +
94 " \"vpn-id\": \"7a7b327d9-287aa00-82c4b0-100001\"," +
95 " \"vpn-name\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"," +
96 " \"resource-version\": \"1494001848224\"," +
97 " \"relationship-list\": {" +
98 " \"relationship\": [" +
100 " \"related-to\": \"l3-network\"," +
101 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
102 " \"related-link\": \"/aai/v15/network/l3-networks/l3-network/148b7892-c654-411d-9bb1-0d31fa6f9cc0\"," +
103 " \"relationship-data\": [" +
105 " \"relationship-key\": \"l3-network.network-id\"," +
106 " \"relationship-value\": \"148b7892-c654-411d-9bb1-0d31fa6f9cc0\"" +
109 " \"related-to-property\": [" +
111 " \"property-key\": \"l3-network.network-name\"," +
112 " \"property-value\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"" +
122 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
124 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
125 assertThat(vpnList, hasSize(1));
129 public void getVpnListTest_notFoundResponse() {
131 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
133 " \"requestError\": {" +
134 " \"serviceException\": {" +
135 " \"messageId\": \"SVC3001\"," +
136 " \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\"," +
137 " \"variables\": [" +
139 " \"network/vpn-bindings\"," +
140 " \"Node Not Found:No Node of type vpn-binding found at: network/vpn-bindings\"," +
141 " \"ERR.5.4.6114\"" +
148 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
150 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
151 assertThat(vpnList, empty());
154 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
155 public void getVpnListTest_errorResponse() {
157 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
161 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
163 aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
167 public void getNetworkListTest_successResponse() {
168 String rawResponse = TestUtils.readFileAsString("/responses/aai/l3-networks-by-cloud-region-and-tenantId.json");
169 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
173 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
174 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
175 assertThat(networkList, hasSize(4));
176 verify(aaiRestInterface).doRest(anyString(), anyString(), eq(URI.create("query?format=resource")),
177 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());
181 public static Object[][] networkRoles() {
182 return new Object[][]{
183 {"", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
184 {null, "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
185 {"ccc", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb&networkRole=ccc\"}"}
189 @Test(dataProvider = "networkRoles")
190 public void testBuildPayloadForL3NetworksByCloudRegion(String networkRole, String expected) {
191 String payload = aaiServiceWithoutMocks.buildPayloadForL3NetworksByCloudRegion("aaa", "bbb", networkRole);
192 assertEquals(expected, payload);
197 public void getNetworkListTest_notFoundResponse() {
199 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
201 " \"requestError\": {" +
202 " \"serviceException\": {" +
203 " \"messageId\": \"SVC3001\"," +
204 " \"text\": \"Some text\"," +
205 " \"variables\": []" +
212 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
214 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
215 assertThat(networkList, empty());
218 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
219 public void getNetworkListTest_errorResponse() {
221 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
226 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
228 aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");