Replaced all tabs with spaces in java and pom.xml
[so.git] / mso-api-handlers / mso-api-handler-infra / src / test / java / org / onap / so / apihandlerinfra / tenantisolation / helpers / SDCClientHelperTest.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.helpers;
22
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;
35
36 public class SDCClientHelperTest extends BaseTest {
37
38     String serviceModelVersionId = "TEST_uuid1";
39     String operationalEnvironmentId = "TEST_operationalEnvironmentId";
40     String workloadContext = "TEST_workloadContext";
41
42     @Autowired
43     private SDCClientHelper sdcClientUtils;
44
45     @Test
46     public void postActivateOperationalEnvironment_Test() throws ApiException {
47
48         JSONObject jsonObject = new JSONObject();
49         jsonObject.put("statusCode", "202");
50         jsonObject.put("message", "Success");
51         jsonObject.put("distributionId", "TEST_distributionId");
52
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)));
56
57         JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId,
58                 operationalEnvironmentId, workloadContext);
59
60         assertEquals("202", jsonResponse.get("statusCode"));
61         assertEquals("Success", jsonResponse.get("message"));
62
63     }
64
65     @Test
66     public void postActivateOperationalEnvironment_InvalidJson_Test() throws ApiException {
67
68         // ERROR in asdc response, invalid json object
69         JSONObject jsonErrorResponse = new JSONObject();
70         jsonErrorResponse.put("requestError", "");
71
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)));
75
76         JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId,
77                 operationalEnvironmentId, workloadContext);
78
79         assertEquals("500", jsonResponse.get("statusCode"));
80         assertEquals("", jsonResponse.get("messageId"));
81         assertEquals(
82                 " Encountered Error while calling SDC POST Activate. JSONObject[\"requestError\"] is not a JSONObject.",
83                 jsonResponse.get("message"));
84
85     }
86
87     @Test
88     public void buildUriBuilderTest() {
89
90         try {
91             String url = sdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
92             assertEquals(
93                     "http://localhost:" + env.getProperty("wiremock.server.port")
94                             + "/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate",
95                     url);
96
97         } catch (Exception e) {
98             fail("Exception caught: " + e.getMessage());
99
100         }
101     }
102
103
104     @Test
105     public void buildJsonWorkloadContextTest() throws JSONException {
106
107         String jsonPayload = sdcClientUtils.buildJsonWorkloadContext(workloadContext);
108         assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
109
110     }
111
112     @Test
113     public void enhanceJsonResponseTest_Success() throws JSONException {
114
115         // build success response data
116         JSONObject sdcResponseJsonObj = new JSONObject();
117         sdcResponseJsonObj.put("distributionId", "TEST_distributionId");
118
119         int statusCode = 202;
120         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
121
122         assertEquals("202", sdcResponseJsonObj.getString("statusCode"));
123         assertEquals("", sdcResponseJsonObj.getString("messageId"));
124         assertEquals("Success", sdcResponseJsonObj.getString("message"));
125         assertEquals("TEST_distributionId", sdcResponseJsonObj.getString("distributionId"));
126
127     }
128
129     @Test
130     public void enhanceJsonResponseTest_Error() throws JSONException {
131
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);
140
141         String responseData = jsonErrorRequest.toString();
142
143         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
144         int statusCode = 409;
145         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
146
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"));
150
151     }
152
153     @Test
154     public void enhanceJsonResponseTest_Error_policyException() throws JSONException {
155
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);
164
165         String responseData = jsonErrorRequest.toString();
166
167         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
168         int statusCode = 403;
169         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
170
171         assertEquals("403", sdcResponseJsonObj.getString("statusCode"));
172         assertEquals("POL5003", sdcResponseJsonObj.getString("messageId"));
173         assertEquals("Error: Not authorized to use the API.", sdcResponseJsonObj.getString("message"));
174
175     }
176
177     @Test
178     public void enhanceJsonResponseTest_Error_UnexpectedFormat() throws JSONException {
179
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);
188
189         String responseData = jsonErrorRequest.toString();
190
191         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
192         int statusCode = 403;
193         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
194
195         assertEquals("500", sdcResponseJsonObj.getString("statusCode"));
196         assertEquals("Undefined Error Message!", sdcResponseJsonObj.getString("messageId"));
197         assertEquals("Unexpected response format from SDC.", sdcResponseJsonObj.getString("message"));
198
199     }
200
201 }