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(
67 new AAITreeNodeBuilder(aaiClient, logging),
68 new AAITreeNodesEnricher(aaiClient, null),
69 new AAITreeConverter(new ModelUtil()), null,
72 return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService, logging);
77 aaiRestInterface = mock(AAIRestInterface.class);
78 CacheProvider cacheProvider = mock(CacheProvider.class);
79 CacheProvider.Cache cache = mock(CacheProvider.Cache.class);
80 when(cache.get(any())).thenReturn("ddd");
81 when(cacheProvider.aaiClientCacheFor(eq("getCloudOwnerByCloudRegionId"), any())).thenReturn(cache);
82 aaiServiceWithoutMocks = createAaiServiceWithoutMocks(aaiRestInterface, cacheProvider);
86 public void getVpnListTest_successResponse() {
88 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
90 " \"vpn-binding\": [" +
92 " \"vpn-id\": \"7a7b327d9-287aa00-82c4b0-100001\"," +
93 " \"vpn-name\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"," +
94 " \"resource-version\": \"1494001848224\"," +
95 " \"relationship-list\": {" +
96 " \"relationship\": [" +
98 " \"related-to\": \"l3-network\"," +
99 " \"relationship-label\": \"org.onap.relationships.inventory.Uses\"," +
100 " \"related-link\": \"/aai/v15/network/l3-networks/l3-network/148b7892-c654-411d-9bb1-0d31fa6f9cc0\"," +
101 " \"relationship-data\": [" +
103 " \"relationship-key\": \"l3-network.network-id\"," +
104 " \"relationship-value\": \"148b7892-c654-411d-9bb1-0d31fa6f9cc0\"" +
107 " \"related-to-property\": [" +
109 " \"property-key\": \"l3-network.network-name\"," +
110 " \"property-value\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"" +
120 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
122 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
123 assertThat(vpnList, hasSize(1));
127 public void getVpnListTest_notFoundResponse() {
129 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
131 " \"requestError\": {" +
132 " \"serviceException\": {" +
133 " \"messageId\": \"SVC3001\"," +
134 " \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\"," +
135 " \"variables\": [" +
137 " \"network/vpn-bindings\"," +
138 " \"Node Not Found:No Node of type vpn-binding found at: network/vpn-bindings\"," +
139 " \"ERR.5.4.6114\"" +
146 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
148 List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
149 assertThat(vpnList, empty());
152 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
153 public void getVpnListTest_errorResponse() {
155 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
159 mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
161 aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
165 public void getNetworkListTest_successResponse() {
166 String rawResponse = TestUtils.readFileAsString("/responses/aai/l3-networks-by-cloud-region-and-tenantId.json");
167 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
171 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
172 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
173 assertThat(networkList, hasSize(4));
174 verify(aaiRestInterface).doRest(anyString(), anyString(), eq(URI.create("query?format=resource")),
175 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());
179 public static Object[][] networkRoles() {
180 return new Object[][]{
181 {"", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
182 {null, "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb\"}"},
183 {"ccc", "{\"start\":\"/cloud-infrastructure/cloud-regions/cloud-region/ddd/aaa\",\"query\":\"query/l3-networks-by-cloud-region?tenantId=bbb&networkRole=ccc\"}"}
187 @Test(dataProvider = "networkRoles")
188 public void testBuildPayloadForL3NetworksByCloudRegion(String networkRole, String expected) {
189 String payload = aaiServiceWithoutMocks.buildPayloadForL3NetworksByCloudRegion("aaa", "bbb", networkRole);
190 assertEquals(expected, payload);
195 public void getNetworkListTest_notFoundResponse() {
197 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
199 " \"requestError\": {" +
200 " \"serviceException\": {" +
201 " \"messageId\": \"SVC3001\"," +
202 " \"text\": \"Some text\"," +
203 " \"variables\": []" +
210 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
212 List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
213 assertThat(networkList, empty());
216 @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
217 public void getNetworkListTest_errorResponse() {
219 final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
224 mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
226 aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");