2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.comm;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.mockito.Matchers.any;
26 import static org.mockito.Matchers.anyBoolean;
27 import static org.mockito.Matchers.anyInt;
28 import static org.mockito.Matchers.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;
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.mockito.Mock;
43 import org.mockito.MockitoAnnotations;
44 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.pdpx.main.XacmlState;
48 public class XacmlPdpHearbeatPublisherTest {
50 private static final long INTERVAL1 = 1000L;
51 private static final long INTERVAL2 = 2000L;
52 private static final long INTERVAL_INVALID = 0;
55 private TopicSinkClient client;
58 private XacmlState state;
61 private ScheduledExecutorService executor;
64 private ScheduledFuture<?> timer1;
67 private ScheduledFuture<?> timer2;
70 private PdpStatus status;
72 private Queue<ScheduledFuture<?>> timers;
74 private XacmlPdpHearbeatPublisher publisher;
78 * Initializes objects, including the publisher.
82 MockitoAnnotations.initMocks(this);
84 when(state.genHeartbeat()).thenReturn(status);
86 timers = new LinkedList<>(Arrays.asList(timer1, timer2));
88 when(executor.scheduleWithFixedDelay(any(), anyLong(), anyLong(), any())).thenAnswer(args -> timers.remove());
90 publisher = new MyPublisher(client, state);
94 public void testRun() {
97 verify(state).genHeartbeat();
98 verify(client).send(status);
102 public void testTerminate() {
104 publisher.terminate();
107 // now start it and then try again
109 publisher.terminate();
111 // timer2 should still be in the queue
112 assertSame(timer2, timers.peek());
115 // repeat - nothing more should happen
116 publisher.terminate();
118 // timer2 should still be in the queue
119 assertSame(timer2, timers.peek());
123 public void testRestart() {
124 // not started yet - should only update the interval
125 publisher.restart(INTERVAL1);
127 assertEquals(INTERVAL1, publisher.getIntervalMs());
128 assertSame(timer1, timers.peek());
132 verify(executor).scheduleWithFixedDelay(publisher, 0, INTERVAL1, TimeUnit.MILLISECONDS);
134 // null interval - no changes
135 publisher.restart(null);
136 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyInt(), anyLong(), any());
137 assertSame(timer2, timers.peek());
139 // same interval - no changes
140 publisher.restart(INTERVAL1);
141 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyInt(), anyLong(), any());
142 assertSame(timer2, timers.peek());
144 // invalid interval - no changes
145 publisher.restart(INTERVAL_INVALID);
146 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyInt(), anyLong(), any());
147 assertSame(timer2, timers.peek());
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);
156 public void testStart() {
159 verify(executor).scheduleWithFixedDelay(publisher, 0, XacmlPdpHearbeatPublisher.DEFAULT_INTERVAL_MS,
160 TimeUnit.MILLISECONDS);
162 // repeat - nothing more should happen
164 verify(executor, times(1)).scheduleWithFixedDelay(any(), anyInt(), anyLong(), any());
165 verify(timer1, never()).cancel(anyBoolean());
169 public void testMakeTimerThread() {
170 // create a plain listener to test the "real" makeTimer() method
171 publisher = new XacmlPdpHearbeatPublisher(client, state);
174 publisher.restart(100L);
175 publisher.terminate();
178 private class MyPublisher extends XacmlPdpHearbeatPublisher {
180 public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
181 super(topicSinkClient, state);
185 protected ScheduledExecutorService makeTimerThread() {