aboutsummaryrefslogtreecommitdiffstats
path: root/models-pdp/src/test/java/org/onap/policy/models/pdp/concepts/PdpMessageTest.java
blob: 10f31312ce9483fc67642f3975010c817e91e71a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*-
 * ============LICENSE_START=======================================================
 * ONAP Policy Models
 * ================================================================================
 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
 * Modifications Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.models.pdp.concepts;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.onap.policy.models.pdp.enums.PdpMessageType;

public class PdpMessageTest {
    private static final String PDP_NAME = "pdpA";
    private static final String PDP_GROUP = "groupA";
    private static final String PDP_SUBGROUP = "subgroupA";
    private static final String DIFFERENT = "differentValue";

    private PdpMessage message;

    @Test
    public void testCopyConstructor() {
        assertThatThrownBy(() -> new PdpMessage((PdpMessage) null)).isInstanceOf(NullPointerException.class);

        // verify with null values
        message = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);
        PdpMessage newmsg = new PdpMessage(message);
        newmsg.setRequestId(message.getRequestId());
        newmsg.setTimestampMs(message.getTimestampMs());
        assertEquals(message.toString(), newmsg.toString());

        // verify with all values
        message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);
        newmsg = new PdpMessage(message);
        newmsg.setRequestId(message.getRequestId());
        newmsg.setTimestampMs(message.getTimestampMs());
        assertEquals(message.toString(), newmsg.toString());
    }

    @Test
    public void testAppliesTo_NameCombos() {
        /*
         * Test cases where the name matches.
         */
        for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
            for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
                message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);

                for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
                    for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
                        assertTrue("name msg " + message + " pdp group " + pdpGroup + "/" + pdpSubgroup,
                                        message.appliesTo(PDP_NAME, pdpGroup, pdpSubgroup));
                    }
                }
            }
        }

        /*
         * Test cases where the name does not match.
         */
        for (String msgGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
            for (String msgSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
                message = makeMessage(PDP_NAME, msgGroup, msgSubgroup);

                for (String pdpGroup : new String[] {null, PDP_GROUP, DIFFERENT}) {
                    for (String pdpSubgroup : new String[] {null, PDP_SUBGROUP, DIFFERENT}) {
                        assertFalse("name msg " + message + " pdp group " + pdpGroup + "/" + pdpSubgroup,
                                        message.appliesTo(DIFFERENT, pdpGroup, pdpSubgroup));
                    }
                }
            }
        }
    }

    @Test
    public void testAppliesTo_BroadcastGroup() {
        /*
         * Test cases where the group matches.
         */
        for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
            message = makeMessage(null, PDP_GROUP, msgSubgroup);

            assertTrue("group msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));
        }

        /*
         * Test cases where the group does not match.
         */
        for (String msgGroup : new String[] {null, PDP_GROUP}) {
            for (String msgSubgroup : new String[] {null, PDP_SUBGROUP}) {
                message = makeMessage(null, msgGroup, msgSubgroup);

                for (String pdpGroup : new String[] {null, DIFFERENT}) {
                    assertFalse("group msg " + message + " pdp group " + pdpGroup,
                                    message.appliesTo(PDP_NAME, pdpGroup, PDP_SUBGROUP));
                }
            }
        }
    }

    @Test
    public void testAppliesTo_BroadcastSubGroup() {
        /*
         * Test cases where the subgroup matches.
         */
        message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);
        assertTrue("subgroup msg " + message, message.appliesTo(PDP_NAME, PDP_GROUP, PDP_SUBGROUP));

        /*
         * Test cases where the subgroup does not match.
         */
        message = makeMessage(null, PDP_GROUP, PDP_SUBGROUP);

        for (String pdpSubgroup : new String[] {null, DIFFERENT}) {
            assertFalse("subgroup msg " + message + " pdp subgroup " + pdpSubgroup,
                            message.appliesTo(PDP_NAME, PDP_GROUP, pdpSubgroup));
        }
    }

    @Test
    public void testAppliesTo_NullPdpName() {
        message = makeMessage(PDP_NAME, PDP_GROUP, PDP_SUBGROUP);

        assertThatThrownBy(() -> message.appliesTo(null, PDP_GROUP, PDP_SUBGROUP))
                        .isInstanceOf(NullPointerException.class);

    }

    private PdpMessage makeMessage(String pdpName, String pdpGroup, String pdpSubgroup) {
        PdpMessage msg = new PdpMessage(PdpMessageType.PDP_STATE_CHANGE);

        msg.setName(pdpName);
        msg.setPdpGroup(pdpGroup);
        msg.setPdpSubgroup(pdpSubgroup);

        return msg;
    }
}