Fix sonars from dependency upgrade
[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  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.comm.listeners;
22
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyLong;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
37 import org.onap.policy.common.endpoints.event.comm.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.powermock.reflect.Whitebox;
44
45 @RunWith(MockitoJUnitRunner.class)
46 public 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;
50
51     @Mock
52     private TopicSinkClient client;
53
54     @Mock
55     private XacmlState state;
56
57     @Mock
58     private XacmlPdpHearbeatPublisher heartbeat;
59
60     @Mock
61     private XacmlPdpApplicationManager appmgr;
62
63     @Mock
64     private XacmlPdpUpdatePublisher publisher;
65
66     private PdpUpdate update;
67
68     private XacmlPdpUpdateListener listener;
69
70     /**
71      * Initializes objects, including the listener.
72      */
73     @Before
74     public void setUp() {
75         listener = new MyListener(client, state, heartbeat, appmgr);
76         update = new PdpUpdate();
77
78         when(state.shouldHandle(update)).thenReturn(true);
79
80         update.setPdpHeartbeatIntervalMs(HB_INTERVAL);
81     }
82
83     @Test
84     public void testOnTopicEvent_Unhandled() {
85         when(state.shouldHandle(update)).thenReturn(false);
86         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
87
88         verify(publisher, never()).handlePdpUpdate(any());
89         verify(heartbeat, never()).restart(anyLong());
90     }
91
92     @Test
93     public void testOnTopicEvent_SendOk() {
94         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
95
96         verify(publisher).handlePdpUpdate(update);
97         verify(heartbeat).restart(HB_INTERVAL);
98     }
99
100     @Test
101     public void testOnTopicEvent_SendEx() {
102         doThrow(new RuntimeException(EXPECTED_EXCEPTION)).when(publisher).handlePdpUpdate(update);
103
104         listener.onTopicEvent(CommInfrastructure.NOOP, TOPIC, null, update);
105
106         verify(publisher).handlePdpUpdate(update);
107         verify(heartbeat, never()).restart(anyLong());
108     }
109
110     @Test
111     public void testMakePublisher() {
112         // create a plain listener to test the "real" makePublisher() method
113         listener = new XacmlPdpUpdateListener(client, state, heartbeat, appmgr);
114         assertNotNull(Whitebox.getInternalState(listener, "publisher"));
115     }
116
117     private class MyListener extends XacmlPdpUpdateListener {
118
119         public MyListener(TopicSinkClient client, XacmlState state, XacmlPdpHearbeatPublisher heartbeat,
120                         XacmlPdpApplicationManager appManager) {
121             super(client, state, heartbeat, appManager);
122         }
123
124         @Override
125         protected XacmlPdpUpdatePublisher makePublisher(TopicSinkClient client, XacmlState state,
126                         XacmlPdpApplicationManager appManager) {
127             return publisher;
128         }
129     }
130 }