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