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