2 * ============LICENSE_START=======================================================
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
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.assertTrue;
29 import org.junit.Test;
30 import org.onap.policy.models.pdp.enums.PdpMessageType;
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";
39 private PdpMessage message;
42 public void testCopyConstructor() {
43 assertThatThrownBy(() -> new PdpMessage((PdpMessage) null)).isInstanceOf(NullPointerException.class);
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());
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());
61 public void testAppliesTo_NameCombos() {
63 * Test cases where the name matches.
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);
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));
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);
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));
96 public void testAppliesTo_BroadcastGroup() {
98 * Test cases where the group matches.
100 for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
101 message = makeMessage(null, PDP_GROUP, msgSubgroup);
103 assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
107 * Test cases where the group does not match.
109 for (String msgGroup : new String[] {null, PDP_GROUP}) {
110 for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
111 message = makeMessage(null, msgGroup, msgSubgroup);
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));
122 public void testAppliesTo_BroadcastSubGroup() {
124 * Test cases where the subgroup matches.
126 message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
127 assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
130 * Test cases where the subgroup does not match.
132 message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
134 for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
135 assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
136 message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
141 public void testAppliesTo_NullPdpName() {
142 message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
144 assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
145 .isInstanceOf(NullPointerException.class);
149 private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
150 PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
152 msg.setName(pdpName);
153 msg.setPdpGroup(pdpGroup);
154 msg.setPdpSubgroup(pdpSubgroup);