Fix simple sonar issues in models: errors to sim-pdp
[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_MSG = " 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                 testName(PDP_NAME, true);
69             }
70         }
71
72         /*
73          * Test cases where the name does not match.
74          */
75         for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
76             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
77                 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
78                 testName(DIFFERENT, false);
79             }
80         }
81     }
82
83     private void testName(String pdpName, boolean expectMatch) {
84         for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
85             for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
86                 assertEquals("name msg " + message + PDP_GROUP_MSG + pdpGroup + "/" + pdpSubgroup, expectMatch,
87                                 message.appliesTo(pdpName, pdpGroup, pdpSubgroup));
88             }
89         }
90     }
91
92     @Test
93     public void testAppliesTo_BroadcastGroup() {
94         /*
95          * Test cases where the group matches.
96          */
97         for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
98             message = makeMessage(null, PDP_GROUP, msgSubgroup);
99
100             assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
101         }
102
103         /*
104          * Test cases where the group does not match.
105          */
106         for (String msgGroup : new String[] {null, PDP_GROUP}) {
107             for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
108                 message = makeMessage(null, msgGroup, msgSubgroup);
109
110                 for (String pdpGroup : new String[] {null, DIFFERENT}) {
111                     assertFalse("group msg " + message + PDP_GROUP_MSG + pdpGroup,
112                                     message.appliesTo(PDP_NAME, pdpGroup, PDP_SUBGROUP));
113                 }
114             }
115         }
116     }
117
118     @Test
119     public void testAppliesTo_BroadcastSubGroup() {
120         /*
121          * Test cases where the subgroup matches.
122          */
123         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
124         assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
125
126         /*
127          * Test cases where the subgroup does not match.
128          */
129         message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
130
131         for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
132             assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
133                             message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
134         }
135     }
136
137     @Test
138     public void testAppliesTo_NullPdpName() {
139         message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
140
141         assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
142                         .isInstanceOf(NullPointerException.class);
143
144     }
145
146     private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
147         PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
148
149         msg.setName(pdpName);
150         msg.setPdpGroup(pdpGroup);
151         msg.setPdpSubgroup(pdpSubgroup);
152
153         return msg;
154     }
155 }