2 * ============LICENSE_START=======================================================
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
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.pap.main.comm.msgdata;
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;
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;
38 class StateChangeReqTest extends CommonRequestBase {
40 private StateChangeReq data;
41 private PdpStatus response;
42 private PdpStateChange msg;
47 * @throws Exception if an error occurs
50 public void setUp() throws Exception {
53 response = new PdpStatus();
55 response.setName(MY_NAME);
56 response.setState(MY_STATE);
58 msg = new PdpStateChange();
60 msg.setState(MY_STATE);
62 data = new StateChangeReq(reqParams, MY_REQ_NAME, msg);
66 public void testGetMessage() {
67 assertEquals(MY_REQ_NAME, data.getName());
68 assertSame(msg, data.getMessage());
72 public void testCheckResponse() {
73 assertNull(data.checkResponse(response));
77 public void testCheckResponse_NullName() {
78 response.setName(null);
80 assertEquals("null PDP name", data.checkResponse(response));
84 public void testCheckResponse_NullMsgName() {
87 assertNull(data.checkResponse(response));
91 public void testCheckResponse_MismatchedState() {
92 response.setState(DIFF_STATE);
94 assertEquals("state is TERMINATED, but expected SAFE", data.checkResponse(response));
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());
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());
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());
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());