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