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