2b670dcc5e1881790ad9bf6ff93834098d173e43
[policy/drools-pdp.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
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.drools.pooling.message;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import org.junit.Test;
26
27 /**
28  * Superclass used to test subclasses of {@link MessageWithAssignments}.
29  * 
30  * @param <T> type of {@link MessageWithAssignments} subclass that this tests
31  */
32 public abstract class MessageWithAssignmentsTester<T extends MessageWithAssignments> extends BasicMessageTester<T> {
33     // values set by makeValidMessage()
34     public static final String[] VALID_ARRAY = {VALID_HOST, VALID_HOST+"_xxx"};
35     public static final BucketAssignments VALID_ASGN = new BucketAssignments(VALID_ARRAY);
36
37     /**
38      * {@code True} if {@code null} assignments are allowed, {@code false}
39      * otherwise.
40      */
41     private boolean nullAssignments;
42
43     /**
44      * 
45      * @param subclazz subclass of {@link MessageWithAssignments} being tested
46      */
47     public MessageWithAssignmentsTester(Class<T> subclazz) {
48         super(subclazz);
49     }
50
51     /**
52      * Indicates whether or not {@code null} assignments should be used for the
53      * remaining tests.
54      * 
55      * @param nullAssignments {@code true} to use {@code null} assignments,
56      *        {@code false} otherwise
57      */
58     public void setNullAssignments(boolean nullAssignments) {
59         this.nullAssignments = nullAssignments;
60     }
61
62     public boolean isNullAssignments() {
63         return nullAssignments;
64     }
65
66     @Test
67     public void testCheckValidity_InvalidFields() throws Exception {
68         // null source (i.e., superclass field)
69         expectCheckValidityFailure(msg -> msg.setSource(null));
70
71         // empty assignments
72         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(new String[0])));
73
74         // invalid assignment
75         String[] invalidAssignment = {"abc", null};
76         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(invalidAssignment)));
77     }
78
79     @Test
80     public void testGetAssignments_testSetAssignments() {
81         MessageWithAssignments msg = makeValidMessage();
82
83         // from constructor
84         assertEquals(VALID_ASGN, msg.getAssignments());
85
86         BucketAssignments asgn = new BucketAssignments();
87         msg.setAssignments(asgn);
88         assertEquals(asgn, msg.getAssignments());
89     }
90
91     @Override
92     public void testDefaultConstructorFields(T msg) {
93         super.testDefaultConstructorFields(msg);
94
95         assertNull(msg.getAssignments());
96     }
97
98     @Override
99     public void testValidFields(T msg) {
100         super.testValidFields(msg);
101
102         if (nullAssignments) {
103             assertNull(msg.getAssignments());
104
105         } else {
106             assertEquals(VALID_ASGN, msg.getAssignments());
107         }
108     }
109
110 }