e16cd899fea266aa415b50a02124a61bf3df57a5
[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.models.pdp.enums.PdpState;
35 import org.onap.policy.pap.main.comm.CommonRequestBase;
36
37 public class StateChangeReqTest extends CommonRequestBase {
38
39     private StateChangeReq data;
40     private PdpStatus response;
41     private PdpStateChange msg;
42
43     /**
44      * Sets up.
45      *
46      * @throws Exception if an error occurs
47      */
48     @Before
49     public void setUp() throws Exception {
50         super.setUp();
51
52         response = new PdpStatus();
53
54         response.setName(MY_NAME);
55         response.setState(MY_STATE);
56
57         msg = new PdpStateChange();
58         msg.setName(MY_NAME);
59         msg.setState(MY_STATE);
60
61         data = new StateChangeReq(reqParams, MY_REQ_NAME, msg);
62     }
63
64     @Test
65     public void testGetMessage() {
66         assertEquals(MY_REQ_NAME, data.getName());
67         assertSame(msg, data.getMessage());
68     }
69
70     @Test
71     public void testCheckResponse() {
72         assertNull(data.checkResponse(response));
73     }
74
75     @Test
76     public void testCheckResponse_NullName() {
77         response.setName(null);
78
79         assertEquals("null PDP name", data.checkResponse(response));
80     }
81
82     @Test
83     public void testCheckResponse_NullMsgName() {
84         msg.setName(null);
85
86         assertEquals(null, data.checkResponse(response));
87     }
88
89     @Test
90     public void testCheckResponse_MismatchedState() {
91         response.setState(DIFF_STATE);
92
93         assertEquals("state is TERMINATED, but expected SAFE", data.checkResponse(response));
94     }
95
96     @Test
97     public void testReconfigure() {
98         // different message type should fail and leave message unchanged
99         assertFalse(data.reconfigure(new PdpUpdate()));
100         assertSame(msg, data.getMessage());
101
102         // same state - should succeed, but leave message unchanged
103         PdpStateChange msg2 = new PdpStateChange();
104         msg2.setState(msg.getState());
105         assertTrue(data.reconfigure(msg2));
106         assertSame(msg, data.getMessage());
107
108         // change old state to active - should fail and leave message unchanged
109         msg2.setState(PdpState.ACTIVE);
110         assertFalse(data.reconfigure(msg2));
111         assertSame(msg, data.getMessage());
112
113         // different, inactive state - should succeed and install NEW message
114         msg2.setState(PdpState.PASSIVE);
115         assertTrue(data.reconfigure(msg2));
116         assertSame(msg2, data.getMessage());
117     }
118 }