Fix sonars from dependency upgrade
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / comm / XacmlPdpHearbeatPublisherTest.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;
22
23 import static org.assertj.core.api.Assertions.assertThatCode;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyLong;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import java.util.LinkedList;
36 import java.util.Queue;
37 import java.util.concurrent.ScheduledExecutorService;
38 import java.util.concurrent.ScheduledFuture;
39 import java.util.concurrent.TimeUnit;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.pdpx.main.XacmlState;
48
49 @RunWith(MockitoJUnitRunner.class)
50 public class XacmlPdpHearbeatPublisherTest {
51
52     private static final long INTERVAL1 = 1000L;
53     private static final long INTERVAL2 = 2000L;
54     private static final long INTERVAL_INVALID = 0;
55
56     @Mock
57     private TopicSinkClient client;
58
59     @Mock
60     private XacmlState state;
61
62     @Mock
63     private ScheduledExecutorService executor;
64
65     @Mock
66     private ScheduledFuture<?> timer1;
67
68     @Mock
69     private ScheduledFuture<?> timer2;
70
71     @Mock
72     private PdpStatus status;
73
74     private Queue<ScheduledFuture<?>> timers;
75
76     private XacmlPdpHearbeatPublisher publisher;
77
78
79     /**
80      * Initializes objects, including the publisher.
81      */
82     @Before
83     public void setUp() {
84         when(state.genHeartbeat()).thenReturn(status);
85
86         timers = new LinkedList<>(Arrays.asList(timer1, timer2));
87
88         when(executor.scheduleWithFixedDelay(any(), anyLong(), anyLong(), any())).thenAnswer(args -> timers.remove());
89
90         publisher = new MyPublisher(client, state);
91     }
92
93     @Test
94     public void testRun() {
95         publisher.run();
96
97         verify(state).genHeartbeat();
98         verify(client).send(status);
99     }
100
101     @Test
102     public void testTerminate() {
103         // not yet started
104         publisher.terminate();
105
106
107         // now start it and then try again
108         publisher.start();
109         publisher.terminate();
110
111         // timer2 should still be in the queue
112         assertSame(timer2, timers.peek());
113
114
115         // repeat - nothing more should happen
116         publisher.terminate();
117
118         // timer2 should still be in the queue
119         assertSame(timer2, timers.peek());
120     }
121
122     @Test
123     public void testRestart() {
124         // not started yet - should only update the interval
125         publisher.restart(INTERVAL1);
126
127         assertEquals(INTERVAL1, publisher.getIntervalMs());
128         assertSame(timer1, timers.peek());
129
130         // now start it
131         publisher.start();
132         verify(executor).scheduleWithFixedDelay(publisher, 0, INTERVAL1, TimeUnit.MILLISECONDS);
133
134         // null interval - no changes
135         publisher.restart(null);
136         verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
137         assertSame(timer2, timers.peek());
138
139         // same interval - no changes
140         publisher.restart(INTERVAL1);
141         verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
142         assertSame(timer2, timers.peek());
143
144         // invalid interval - no changes
145         publisher.restart(INTERVAL_INVALID);
146         verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
147         assertSame(timer2, timers.peek());
148
149         // new interval - old timer should be cancelled and new started
150         publisher.restart(INTERVAL2);
151         verify(timer1).cancel(anyBoolean());
152         verify(executor).scheduleWithFixedDelay(publisher, 0, INTERVAL2, TimeUnit.MILLISECONDS);
153     }
154
155     @Test
156     public void testStart() {
157         publisher.start();
158
159         verify(executor).scheduleWithFixedDelay(publisher, 0, XacmlPdpHearbeatPublisher.DEFAULT_INTERVAL_MS,
160                         TimeUnit.MILLISECONDS);
161
162         // repeat - nothing more should happen
163         publisher.start();
164         verify(executor, times(1)).scheduleWithFixedDelay(any(), anyLong(), anyLong(), any());
165         verify(timer1, never()).cancel(anyBoolean());
166     }
167
168     @Test
169     public void testMakeTimerThread() {
170         // create a plain listener to test the "real" makeTimer() method
171         publisher = new XacmlPdpHearbeatPublisher(client, state);
172
173         assertThatCode(() -> {
174             publisher.start();
175             publisher.restart(100L);
176             publisher.terminate();
177         }).doesNotThrowAnyException();
178     }
179
180     private class MyPublisher extends XacmlPdpHearbeatPublisher {
181
182         public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
183             super(topicSinkClient, state);
184         }
185
186         @Override
187         protected ScheduledExecutorService makeTimerThread() {
188             return executor;
189         }
190     }
191 }