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