Merge "Refactor request map"
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / msgdata / StateChangeReqTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm.msgdata;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertSame;
27 import static org.junit.Assert.assertTrue;
28
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.models.pdp.concepts.PdpStateChange;
32 import org.onap.policy.models.pdp.concepts.PdpStatus;
33 import org.onap.policy.models.pdp.concepts.PdpUpdate;
34 import org.onap.policy.pap.main.comm.CommonRequestBase;
35
36 public class StateChangeReqTest extends CommonRequestBase {
37
38     private StateChangeReq data;
39     private PdpStatus response;
40     private PdpStateChange msg;
41
42     /**
43      * Sets up.
44      * @throws Exception if an error occurs
45      */
46     @Before
47     public void setUp() throws Exception {
48         super.setUp();
49
50         response = new PdpStatus();
51
52         response.setName(MY_NAME);
53         response.setState(MY_STATE);
54
55         msg = new PdpStateChange();
56         msg.setName(MY_NAME);
57         msg.setState(MY_STATE);
58
59         data = new StateChangeReq(reqParams, MY_REQ_NAME, msg);
60     }
61
62     @Test
63     public void testGetMessage() {
64         assertEquals(MY_REQ_NAME, data.getName());
65         assertSame(msg, data.getMessage());
66     }
67
68     @Test
69     public void testCheckResponse() {
70         assertNull(data.checkResponse(response));
71     }
72
73     @Test
74     public void testCheckResponse_NullName() {
75         response.setName(null);
76
77         assertEquals("null PDP name", data.checkResponse(response));
78     }
79
80     @Test
81     public void testCheckResponse_NullMsgName() {
82         msg.setName(null);
83
84         assertEquals(null, data.checkResponse(response));
85     }
86
87     @Test
88     public void testCheckResponse_MismatchedState() {
89         response.setState(DIFF_STATE);
90
91         assertEquals("state is TERMINATED, but expected SAFE", data.checkResponse(response));
92     }
93
94     @Test
95     public void isSameContent() {
96         PdpStateChange msg2 = new PdpStateChange();
97         msg2.setName("world");
98         msg2.setState(MY_STATE);
99         assertTrue(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, msg2)));
100
101         // different state
102         msg2.setState(DIFF_STATE);
103         assertFalse(data.isSameContent(new StateChangeReq(reqParams, MY_REQ_NAME, msg2)));
104
105         // different request type
106         assertFalse(data.isSameContent(new UpdateReq(reqParams, MY_REQ_NAME, new PdpUpdate())));
107     }
108
109     @Test
110     public void testGetPriority() {
111         assertTrue(data.getPriority() < new UpdateReq(reqParams, MY_REQ_NAME, new PdpUpdate()).getPriority());
112     }
113 }