a73a5a7bd012679a890e2d298da9b7715b237807
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / AAIServiceIntegrativeTest.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.services;
22
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;
34
35 import com.google.common.util.concurrent.MoreExecutors;
36 import java.net.URI;
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;
56
57 public class AAIServiceIntegrativeTest extends TestWithAaiClient {
58
59     private AAIRestInterface aaiRestInterface;
60     private AaiServiceImpl aaiServiceWithoutMocks;
61     private Logging logging = new Logging();
62
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                 aaiClient,
68                 new AAITreeNodeBuilder(aaiClient, logging),
69                 new AAITreeConverter(new ModelUtil()),
70                 null,
71                 null,
72                 executorService
73         );
74         return new AaiServiceImpl(aaiClient, null, aaiServiceTree, executorService, logging);
75     }
76
77     @BeforeMethod
78     public void setUp() {
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);
85     }
86
87     @Test
88     public void getVpnListTest_successResponse() {
89
90         final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.OK,
91                 "{" +
92                         "    \"vpn-binding\": [" +
93                         "        {" +
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\": [" +
99                         "                    {" +
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\": [" +
104                         "                            {" +
105                         "                                \"relationship-key\": \"l3-network.network-id\"," +
106                         "                                \"relationship-value\": \"148b7892-c654-411d-9bb1-0d31fa6f9cc0\"" +
107                         "                            }" +
108                         "                        ]," +
109                         "                        \"related-to-property\": [" +
110                         "                            {" +
111                         "                                \"property-key\": \"l3-network.network-name\"," +
112                         "                                \"property-value\": \"GN_EVPN_direct_net_0_ST_Subnets_Pools_Ipv4\"" +
113                         "                            }" +
114                         "                        ]" +
115                         "                    }" +
116                         "                ]" +
117                         "            }" +
118                         "        }" +
119                         "       ]}"
120         );
121
122         mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
123
124         List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
125         assertThat(vpnList, hasSize(1));
126     }
127
128     @Test
129     public void getVpnListTest_notFoundResponse() {
130
131         final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
132                 "{" +
133                         "    \"requestError\": {" +
134                         "        \"serviceException\": {" +
135                         "            \"messageId\": \"SVC3001\"," +
136                         "            \"text\": \"Resource not found for %1 using id %2 (msg=%3) (ec=%4)\"," +
137                         "            \"variables\": [" +
138                         "                \"GET\"," +
139                         "                \"network/vpn-bindings\"," +
140                         "                \"Node Not Found:No Node of type vpn-binding found at: network/vpn-bindings\"," +
141                         "                \"ERR.5.4.6114\"" +
142                         "            ]" +
143                         "        }" +
144                         "    }" +
145                         "}"
146         );
147
148         mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
149
150         List<VpnBinding> vpnList = aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
151         assertThat(vpnList, empty());
152     }
153
154     @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
155     public void getVpnListTest_errorResponse() {
156
157         final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
158                 "entity"
159         );
160
161         mockForGetRequest(aaiRestInterface, responseWithRequestInfo);
162
163         aaiServiceWithoutMocks.getVpnListByVpnType("aaa");
164     }
165
166     @Test
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,
170                 rawResponse,
171                 "/my/mocked/url",
172                 HttpMethod.PUT);
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());
178     }
179
180     @DataProvider
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\"}"}
186         };
187     }
188
189     @Test(dataProvider = "networkRoles")
190     public void testBuildPayloadForL3NetworksByCloudRegion(String networkRole, String expected) {
191         String payload = aaiServiceWithoutMocks.buildPayloadForL3NetworksByCloudRegion("aaa", "bbb", networkRole);
192         assertEquals(expected, payload);
193
194     }
195
196     @Test
197     public void getNetworkListTest_notFoundResponse() {
198
199         final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.NOT_FOUND,
200                 "{" +
201                         "    \"requestError\": {" +
202                         "        \"serviceException\": {" +
203                         "            \"messageId\": \"SVC3001\"," +
204                         "            \"text\": \"Some text\"," +
205                         "            \"variables\": []" +
206                         "        }" +
207                         "    }" +
208                         "}",
209                 "/my/mocked/url",
210                 HttpMethod.PUT);
211
212         mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
213
214         List<Network> networkList = aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
215         assertThat(networkList, empty());
216     }
217
218     @Test(expectedExceptions = ExceptionWithRequestInfo.class, expectedExceptionsMessageRegExp = ".*errorCode.*500.*raw.*entity")
219     public void getNetworkListTest_errorResponse() {
220
221         final ResponseWithRequestInfo responseWithRequestInfo = mockedResponseWithRequestInfo(Response.Status.INTERNAL_SERVER_ERROR,
222                 "entity",
223                 "/my/mocked/url",
224                 HttpMethod.PUT);
225
226         mockForPutRequest(aaiRestInterface, responseWithRequestInfo);
227
228         aaiServiceWithoutMocks.getL3NetworksByCloudRegion("aaa", "bbb", "ccc");
229     }
230 }