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