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;
 
  26 import org.junit.Test;
 
  29  * Superclass used to test subclasses of {@link MessageWithAssignments}.
 
  31  * @param <T> type of {@link MessageWithAssignments} subclass that this tests
 
  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);
 
  39      * {@code True} if {@code null} assignments are allowed, {@code false}
 
  42     private boolean nullAssignments;
 
  47      * @param subclazz subclass of {@link MessageWithAssignments} being tested
 
  49     public MessageWithAssignmentsTester(Class<T> subclazz) {
 
  54      * Indicates whether or not {@code null} assignments should be used for the
 
  57      * @param nullAssignments {@code true} to use {@code null} assignments,
 
  58      *        {@code false} otherwise
 
  60     public void setNullAssignments(boolean nullAssignments) {
 
  61         this.nullAssignments = nullAssignments;
 
  64     public boolean isNullAssignments() {
 
  65         return nullAssignments;
 
  69     public void testCheckValidity_InvalidFields() throws Exception {
 
  70         // null source (i.e., superclass field)
 
  71         expectCheckValidityFailure(msg -> msg.setSource(null));
 
  74         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(new String[0])));
 
  77         String[] invalidAssignment = {"abc", null};
 
  78         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(invalidAssignment)));
 
  82     public void testGetAssignments_testSetAssignments() {
 
  83         MessageWithAssignments msg = makeValidMessage();
 
  86         assertEquals(VALID_ASGN, msg.getAssignments());
 
  88         BucketAssignments asgn = new BucketAssignments();
 
  89         msg.setAssignments(asgn);
 
  90         assertEquals(asgn, msg.getAssignments());
 
  94     public void testDefaultConstructorFields(T msg) {
 
  95         super.testDefaultConstructorFields(msg);
 
  97         assertNull(msg.getAssignments());
 
 101     public void testValidFields(T msg) {
 
 102         super.testValidFields(msg);
 
 104         if (nullAssignments) {
 
 105             assertNull(msg.getAssignments());
 
 108             assertEquals(VALID_ASGN, msg.getAssignments());