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