2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2019 NOKIA 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.onap.dmaap.dbcapi.resources;
23 import org.hamcrest.BaseMatcher;
24 import org.hamcrest.Description;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.onap.dmaap.dbcapi.model.ApiError;
30 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
31 import static org.junit.Assert.fail;
33 public class RequiredCheckerTest {
35 private static final String NAME = "field_name";
37 public ExpectedException thrown = ExpectedException.none();
38 private RequiredChecker requiredChecker = new RequiredChecker();
42 public void required_shouldThrowExceptionWhenObjectIsNull() throws RequiredFieldException {
43 thrown.expect(RequiredFieldException.class);
44 thrown.expect(new ApiErrorMatcher(new ApiError(BAD_REQUEST.getStatusCode(),
45 "missing required field", NAME)));
47 requiredChecker.required(NAME, null);
51 public void required_shouldThrowExceptionWhenRegexValidationFailed() throws RequiredFieldException {
52 thrown.expect(RequiredFieldException.class);
53 thrown.expect(new ApiErrorMatcher(new ApiError(BAD_REQUEST.getStatusCode(),
54 "value 'with white space' violates regexp check '^\\S+$'", NAME)));
56 requiredChecker.required(NAME, "with white space", "^\\S+$");
60 public void required_shouldPassValidation() {
62 requiredChecker.required(NAME, "value", "^\\S+$");
63 } catch (RequiredFieldException e) {
64 fail("No exception should be thrown");
68 class ApiErrorMatcher extends BaseMatcher {
70 private final ApiError expectedApiEror;
72 ApiErrorMatcher(ApiError expectedApiEror) {
73 this.expectedApiEror = expectedApiEror;
77 public boolean matches(Object exception) {
78 return expectedApiEror.equals(((RequiredFieldException) exception).getApiError());
82 public void describeTo(Description description) {
83 description.appendText("Following ApiError is expected: ").appendValue(expectedApiEror);