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