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.stubFor;
26 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.fail;
30 import org.apache.http.HttpStatus;
31 import org.json.JSONException;
32 import org.json.JSONObject;
33 import org.junit.Test;
34 import org.onap.so.apihandlerinfra.BaseTest;
35 import org.onap.so.apihandlerinfra.exceptions.ApiException;
36 import org.springframework.beans.factory.annotation.Autowired;
38 public class SDCClientHelperTest extends BaseTest{
40 String serviceModelVersionId = "TEST_uuid1";
41 String operationalEnvironmentId = "TEST_operationalEnvironmentId";
42 String workloadContext = "TEST_workloadContext";
45 private SDCClientHelper sdcClientUtils;
48 public void postActivateOperationalEnvironment_Test() throws ApiException {
50 JSONObject jsonObject = new JSONObject();
51 jsonObject.put("statusCode", "202");
52 jsonObject.put("message", "Success");
53 jsonObject.put("distributionId", "TEST_distributionId");
55 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
56 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
58 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, 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 stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
73 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_BAD_REQUEST)));
75 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
77 assertEquals("500", jsonResponse.get("statusCode"));
78 assertEquals("", jsonResponse.get("messageId"));
79 assertEquals(" Encountered Error while calling SDC POST Activate. JSONObject[\"requestError\"] is not a JSONObject.", jsonResponse.get("message"));
84 public void buildUriBuilderTest() {
87 String url = sdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
88 assertEquals("http://localhost:" + env.getProperty("wiremock.server.port") + "/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate", url);
90 } catch (Exception e) {
91 fail("Exception caught: " + e.getMessage());
98 public void buildJsonWorkloadContextTest() throws JSONException {
100 String jsonPayload = sdcClientUtils.buildJsonWorkloadContext(workloadContext);
101 assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
106 public void enhanceJsonResponseTest_Success() throws JSONException {
108 // build success response data
109 JSONObject sdcResponseJsonObj = new JSONObject();
110 sdcResponseJsonObj.put("distributionId", "TEST_distributionId");
112 int statusCode = 202;
113 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
115 assertEquals("202", sdcResponseJsonObj.getString("statusCode"));
116 assertEquals("", sdcResponseJsonObj.getString("messageId"));
117 assertEquals("Success", sdcResponseJsonObj.getString("message"));
118 assertEquals("TEST_distributionId", sdcResponseJsonObj.getString("distributionId"));
123 public void enhanceJsonResponseTest_Error() throws JSONException {
125 // build error response data
126 JSONObject jsonMessages = new JSONObject();
127 jsonMessages.put("messageId", "SVC4675");
128 jsonMessages.put("text", "Error: Service state is invalid for this action.");
129 JSONObject jsonServException = new JSONObject();
130 jsonServException.put("serviceException", jsonMessages);
131 JSONObject jsonErrorRequest = new JSONObject();
132 jsonErrorRequest.put("requestError", jsonServException);
134 String responseData = jsonErrorRequest.toString();
136 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
137 int statusCode = 409;
138 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
140 assertEquals("409", sdcResponseJsonObj.getString("statusCode"));
141 assertEquals("SVC4675", sdcResponseJsonObj.getString("messageId"));
142 assertEquals("Error: Service state is invalid for this action.", sdcResponseJsonObj.getString("message"));
147 public void enhanceJsonResponseTest_Error_policyException() throws JSONException {
149 // build error response data
150 JSONObject jsonMessages = new JSONObject();
151 jsonMessages.put("messageId", "POL5003");
152 jsonMessages.put("text", "Error: Not authorized to use the API.");
153 JSONObject jsonServException = new JSONObject();
154 jsonServException.put("policyException", jsonMessages);
155 JSONObject jsonErrorRequest = new JSONObject();
156 jsonErrorRequest.put("requestError", jsonServException);
158 String responseData = jsonErrorRequest.toString();
160 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
161 int statusCode = 403;
162 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
164 assertEquals("403", sdcResponseJsonObj.getString("statusCode"));
165 assertEquals("POL5003", sdcResponseJsonObj.getString("messageId"));
166 assertEquals("Error: Not authorized to use the API.", sdcResponseJsonObj.getString("message"));
171 public void enhanceJsonResponseTest_Error_UnexpectedFormat() throws JSONException {
173 // build error response data
174 JSONObject jsonMessages = new JSONObject();
175 jsonMessages.put("messageId", "POL5003");
176 jsonMessages.put("text", "Error: Not authorized to use the API.");
177 JSONObject jsonServException = new JSONObject();
178 jsonServException.put("policyException", jsonMessages);
179 JSONObject jsonErrorRequest = new JSONObject();
180 jsonErrorRequest.put("unexpectedResponseTag", jsonServException);
182 String responseData = jsonErrorRequest.toString();
184 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
185 int statusCode = 403;
186 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
188 assertEquals("500", sdcResponseJsonObj.getString("statusCode"));
189 assertEquals("Undefined Error Message!", sdcResponseJsonObj.getString("messageId"));
190 assertEquals("Unexpected response format from SDC.", sdcResponseJsonObj.getString("message"));