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