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