39f5ed834fa4f2b29cffdef015dcc854d3dcd154
[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
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;
36
37 public class SDCClientHelperTest extends BaseTest{
38
39         String serviceModelVersionId = "TEST_uuid1";
40         String operationalEnvironmentId = "TEST_operationalEnvironmentId";
41         String workloadContext = "TEST_workloadContext";
42
43         @Autowired
44         private SDCClientHelper sdcClientUtils;
45
46         @Test
47         public void postActivateOperationalEnvironment_Test() throws ApiException {
48
49                 JSONObject jsonObject = new JSONObject();
50                 jsonObject.put("statusCode", "202");
51                 jsonObject.put("message", "Success");
52                 jsonObject.put("distributionId", "TEST_distributionId");
53
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)));
56
57             JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
58
59             assertEquals("202", jsonResponse.get("statusCode"));
60             assertEquals("Success", jsonResponse.get("message"));
61
62         }
63
64         @Test
65         public void postActivateOperationalEnvironment_InvalidJson_Test() throws ApiException {
66
67                 // ERROR in asdc response, invalid json object
68                 JSONObject jsonErrorResponse = new JSONObject();
69                 jsonErrorResponse.put("requestError", "");
70
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)));
73
74             JSONObject jsonResponse = sdcClientUtils.postActivateOperationalEnvironment(serviceModelVersionId, operationalEnvironmentId, workloadContext);
75
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"));
79
80         }
81
82         @Test
83         public void buildUriBuilderTest() {
84
85                 try {
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);
88
89                 } catch (Exception e) {
90                         fail("Exception caught: " + e.getMessage());
91
92                 }
93         }
94
95
96         @Test
97         public void buildJsonWorkloadContextTest() throws JSONException {
98
99                         String jsonPayload = sdcClientUtils.buildJsonWorkloadContext(workloadContext);
100                         assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
101
102         }
103
104         @Test
105         public void enhanceJsonResponseTest_Success() throws JSONException {
106
107                         // build success response data
108                         JSONObject sdcResponseJsonObj = new JSONObject();
109                         sdcResponseJsonObj.put("distributionId", "TEST_distributionId");
110
111                         int statusCode = 202;
112                         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
113
114                         assertEquals("202", sdcResponseJsonObj.getString("statusCode"));
115                         assertEquals("", sdcResponseJsonObj.getString("messageId"));
116                         assertEquals("Success", sdcResponseJsonObj.getString("message"));
117                         assertEquals("TEST_distributionId", sdcResponseJsonObj.getString("distributionId"));
118
119         }
120
121         @Test
122         public void enhanceJsonResponseTest_Error() throws JSONException {
123
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);
132
133                         String responseData =  jsonErrorRequest.toString();
134
135                         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
136                         int statusCode = 409;
137                         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
138
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"));
142
143         }
144
145         @Test
146         public void enhanceJsonResponseTest_Error_policyException() throws JSONException {
147
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);
156
157                         String responseData =  jsonErrorRequest.toString();
158
159                         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
160                         int statusCode = 403;
161                         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
162
163                         assertEquals("403", sdcResponseJsonObj.getString("statusCode"));
164                         assertEquals("POL5003", sdcResponseJsonObj.getString("messageId"));
165                         assertEquals("Error: Not authorized to use the API.", sdcResponseJsonObj.getString("message"));
166
167         }
168
169         @Test
170         public void enhanceJsonResponseTest_Error_UnexpectedFormat() throws JSONException {
171
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);
180
181                         String responseData =  jsonErrorRequest.toString();
182
183                         JSONObject sdcResponseJsonObj = new JSONObject(responseData);
184                         int statusCode = 403;
185                         sdcResponseJsonObj = sdcClientUtils.enhanceJsonResponse(sdcResponseJsonObj, statusCode);
186
187                         assertEquals("500", sdcResponseJsonObj.getString("statusCode"));
188                         assertEquals("Undefined Error Message!", sdcResponseJsonObj.getString("messageId"));
189                         assertEquals("Unexpected response format from SDC.", sdcResponseJsonObj.getString("message"));
190
191         }
192
193 }