Merge "Fix more sonar issues in models: yaml to dao"
[policy/models.git] / models-pdp / src / test / java / org / onap / policy / models / pdp / concepts / PdpMessageTest.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 public class PdpMessageTest {
33     private static final String PDP_GROUP_STRING = " pdp group ";
34     private static final String PDP_NAME = "pdpA";
35     private static final String PDP_GROUP = "groupA";
36     private static final String PDP_SUBGROUP = "subgroupA";
37     private static final String DIFFERENT = "differentValue";
38
39     private PdpMessage message;
40
41     @Test
42     public void testCopyConstructor() {
43         assertThatThrownBy(() -> new PdpMessage((PdpMessage) null)).isInstanceOf(NullPointerException.class);
44
45         // verify with null values
46         message = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
47         PdpMessage newmsg = new PdpMessage(message);
48         newmsg.setRequestId(message.getRequestId());
49         newmsg.setTimestampMs(message.getTimestampMs());
50         assertEquals(message.toString(), newmsg.toString());
51
52         // verify with all values
53         message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
54         newmsg = new PdpMessage(message);
55         newmsg.setRequestId(message.getRequestId());
56         newmsg.setTimestampMs(message.getTimestampMs());
57         assertEquals(message.toString(), newmsg.toString());
58     }
59
60     @Test
61     public void testAppliesTo_NameCombos() {
62         /*
63          * Test cases where the name matches.
64          */
65         for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
66             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
67                 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
68
69                 for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
70                     for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
71                         assertTrue("name msg " + message + PDP_GROUP_STRING + pdpGroup + "/" + pdpSubgroup,
72                                         message.appliesTo(PDP_NAME, pdpGroup, pdpSubgroup));
73                     }
74                 }
75             }
76         }
77
78         /*
79          * Test cases where the name does not match.
80          */
81         for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
82             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
83                 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
84
85                 for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
86                     for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
87                         assertFalse("name msg " + message + PDP_GROUP_STRING + pdpGroup + "/" + pdpSubgroup,
88                                         message.appliesTo(DIFFERENT, pdpGroup, pdpSubgroup));
89                     }
90                 }
91             }
92         }
93     }
94
95     @Test
96     public void testAppliesTo_BroadcastGroup() {
97         /*
98          * Test cases where the group matches.
99          */
100         for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
101             message = makeMessage(null, PDP_GROUP, msgSubgroup);
102
103             assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
104         }
105
106         /*
107          * Test cases where the group does not match.
108          */
109         for (String msgGroup : new String[] {null, PDP_GROUP}) {
110             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
111                 message = makeMessage(null, msgGroup, msgSubgroup);
112
113                 for (String pdpGroup : new String[] {null, DIFFERENT}) {
114                     assertFalse("group msg " + message + PDP_GROUP_STRING + pdpGroup,
115                                     message.appliesTo(PDP_NAME, pdpGroup, PDP_SUBGROUP));
116                 }
117             }
118         }
119     }
120
121     @Test
122     public void testAppliesTo_BroadcastSubGroup() {
123         /*
124          * Test cases where the subgroup matches.
125          */
126         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
127         assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
128
129         /*
130          * Test cases where the subgroup does not match.
131          */
132         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
133
134         for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
135             assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
136                             message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
137         }
138     }
139
140     @Test
141     public void testAppliesTo_NullPdpName() {
142         message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
143
144         assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
145                         .isInstanceOf(NullPointerException.class);
146
147     }
148
149     private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
150         PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
151
152         msg.setName(pdpName);
153         msg.setPdpGroup(pdpGroup);
154         msg.setPdpSubgroup(pdpSubgroup);
155
156         return msg;
157     }
158 }