Add Logging
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / vnf / MsoVnfPluginAdapterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 - 2018 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.so.adapters.vnf;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.github.tomakehurst.wiremock.client.WireMock;
25 import org.apache.http.HttpStatus;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.junit.rules.ExpectedException;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.so.adapters.vdu.CloudInfo;
32 import org.onap.so.adapters.vdu.VduInstance;
33 import org.onap.so.adapters.vdu.VduStateType;
34 import org.onap.so.adapters.vdu.VduStatus;
35 import org.onap.so.adapters.vnf.exceptions.VnfException;
36 import org.onap.so.db.catalog.beans.AuthenticationType;
37 import org.onap.so.db.catalog.beans.CloudIdentity;
38 import org.onap.so.db.catalog.beans.CloudSite;
39 import org.onap.so.db.catalog.beans.ServerType;
40 import org.onap.so.entity.MsoRequest;
41 import org.onap.so.openstack.beans.HeatStatus;
42 import org.onap.so.openstack.beans.StackInfo;
43 import org.onap.so.openstack.beans.VnfRollback;
44 import org.onap.so.openstack.utils.MsoMulticloudUtils;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 import javax.ws.rs.core.MediaType;
48 import javax.xml.ws.Holder;
49 import java.util.HashMap;
50 import java.util.Map;
51
52 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
53 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
54 import static com.github.tomakehurst.wiremock.client.WireMock.get;
55 import static com.github.tomakehurst.wiremock.client.WireMock.reset;
56 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
57 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
58 import static org.mockito.Mockito.when;
59 import static org.onap.so.bpmn.mock.StubOpenStack.mockOpenStackGetStackVfModule_200;
60 import static org.onap.so.bpmn.mock.StubOpenStack.mockOpenStackGetStackVfModule_404;
61 import static org.onap.so.bpmn.mock.StubOpenStack.mockOpenStackResponseAccess;
62 import static org.onap.so.bpmn.mock.StubOpenStack.mockOpenStackResponseAccessMulticloud;
63 import static org.onap.so.bpmn.mock.StubOpenStack.mockOpenstackGetWithResponse;
64
65 public class MsoVnfPluginAdapterImplTest extends BaseRestTestUtils {
66
67     @Rule
68     public ExpectedException expectedException = ExpectedException.none();
69
70     @Autowired
71     MsoVnfPluginAdapterImpl msoVnfPluginAdapter;
72
73     String vnfName = "DEV-VF-1802-it3-pwt3-v6-vSAMP10a-addon2-Replace-1001/stackId";
74
75     /***
76      * Before each test execution, updating IdentityUrl port value to the ramdom wireMockPort
77      * Since URL will be used as a rest call and required to be mocked in unit tests
78      */
79     @Before
80     public void setUp() throws Exception {
81         reset();
82         mapper = new ObjectMapper();
83
84         CloudIdentity identity = new CloudIdentity();
85         identity.setId("MTN13");
86         identity.setMsoId("m93945");
87         identity.setMsoPass("93937EA01B94A10A49279D4572B48369");
88         identity.setAdminTenant("admin");
89         identity.setMemberRole("admin");
90         identity.setTenantMetadata(new Boolean(true));
91         identity.setIdentityUrl("http://localhost:"+wireMockPort+"/v2.0");
92         identity.setIdentityAuthenticationType(AuthenticationType.USERNAME_PASSWORD);
93
94         CloudSite cloudSite = new CloudSite();
95         cloudSite.setId("MTN13");
96         cloudSite.setCloudVersion("3.0");
97         cloudSite.setClli("MDT13");
98         cloudSite.setRegionId("MTN13");
99         cloudSite.setOrchestrator("multicloud" +
100                 "");
101         identity.setIdentityServerType(ServerType.KEYSTONE);
102         cloudSite.setIdentityService(identity);
103
104
105
106         stubFor(get(urlPathEqualTo("/cloudSite/MTN13")).willReturn(aResponse()
107                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
108                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
109                 .withStatus(HttpStatus.SC_OK)));
110         stubFor(get(urlPathEqualTo("/cloudSite/DEFAULT")).willReturn(aResponse()
111                 .withBody(getBody(mapper.writeValueAsString(cloudSite),wireMockPort, ""))
112                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
113                 .withStatus(HttpStatus.SC_OK)));
114         stubFor(get(urlPathEqualTo("/cloudIdentity/MTN13")).willReturn(aResponse()
115                 .withBody(getBody(mapper.writeValueAsString(identity),wireMockPort, ""))
116                 .withHeader(org.apache.http.HttpHeaders.CONTENT_TYPE,MediaType.APPLICATION_JSON)
117                 .withStatus(HttpStatus.SC_OK)));
118         cloudConfig.getCloudSite("MTN13").get().getIdentityService().setIdentityUrl("http://localhost:" + wireMockPort + "/v2.0");
119
120     }
121
122     @Test
123     public void createVfModule_ModelCustUuidIsNull() throws Exception {
124         expectedException.expect(VnfException.class);
125         MsoRequest msoRequest = getMsoRequest();
126         Map<String, String> map = new HashMap<>();
127         map.put("key1", "value1");
128         msoVnfPluginAdapter.createVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
129                 "volumeGroupHeatStackId|1", "baseVfHeatStackId", null, map,
130                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
131                 new Holder<VnfRollback>());
132     }
133
134     @Test
135     public void createVfModule_ModelCustUuidIsNotFound() throws Exception {
136         expectedException.expect(VnfException.class);
137         MsoRequest msoRequest = getMsoRequest();
138         Map<String, String> map = new HashMap<>();
139         map.put("key1", "value1");
140         msoVnfPluginAdapter.createVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
141                 "volumeGroupHeatStackId|1", "baseVfHeatStackId", "88a6ca3ee0394ade9403f075db23167e", map,
142                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
143                 new Holder<VnfRollback>());
144     }
145
146     @Test
147     public void createVfModule_VduException() throws Exception {
148         expectedException.expect(VnfException.class);
149         MsoRequest msoRequest = getMsoRequest();
150         Map<String, String> map = new HashMap<>();
151         map.put("key1", "value1");
152         msoVnfPluginAdapter.createVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
153                 "volumeGroupHeatStackId|1", "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
154                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
155                 new Holder<VnfRollback>());
156     }
157
158    /* @Test
159     public void createVfModule_INSTANTIATED() throws Exception {
160         mockOpenStackResponseAccess(wireMockPort);
161         mockOpenStackGetStackVfModule_200();
162
163         MsoRequest msoRequest = getMsoRequest();
164         Map<String, String> map = new HashMap<>();
165         map.put("key1", "value1");
166         msoVnfPluginAdapter.createVfModule("MTN13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
167                 null, "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
168                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
169                 new Holder<VnfRollback>());
170     }*/
171
172     @Test
173     public void createVfModule_INSTANTIATED_Multicloud() throws Exception {
174         mockOpenStackResponseAccessMulticloud(wireMockPort);
175         mockOpenStackGetStackVfModule_200();
176
177         MsoRequest msoRequest = getMsoRequest();
178         Map<String, String> map = new HashMap<>();
179         map.put("key1", "value1");
180         msoVnfPluginAdapter.createVfModule("MTN13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
181                 null, "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
182                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
183                 new Holder<VnfRollback>());
184     }
185
186     /*
187     @Test
188     public void createVfModule_Multicloud() throws Exception {
189         expectedException.expect(VnfException.class);
190         mockOpenStackResponseAccessMulticloud(wireMockPort);
191         mockOpenStackGetStackVfModule_404();
192
193         MsoRequest msoRequest = getMsoRequest();
194         Map<String, String> map = new HashMap<>();
195         map.put("key1", "value1");
196         map.put("oof_directives", "{ abc: 123 }");
197         map.put("sdnc_directives", "{ def: 456 }");
198         msoVnfPluginAdapter.createVfModule("MTN13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
199                 null, "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
200                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
201                 new Holder<VnfRollback>());
202     }
203     */
204
205     @Test
206     public void createVfModule_queryVduNotFoundWithVolumeGroupId() throws Exception {
207         expectedException.expect(VnfException.class);
208         mockOpenStackResponseAccess(wireMockPort);
209         MsoRequest msoRequest = getMsoRequest();
210         Map<String, String> map = new HashMap<>();
211         map.put("key1", "value1");
212         msoVnfPluginAdapter.createVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
213                 "volumeGroupHeatStackId|1", "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
214                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
215                 new Holder<VnfRollback>());
216     }
217
218     @Test
219     public void createVfModule_CreateVduException() throws Exception {
220         expectedException.expect(VnfException.class);
221         mockOpenStackResponseAccess(wireMockPort);
222         mockOpenStackGetStackVfModule_404();
223         stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/volumeGroupHeatStackId"))
224                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
225                         .withBodyFile("OpenstackResponse_Stack_Created_VfModule.json")
226                         .withStatus(HttpStatus.SC_OK)));
227         MsoRequest msoRequest = getMsoRequest();
228         Map<String, String> map = new HashMap<>();
229         map.put("key1", "value1");
230         msoVnfPluginAdapter.createVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vnf", "1", vnfName, "VFMOD",
231                 "volumeGroupHeatStackId", "baseVfHeatStackId", "9b339a61-69ca-465f-86b8-1c72c582b8e8", map,
232                 Boolean.FALSE, Boolean.TRUE, Boolean.FALSE, msoRequest, new Holder<>(), new Holder<Map<String, String>>(),
233                 new Holder<VnfRollback>());
234     }
235
236     @Test
237     public void deleteVfModule_QueryVduException() throws Exception {
238         expectedException.expect(VnfException.class);
239         MsoRequest msoRequest = getMsoRequest();
240         msoVnfPluginAdapter.deleteVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vSAMP12", msoRequest,
241                 new Holder<Map<String, String>>());
242     }
243
244     @Test
245     public void deleteVfModule_DeleteVduException() throws Exception {
246         expectedException.expect(VnfException.class);
247         mockOpenStackResponseAccess(wireMockPort);
248         mockOpenStackGetStackVfModule_200();
249         stubFor(get(urlPathEqualTo("/mockPublicUrl/stacks/vSAMP12"))
250                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
251                         .withBodyFile("OpenstackResponse_Stack_Created_VfModule.json")
252                         .withStatus(HttpStatus.SC_OK)));
253         stubFor(delete(urlPathEqualTo("/mockPublicUrl/stacks/DEV-VF-1802-it3-pwt3-v6-vSAMP10a-addon2-Replace-1001/stackId"))
254                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
255                         .withStatus(HttpStatus.SC_INTERNAL_SERVER_ERROR)));
256         MsoRequest msoRequest = getMsoRequest();
257         msoVnfPluginAdapter.deleteVfModule("mtn13", "88a6ca3ee0394ade9403f075db23167e", "vSAMP12", msoRequest,
258                 new Holder<Map<String, String>>());
259     }
260
261     private MsoRequest getMsoRequest() {
262         MsoRequest msoRequest = new MsoRequest();
263         msoRequest.setRequestId("12345");
264         msoRequest.setServiceInstanceId("12345");
265         return msoRequest;
266     }
267
268 }