18af7fbaa090d9eeb44d38170604c09b37e3b56e
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / process / ActivateVnfOperationalEnvironmentTest.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.get;
25 import static com.github.tomakehurst.wiremock.client.WireMock.post;
26 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
29 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
30 import static com.github.tomakehurst.wiremock.client.WireMock.containing;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertNotNull;
33
34 import java.util.ArrayList;
35 import java.util.List;
36
37 import com.fasterxml.jackson.core.JsonProcessingException;
38 import com.fasterxml.jackson.databind.ObjectMapper;
39 import org.apache.http.HttpStatus;
40 import org.json.JSONObject;
41 import org.junit.Before;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.rules.ExpectedException;
45 import org.onap.aai.domain.yang.OperationalEnvironment;
46 import org.onap.so.apihandlerinfra.BaseTest;
47 import org.onap.so.apihandlerinfra.exceptions.ApiException;
48 import org.onap.so.apihandlerinfra.exceptions.ValidateException;
49 import org.onap.so.apihandlerinfra.tenantisolation.CloudOrchestrationRequest;
50 import org.onap.so.apihandlerinfra.tenantisolation.helpers.AAIClientHelper;
51 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestDetails;
52 import org.onap.so.apihandlerinfra.tenantisolationbeans.RequestParameters;
53 import org.onap.so.apihandlerinfra.tenantisolationbeans.ServiceModelList;
54 import org.onap.so.apihandlerinfra.tenantisolationbeans.RecoveryAction;
55 import org.onap.so.apihandlerinfra.tenantisolationbeans.Manifest;
56 import org.onap.so.client.aai.AAIVersion;
57 import org.onap.so.client.aai.entities.AAIResultWrapper;
58 import org.onap.so.db.request.beans.InfraActiveRequests;
59 import org.springframework.beans.factory.annotation.Autowired;
60
61 import javax.ws.rs.core.HttpHeaders;
62 import javax.ws.rs.core.MediaType;
63
64
65 public class ActivateVnfOperationalEnvironmentTest extends BaseTest{
66
67         @Rule
68         public ExpectedException thrown = ExpectedException.none();
69
70         @Autowired
71         private ActivateVnfOperationalEnvironment activateVnf;
72         @Autowired
73         private AAIClientHelper clientHelper;
74
75         private final String requestId = "TEST_requestId";
76         private final String operationalEnvironmentId = "1dfe7154-eae0-44f2-8e7a-8e5e7882e55d"; 
77         private final CloudOrchestrationRequest request = new CloudOrchestrationRequest();
78         private final String workloadContext = "PVT";
79         String recoveryActionRetry  = "RETRY";
80         private final String serviceModelVersionId = "TEST_serviceModelVersionId";      
81         int retryCount = 3;
82         private final String sdcDistributionId = "TEST_distributionId";
83         private final String statusSent = "SENT";
84         private final ObjectMapper mapper = new ObjectMapper();
85
86         @Before
87         public void init(){
88                 stubFor(post(urlPathEqualTo("/operationalEnvServiceModelStatus/"))
89                                 .withRequestBody(equalTo("{\"requestId\":\"TEST_requestId\",\"operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\",\"serviceModelVersionId\":\"TEST_serviceModelVersionId\",\"serviceModelVersionDistrStatus\":\"SENT\",\"recoveryAction\":\"RETRY\",\"retryCount\":3,\"workloadContext\":\"PVT\",\"createTime\":null,\"modifyTime\":null}"))
90                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
91                                                 .withStatus(HttpStatus.SC_OK)));
92                 stubFor(post(urlPathEqualTo("/operationalEnvDistributionStatus/"))
93                                 .withRequestBody(equalTo("{\"distributionId\":\"TEST_distributionId\",\"operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\",\"serviceModelVersionId\":\"TEST_serviceModelVersionId\",\"requestId\":\"TEST_requestId\",\"distributionIdStatus\":\"SENT\",\"distributionIdErrorReason\":\"\",\"createTime\":null,\"modifyTime\":null}"))
94                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
95                                                 .withStatus(HttpStatus.SC_OK)));
96         }
97         
98
99         @Test
100         public void getAAIOperationalEnvironmentTest() {
101
102                 OperationalEnvironment aaiOpEnv;
103
104                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
105                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironmentWithRelationship.json").withStatus(HttpStatus.SC_ACCEPTED)));
106                 
107                 AAIResultWrapper wrapper = clientHelper.getAaiOperationalEnvironment("EMOE-001");
108                 aaiOpEnv = wrapper.asBean(OperationalEnvironment.class).get();
109                 assertEquals("EMOE-001", aaiOpEnv.getOperationalEnvironmentId());                       
110                 assertEquals("1dfe7154-eae0-44f2-8e7a-8e5e7882e55d", aaiOpEnv.getRelationshipList().getRelationship().get(0).getRelationshipData().get(0).getRelationshipValue());
111                 assertNotNull(activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId));      
112                 assertEquals( "EMOE-001", activateVnf.getAAIOperationalEnvironment(operationalEnvironmentId).asBean(OperationalEnvironment.class).get().getOperationalEnvironmentId());         
113                 
114         }       
115                 
116         @Test
117         public void executionTest() throws Exception {
118
119                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
120                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
121                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
122                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
123                 serviceModelVersionIdList.add(serviceModelList1);
124                 
125                 RequestDetails requestDetails = new RequestDetails();
126                 RequestParameters requestParameters = new RequestParameters();
127                 Manifest manifest = new Manifest();
128                 manifest.setServiceModelList(serviceModelVersionIdList);
129                 requestParameters.setManifest(manifest);
130                 requestParameters.setWorkloadContext(workloadContext);
131                 requestDetails.setRequestParameters(requestParameters);
132                 
133                 request.setOperationalEnvironmentId(operationalEnvironmentId);
134                 request.setRequestDetails(requestDetails);
135                 
136                 JSONObject jsonObject = new JSONObject();
137                 jsonObject.put("statusCode", "202");
138                 jsonObject.put("message", "Success");
139                 jsonObject.put("distributionId", sdcDistributionId);
140                 
141                 stubFor(get(urlPathMatching("/aai/" + AAIVersion.LATEST + "/cloud-infrastructure/operational-environments/.*"))
142                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBodyFile("vnfoperenv/ecompOperationalEnvironmentWithRelationship.json").withStatus(HttpStatus.SC_ACCEPTED)));
143                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
144                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
145                 activateVnf.execute(requestId, request);
146         }                       
147         
148         @Test
149         public void processActivateSDCRequestTest_202() throws Exception {
150
151                 String distributionId = "TEST_distributionId";
152                 
153                 JSONObject jsonObject = new JSONObject();
154                 jsonObject.put("statusCode", "202");
155                 jsonObject.put("message", "Success");
156                 jsonObject.put("distributionId", distributionId);
157                 
158                 // prepare request detail
159                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
160                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
161                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
162                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
163                 serviceModelVersionIdList.add(serviceModelList1);
164                 
165                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
166                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
167                 
168                 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);
169         }       
170         
171         @Test
172         public void processActivateSDCRequestTest_409() throws ApiException, JsonProcessingException {
173
174                 // ERROR in asdc
175                 JSONObject jsonMessages = new JSONObject();
176                 jsonMessages.put("message", "Failure");
177                 jsonMessages.put("messageId", "SVC4675");
178                 jsonMessages.put("text", "Error: Service state is invalid for this action.");
179                 JSONObject jsonServException = new JSONObject();
180                 jsonServException.put("policyException", jsonMessages);
181                 //jsonServException.put("serviceException", jsonMessages);
182                 JSONObject jsonErrorResponse = new JSONObject();
183                 jsonErrorResponse.put("requestError", jsonServException);
184                 
185                 // prepare request detail
186                 List<ServiceModelList> serviceModelVersionIdList = new ArrayList<>();
187                 ServiceModelList serviceModelList1 = new ServiceModelList(); 
188                 serviceModelList1.setRecoveryAction(RecoveryAction.retry);
189                 serviceModelList1.setServiceModelVersionId(serviceModelVersionId);
190                 serviceModelVersionIdList.add(serviceModelList1);
191                 
192                 InfraActiveRequests iar = new InfraActiveRequests();
193                 iar.setRequestId(requestId);
194                 iar.setRequestStatus("PENDING");
195                 stubFor(get(urlPathEqualTo("/infraActiveRequests/"+requestId))
196                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
197                                                 .withBody(mapper.writeValueAsString(iar))
198                                                 .withStatus(HttpStatus.SC_OK)));
199                 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_serviceModelVersionId/distr.*"))
200                                 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_CONFLICT)));
201                 stubFor(post(urlPathEqualTo("/infraActiveRequests/"))
202                                 .withRequestBody(containing("operationalEnvId\":\"1dfe7154-eae0-44f2-8e7a-8e5e7882e55d\""))
203                                 .willReturn(aResponse().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON)
204                                                 .withStatus(HttpStatus.SC_OK)));
205
206                 thrown.expect(ValidateException.class);
207
208                 activateVnf.processActivateSDCRequest(requestId, operationalEnvironmentId, serviceModelVersionIdList, workloadContext);
209         }               
210         
211 }