[DMAAP-BC] Consolidate bus controller repos
[dmaap/buscontroller.git] / dmaap-bc / src / test / java / org / onap / dmaap / dbcapi / resources / RequiredCheckerTest.java
1 /*
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
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.onap.dmaap.dbcapi.resources;
22
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;
29
30 import static javax.ws.rs.core.Response.Status.BAD_REQUEST;
31 import static org.junit.Assert.fail;
32
33 public class RequiredCheckerTest {
34
35     private static final String NAME = "field_name";
36     @Rule
37     public ExpectedException thrown = ExpectedException.none();
38     private RequiredChecker requiredChecker = new RequiredChecker();
39
40
41     @Test
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)));
46
47         requiredChecker.required(NAME, null);
48     }
49
50     @Test
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)));
55
56         requiredChecker.required(NAME, "with white space", "^\\S+$");
57     }
58
59     @Test
60     public void required_shouldPassValidation() {
61         try {
62             requiredChecker.required(NAME, "value", "^\\S+$");
63         } catch (RequiredFieldException e) {
64             fail("No exception should be thrown");
65         }
66     }
67
68     class ApiErrorMatcher extends BaseMatcher {
69
70         private final ApiError expectedApiEror;
71
72         ApiErrorMatcher(ApiError expectedApiEror) {
73             this.expectedApiEror = expectedApiEror;
74         }
75
76         @Override
77         public boolean matches(Object exception) {
78             return expectedApiEror.equals(((RequiredFieldException) exception).getApiError());
79         }
80
81         @Override
82         public void describeTo(Description description) {
83             description.appendText("Following ApiError is expected: ").appendValue(expectedApiEror);
84         }
85     }
86 }