Merge "Add bug fixes and tests for filters"
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / TestPdpMessage.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP Policy Models
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2019 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.models.pdp.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Test;
30 import org.onap.policy.models.pdp.enums.PdpMessageType;
31
32 /**
33  * Tests methods not already tested by {@link TestModels}.
34  */
35 public class TestPdpMessage {
36     private static final String PDP_NAME = "pdpA";
37     private static final String PDP_GROUP = "groupA";
38     private static final String PDP_SUBGROUP = "subgroupA";
39     private static final String DIFFERENT = "differentValue";
40
41     private PdpMessage message;
42
43     @Test
44     public void testCopyConstructor() {
45         assertThatThrownBy(() -> new PdpMessage((PdpMessage) null)).isInstanceOf(NullPointerException.class);
46
47         // verify with null values
48         message = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
49         PdpMessage newmsg = new PdpMessage(message);
50         newmsg.setRequestId(message.getRequestId());
51         newmsg.setTimestampMs(message.getTimestampMs());
52         assertEquals(message.toString(), newmsg.toString());
53
54         // verify with all values
55         message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
56         newmsg = new PdpMessage(message);
57         newmsg.setRequestId(message.getRequestId());
58         newmsg.setTimestampMs(message.getTimestampMs());
59         assertEquals(message.toString(), newmsg.toString());
60     }
61
62     @Test
63     public void testAppliesTo_NameCombos() {
64         /*
65          * Test cases where the name matches.
66          */
67         for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
68             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
69                 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
70
71                 for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
72                     for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
73                         assertTrue("name msg " + message + " pdp group " + pdpGroup + "/" + pdpSubgroup,
74                                         message.appliesTo(PDP_NAME, pdpGroup, pdpSubgroup));
75                     }
76                 }
77             }
78         }
79
80         /*
81          * Test cases where the name does not match.
82          */
83         for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
84             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
85                 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
86
87                 for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
88                     for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
89                         assertFalse("name msg " + message + " pdp group " + pdpGroup + "/" + pdpSubgroup,
90                                         message.appliesTo(DIFFERENT, pdpGroup, pdpSubgroup));
91                     }
92                 }
93             }
94         }
95     }
96
97     @Test
98     public void testAppliesTo_BroadcastGroup() {
99         /*
100          * Test cases where the group matches.
101          */
102         for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
103             message = makeMessage(null, PDP_GROUP, msgSubgroup);
104
105             assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
106         }
107
108         /*
109          * Test cases where the group does not match.
110          */
111         for (String msgGroup : new String[] {null, PDP_GROUP}) {
112             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
113                 message = makeMessage(null, msgGroup, msgSubgroup);
114
115                 for (String pdpGroup : new String[] {null, DIFFERENT}) {
116                     assertFalse("group msg " + message + " pdp group " + pdpGroup,
117                                     message.appliesTo(PDP_NAME, pdpGroup, PDP_SUBGROUP));
118                 }
119             }
120         }
121     }
122
123     @Test
124     public void testAppliesTo_BroadcastSubGroup() {
125         /*
126          * Test cases where the subgroup matches.
127          */
128         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
129         assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
130
131         /*
132          * Test cases where the subgroup does not match.
133          */
134         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
135
136         for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
137             assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
138                             message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
139         }
140     }
141
142     @Test
143     public void testAppliesTo_NullPdpName() {
144         message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
145
146         assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
147                         .isInstanceOf(NullPointerException.class);
148
149     }
150
151     private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
152         PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
153
154         msg.setName(pdpName);
155         msg.setPdpGroup(pdpGroup);
156         msg.setPdpSubgroup(pdpSubgroup);
157
158         return msg;
159     }
160 }