2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.pooling.message;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import org.junit.Test;
28 * Superclass used to test subclasses of {@link MessageWithAssignments}.
30 * @param <T> type of {@link MessageWithAssignments} subclass that this tests
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);
38 * {@code True} if {@code null} assignments are allowed, {@code false}
41 private boolean nullAssignments;
45 * @param subclazz subclass of {@link MessageWithAssignments} being tested
47 public MessageWithAssignmentsTester(Class<T> subclazz) {
52 * Indicates whether or not {@code null} assignments should be used for the
55 * @param nullAssignments {@code true} to use {@code null} assignments,
56 * {@code false} otherwise
58 public void setNullAssignments(boolean nullAssignments) {
59 this.nullAssignments = nullAssignments;
62 public boolean isNullAssignments() {
63 return nullAssignments;
67 public void testCheckValidity_InvalidFields() throws Exception {
68 // null source (i.e., superclass field)
69 expectCheckValidityFailure(msg -> msg.setSource(null));
72 expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(new String[0])));
75 String[] invalidAssignment = {"abc", null};
76 expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(invalidAssignment)));
80 public void testGetAssignments_testSetAssignments() {
81 MessageWithAssignments msg = makeValidMessage();
84 assertEquals(VALID_ASGN, msg.getAssignments());
86 BucketAssignments asgn = new BucketAssignments();
87 msg.setAssignments(asgn);
88 assertEquals(asgn, msg.getAssignments());
92 public void testDefaultConstructorFields(T msg) {
93 super.testDefaultConstructorFields(msg);
95 assertNull(msg.getAssignments());
99 public void testValidFields(T msg) {
100 super.testValidFields(msg);
102 if (nullAssignments) {
103 assertNull(msg.getAssignments());
106 assertEquals(VALID_ASGN, msg.getAssignments());