1e11b74215bbc38300ee74d3412b60e035b458ec
[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 com.fasterxml.jackson.databind.ObjectMapper;
24 import org.apache.http.HttpStatus;
25 import org.junit.Before;
26 import org.junit.Ignore;
27 import org.junit.Test;
28 import org.onap.so.apihandlerinfra.BaseTest;
29 import org.onap.so.apihandlerinfra.Status;
30 import org.onap.so.apihandlerinfra.tenantisolationbeans.Action;
31 import org.onap.so.apihandlerinfra.tenantisolationbeans.TenantIsolationRequest;
32 import org.onap.so.db.request.beans.InfraActiveRequests;
33 import org.springframework.http.HttpEntity;
34 import org.springframework.http.HttpHeaders;
35 import org.springframework.http.HttpMethod;
36 import org.springframework.http.ResponseEntity;
37 import org.springframework.web.util.UriComponentsBuilder;
38
39 import javax.ws.rs.core.MediaType;
40 import java.io.File;
41 import java.io.IOException;
42
43 import static com.github.tomakehurst.wiremock.client.WireMock.*;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertTrue;
46
47
48 public class CloudOrchestrationTest extends BaseTest {
49         
50         private static final String path = "/onap/so/infra/cloudResources/v1";
51
52         private HttpHeaders headers = new HttpHeaders();
53
54         @Before
55         public void setupTestClass() throws Exception{
56                 stubFor(post(urlPathEqualTo(getTestUrl(""))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON).withStatus(HttpStatus.SC_CREATED)));
57         }
58
59         @Test
60         public void testCreateOpEnvObjectMapperError() throws IOException {
61                 
62                 headers.set("Accept", MediaType.APPLICATION_JSON);
63                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
64                 HttpEntity<String> entity = new HttpEntity<String>(null, headers);
65         
66                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
67                 ResponseEntity<String> response = restTemplate.exchange(
68                                 builder.toUriString(),
69                                 HttpMethod.POST, entity, String.class);
70
71                 String body = response.getBody().toString();
72                 assertTrue(body.contains("Mapping of request to JSON object failed."));
73                 assertEquals(400, response.getStatusCodeValue());
74         }
75         
76         @Test
77         public void testCreateOpEnvError() throws IOException {
78                 
79                 String request = "{\"requestDetails\":{\"requestInfo\":{\"resourceType\":\"operationalEnvironment\",\"instanceName\": \"myOpEnv\",\"source\": \"VID\",\"requestorId\": \"xxxxxx\"},"
80                                                                                         + "     \"requestParameters\": {\"tenantContext\": \"Test\",\"workloadContext\": \"ECOMP_E2E-IST\"}}}";
81                 headers.set("Accept", MediaType.APPLICATION_JSON);
82                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
83                 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
84         
85                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
86                        
87                 ResponseEntity<String> response = restTemplate.exchange(
88                                 builder.toUriString(),
89                                 HttpMethod.POST, entity, String.class);
90
91                 String body = response.getBody().toString();
92                 assertTrue(body.contains("Mapping of request to JSON object failed"));
93                 assertEquals(400, response.getStatusCodeValue());
94         }
95         
96         @Test
97         public void testCreateOpEnvReqRecordDuplicateCheck() throws IOException {
98                 stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
99                                 .withBody(String.format(getResponseTemplate, "123", "PENDING"))
100                                 .withStatus(HttpStatus.SC_OK)));
101                 ObjectMapper mapper = new ObjectMapper();
102                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
103                 headers.set("Accept", MediaType.APPLICATION_JSON);
104                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
105                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
106
107
108                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
109                        
110                 ResponseEntity<String> response = restTemplate.exchange(
111                                 builder.toUriString(),
112                                 HttpMethod.POST, entity, String.class);
113                 
114                 assertEquals(409, response.getStatusCodeValue());
115         }
116         
117         @Test
118         public void testCreateOperationalEnvironment() throws IOException {
119                 ObjectMapper mapper = new ObjectMapper();
120                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ECOMPOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
121                 headers.set("Accept", MediaType.APPLICATION_JSON);
122                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
123                 headers.set("X-TransactionID", "987654321");
124                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
125         
126                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
127                 stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
128                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
129                 ResponseEntity<String> response = restTemplate.exchange(
130                                 builder.toUriString(),
131                                 HttpMethod.POST, entity, String.class);
132                 assertEquals(200, response.getStatusCodeValue());
133         }
134         
135         @Test
136         public void testCreateVNFDuplicateCheck() throws IOException {
137                 stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
138                                 .withBody(String.format(getResponseTemplate, "requestId", Status.IN_PROGRESS.toString()))
139                                 .withStatus(HttpStatus.SC_OK)));
140                 ObjectMapper mapper = new ObjectMapper();
141                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
142                 headers.set("Accept", MediaType.APPLICATION_JSON);
143                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
144                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
145         
146                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
147                        
148                 ResponseEntity<String> response = restTemplate.exchange(
149                                 builder.toUriString(),
150                                 HttpMethod.POST, entity, String.class);
151                 assertEquals(409, response.getStatusCodeValue());
152         }
153         
154         @Test
155         public void testCreateVNF() throws IOException {
156                 ObjectMapper mapper = new ObjectMapper();
157                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/VNFOperationEnvironmentCreate.json"), TenantIsolationRequest.class);
158                 headers.set("Accept", MediaType.APPLICATION_JSON);
159                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
160                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
161         
162                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments");
163                 stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":null,\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
164                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
165
166                 ResponseEntity<String> response = restTemplate.exchange(
167                                 builder.toUriString(),
168                                 HttpMethod.POST, entity, String.class);
169                 assertEquals(200, response.getStatusCodeValue());
170         }
171         
172         @Test
173         public void testActivate() throws IOException {
174                 ObjectMapper mapper = new ObjectMapper();
175                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/ActivateOperationEnvironment.json"), TenantIsolationRequest.class);
176                 headers.set("Accept", MediaType.APPLICATION_JSON);
177                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
178                 HttpEntity<TenantIsolationRequest> entity = new HttpEntity<TenantIsolationRequest>(request, headers);
179         
180                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/activate");
181                 stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7ff\"},\"instanceName\":\"myVnfOpEnv\",\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
182                                 .withStatus(HttpStatus.SC_NOT_FOUND)));
183
184                 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7ff"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
185                                 .withStatus(HttpStatus.SC_OK)));
186
187                 ResponseEntity<String> response = restTemplate.exchange(
188                                 builder.toUriString(),
189                                 HttpMethod.POST, entity, String.class);
190                 
191                 assertEquals(200, response.getStatusCodeValue());
192         }
193         
194         @Test
195         public void testDeactivate() throws IOException {
196                 ObjectMapper mapper = new ObjectMapper();
197                 TenantIsolationRequest request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), TenantIsolationRequest.class);
198                 
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
204 //              stubFor(post(urlPathEqualTo(getTestUrl("checkInstanceNameDuplicate"))).withRequestBody(equalTo("{\"instanceIdMap\":{\"operationalEnvironmentId\":\"ff3514e3-5a33-55df-13ab-12abad84e7fa\"},\"instanceName\":null,\"requestScope\":\"operationalEnvironment\"}")).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
205 //                              .withBodyFile((String.format(getResponseTemplate, "ff3514e3-5a33-55df-13ab-12abad84e7fa", Status.COMPLETE.toString()))).withStatus(HttpStatus.SC_OK)));
206
207                 stubFor(get(urlPathEqualTo(getTestUrl("checkVnfIdStatus/ff3514e3-5a33-55df-13ab-12abad84e7fa"))).willReturn(aResponse().withHeader(javax.ws.rs.core.HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
208                                 .withStatus(HttpStatus.SC_OK)));
209
210                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7fa/deactivate");
211                        
212                 ResponseEntity<String> response = restTemplate.exchange(
213                                 builder.toUriString(),
214                                 HttpMethod.POST, entity, String.class);
215                 
216                 assertEquals(200, response.getStatusCodeValue());
217         }
218         
219
220         @Test
221         @Ignore
222         public void testDeactivateDupCheck() throws IOException {
223                 
224                 InfraActiveRequests iar = new InfraActiveRequests();
225                 iar.setRequestId("requestId");
226                 iar.setOperationalEnvName("myVnfOpEnv");
227                 iar.setRequestStatus(Status.IN_PROGRESS.toString());
228                 iar.setAction(Action.create.toString());
229                 iar.setRequestAction(Action.create.toString());
230                 iar.setRequestScope("UNKNOWN");
231                 //iarRepo.saveAndFlush(iar);
232                 ObjectMapper mapper = new ObjectMapper();
233                 String request = mapper.readValue(new File("src/test/resources/TenantIsolation/DeactivateOperationEnvironment.json"), String.class);
234                 
235                 headers.set("Accept", MediaType.APPLICATION_JSON);
236                 headers.set("Content-Type", MediaType.APPLICATION_JSON);
237                 HttpEntity<String> entity = new HttpEntity<String>(request, headers);
238         
239                 UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(createURLWithPort(path) + "/operationalEnvironments/ff3514e3-5a33-55df-13ab-12abad84e7ff/deactivate");
240                        
241                 ResponseEntity<String> response = restTemplate.exchange(
242                                 builder.toUriString(),
243                                 HttpMethod.POST, entity, String.class);
244                 assertEquals(409, response.getStatusCodeValue());
245         }
246         
247 }