Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-pooling-messages / src / test / java / org / onap / policy / drools / pooling / message / SupportMessageWithAssignmentsTester.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP
4  * ================================================================================
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2024 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  * 
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.drools.pooling.message;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertNull;
26
27 import lombok.Getter;
28 import lombok.Setter;
29 import org.junit.jupiter.api.Test;
30
31 /**
32  * Superclass used to test subclasses of {@link MessageWithAssignments}.
33  * 
34  * @param <T> type of {@link MessageWithAssignments} subclass that this tests
35  */
36 @Setter
37 @Getter
38 public abstract class SupportMessageWithAssignmentsTester<T extends MessageWithAssignments>
39         extends SupportBasicMessageTester<T> {
40     // values set by makeValidMessage()
41     public static final String[] VALID_ARRAY = {VALID_HOST, VALID_HOST + "_xxx"};
42     public static final BucketAssignments VALID_ASGN = new BucketAssignments(VALID_ARRAY);
43
44     /**
45      * {@code True} if {@code null} assignments are allowed, {@code false}
46      * otherwise.
47      */
48     private boolean nullAssignments;
49
50     /**
51      * Constructor.
52      * 
53      * @param subclazz subclass of {@link MessageWithAssignments} being tested
54      */
55     public SupportMessageWithAssignmentsTester(Class<T> subclazz) {
56         super(subclazz);
57     }
58
59     @Test
60     public void testCheckValidity_InvalidFields() {
61         // null source (i.e., superclass field)
62         expectCheckValidityFailure(msg -> msg.setSource(null));
63
64         // empty assignments
65         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(new String[0])));
66
67         // invalid assignment
68         String[] invalidAssignment = {"abc", null};
69         expectCheckValidityFailure(msg -> msg.setAssignments(new BucketAssignments(invalidAssignment)));
70     }
71
72     @Test
73     public void testGetAssignments_testSetAssignments() {
74         MessageWithAssignments msg = makeValidMessage();
75
76         // from constructor
77         assertEquals(VALID_ASGN, msg.getAssignments());
78
79         BucketAssignments asgn = new BucketAssignments();
80         msg.setAssignments(asgn);
81         assertEquals(asgn, msg.getAssignments());
82     }
83
84     @Override
85     public void testDefaultConstructorFields(T msg) {
86         super.testDefaultConstructorFields(msg);
87
88         assertNull(msg.getAssignments());
89     }
90
91     @Override
92     public void testValidFields(T msg) {
93         super.testValidFields(msg);
94
95         if (nullAssignments) {
96             assertNull(msg.getAssignments());
97
98         } else {
99             assertEquals(VALID_ASGN, msg.getAssignments());
100         }
101     }
102
103 }