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