1e5fcfcdc0cd66c3a2d02a56dd6b8236377770ec
[policy/common.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Engine - Common Modules
4  * ================================================================================
5  * Copyright (C) 2018 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.onap.policy.common.utils.properties.exception;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertThat;
26 import org.hamcrest.CoreMatchers;
27
28 /**
29  * Superclass used to test subclasses of {@link PropertyException}.
30  */
31 public class BasicPropertyExceptionTester {
32
33     /**
34      * The "message" that's passed each time an exception is constructed.
35      */
36     protected static final String MESSAGE = "some error";
37
38     /**
39      * The "throwable" that's passed each time an exception is constructed.
40      */
41     protected static final Throwable THROWABLE = new Throwable();
42
43     /**
44      * Name of the "property" to be passed each time an exception is constructed.
45      */
46     protected static final String PROPERTY = "myName";
47
48     /**
49      * Name of the "property" field.
50      */
51     protected static final String FIELD = "PROPERTY";
52
53     /*
54      * Methods to perform various tests on the except subclass. 
55      */
56
57     protected void doTestPropertyExceptionStringField_AllPopulated(PropertyException ex) {
58         standardTests(ex);
59     }
60
61     protected void doTestPropertyExceptionStringField_NullProperty(PropertyException ex) {
62         assertEquals(null, ex.getPropertyName());
63         assertEquals(FIELD, ex.getFieldName());
64         assertNotNull(ex.getMessage());
65         assertNotNull(ex.toString());
66     }
67
68     protected void doTestPropertyExceptionStringField_NullField(PropertyException ex) {
69         assertEquals(PROPERTY, ex.getPropertyName());
70         assertEquals(null, ex.getFieldName());
71         assertNotNull(ex.getMessage());
72         assertNotNull(ex.toString());
73     }
74
75     protected void doTestPropertyExceptionStringField_BothNull(PropertyException ex) {
76         assertEquals(null, ex.getPropertyName());
77         assertEquals(null, ex.getFieldName());
78         assertNotNull(ex.getMessage());
79         assertNotNull(ex.toString());
80     }
81
82     protected void doTestPropertyExceptionStringFieldString(PropertyException ex) {
83         standardTests(ex);
84         standardMessageTests(ex);
85     }
86
87     protected void doTestPropertyExceptionStringFieldThrowable(PropertyException ex) {
88         standardTests(ex);
89         standardThrowableTests(ex);
90     }
91
92     protected void doTestPropertyExceptionStringFieldStringThrowable(PropertyException ex) {
93         standardTests(ex);
94         standardMessageTests(ex);
95         standardThrowableTests(ex);
96     }
97
98     /**
99      * Performs standard tests that should apply to all subclasses.
100      * 
101      * @param ex exception to test
102      */
103     protected void standardTests(PropertyException ex) {
104         assertEquals(PROPERTY, ex.getPropertyName());
105         assertEquals(FIELD, ex.getFieldName());
106         assertNotNull(ex.getMessage());
107         assertNotNull(ex.toString());
108     }
109
110     /**
111      * Performs standard tests for exceptions that were provided a message in their
112      * constructor.
113      * 
114      * @param ex exception to test
115      */
116     protected void standardMessageTests(PropertyException ex) {
117         assertThat(ex.getMessage(), CoreMatchers.endsWith(MESSAGE));
118     }
119
120     /**
121      * Performs standard tests for exceptions that were provided a throwable in their
122      * constructor.
123      * 
124      * @param ex exception to test
125      */
126     protected void standardThrowableTests(PropertyException ex) {
127         assertEquals(THROWABLE, ex.getCause());
128     }
129
130 }