9d13cc10a3616dc57bb7b9a45e1e8cf1298491f4
[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.urlPathEqualTo;
28 import static org.junit.Assert.assertEquals;
29 import static org.junit.Assert.assertTrue;
30 import static org.junit.Assert.fail;
31 import java.util.HashMap;
32 import java.util.Map;
33 import javax.xml.ws.Holder;
34 import org.apache.http.HttpStatus;
35 import org.junit.Before;
36 import org.junit.Rule;
37 import org.junit.Test;
38 import org.junit.rules.ExpectedException;
39 import org.onap.so.adapters.vdu.VduModelInfo;
40 import org.onap.so.adapters.vnf.exceptions.VnfException;
41 import org.onap.so.cloud.CloudConfig;
42 import org.onap.so.entity.MsoRequest;
43 import org.onap.so.openstack.beans.StackInfo;
44 import org.onap.so.openstack.exceptions.MsoException;
45 import org.springframework.beans.factory.annotation.Autowired;
46
47 public class MsoVnfMulticloudAdapterImplTest extends BaseRestTestUtils {
48     @Rule
49     public ExpectedException expectedException = ExpectedException.none();
50
51     @Autowired
52     private MsoVnfPluginAdapterImpl instance;
53
54     @Autowired
55     private CloudConfig cloudConfig;
56
57     private static final String CREATE_STACK_RESPONSE = "{\"template_type\": \"TEST-template\", \"workload_id\": "
58             + "\"workload-id\", \"template_response\": {\"stack\": {\"id\": \"TEST-stack\", \"links\": []}}}";
59     private static final String UPDATE_STACK_RESPONSE =
60             "{\"template_type\": \"heat\", \"workload_id\": " + "\"workload-id\"}";
61     private static final String GET_CREATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
62             + "\"workload-id\", \"workload_status\": \"CREATE_COMPLETE\"}";
63     private static final String GET_UPDATE_STACK_RESPONSE = "{\"template_type\": \"heat\", \"workload_id\": "
64             + "\"workload-id\", \"workload_status\": \"UPDATE_COMPLETE\"}";
65
66     private static final String MULTICLOUD_CREATE_PATH = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
67     private static final String MULTICLOUD_UPDATE_PATH =
68             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
69     private static final String MULTICLOUD_GET_PATH_BY_NAME =
70             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/vfname";
71     private static final String MULTICLOUD_GET_PATH_BY_ID =
72             "/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id";
73
74     @Before
75     public void before() throws Exception {
76         super.orchestrator = "multicloud";
77         super.cloudEndpoint = "/api/multicloud/v1/CloudOwner/MTN13/infra_workload";
78         super.setUp();
79     }
80
81     @Test
82     public void createVfModule() throws Exception {
83
84         Map<String, Object> stackInputs = new HashMap<>();
85         stackInputs.put("oof_directives", "{}");
86         stackInputs.put("sdnc_directives", "{}");
87         stackInputs.put("user_directives", "{}");
88         stackInputs.put("generic_vnf_id", "genVNFID");
89         stackInputs.put("vf_module_id", "vfMODULEID");
90
91         MsoRequest msoRequest = new MsoRequest();
92         msoRequest.setRequestId("12345");
93         msoRequest.setServiceInstanceId("12345");
94
95         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_NAME)).willReturn(
96                 aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_NOT_FOUND)));
97
98         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
99                 .whenScenarioStateIs("CREATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
100                         .withBody(GET_CREATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
101
102         wireMockServer.stubFor(get(urlPathEqualTo(MULTICLOUD_GET_PATH_BY_ID)).inScenario("CREATE")
103                 .whenScenarioStateIs("UPDATING").willReturn(aResponse().withHeader("Content-Type", "application/json")
104                         .withBody(GET_UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_OK)));
105
106         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_CREATE_PATH)).inScenario("CREATE")
107                 .willReturn(aResponse().withHeader("Content-Type", "application/json")
108                         .withBodyFile("MulticloudResponse_Stack_Create.json").withStatus(HttpStatus.SC_CREATED))
109                 .willSetStateTo("CREATING"));
110
111         wireMockServer.stubFor(post(urlPathEqualTo(MULTICLOUD_UPDATE_PATH))
112                 .inScenario("CREATE").willReturn(aResponse().withHeader("Content-Type", "application/json")
113                         .withBody(UPDATE_STACK_RESPONSE).withStatus(HttpStatus.SC_ACCEPTED))
114                 .willSetStateTo("UPDATING"));
115
116         try {
117             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
118                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
119                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
120         } catch (VnfException e) {
121             fail("createVfModule success expected, failed with exception: " + e.toString());
122         }
123         wireMockServer.resetScenarios();
124     }
125
126     @Test
127     public void createVfModuleAlreadyExists() throws Exception {
128
129         Map<String, Object> stackInputs = new HashMap<>();
130         stackInputs.put("oof_directives", "{}");
131         stackInputs.put("sdnc_directives", "{}");
132         stackInputs.put("user_directives", "{}");
133         stackInputs.put("generic_vnf_id", "genVNFID");
134         stackInputs.put("vf_module_id", "vfMODULEID");
135
136         MsoRequest msoRequest = new MsoRequest();
137         msoRequest.setRequestId("12345");
138         msoRequest.setServiceInstanceId("12345");
139
140         wireMockServer.stubFor(
141                 get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/vfname")).willReturn(aResponse()
142                         // .withHeader()
143                         .withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
144
145         try {
146             instance.createVfModule("MTN13", "CloudOwner", "123", "vf", "v1", "genericVnfId", "vfname", "vfModuleId",
147                     "create", null, "234", "9b339a61-69ca-465f-86b8-1c72c582b8e8", stackInputs, true, true, true,
148                     msoRequest, new Holder<>(), new Holder<>(), new Holder<>());
149         } catch (VnfException e) {
150             assertTrue(e.toString().contains(
151                     "Resource vfname already exists in owner/cloud/tenant CloudOwner/MTN13/123 with ID vfname/vfname"));
152             return;
153         }
154         fail("VnfAlreadyExists Exception expected!");
155     }
156
157     @Test
158     public void deleteVfModule() throws Exception {
159         MsoRequest msoRequest = new MsoRequest();
160         msoRequest.setRequestId("12345");
161         msoRequest.setServiceInstanceId("12345");
162
163         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
164                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
165
166         wireMockServer.stubFor(delete(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
167                 .willReturn(aResponse().withStatus(HttpStatus.SC_NO_CONTENT)));
168
169         instance.deleteVfModule("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>());
170     }
171
172     @Test
173     public void queryVfModule() throws Exception {
174         MsoRequest msoRequest = new MsoRequest();
175         msoRequest.setRequestId("12345");
176         msoRequest.setServiceInstanceId("12345");
177
178         wireMockServer.stubFor(get(urlPathEqualTo("/api/multicloud/v1/CloudOwner/MTN13/infra_workload/workload-id"))
179                 .willReturn(aResponse().withBodyFile("MulticloudResponse_Stack.json").withStatus(HttpStatus.SC_OK)));
180
181         instance.queryVnf("MTN13", "CloudOwner", "123", "workload-id", msoRequest, new Holder<>(), new Holder<>(),
182                 new Holder<>(), new Holder<>());
183     }
184
185     // TODO Error Tests
186 }