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