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 org.openecomp.sdc.be.dao.api.ActionStatus;
24 import org.openecomp.sdc.ci.tests.datatypes.enums.ErrorInfo;
25 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
26 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
27 import org.openecomp.sdc.exception.ResponseFormat;
28 import org.testng.Assert;
30 import java.io.FileNotFoundException;
31 import java.util.Arrays;
33 import static org.testng.AssertJUnit.assertEquals;
35 public class BaseValidationUtils {
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;
51 protected static Boolean checkErrorCode(RestResponse deleteResponse) {
52 if (deleteResponse.getErrorCode() == STATUS_CODE_SUCCESS
53 || deleteResponse.getErrorCode() == STATUS_CODE_DELETE) {
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) {
70 Assert.fail(assertMessage + " status: " + statusCode);
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
79 public static void checkCreateResponse(RestResponse response) {
80 checkStatusCode(response, "create request failed", false, STATUS_CODE_CREATED);
83 public static void checkSuccess(RestResponse response) {
84 checkStatusCode(response, "request failed", false, STATUS_CODE_SUCCESS);
87 public static void checkErrorResponse(RestResponse errorResponse, ActionStatus actionStatus,
88 String... expectedVariables) throws FileNotFoundException {
90 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
91 String expectedMessage = expectedError.getMessage();
94 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
95 ResponseFormat.class);
96 String actualMessage = responseFormat.getText();
97 String[] actualVariables = responseFormat.getVariables();
99 assertEquals("Unexpected error message", expectedMessage, actualMessage);
100 assertEquals("Unexpected error variables", Arrays.asList(expectedVariables), Arrays.asList(actualVariables));
103 public static void checkErrorMessageResponse(RestResponse errorResponse, ActionStatus actionStatus)
104 throws FileNotFoundException {
106 ErrorInfo expectedError = ErrorValidationUtils.parseErrorConfigYaml(actionStatus.name());
107 String expectedMessage = expectedError.getMessage();
110 ResponseFormat responseFormat = ResponseParser.parseToObjectUsingMapper(errorResponse.getResponse(),
111 ResponseFormat.class);
112 String actualMessage = responseFormat.getText();
114 assertEquals("Unexpected error message", expectedMessage, actualMessage);