Containerization feature of SO
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / CloudOrchestrationTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2017 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.apihandlerinfra.tenantisolation;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.io.IOException;
28
29 import javax.ws.rs.core.MediaType;
30
31 import org.apache.commons.io.IOUtils;
32 import org.apache.commons.lang.CharEncoding;
33 import org.junit.Before;
34 import org.junit.Ignore;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.onap.so.apihandlerinfra.ApiHandlerApplication;
38 import org.onap.so.apihandlerinfra.BaseTest;
39 import org.onap.so.apihandlerinfra.Status;
40 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
41 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
42 import org.onap.so.db.request.beans.InfraActiveRequests;
43 import org.onap.so.db.request.data.repository.InfraActiveRequestsRepository;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.boot.context.embedded.LocalServerPort;
46 import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
47 import org.springframework.boot.test.context.SpringBootTest;
48 import org.springframework.boot.test.web.client.TestRestTemplate;
49 import org.springframework.http.HttpEntity;
50 import org.springframework.http.HttpHeaders;
51 import org.springframework.http.HttpMethod;
52 import org.springframework.http.ResponseEntity;
53 import org.springframework.test.context.ActiveProfiles;
54 import org.springframework.test.context.junit4.SpringRunner;
55 import org.springframework.web.util.UriComponentsBuilder;
56
57 import com.fasterxml.jackson.databind.ObjectMapper;
58
59
60 public class CloudOrchestrationTest extends BaseTest {
61         
62         private static final String path = "/onap/so/infra/cloudResources/v1";
63         private HttpHeaders headers = new HttpHeaders();
64
65         
66         @LocalServerPort
67         private int port;
68         
69         @Autowired 
70         private InfraActiveRequestsRepository iarRepo;
71         
72         @Test
73         public void testCreateOpEnvObjectMapperError() throws IOException {
74                 
75                 headers.set("Accept", MediaType.APPLICATION_JSON);
76                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
77                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
78         
79                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
80                        
81                 ResponseEntity<String> response = restTemplate.exchange(
82                                 builder.toUriString(),
83                                 HttpMethod.POST, entity, String.class);
84
85                 String body = response.getBody().toString();
86                 assertTrue(body.contains("Mapping of request to JSON object failed."));
87                 assertEquals(400, response.getStatusCodeValue());
88         }
89         
90         @Test
91         public void testCreateOpEnvError() throws IOException {
92                 
93                 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
94                                                                                         + "     \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
95                 headers.set("Accept", MediaType.APPLICATION_JSON);
96                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
97                 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
98         
99                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
100                        
101                 ResponseEntity<String> response = restTemplate.exchange(
102                                 builder.toUriString(),
103                                 HttpMethod.POST, entity, String.class);
104
105                 String body = response.getBody().toString();
106                 assertTrue(body.contains("Mapping of request to JSON object failed"));
107                 assertEquals(400, response.getStatusCodeValue());
108         }
109         
110         @Test
111         public void testCreateOpEnvReqRecord() throws IOException {
112                 ObjectMapper mapper = new ObjectMapper();
113                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
114                 headers.set("Accept", MediaType.APPLICATION_JSON);
115                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
116                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
117                 
118                 InfraActiveRequests iar = new InfraActiveRequests();
119                 iar.setRequestId("123");
120                 iar.setOperationalEnvName("myOpEnv");
121                 iar.setRequestScope("create");
122                 iar.setRequestStatus("PENDING");
123                 iar.setRequestAction("UNKNOWN");
124                 iarRepo.saveAndFlush(iar);
125                 
126                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
127                        
128                 ResponseEntity<String> response = restTemplate.exchange(
129                                 builder.toUriString(),
130                                 HttpMethod.POST, entity, String.class);
131                 
132                 assertEquals(409, response.getStatusCodeValue());
133         }
134         
135         @Test
136         public void testCreateOperationalEnvironment() throws IOException {
137                 ObjectMapper mapper = new ObjectMapper();
138                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
139                 headers.set("Accept", MediaType.APPLICATION_JSON);
140                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
141                 headers.set("X-TransactionID", "987654321");
142                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
143         
144                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
145                        
146                 ResponseEntity<String> response = restTemplate.exchange(
147                                 builder.toUriString(),
148                                 HttpMethod.POST, entity, String.class);
149                 InfraActiveRequests iar = iarRepo.findOneByRequestId("987654321");
150                 assertEquals(iar.getRequestBody(), mapper.writeValueAsString(request.getRequestDetails()));
151                 assertEquals(200, response.getStatusCodeValue());
152         }
153         
154         @Test
155         public void testCreateVNFDuplicateCheck() throws IOException {
156                 InfraActiveRequests iar = new InfraActiveRequests();
157                 iar.setRequestId("requestId");
158                 iar.setOperationalEnvName("myVnfOpEnv");
159                 iar.setRequestStatus(Status.IN_PROGRESS.toString());
160                 iar.setAction(Action.create.toString());
161                 iar.setRequestAction(Action.create.toString());
162                 iar.setRequestScope("UNKNOWN");
163                 iarRepo.saveAndFlush(iar);
164                 
165                 ObjectMapper mapper = new ObjectMapper();
166                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
167                 headers.set("Accept", MediaType.APPLICATION_JSON);
168                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
169                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
170         
171                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
172                        
173                 ResponseEntity<String> response = restTemplate.exchange(
174                                 builder.toUriString(),
175                                 HttpMethod.POST, entity, String.class);
176                 assertEquals(409, response.getStatusCodeValue());
177         }
178         
179         @Test
180         public void testCreateVNF() throws IOException {
181                 ObjectMapper mapper = new ObjectMapper();
182                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
183                 headers.set("Accept", MediaType.APPLICATION_JSON);
184                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
185                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
186         
187                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
188                        
189                 ResponseEntity<String> response = restTemplate.exchange(
190                                 builder.toUriString(),
191                                 HttpMethod.POST, entity, String.class);
192                 assertEquals(200, response.getStatusCodeValue());
193         }
194         
195         @Test
196         public void testActivate() throws IOException {
197                 ObjectMapper mapper = new ObjectMapper();
198                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"), TenantIsolationRequest.class);
199                 headers.set("Accept", MediaType.APPLICATION_JSON);
200                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
201                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
202         
203                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
204                        
205                 ResponseEntity<String> response = restTemplate.exchange(
206                                 builder.toUriString(),
207                                 HttpMethod.POST, entity, String.class);
208                 
209                 assertEquals(200, response.getStatusCodeValue());
210         }
211         
212         @Test
213         public void testDeactivate() throws IOException {
214                 InfraActiveRequests iar = new InfraActiveRequests();
215                 iar.setRequestId("ff3514e3-5a33-55df-13ab-12abad84e7fa");
216                 iar.setRequestStatus(Status.COMPLETE.toString());
217                 iar.setRequestAction("UNKNOWN");
218                 iar.setRequestScope("UNKNOWN");
219                 iarRepo.saveAndFlush(iar);
220                 
221                                 
222                 ObjectMapper mapper = new ObjectMapper();
223                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
224                 
225                 headers.set("Accept", MediaType.APPLICATION_JSON);
226                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
227                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
228
229         
230         
231                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
232                        
233                 ResponseEntity<String> response = restTemplate.exchange(
234                                 builder.toUriString(),
235                                 HttpMethod.POST, entity, String.class);
236                 
237                 assertEquals(200, response.getStatusCodeValue());
238         }
239         
240         @Test
241         public void testDeactivateThreadException() throws IOException {
242                 //need to simulate a 500 error
243                 /*CloudOrchestration co = new CloudOrchestration();
244                 TenantIsolationRequest tenantIsolationRequest = mock(TenantIsolationRequest.class);
245                 RequestsDatabase reqDB = mock(RequestsDatabase.class);
246                 TenantIsolationRunnable thread = mock(TenantIsolationRunnable.class);
247                 Response res = Response.status(500).entity("Failed creating a Thread").build();
248                 
249                 co.setRequestsDatabase(reqDB);
250                 co.setThread(thread);
251                 co.setTenantIsolationRequest(tenantIsolationRequest);
252                 String request = IOUtils.toString(ClassLoader.class.getResourceAsStream ("/DeactivateOperationEnvironment.json"), CharEncoding.UTF_8);
253                 when(reqDB.checkInstanceNameDuplicate(null, "myVnfOpEnv", "operationalEnvironment")).thenReturn(null);
254                 doNothing().when(tenantIsolationRequest).createRequestRecord(Status.IN_PROGRESS, Action.deactivate);
255                 doThrow(Exception.class).when(thread).run();
256                 when(tenantIsolationRequest.buildServiceErrorResponse(any(Integer.class), any(MsoException.class), any(String.class), any(String.class), any(List.class))).thenReturn(res);
257
258                 Response response = co.activateOperationEnvironment(request, null, "ff3514e3-5a33-55df-13ab-12abad84e7ff");
259                 assertEquals(500, response.getStatus());*/
260         }
261         
262         @Test
263         @Ignore
264         public void testDeactivateDupCheck() throws IOException {
265                 
266                 InfraActiveRequests iar = new InfraActiveRequests();
267                 iar.setRequestId("requestId");
268                 iar.setOperationalEnvName("myVnfOpEnv");
269                 iar.setRequestStatus(Status.IN_PROGRESS.toString());
270                 iar.setAction(Action.create.toString());
271                 iar.setRequestAction(Action.create.toString());
272                 iar.setRequestScope("UNKNOWN");
273                 iarRepo.saveAndFlush(iar);
274                 ObjectMapper mapper = new ObjectMapper();
275                 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
276                 
277                 headers.set("Accept", MediaType.APPLICATION_JSON);
278                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
279                 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
280         
281                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
282                        
283                 ResponseEntity<String> response = restTemplate.exchange(
284                                 builder.toUriString(),
285                                 HttpMethod.POST, entity, String.class);
286                 assertEquals(409, response.getStatusCodeValue());
287         }
288         
289 }