Merge "Fixed issues found in integration testing" into dublin
[so.git] / adapters / mso-openstack-adapters / src / test / java / org / onap / so / adapters / vnf / MsoVnfMulticloudAdapterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2018 Intel Corp. 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 static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.delete;
25 import static com.github.tomakehurst.wiremock.client.WireMock.get;
26 import static com.github.tomakehurst.wiremock.client.WireMock.post;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static org.junit.Assert.assertEquals;
30 import static org.junit.Assert.assertTrue;
31 import static org.junit.Assert.fail;
32 import java.util.HashMap;
33 import java.util.Map;
34 import javax.xml.ws.Holder;
35 import org.apache.http.HttpStatus;
36 import org.junit.Before;
37 import org.junit.Rule;
38 import org.junit.Test;
39 import org.junit.rules.ExpectedException;
40 import org.onap.so.adapters.vdu.VduModelInfo;
41 import org.onap.so.adapters.vnf.exceptions.VnfException;
42 import org.onap.so.cloud.CloudConfig;
43 import org.onap.so.entity.MsoRequest;
44 import org.onap.so.openstack.beans.StackInfo;
45 import org.onap.so.openstack.exceptions.MsoException;
46 import org.springframework.beans.factory.annotation.Autowired;
47
48 public class MsoVnfMulticloudAdapterImplTest extends BaseRestTestUtils {
49     @Rule
50     public ExpectedException expectedException = ExpectedException.none();
51
52     @Autowired
53     private MsoVnfPluginAdapterImpl instance;
54
55     @Autowired
56     private CloudConfig cloudConfig;
57
58     private static final String UPDATE_STACK_RESPONSE =
59             "{\"template_type\": \"heat\", \"workload_id\": " + "\"workload-id\"}";
60     private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
61             + "\"workload-id\", \"workload_status\": \"CREATE_COMPLETE\"}";
62     private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
63             + "\"workload-id\", \"workload_status\": \"UPDATE_COMPLETE\"}";
64
65     private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
66     private static final String MULTICLOUD_UPDATE_PATH =
67             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
68     private static final String MULTICLOUD_GET_PATH_BY_NAME =
69             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/vfname";
70     private static final String MULTICLOUD_GET_PATH_BY_ID =
71             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
72
73     @Before
74     public void before() throws Exception {
75         super.orchestrator = "multicloud";
76         super.cloudEndpoint = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
77         super.setUp();
78     }
79
80     @Test
81     public void createVfModule() throws Exception {
82
83         Map<String, Object> stackInputs = new HashMap<>();
84         stackInputs.put("oof_directives", "{}");
85         stackInputs.put("sdnc_directives", "{}");
86         stackInputs.put("user_directives", "{}");
87         stackInputs.put("generic_vnf_id", "genVNFID");
88         stackInputs.put("vf_module_id", "vfMODULEID");
89
90         MsoRequest msoRequest = new MsoRequest();
91         msoRequest.setRequestId("12345");
92         msoRequest.setServiceInstanceId("12345");
93
94         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_NAME)).willReturn(
95                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
96
97         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
98                 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
99                         .withBody(GET_CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
100
101         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
102                 .whenScenarioStateIs("UPDATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
103                         .withBody(GET_UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
104
105         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
106                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
107                         .withBodyFile("MulticloudResponse_Stack_Create.json").withStatus(HttpStatus.SC_CREATED))
108                 .willSetStateTo("CREATING"));
109
110         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
111                 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
112                         .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
113                 .willSetStateTo("UPDATING"));
114
115         try {
116             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
117                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
118                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
119         } catch (VnfException e) {
120             fail("createVfModule success expected, failed with exception: " + e.toString());
121         }
122         wireMockServer.resetScenarios();
123     }
124
125     @Test
126     public void createVfModule2() throws Exception {
127
128         Map<String, Object> stackInputs = new HashMap<>();
129         stackInputs.put("oof_directives", "{}");
130         stackInputs.put("sdnc_directives", "{}");
131         stackInputs.put("user_directives", "{}");
132         stackInputs.put("generic_vnf_id", "genVNFID");
133         stackInputs.put("vf_module_id", "vfMODULEID");
134
135         MsoRequest msoRequest = new MsoRequest();
136         msoRequest.setRequestId("12345");
137         msoRequest.setServiceInstanceId("12345");
138
139         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_NAME)).willReturn(
140                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
141
142         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
143                 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
144                         .withBody(GET_CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
145
146         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
147                 .whenScenarioStateIs("UPDATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
148                         .withBody(GET_UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
149
150         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
151                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
152                         .withBodyFile("MulticloudResponse_Stack_Create2.json").withStatus(HttpStatus.SC_CREATED))
153                 .willSetStateTo("CREATING"));
154
155         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
156                 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
157                         .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
158                 .willSetStateTo("UPDATING"));
159
160         try {
161             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
162                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
163                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
164         } catch (VnfException e) {
165             fail("createVfModule success expected, failed with exception: " + e.toString());
166         }
167         wireMockServer.resetScenarios();
168     }
169
170     @Test
171     public void createVfModuleAlreadyExists() throws Exception {
172
173         Map<String, Object> stackInputs = new HashMap<>();
174         stackInputs.put("oof_directives", "{}");
175         stackInputs.put("sdnc_directives", "{}");
176         stackInputs.put("user_directives", "{}");
177         stackInputs.put("generic_vnf_id", "genVNFID");
178         stackInputs.put("vf_module_id", "vfMODULEID");
179
180         MsoRequest msoRequest = new MsoRequest();
181         msoRequest.setRequestId("12345");
182         msoRequest.setServiceInstanceId("12345");
183
184         wireMockServer.stubFor(
185                 get(urlEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload?name=vfname")).willReturn(aResponse()
186                         // .withHeader()
187                         .withBodyFile("MulticloudGetStackExists.json").withStatus(HttpStatus.SC_OK)));
188
189         try {
190             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
191                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
192                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
193         } catch (VnfException e) {
194             assertTrue(e.toString().contains(
195                     "Resource vfname already exists in owner/cloud/tenant CloudOwner/MTN13/123 with ID vfname/vfname"));
196             return;
197         }
198         fail("VnfAlreadyExists Exception expected!");
199     }
200
201     @Test
202     public void deleteVfModule() throws Exception {
203         MsoRequest msoRequest = new MsoRequest();
204         msoRequest.setRequestId("12345");
205         msoRequest.setServiceInstanceId("12345");
206
207         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
208                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
209
210         wireMockServer.stubFor(delete(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
211                 .willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT)));
212
213         instance.deleteVfModule("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>());
214     }
215
216     @Test
217     public void queryVfModule() throws Exception {
218         MsoRequest msoRequest = new MsoRequest();
219         msoRequest.setRequestId("12345");
220         msoRequest.setServiceInstanceId("12345");
221
222         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
223                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
224
225         instance.queryVnf("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>(), new Holder<>(),
226                 new Holder<>(), new Holder<>());
227     }
228
229     // TODO Error Tests
230 }