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