[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / utils / validation / ErrorValidationUtils.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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.openecomp.sdc.ci.tests.utils.validation;
22
23 import static org.testng.AssertJUnit.assertEquals;
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.InputStream;
29 import java.util.List;
30 import java.util.Map;
31
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;
41
42 public class ErrorValidationUtils {
43         static Logger logger = LoggerFactory.getLogger(ErrorValidationUtils.class.getName());
44
45         public static void checkBodyResponseOnError(String errorType, List<String> variables, String actualResponse)
46                         throws FileNotFoundException, JSONException {
47
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());
54                         } else {
55                                 expectedResponseBody = restExceptionFormatBuilder(errorInfo.getMessageId(), errorInfo.getMessage(),
56                                                 variables, ExceptionEnumType.POLICY_EXCPTION.getValue());
57                         }
58                 }
59                 actualResponse = actualResponse.replaceAll("\\n", "");
60                 logger.debug("actualResponse - {}", actualResponse);
61                 logger.debug("expectedResponseBody - {}", expectedResponseBody);
62                 assertEquals(expectedResponseBody, new JSONObject(actualResponse));
63         }
64         
65         public static String checkUIResponseOnError(String errorType)
66                         throws FileNotFoundException, JSONException {
67
68                 ErrorInfo errorInfo = parseErrorConfigYaml(errorType);
69                 String messageId = errorInfo.getMessageId();
70                 
71                 return messageId;
72         }
73
74         public static JSONObject restExceptionFormatBuilder(String messageId, String text, List<String> variables,
75                         String type) {
76
77                 JSONObject simpleElements = new JSONObject();
78                 JSONObject exceptionType = new JSONObject();
79                 JSONObject requestError = new JSONObject();
80
81                 try {
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);
87
88                 } catch (JSONException e) {
89                         e.printStackTrace();
90                 }
91
92                 return requestError;
93
94         }
95
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);
102                 // File file = new
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);
111
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);
116
117                 return errInfo;
118         }
119
120 }