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;
29 import org.apache.http.HttpStatus;
30 import org.json.JSONException;
31 import org.json.JSONObject;
32 import org.junit.Test;
33 import org.onap.so.apihandlerinfra.BaseTest;
34 import org.onap.so.apihandlerinfra.exceptions.ApiException;
35 import org.springframework.beans.factory.annotation.Autowired;
37 public class SDCClientHelperTest extends BaseTest{
39 String serviceModelVersionId = "TEST_uuid1";
40 String operationalEnvironmentId = "TEST_operationalEnvironmentId";
41 String workloadContext = "TEST_workloadContext";
44 private SDCClientHelper sdcClientUtils;
47 public void postActivateOperationalEnvironment_Test() throws ApiException {
49 JSONObject jsonObject = new JSONObject();
50 jsonObject.put("statusCode", "202");
51 jsonObject.put("message", "Success");
52 jsonObject.put("distributionId", "TEST_distributionId");
54 wireMockServer.stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
55 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonObject.toString()).withStatus(HttpStatus.SC_ACCEPTED)));
57 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
59 assertEquals("202", jsonResponse.get("statusCode"));
60 assertEquals("Success", jsonResponse.get("message"));
65 public void postActivateOperationalEnvironment_InvalidJson_Test() throws ApiException {
67 // ERROR in asdc response, invalid json object
68 JSONObject jsonErrorResponse = new JSONObject();
69 jsonErrorResponse.put("requestError", "");
71 wireMockServer.stubFor(post(urlPathMatching("/sdc/v1/catalog/services/TEST_uuid1/distr.*"))
72 .willReturn(aResponse().withHeader("Content-Type", "application/json").withBody(jsonErrorResponse.toString()).withStatus(HttpStatus.SC_BAD_REQUEST)));
74 JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
76 assertEquals("500", jsonResponse.get("statusCode"));
77 assertEquals("", jsonResponse.get("messageId"));
78 assertEquals(" Encountered Error while calling SDC POST Activate. JSONObject[\"requestError\"] is not a JSONObject.", jsonResponse.get("message"));
83 public void buildUriBuilderTest() {
86 String url = sdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
87 assertEquals("http://localhost:" + env.getProperty("wiremock.server.port") + "/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate", url);
89 } catch (Exception e) {
90 fail("Exception caught: " + e.getMessage());
97 public void buildJsonWorkloadContextTest() throws JSONException {
99 String jsonPayload = sdcClientUtils.buildJsonWorkloadContext(workloadContext);
100 assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
105 public void enhanceJsonResponseTest_Success() throws JSONException {
107 // build success response data
108 JSONObject sdcResponseJsonObj = new JSONObject();
109 sdcResponseJsonObj.put("distributionId", "TEST_distributionId");
111 int statusCode = 202;
112 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
114 assertEquals("202", sdcResponseJsonObj.getString("statusCode"));
115 assertEquals("", sdcResponseJsonObj.getString("messageId"));
116 assertEquals("Success", sdcResponseJsonObj.getString("message"));
117 assertEquals("TEST_distributionId", sdcResponseJsonObj.getString("distributionId"));
122 public void enhanceJsonResponseTest_Error() throws JSONException {
124 // build error response data
125 JSONObject jsonMessages = new JSONObject();
126 jsonMessages.put("messageId", "SVC4675");
127 jsonMessages.put("text", "Error: Service state is invalid for this action.");
128 JSONObject jsonServException = new JSONObject();
129 jsonServException.put("serviceException", jsonMessages);
130 JSONObject jsonErrorRequest = new JSONObject();
131 jsonErrorRequest.put("requestError", jsonServException);
133 String responseData = jsonErrorRequest.toString();
135 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
136 int statusCode = 409;
137 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
139 assertEquals("409", sdcResponseJsonObj.getString("statusCode"));
140 assertEquals("SVC4675", sdcResponseJsonObj.getString("messageId"));
141 assertEquals("Error: Service state is invalid for this action.", sdcResponseJsonObj.getString("message"));
146 public void enhanceJsonResponseTest_Error_policyException() throws JSONException {
148 // build error response data
149 JSONObject jsonMessages = new JSONObject();
150 jsonMessages.put("messageId", "POL5003");
151 jsonMessages.put("text", "Error: Not authorized to use the API.");
152 JSONObject jsonServException = new JSONObject();
153 jsonServException.put("policyException", jsonMessages);
154 JSONObject jsonErrorRequest = new JSONObject();
155 jsonErrorRequest.put("requestError", jsonServException);
157 String responseData = jsonErrorRequest.toString();
159 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
160 int statusCode = 403;
161 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
163 assertEquals("403", sdcResponseJsonObj.getString("statusCode"));
164 assertEquals("POL5003", sdcResponseJsonObj.getString("messageId"));
165 assertEquals("Error: Not authorized to use the API.", sdcResponseJsonObj.getString("message"));
170 public void enhanceJsonResponseTest_Error_UnexpectedFormat() throws JSONException {
172 // build error response data
173 JSONObject jsonMessages = new JSONObject();
174 jsonMessages.put("messageId", "POL5003");
175 jsonMessages.put("text", "Error: Not authorized to use the API.");
176 JSONObject jsonServException = new JSONObject();
177 jsonServException.put("policyException", jsonMessages);
178 JSONObject jsonErrorRequest = new JSONObject();
179 jsonErrorRequest.put("unexpectedResponseTag", jsonServException);
181 String responseData = jsonErrorRequest.toString();
183 JSONObject sdcResponseJsonObj = new JSONObject(responseData);
184 int statusCode = 403;
185 sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
187 assertEquals("500", sdcResponseJsonObj.getString("statusCode"));
188 assertEquals("Undefined Error Message!", sdcResponseJsonObj.getString("messageId"));
189 assertEquals("Unexpected response format from SDC.", sdcResponseJsonObj.getString("message"));