Set all cross references of policy/xacml-pdp
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / comm / listeners / XacmlPdpUpdateListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2023 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main.comm.listeners;
23
24 import static org.junit.Assert.assertNotNull;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyLong;
27 import static org.mockito.Mockito.doThrow;
28 import static org.mockito.Mockito.never;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
38 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
39 import org.onap.policy.models.pdp.concepts.PdpUpdate;
40 import org.onap.policy.pdpx.main.XacmlState;
41 import org.onap.policy.pdpx.main.comm.XacmlPdpHearbeatPublisher;
42 import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
43 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
44 import org.springframework.test.util.ReflectionTestUtils;
45
46 @RunWith(MockitoJUnitRunner.class)
47 public class XacmlPdpUpdateListenerTest {
48     private static final String EXPECTED_EXCEPTION = "expected exception";
49     private static final String TOPIC = "my-topic";
50     private static final long HB_INTERVAL = 100L;
51
52     @Mock
53     private TopicSinkClient client;
54
55     @Mock
56     private XacmlState state;
57
58     @Mock
59     private XacmlPdpHearbeatPublisher heartbeat;
60
61     @Mock
62     private XacmlPdpApplicationManager appmgr;
63
64     @Mock
65     private XacmlPdpUpdatePublisher publisher;
66
67     private PdpUpdate update;
68
69     private XacmlPdpUpdateListener listener;
70
71     /**
72      * Initializes objects, including the listener.
73      */
74     @Before
75     public void setUp() {
76         listener = new MyListener(client, state, heartbeat, appmgr);
77         update = new PdpUpdate();
78
79         when(state.shouldHandle(update)).thenReturn(true);
80
81         update.setPdpHeartbeatIntervalMs(HB_INTERVAL);
82     }
83
84     @Test
85     public void testOnTopicEvent_Unhandled() {
86         when(state.shouldHandle(update)).thenReturn(false);
87         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
88
89         verify(publisher, never()).handlePdpUpdate(any());
90         verify(heartbeat, never()).restart(anyLong());
91     }
92
93     @Test
94     public void testOnTopicEvent_SendOk() {
95         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
96
97         verify(publisher).handlePdpUpdate(update);
98         verify(heartbeat).restart(HB_INTERVAL);
99     }
100
101     @Test
102     public void testOnTopicEvent_SendEx() {
103         doThrow(new RuntimeException(EXPECTED_EXCEPTION)).when(publisher).handlePdpUpdate(update);
104
105         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
106
107         verify(publisher).handlePdpUpdate(update);
108         verify(heartbeat, never()).restart(anyLong());
109     }
110
111     @Test
112     public void testMakePublisher() {
113         // create a plain listener to test the "real" makePublisher() method
114         listener = new XacmlPdpUpdateListener(client, state, heartbeat, appmgr);
115         assertNotNull(ReflectionTestUtils.getField(listener, "publisher"));
116     }
117
118     private class MyListener extends XacmlPdpUpdateListener {
119
120         public MyListener(TopicSinkClient client, XacmlState state, XacmlPdpHearbeatPublisher heartbeat,
121                         XacmlPdpApplicationManager appManager) {
122             super(client, state, heartbeat, appManager);
123         }
124
125         @Override
126         protected XacmlPdpUpdatePublisher makePublisher(TopicSinkClient client, XacmlState state,
127                         XacmlPdpApplicationManager appManager) {
128             return publisher;
129         }
130     }
131 }