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.anyLong;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
32 import java.util.Arrays;
33 import java.util.LinkedList;
34 import java.util.Queue;
35 import java.util.Timer;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.pdpx.main.XacmlState;
44 public class XacmlPdpHearbeatPublisherTest {
46 private static final long INTERVAL1 = 1000L;
47 private static final long INTERVAL2 = 2000L;
48 private static final long INTERVAL_INVALID = 0;
51 private TopicSinkClient client;
54 private XacmlState state;
63 private PdpStatus status;
65 private Queue<Timer> timers;
67 private XacmlPdpHearbeatPublisher publisher;
71 * Initializes objects, including the publisher.
75 MockitoAnnotations.initMocks(this);
77 when(state.genHeartbeat()).thenReturn(status);
79 timers = new LinkedList<>(Arrays.asList(timer1, timer2));
81 publisher = new MyPublisher(client, state);
85 public void testRun() {
88 verify(state).genHeartbeat();
89 verify(client).send(status);
93 public void testTerminate() {
95 publisher.terminate();
97 verify(timer1, never()).cancel();
98 verify(timer2, never()).cancel();
101 // now start it and then try again
103 publisher.terminate();
105 verify(timer1).cancel();
107 // timer2 should still be in the queue
108 assertSame(timer2, timers.peek());
111 // repeat - nothing more should happen
112 publisher.terminate();
114 verify(timer1, times(1)).cancel();
116 // timer2 should still be in the queue
117 assertSame(timer2, timers.peek());
121 public void testRestart() {
122 // not started yet - should only update the interval
123 publisher.restart(INTERVAL1);
125 assertEquals(INTERVAL1, publisher.getIntervalMs());
126 assertSame(timer1, timers.peek());
130 verify(timer1).scheduleAtFixedRate(publisher, 0, INTERVAL1);
132 // null interval - no changes
133 publisher.restart(null);
134 verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
135 assertSame(timer2, timers.peek());
137 // same interval - no changes
138 publisher.restart(INTERVAL1);
139 verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
140 assertSame(timer2, timers.peek());
142 // invalid interval - no changes
143 publisher.restart(INTERVAL_INVALID);
144 verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
145 assertSame(timer2, timers.peek());
147 // new interval - old timer should be cancelled and new started
148 publisher.restart(INTERVAL2);
149 verify(timer1).cancel();
150 verify(timer2).scheduleAtFixedRate(publisher, 0, INTERVAL2);
154 public void testStart() {
157 verify(timer1).scheduleAtFixedRate(publisher, 0, XacmlPdpHearbeatPublisher.DEFAULT_INTERVAL_MS);
159 // repeat - nothing more should happen
161 verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
162 verify(timer1, never()).cancel();
166 public void testMakeTimer() {
167 // create a plain listener to test the "real" makeTimer() method
168 publisher = new XacmlPdpHearbeatPublisher(client, state);
171 publisher.terminate();
174 private class MyPublisher extends XacmlPdpHearbeatPublisher {
176 public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
177 super(topicSinkClient, state);
181 protected Timer makeTimer() {
182 return timers.remove();