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.openecomp.sdc.ci.tests.utils.validation;
23 import static org.testng.AssertJUnit.assertEquals;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.util.List;
32 import org.yaml.snakeyaml.Yaml;
33 import org.codehaus.jettison.json.JSONException;
34 import org.codehaus.jettison.json.JSONObject;
35 import org.openecomp.sdc.ci.tests.config.Config;
36 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
37 import org.openecomp.sdc.ci.tests.datatypes.enums.ExceptionEnumType;
38 import org.openecomp.sdc.ci.tests.utils.Utils;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
42 public class ErrorValidationUtils {
43 static Logger logger = LoggerFactory.getLogger(ErrorValidationUtils.class.getName());
45 public static void checkBodyResponseOnError(String errorType, List<String> variables, String actualResponse)
46 throws FileNotFoundException, JSONException {
48 ErrorInfo errorInfo = parseErrorConfigYaml(errorType);
49 JSONObject expectedResponseBody = null;
50 if (errorInfo.getMessageId() != null) {
51 if (errorInfo.getMessageId().contains("SVC")) {
52 expectedResponseBody = restExceptionFormatBuilder(errorInfo.getMessageId(), errorInfo.getMessage(),
53 variables, ExceptionEnumType.SERVICE_EXCEPTION.getValue());
55 expectedResponseBody = restExceptionFormatBuilder(errorInfo.getMessageId(), errorInfo.getMessage(),
56 variables, ExceptionEnumType.POLICY_EXCPTION.getValue());
59 actualResponse = actualResponse.replaceAll("\\n", "");
60 logger.debug("actualResponse - {}", actualResponse);
61 logger.debug("expectedResponseBody - {}", expectedResponseBody);
62 assertEquals(expectedResponseBody, new JSONObject(actualResponse));
65 public static String checkUIResponseOnError(String errorType)
66 throws FileNotFoundException, JSONException {
68 ErrorInfo errorInfo = parseErrorConfigYaml(errorType);
69 String messageId = errorInfo.getMessageId();
74 public static JSONObject restExceptionFormatBuilder(String messageId, String text, List<String> variables,
77 JSONObject simpleElements = new JSONObject();
78 JSONObject exceptionType = new JSONObject();
79 JSONObject requestError = new JSONObject();
82 simpleElements.put("messageId", messageId);
83 simpleElements.put("text", text);
84 simpleElements.put("variables", variables);
85 exceptionType.put(type, simpleElements);
86 requestError.put("requestError", exceptionType);
88 } catch (JSONException e) {
96 public static ErrorInfo parseErrorConfigYaml(String error) throws FileNotFoundException {
97 Yaml yaml = new Yaml();
98 ErrorInfo errInfo = null;
99 Config config = Utils.getConfig();
100 String errorConfigurationFile = config.getErrorConfigurationFile();
101 File file = new File(errorConfigurationFile);
103 // File("../catalog-be/src/main/resources/config/error-configuration.yaml");
104 InputStream inputStream = new FileInputStream(file);
105 Map<?, ?> map = (Map<?, ?>) yaml.load(inputStream);
106 // System.out.println(map.get("errors"));
107 @SuppressWarnings("unchecked")
108 Map<String, ErrorInfo> errorMap = (Map<String, ErrorInfo>) map.get("errors");
109 @SuppressWarnings("unchecked")
110 Map<String, Object> errorInfo = (Map<String, Object>) errorMap.get(error);
112 String message = (String) errorInfo.get("message");
113 String messageId = (String) errorInfo.get("messageId");
114 int code = (Integer) errorInfo.get("code");
115 errInfo = new ErrorInfo(code, message, messageId);