Fixed xacml-pdp registration
[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.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;
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.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;
47
48 public class XacmlPdpHearbeatPublisherTest {
49
50     private static final long INTERVAL1 = 1000L;
51     private static final long INTERVAL2 = 2000L;
52     private static final long INTERVAL_INVALID = 0;
53
54     @Mock
55     private TopicSinkClient client;
56
57     @Mock
58     private XacmlState state;
59
60     @Mock
61     private ScheduledExecutorService executor;
62
63     @Mock
64     private ScheduledFuture<?> timer1;
65
66     @Mock
67     private ScheduledFuture<?> timer2;
68
69     @Mock
70     private PdpStatus status;
71
72     private Queue<ScheduledFuture<?>> timers;
73
74     private XacmlPdpHearbeatPublisher publisher;
75
76
77     /**
78      * Initializes objects, including the publisher.
79      */
80     @Before
81     public void setUp() {
82         MockitoAnnotations.initMocks(this);
83
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(), anyInt(), anyLong(), any());
137         assertSame(timer2, timers.peek());
138
139         // same interval - no changes
140         publisher.restart(INTERVAL1);
141         verify(executor, times(1)).scheduleWithFixedDelay(any(), anyInt(), 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(), anyInt(), 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(), anyInt(), 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         publisher.start();
174         publisher.restart(100L);
175         publisher.terminate();
176     }
177
178     private class MyPublisher extends XacmlPdpHearbeatPublisher {
179
180         public MyPublisher(TopicSinkClient topicSinkClient, XacmlState state) {
181             super(topicSinkClient, state);
182         }
183
184         @Override
185         protected ScheduledExecutorService makeTimerThread() {
186             return executor;
187         }
188     }
189 }