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