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