Fix for radio buttons
[sdc.git] / asdc-tests / src / main / java / org / openecomp / sdc / ci / tests / utils / validation / BaseValidationUtils.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.FileNotFoundException;
26 import java.util.Arrays;
27
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
30 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
31 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
32 import org.openecomp.sdc.exception.ResponseFormat;
33 import org.testng.Assert;
34
35 public class BaseValidationUtils {
36
37         public static final int STATUS_CODE_SUCCESS = 200;
38         public static final int STATUS_CODE_CREATED = 201;
39         public static final int STATUS_CODE_DELETE = 204;
40         public static final int STATUS_CODE_NOT_FOUND = 404;
41         public static final int STATUS_CODE_SUCCESS_NO_CONTENT = 204;
42         public static final int STATUS_CODE_SUCCESS_DELETE = 204;
43         public static final int STATUS_CODE_INVALID_CONTENT = 400;
44         public static final int STATUS_CODE_MISSING_DATA = 400;
45         public static final int STATUS_CODE_MISSING_INFORMATION = 403;
46         public static final int STATUS_CODE_RESTRICTED_ACCESS = 403;
47         public static final int STATUS_CODE_RESTRICTED_OPERATION = 409;
48         public static final int STATUS_CODE_ALREADY_EXISTS = 409;
49
50         // ------
51         protected static Boolean checkErrorCode(RestResponse deleteResponse) {
52                 if (deleteResponse.getErrorCode() == STATUS_CODE_SUCCESS
53                                 || deleteResponse.getErrorCode() == STATUS_CODE_DELETE) {
54                         return true;
55                 }
56                 return false;
57         }
58
59         // *** STATUS CODE VALIDATION UTIITIES ****
60         public static void checkStatusCode(RestResponse response, String assertMessage, boolean AND, int... statuses) {
61                 int statusCode = response.getErrorCode();
62                 for (int status : statuses) {
63                         if (AND && statusCode != status) {
64                                 Assert.fail(assertMessage + " status: " + statusCode);
65                         } else if (statusCode == status) {
66                                 return;
67                         }
68                 }
69                 if (!AND) {
70                         Assert.fail(assertMessage + " status: " + statusCode);
71                 }
72         }
73
74         public static void checkDeleteResponse(RestResponse response) {
75                 checkStatusCode(response, "delete request failed", false, STATUS_CODE_DELETE, STATUS_CODE_NOT_FOUND,
76                                 STATUS_CODE_SUCCESS); // STATUS_CODE_SUCCESS for deActivate user
77         }
78
79         public static void checkCreateResponse(RestResponse response) {
80                 checkStatusCode(response, "create request failed", false, STATUS_CODE_CREATED);
81         }
82
83         public static void checkSuccess(RestResponse response) {
84                 checkStatusCode(response, "request failed", false, STATUS_CODE_SUCCESS);
85         }
86
87         public static void checkErrorResponse(RestResponse errorResponse, ActionStatus actionStatus,
88                         String... expectedVariables) throws FileNotFoundException {
89                 // Expected error
90                 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
91                 String expectedMessage = expectedError.getMessage();
92
93                 // Actual error
94                 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
95                                 ResponseFormat.class);
96                 String actualMessage = responseFormat.getText();
97                 String[] actualVariables = responseFormat.getVariables();
98
99                 assertEquals("Unexpected error message", expectedMessage, actualMessage);
100                 assertEquals("Unexpected error variables", Arrays.asList(expectedVariables), Arrays.asList(actualVariables));
101         }
102
103         public static void checkErrorMessageResponse(RestResponse errorResponse, ActionStatus actionStatus)
104                         throws FileNotFoundException {
105                 // Expected error
106                 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
107                 String expectedMessage = expectedError.getMessage();
108
109                 // Actual error
110                 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
111                                 ResponseFormat.class);
112                 String actualMessage = responseFormat.getText();
113
114                 assertEquals("Unexpected error message", expectedMessage, actualMessage);
115         }
116 }