34bb0fa32f38a95f836215fdde7fd0d9b1ed775e
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / comm / XacmlPdpHearbeatPublisherTest.java
1 /*-
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
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.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;
31
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;
43
44 public class XacmlPdpHearbeatPublisherTest {
45
46     private static final long INTERVAL1 = 1000L;
47     private static final long INTERVAL2 = 2000L;
48     private static final long INTERVAL_INVALID = 0;
49
50     @Mock
51     private TopicSinkClient client;
52
53     @Mock
54     private XacmlState state;
55
56     @Mock
57     private Timer timer1;
58
59     @Mock
60     private Timer timer2;
61
62     @Mock
63     private PdpStatus status;
64
65     private Queue<Timer> timers;
66
67     private XacmlPdpHearbeatPublisher publisher;
68
69
70     /**
71      * Initializes objects, including the publisher.
72      */
73     @Before
74     public void setUp() {
75         MockitoAnnotations.initMocks(this);
76
77         when(state.genHeartbeat()).thenReturn(status);
78
79         timers = new LinkedList<>(Arrays.asList(timer1, timer2));
80
81         publisher = new MyPublisher(client, state);
82     }
83
84     @Test
85     public void testRun() {
86         publisher.run();
87
88         verify(state).genHeartbeat();
89         verify(client).send(status);
90     }
91
92     @Test
93     public void testTerminate() {
94         // not yet started
95         publisher.terminate();
96
97         verify(timer1, never()).cancel();
98         verify(timer2, never()).cancel();
99
100
101         // now start it and then try again
102         publisher.start();
103         publisher.terminate();
104
105         verify(timer1).cancel();
106
107         // timer2 should still be in the queue
108         assertSame(timer2, timers.peek());
109
110
111         // repeat - nothing more should happen
112         publisher.terminate();
113
114         verify(timer1, times(1)).cancel();
115
116         // timer2 should still be in the queue
117         assertSame(timer2, timers.peek());
118     }
119
120     @Test
121     public void testRestart() {
122         // not started yet - should only update the interval
123         publisher.restart(INTERVAL1);
124
125         assertEquals(INTERVAL1, publisher.getIntervalMs());
126         assertSame(timer1, timers.peek());
127
128         // now start it
129         publisher.start();
130         verify(timer1).scheduleAtFixedRate(publisher, 0, INTERVAL1);
131
132         // null interval - no changes
133         publisher.restart(null);
134         verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
135         assertSame(timer2, timers.peek());
136
137         // same interval - no changes
138         publisher.restart(INTERVAL1);
139         verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
140         assertSame(timer2, timers.peek());
141
142         // invalid interval - no changes
143         publisher.restart(INTERVAL_INVALID);
144         verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
145         assertSame(timer2, timers.peek());
146
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);
151     }
152
153     @Test
154     public void testStart() {
155         publisher.start();
156
157         verify(timer1).scheduleAtFixedRate(publisher, 0, XacmlPdpHearbeatPublisher.DEFAULT_INTERVAL_MS);
158
159         // repeat - nothing more should happen
160         publisher.start();
161         verify(timer1, times(1)).scheduleAtFixedRate(any(), anyLong(), anyLong());
162         verify(timer1, never()).cancel();
163     }
164
165     @Test
166     public void testMakeTimer() {
167         // create a plain listener to test the "real" makeTimer() method
168         publisher = new XacmlPdpHearbeatPublisher(client, state);
169
170         publisher.start();
171         publisher.terminate();
172     }
173
174     private class MyPublisher extends XacmlPdpHearbeatPublisher {
175
176         public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
177             super(topicSinkClient, state);
178         }
179
180         @Override
181         protected Timer makeTimer() {
182             return timers.remove();
183         }
184     }
185 }