2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.models.pdp.concepts;
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.assertNotEquals;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.onap.policy.models.pdp.enums.PdpMessageType;
33 public class PdpMessageTest {
34 private static final String PDP_GROUP_MSG = " pdp group ";
35 private static final String PDP_NAME = "pdpA";
36 private static final String PDP_GROUP = "groupA";
37 private static final String PDP_SUBGROUP = "subgroupA";
38 private static final String DIFFERENT = "differentValue";
40 private PdpMessage message;
43 public void testCopyConstructorAndEquals() {
44 assertThatThrownBy(() -> new PdpMessage((PdpMessage) null)).isInstanceOf(NullPointerException.class);
46 // verify with null values
47 message = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
48 PdpMessage newmsg = new PdpMessage(message);
49 newmsg.setRequestId(message.getRequestId());
50 newmsg.setTimestampMs(message.getTimestampMs());
51 assertEquals(message.toString(), newmsg.toString());
52 assertEquals(message, newmsg);
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 assertEquals(message, newmsg);
62 newmsg.setTimestampMs(1);
63 assertNotEquals(message, newmsg);
67 public void testAppliesTo_NameCombos() {
69 * Test cases where the name matches.
71 for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
72 for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
73 message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);
74 testName(PDP_NAME, true);
79 * Test cases where the name does not match.
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 testName(DIFFERENT, false);
89 private void testName(String pdpName, boolean expectMatch) {
90 for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
91 for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
92 assertEquals("name msg " + message + PDP_GROUP_MSG + pdpGroup + "/" + pdpSubgroup, expectMatch,
93 message.appliesTo(pdpName, pdpGroup, pdpSubgroup));
99 public void testAppliesTo_BroadcastGroup() {
101 * Test cases where the group matches.
103 for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
104 message = makeMessage(null, PDP_GROUP, msgSubgroup);
106 assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
110 * Test cases where the group does not match.
112 for (String msgGroup : new String[] {null, PDP_GROUP}) {
113 for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
114 message = makeMessage(null, msgGroup, msgSubgroup);
116 for (String pdpGroup : new String[] {null, DIFFERENT}) {
117 assertFalse("group msg " + message + PDP_GROUP_MSG + pdpGroup,
118 message.appliesTo(PDP_NAME, pdpGroup, PDP_SUBGROUP));
125 public void testAppliesTo_BroadcastSubGroup() {
127 * Test cases where the subgroup matches.
129 message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
130 assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
133 * Test cases where the subgroup does not match.
135 message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
137 for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
138 assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
139 message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
144 public void testAppliesTo_NullPdpName() {
145 message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
147 assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
148 .isInstanceOf(NullPointerException.class);
152 private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
153 PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
155 msg.setName(pdpName);
156 msg.setPdpGroup(pdpGroup);
157 msg.setPdpSubgroup(pdpSubgroup);