5f2057524f748508ea8c380162f1930bba675c1d
[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 CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
59             + "\"workload-id\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
60     private static final String UPDATE_STACK_RESPONSE =
61             "{\"template_type\": \"heat\", \"workload_id\": " + "\"workload-id\"}";
62     private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
63             + "\"workload-id\", \"workload_status\": \"CREATE_COMPLETE\"}";
64     private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
65             + "\"workload-id\", \"workload_status\": \"UPDATE_COMPLETE\"}";
66
67     private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
68     private static final String MULTICLOUD_UPDATE_PATH =
69             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
70     private static final String MULTICLOUD_GET_PATH_BY_NAME =
71             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/vfname";
72     private static final String MULTICLOUD_GET_PATH_BY_ID =
73             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
74
75     @Before
76     public void before() throws Exception {
77         super.orchestrator = "multicloud";
78         super.cloudEndpoint = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
79         super.setUp();
80     }
81
82     @Test
83     public void createVfModule() throws Exception {
84
85         Map<String, Object> stackInputs = new HashMap<>();
86         stackInputs.put("oof_directives", "{}");
87         stackInputs.put("sdnc_directives", "{}");
88         stackInputs.put("user_directives", "{}");
89         stackInputs.put("generic_vnf_id", "genVNFID");
90         stackInputs.put("vf_module_id", "vfMODULEID");
91
92         MsoRequest msoRequest = new MsoRequest();
93         msoRequest.setRequestId("12345");
94         msoRequest.setServiceInstanceId("12345");
95
96         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_NAME)).willReturn(
97                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
98
99         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
100                 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
101                         .withBody(GET_CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
102
103         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
104                 .whenScenarioStateIs("UPDATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
105                         .withBody(GET_UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
106
107         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
108                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
109                         .withBodyFile("MulticloudResponse_Stack_Create.json").withStatus(HttpStatus.SC_CREATED))
110                 .willSetStateTo("CREATING"));
111
112         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
113                 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
114                         .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
115                 .willSetStateTo("UPDATING"));
116
117         try {
118             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
119                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
120                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
121         } catch (VnfException e) {
122             fail("createVfModule success expected, failed with exception: " + e.toString());
123         }
124         wireMockServer.resetScenarios();
125     }
126
127     @Test
128     public void createVfModuleAlreadyExists() throws Exception {
129
130         Map<String, Object> stackInputs = new HashMap<>();
131         stackInputs.put("oof_directives", "{}");
132         stackInputs.put("sdnc_directives", "{}");
133         stackInputs.put("user_directives", "{}");
134         stackInputs.put("generic_vnf_id", "genVNFID");
135         stackInputs.put("vf_module_id", "vfMODULEID");
136
137         MsoRequest msoRequest = new MsoRequest();
138         msoRequest.setRequestId("12345");
139         msoRequest.setServiceInstanceId("12345");
140
141         wireMockServer.stubFor(
142                 get(urlEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload?name=vfname")).willReturn(aResponse()
143                         // .withHeader()
144                         .withBodyFile("MulticloudGetStackExists.json").withStatus(HttpStatus.SC_OK)));
145
146         try {
147             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
148                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
149                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
150         } catch (VnfException e) {
151             assertTrue(e.toString().contains(
152                     "Resource vfname already exists in owner/cloud/tenant CloudOwner/MTN13/123 with ID vfname/vfname"));
153             return;
154         }
155         fail("VnfAlreadyExists Exception expected!");
156     }
157
158     @Test
159     public void deleteVfModule() throws Exception {
160         MsoRequest msoRequest = new MsoRequest();
161         msoRequest.setRequestId("12345");
162         msoRequest.setServiceInstanceId("12345");
163
164         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
165                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
166
167         wireMockServer.stubFor(delete(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
168                 .willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT)));
169
170         instance.deleteVfModule("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>());
171     }
172
173     @Test
174     public void queryVfModule() throws Exception {
175         MsoRequest msoRequest = new MsoRequest();
176         msoRequest.setRequestId("12345");
177         msoRequest.setServiceInstanceId("12345");
178
179         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
180                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
181
182         instance.queryVnf("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>(), new Holder<>(),
183                 new Holder<>(), new Holder<>());
184     }
185
186     // TODO Error Tests
187 }