2 * ============LICENSE_START=======================================================
\r
4 * ================================================================================
\r
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============LICENSE_END=========================================================
\r
21 package org.openecomp.mso.apihandlerinfra.tenantisolation.helpers;
\r
23 import static org.junit.Assert.assertEquals;
\r
24 import static org.junit.Assert.fail;
\r
26 import java.util.LinkedHashMap;
\r
27 import java.util.List;
\r
29 import org.json.JSONObject;
\r
30 import org.junit.After;
\r
31 import org.junit.Assert;
\r
32 import org.junit.BeforeClass;
\r
33 import org.junit.Rule;
\r
34 import org.junit.Test;
\r
35 import org.openecomp.mso.apihandlerinfra.Constants;
\r
36 import org.openecomp.mso.apihandlerinfra.MsoPropertiesUtils;
\r
37 import org.openecomp.mso.properties.MsoJavaProperties;
\r
38 import org.openecomp.mso.properties.MsoPropertiesFactory;
\r
39 import org.openecomp.mso.rest.RESTClient;
\r
40 import org.openecomp.mso.rest.RESTConfig;
\r
42 import com.github.tomakehurst.wiremock.core.WireMockConfiguration;
\r
43 import com.github.tomakehurst.wiremock.junit.WireMockRule;
\r
45 public class AsdcClientHelperTest {
\r
47 MsoJavaProperties properties = MsoPropertiesUtils.loadMsoProperties();
\r
48 AsdcClientHelper asdcClientUtils = new AsdcClientHelper(properties);
\r
50 String serviceModelVersionId = "TEST_uuid1";
\r
51 String operationalEnvironmentId = "TEST_operationalEnvironmentId";
\r
52 String workloadContext = "TEST_workloadContext";
\r
55 public final WireMockRule wireMockRule = new WireMockRule(WireMockConfiguration.wireMockConfig().port(28090)); //.extensions(transformerArray));
\r
58 public static void setUp() throws Exception {
\r
59 MsoPropertiesFactory msoPropertiesFactory = new MsoPropertiesFactory();
\r
60 msoPropertiesFactory.removeAllMsoProperties();
\r
61 msoPropertiesFactory.initializeMsoProperties(Constants.MSO_PROP_APIHANDLER_INFRA, "src/test/resources/mso.apihandler-infra.properties");
\r
65 public void tearDown() throws Exception {
\r
70 public void getPropertiesTest() {
\r
72 String asdcInstanceId = asdcClientUtils.getAsdcInstanceId();
\r
73 Assert.assertEquals("Asdc InstanceId - " , "test", asdcInstanceId);
\r
75 String asdcEndpoint = asdcClientUtils.getAsdcEndpoint();
\r
76 Assert.assertEquals("Asdc Endpoint - " , "http://localhost:28090", asdcEndpoint);
\r
78 String userid = asdcClientUtils.getAsdcUserId();
\r
79 Assert.assertEquals("userid - " , "cs0008", userid);
\r
84 public void buildUriBuilderTest() {
\r
87 String url = asdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
\r
88 assertEquals("http://localhost:28090/sdc/v1/catalog/services/TEST_uuid1/distribution/TEST_operationalEnvironmentId/activate", url);
\r
90 } catch (Exception e) {
\r
91 fail("Exception caught: " + e.getMessage());
\r
97 public void buildJsonWorkloadContextTest() {
\r
100 String jsonPayload = asdcClientUtils.buildJsonWorkloadContext(workloadContext);
\r
101 assertEquals("{\"workloadContext\":\"TEST_workloadContext\"}", jsonPayload);
\r
103 } catch (Exception e) {
\r
104 fail("Exception caught: " + e.getMessage());
\r
110 public void setRestClientTest() {
\r
113 String url = asdcClientUtils.buildUriBuilder(serviceModelVersionId, operationalEnvironmentId);
\r
114 RESTConfig config = new RESTConfig(url);
\r
115 RESTClient client = asdcClientUtils.setRestClient(config);
\r
116 LinkedHashMap<String, List<String>> headers = client.getHeaders();
\r
117 assertEquals("[cs0008]", headers.get("USER_ID").toString());
\r
119 } catch (Exception e) {
\r
120 fail("Exception caught: " + e.getMessage());
\r
126 public void enhanceJsonResponseTest_Success() {
\r
129 // build success response data
\r
130 JSONObject asdcResponseJsonObj = new JSONObject();
\r
131 asdcResponseJsonObj.put("distributionId", "TEST_distributionId");
\r
133 int statusCode = 202;
\r
134 asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);
\r
136 assertEquals("202", asdcResponseJsonObj.getString("statusCode"));
\r
137 assertEquals("", asdcResponseJsonObj.getString("messageId"));
\r
138 assertEquals("Success", asdcResponseJsonObj.getString("message"));
\r
139 assertEquals("TEST_distributionId", asdcResponseJsonObj.getString("distributionId"));
\r
141 } catch (Exception e) {
\r
142 fail("Exception caught: " + e.getMessage());
\r
148 public void enhanceJsonResponseTest_Error() {
\r
152 // build error response data
\r
153 JSONObject jsonMessages = new JSONObject();
\r
154 jsonMessages.put("messageId", "SVC4675");
\r
155 jsonMessages.put("text", "Error: Service state is invalid for this action.");
\r
156 JSONObject jsonServException = new JSONObject();
\r
157 jsonServException.put("serviceException", jsonMessages);
\r
158 JSONObject jsonErrorRequest = new JSONObject();
\r
159 jsonErrorRequest.put("requestError", jsonServException);
\r
161 String responseData = jsonErrorRequest.toString();
\r
163 JSONObject asdcResponseJsonObj = new JSONObject(responseData);
\r
164 int statusCode = 409;
\r
165 asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);
\r
167 assertEquals("409", asdcResponseJsonObj.getString("statusCode"));
\r
168 assertEquals("SVC4675", asdcResponseJsonObj.getString("messageId"));
\r
169 assertEquals("Error: Service state is invalid for this action.", asdcResponseJsonObj.getString("message"));
\r
172 } catch (Exception e) {
\r
173 fail("Exception caught: " + e.getMessage());
\r
179 public void enhanceJsonResponseTest_Error_policyException() {
\r
183 // build error response data
\r
184 JSONObject jsonMessages = new JSONObject();
\r
185 jsonMessages.put("messageId", "POL5003");
\r
186 jsonMessages.put("text", "Error: Not authorized to use the API.");
\r
187 JSONObject jsonServException = new JSONObject();
\r
188 jsonServException.put("policyException", jsonMessages);
\r
189 JSONObject jsonErrorRequest = new JSONObject();
\r
190 jsonErrorRequest.put("requestError", jsonServException);
\r
192 String responseData = jsonErrorRequest.toString();
\r
194 JSONObject asdcResponseJsonObj = new JSONObject(responseData);
\r
195 int statusCode = 403;
\r
196 asdcResponseJsonObj = asdcClientUtils.enhanceJsonResponse(asdcResponseJsonObj, statusCode);
\r
198 assertEquals("403", asdcResponseJsonObj.getString("statusCode"));
\r
199 assertEquals("POL5003", asdcResponseJsonObj.getString("messageId"));
\r
200 assertEquals("Error: Not authorized to use the API.", asdcResponseJsonObj.getString("message"));
\r
203 } catch (Exception e) {
\r
204 fail("Exception caught: " + e.getMessage());
\r