Fix for radio buttons
[sdc.git] / asdc-tests / 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.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;
41
42 public class ErrorValidationUtils {
43
44         static Logger logger = LoggerFactory.getLogger(Utils.class.getName());
45
46         public static void checkBodyResponseOnError(String errorType, List<String> variables, String actualResponse)
47                         throws FileNotFoundException, JSONException {
48
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());
55                         } else {
56                                 expectedResponseBody = restExceptionFormatBuilder(errorInfo.getMessageId(), errorInfo.getMessage(),
57                                                 variables, ExceptionEnumType.POLICY_EXCPTION.getValue());
58                         }
59                 }
60                 actualResponse = actualResponse.replaceAll("\\n", "");
61                 logger.debug("actualResponse - {}",actualResponse);
62                 logger.debug("expectedResponseBody - {}",expectedResponseBody);
63                 assertEquals(expectedResponseBody, new JSONObject(actualResponse));
64         }
65         
66         public static String checkUIResponseOnError(String errorType)
67                         throws FileNotFoundException, JSONException {
68
69                 ErrorInfo errorInfo = parseErrorConfigYaml(errorType);
70                 String messageId = errorInfo.getMessageId();
71                 
72                 return messageId;
73         }
74
75         public static JSONObject restExceptionFormatBuilder(String messageId, String text, List<String> variables,
76                         String type) {
77
78                 JSONObject simpleElements = new JSONObject();
79                 JSONObject exceptionType = new JSONObject();
80                 JSONObject requestError = new JSONObject();
81
82                 try {
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);
88
89                 } catch (JSONException e) {
90                         e.printStackTrace();
91                 }
92
93                 return requestError;
94
95         }
96
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);
103                 // File file = new
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);
112
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);
117
118                 return errInfo;
119         }
120
121 }