2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2023-2024 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.comm.listeners;
24 import static org.junit.jupiter.api.Assertions.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyLong;
27 import static org.mockito.Mockito.lenient;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.junit.jupiter.api.extension.ExtendWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.jupiter.MockitoExtension;
36 import org.onap.policy.common.message.bus.event.Topic.CommInfrastructure;
37 import org.onap.policy.common.message.bus.event.client.TopicSinkClient;
38 import org.onap.policy.models.pdp.concepts.PdpUpdate;
39 import org.onap.policy.pdpx.main.XacmlState;
40 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
41 import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
42 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
43 import org.springframework.test.util.ReflectionTestUtils;
45 @ExtendWith(MockitoExtension.class)
46 class XacmlPdpUpdateListenerTest {
47 private static final String EXPECTED_EXCEPTION = "expected exception";
48 private static final String TOPIC = "my-topic";
49 private static final long HB_INTERVAL = 100L;
52 private TopicSinkClient client;
55 private XacmlState state;
58 private XacmlPdpHearbeatPublisher heartbeat;
61 private XacmlPdpApplicationManager appmgr;
64 private XacmlPdpUpdatePublisher publisher;
66 private PdpUpdate update;
68 private XacmlPdpUpdateListener listener;
71 * Initializes objects, including the listener.
75 listener = new MyListener(client, state, heartbeat, appmgr);
76 update = new PdpUpdate();
78 lenient().when(state.shouldHandle(update)).thenReturn(true);
80 update.setPdpHeartbeatIntervalMs(HB_INTERVAL);
84 void testOnTopicEvent_Unhandled() {
85 lenient().when(state.shouldHandle(update)).thenReturn(false);
86 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
88 verify(publisher, never()).handlePdpUpdate(any());
89 verify(heartbeat, never()).restart(anyLong());
93 void testOnTopicEvent_SendOk() {
94 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
96 verify(publisher).handlePdpUpdate(update);
97 verify(heartbeat).restart(HB_INTERVAL);
101 void testOnTopicEvent_SendEx() {
102 lenient().doThrow(new RuntimeException(EXPECTED_EXCEPTION)).when(publisher).handlePdpUpdate(update);
104 listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
106 verify(publisher).handlePdpUpdate(update);
107 verify(heartbeat, never()).restart(anyLong());
111 void testMakePublisher() {
112 // create a plain listener to test the "real" makePublisher() method
113 listener = new XacmlPdpUpdateListener(client, state, heartbeat, appmgr);
114 assertNotNull(ReflectionTestUtils.getField(listener, "publisher"));
117 private class MyListener extends XacmlPdpUpdateListener {
119 MyListener(TopicSinkClient client, XacmlState state, XacmlPdpHearbeatPublisher heartbeat,
120 XacmlPdpApplicationManager appManager) {
121 super(client, state, heartbeat, appManager);
125 protected XacmlPdpUpdatePublisher makePublisher(TopicSinkClient client, XacmlState state,
126 XacmlPdpApplicationManager appManager) {