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