2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.so.apihandlerinfra.tenantisolation.helpers;
23 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
24 import static com.github.tomakehurst.wiremock.client.WireMock.post;
25 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.fail;
28 import org.apache.http.HttpStatus;
29 import org.json.JSONException;
30 import org.json.JSONObject;
31 import org.junit.Test;
32 import org.onap.so.apihandlerinfra.BaseTest;
33 import org.onap.so.apihandlerinfra.exceptions.ApiException;
34 import org.springframework.beans.factory.annotation.Autowired;
36 public class SDCClientHelperTest extends BaseTest {
38 String serviceModelVersionId = "TEST_uuid1";
39 String operationalEnvironmentId = "TEST_operationalEnvironmentId";
40 String workloadContext = "TEST_workloadContext";
43 private SDCClientHelper sdcClientUtils;
46 public void postActivateOperationalEnvironment_Test() throws ApiException {
48 JSONObject jsonObject = new JSONObject();
49 jsonObject.put("statusCode", "202");
50 jsonObject.put("message", "Success");
51 jsonObject.put("distributionId", "TEST_distributionId");
53 wireMockServer.stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
54 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString())
55 .withStatus(HttpStatus.SC_ACCEPTED)));
57 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId,
58 operationalEnvironmentId, workloadContext);
60 assertEquals("202", jsonResponse.get("statusCode"));
61 assertEquals("Success", jsonResponse.get("message"));
66 public void postActivateOperationalEnvironment_InvalidJson_Test() throws ApiException {
68 // ERROR in asdc response, invalid json object
69 JSONObject jsonErrorResponse = new JSONObject();
70 jsonErrorResponse.put("requestError", "");
72 wireMockServer.stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
73 .willReturn(aResponse().withHeader("Content-Type", "application/json")
74 .withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_BAD_REQUEST)));
76 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId,
77 operationalEnvironmentId, workloadContext);
79 assertEquals("500", jsonResponse.get("statusCode"));
80 assertEquals("", jsonResponse.get("messageId"));
82 " Encountered Error while calling SDC POST Activate. JSONObject[\"requestError\"] is not a JSONObject.",
83 jsonResponse.get("message"));
88 public void buildUriBuilderTest() {
91 String url = sdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
93 "http://localhost:" + env.getProperty("wiremock.server.port")
94 + "/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate",
97 } catch (Exception e) {
98 fail("Exception caught: " + e.getMessage());
105 public void buildJsonWorkloadContextTest() throws JSONException {
107 String jsonPayload = sdcClientUtils.buildJsonWorkloadContext(workloadContext);
108 assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
113 public void enhanceJsonResponseTest_Success() throws JSONException {
115 // build success response data
116 JSONObject sdcResponseJsonObj = new JSONObject();
117 sdcResponseJsonObj.put("distributionId", "TEST_distributionId");
119 int statusCode = 202;
120 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
122 assertEquals("202", sdcResponseJsonObj.getString("statusCode"));
123 assertEquals("", sdcResponseJsonObj.getString("messageId"));
124 assertEquals("Success", sdcResponseJsonObj.getString("message"));
125 assertEquals("TEST_distributionId", sdcResponseJsonObj.getString("distributionId"));
130 public void enhanceJsonResponseTest_Error() throws JSONException {
132 // build error response data
133 JSONObject jsonMessages = new JSONObject();
134 jsonMessages.put("messageId", "SVC4675");
135 jsonMessages.put("text", "Error: Service state is invalid for this action.");
136 JSONObject jsonServException = new JSONObject();
137 jsonServException.put("serviceException", jsonMessages);
138 JSONObject jsonErrorRequest = new JSONObject();
139 jsonErrorRequest.put("requestError", jsonServException);
141 String responseData = jsonErrorRequest.toString();
143 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
144 int statusCode = 409;
145 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
147 assertEquals("409", sdcResponseJsonObj.getString("statusCode"));
148 assertEquals("SVC4675", sdcResponseJsonObj.getString("messageId"));
149 assertEquals("Error: Service state is invalid for this action.", sdcResponseJsonObj.getString("message"));
154 public void enhanceJsonResponseTest_Error_policyException() throws JSONException {
156 // build error response data
157 JSONObject jsonMessages = new JSONObject();
158 jsonMessages.put("messageId", "POL5003");
159 jsonMessages.put("text", "Error: Not authorized to use the API.");
160 JSONObject jsonServException = new JSONObject();
161 jsonServException.put("policyException", jsonMessages);
162 JSONObject jsonErrorRequest = new JSONObject();
163 jsonErrorRequest.put("requestError", jsonServException);
165 String responseData = jsonErrorRequest.toString();
167 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
168 int statusCode = 403;
169 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
171 assertEquals("403", sdcResponseJsonObj.getString("statusCode"));
172 assertEquals("POL5003", sdcResponseJsonObj.getString("messageId"));
173 assertEquals("Error: Not authorized to use the API.", sdcResponseJsonObj.getString("message"));
178 public void enhanceJsonResponseTest_Error_UnexpectedFormat() throws JSONException {
180 // build error response data
181 JSONObject jsonMessages = new JSONObject();
182 jsonMessages.put("messageId", "POL5003");
183 jsonMessages.put("text", "Error: Not authorized to use the API.");
184 JSONObject jsonServException = new JSONObject();
185 jsonServException.put("policyException", jsonMessages);
186 JSONObject jsonErrorRequest = new JSONObject();
187 jsonErrorRequest.put("unexpectedResponseTag", jsonServException);
189 String responseData = jsonErrorRequest.toString();
191 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
192 int statusCode = 403;
193 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
195 assertEquals("500", sdcResponseJsonObj.getString("statusCode"));
196 assertEquals("Undefined Error Message!", sdcResponseJsonObj.getString("messageId"));
197 assertEquals("Unexpected response format from SDC.", sdcResponseJsonObj.getString("message"));