replace all fixed wiremock ports
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / process / CreateVnfOperationalEnvironmentTest.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.process;
22
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.containing;
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.put;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
30 import static org.junit.Assert.assertEquals;
31
32 import java.io.File;
33 import java.nio.file.Files;
34 import java.util.List;
35 import java.util.UUID;
36
37 import javax.ws.rs.core.HttpHeaders;
38 import javax.ws.rs.core.MediaType;
39
40 import org.apache.http.HttpStatus;
41 import org.junit.Before;
42 import org.junit.Test;
43 import org.onap.aai.domain.yang.OperationalEnvironment;
44 import org.onap.so.apihandlerinfra.BaseTest;
45 import org.onap.so.apihandlerinfra.exceptions.ApiException;
46 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
47 import org.onap.so.client.aai.AAIVersion;
48 import org.onap.so.client.grm.beans.Property;
49 import org.onap.so.client.grm.beans.ServiceEndPointList;
50 import org.onap.so.db.request.beans.InfraActiveRequests;
51 import org.springframework.beans.factory.annotation.Autowired;
52
53 import com.fasterxml.jackson.core.JsonProcessingException;
54 import com.fasterxml.jackson.databind.ObjectMapper;
55
56
57 public class CreateVnfOperationalEnvironmentTest extends BaseTest{
58         
59         private CloudOrchestrationRequest request;
60         private ServiceEndPointList serviceEndpoints;
61         private ServiceEndPointList serviceEndpoints2;
62         
63         @Autowired
64         private CreateVnfOperationalEnvironment createVnfOpEnv;
65
66         @Before
67         public void testSetUp() throws Exception {
68                 ObjectMapper mapper = new ObjectMapper();
69                 String jsonRequest = getFileContentsAsString("__files/vnfoperenv/createVnfOperationalEnvironmentRequest.json");
70                 request = mapper.readValue(jsonRequest, CloudOrchestrationRequest.class);
71                 String jsonServiceEndpoints = getFileContentsAsString("__files/vnfoperenv/endpoints.json");
72                 serviceEndpoints = mapper.readValue(jsonServiceEndpoints, ServiceEndPointList.class);
73                 String jsonServiceEndpoints2 = getFileContentsAsString("__files/vnfoperenv/endpoints2.json");
74                 serviceEndpoints2 = mapper.readValue(jsonServiceEndpoints2, ServiceEndPointList.class);
75         }
76         @Test
77         public void testGetEcompManagingEnvironmentId() throws Exception { 
78                 createVnfOpEnv.setRequest(request);
79                 assertEquals("ff305d54-75b4-431b-adb2-eb6b9e5ff000", createVnfOpEnv.getEcompManagingEnvironmentId());
80         }
81         
82         @Test
83         public void testGetTenantContext() throws Exception { 
84                 createVnfOpEnv.setRequest(request);
85                 assertEquals("Test", createVnfOpEnv.getTenantContext());
86         }
87         
88         @Test
89         public void testGetEnvironmentName() {
90                 createVnfOpEnv.setRequest(request);
91                 List<Property> props = serviceEndpoints.getServiceEndPointList().get(0).getProperties();
92                 assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
93         }
94         
95         @Test
96         public void testGetEnvironmentNameEndpointListBeginWithUpperCase() {
97                 createVnfOpEnv.setRequest(request);
98                 List<Property> props = serviceEndpoints2.getServiceEndPointList().get(0).getProperties();
99                 assertEquals("DEV", createVnfOpEnv.getEnvironmentName(props));
100         }
101         
102         @Test 
103         public void testBuildServiceNameForVnf() throws Exception {
104                 createVnfOpEnv.setRequest(request);
105                 assertEquals("Test.VNF_E2E-IST.Inventory", createVnfOpEnv.buildServiceNameForVnf("TEST.ECOMP_PSL.Inventory"));
106         }
107         
108         @Test
109         public void testGetSearchKey() {
110                 createVnfOpEnv.setRequest(request);
111                 OperationalEnvironment ecompEnv = new OperationalEnvironment();
112                 ecompEnv.setTenantContext("Test");
113                 ecompEnv.setWorkloadContext("ECOMPL_PSL");
114                 assertEquals("Test.ECOMPL_PSL.*", createVnfOpEnv.getSearchKey(ecompEnv));
115         }
116         
117         public String getFileContentsAsString(String fileName) {
118                 String content = "";
119                 try {
120                         ClassLoader classLoader = this.getClass().getClassLoader();
121                         File file = new File(classLoader.getResource(fileName).getFile());
122                         content = new String(Files.readAllBytes(file.toPath()));
123                 }
124                 catch(Exception e) {
125                         e.printStackTrace();
126                         System.out.println("Exception encountered reading " + fileName + ". Error: " + e.getMessage());
127                 }
128                 return content;
129         }
130         
131         @Test
132         public void testExecute() throws ApiException, JsonProcessingException {
133                 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
134                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
135                 wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
136                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/endpoints.json").withStatus(HttpStatus.SC_ACCEPTED)));
137                 wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add"))
138                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
139                 wireMockServer.stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
140                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
141                 String requestId = UUID.randomUUID().toString();
142                 InfraActiveRequests iar = new InfraActiveRequests();
143                 iar.setRequestId(requestId);
144                 iar.setOperationalEnvName("myOpEnv");
145                 iar.setRequestScope("create");
146                 iar.setRequestStatus("PENDING");
147                 iar.setRequestAction("UNKNOWN");
148                 ObjectMapper mapper = new ObjectMapper();
149                 wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
150                                 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
151                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
152                                                 .withStatus(HttpStatus.SC_OK)));
153
154                 wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
155                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
156                                                 .withBody(mapper.writeValueAsString(iar))
157                                                 .withStatus(HttpStatus.SC_OK)));
158                 createVnfOpEnv.execute(requestId, request);
159         }
160         
161         @Test
162         public void testExecuteEndpointsListBeginWithUppercase() throws ApiException, JsonProcessingException {
163                 wireMockServer.stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
164                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
165                 wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/findRunning"))
166                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/endpoints2.json").withStatus(HttpStatus.SC_ACCEPTED)));
167                 wireMockServer.stubFor(post(urlPathMatching("/GRMLWPService/v1/serviceEndPoint/add"))
168                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withStatus(HttpStatus.SC_ACCEPTED)));
169                 wireMockServer.stubFor(put(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
170                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironment.json").withStatus(HttpStatus.SC_ACCEPTED)));
171                 String requestId = UUID.randomUUID().toString();
172                 InfraActiveRequests iar = new InfraActiveRequests();
173                 iar.setRequestId(requestId);
174                 iar.setOperationalEnvName("myOpEnv");
175                 iar.setRequestScope("create");
176                 iar.setRequestStatus("PENDING");
177                 iar.setRequestAction("UNKNOWN");
178                 ObjectMapper mapper = new ObjectMapper();
179                 wireMockServer.stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
180                                 .withRequestBody(containing("{\"requestId\":\""+ requestId+"\",\"clientRequestId\":null,\"action\":null,\"requestStatus\":\"COMPLETE\",\"statusMessage\":\"SUCCESSFUL"))
181                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
182                                                 .withStatus(HttpStatus.SC_OK)));
183
184                 wireMockServer.stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
185                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
186                                                 .withBody(mapper.writeValueAsString(iar))
187                                                 .withStatus(HttpStatus.SC_OK)));
188                 createVnfOpEnv.execute(requestId, request);
189         }
190 }