2 * ============LICENSE_START=======================================================
3 * PNF-REGISTRATION-HANDLER
4 * ================================================================================
5 * Copyright (C) 2018 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.pnfsimulator.simulator.scheduler;
23 import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
28 import com.google.gson.JsonObject;
30 import java.net.MalformedURLException;
31 import java.util.ArrayList;
32 import java.util.Arrays;
33 import java.util.List;
34 import org.junit.jupiter.api.BeforeEach;
35 import org.junit.jupiter.api.Test;
36 import org.mockito.ArgumentCaptor;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.quartz.JobDataMap;
41 import org.quartz.JobDetail;
42 import org.quartz.JobExecutionContext;
43 import org.quartz.JobKey;
44 import org.quartz.Scheduler;
45 import org.quartz.SchedulerException;
46 import org.quartz.SimpleTrigger;
48 class EventSchedulerTest {
51 EventScheduler eventScheduler;
54 Scheduler quartzScheduler;
58 MockitoAnnotations.initMocks(this);
62 void shouldTriggerEventWithGivenConfiguration() throws SchedulerException, MalformedURLException {
64 ArgumentCaptor<JobDetail> jobDetailCaptor = ArgumentCaptor.forClass(JobDetail.class);
65 ArgumentCaptor<SimpleTrigger> triggerCaptor = ArgumentCaptor.forClass(SimpleTrigger.class);
67 String vesUrl = "http://some:80/";
68 int repeatInterval = 1;
70 String testName = "testName";
72 JsonObject body = new JsonObject();
75 eventScheduler.scheduleEvent(vesUrl, repeatInterval, repeatCount, testName, eventId, body);
78 verify(quartzScheduler).scheduleJob(jobDetailCaptor.capture(), triggerCaptor.capture());
79 JobDataMap actualJobDataMap = jobDetailCaptor.getValue().getJobDataMap();
80 assertThat(actualJobDataMap.get(EventJob.BODY)).isEqualTo(body);
81 assertThat(actualJobDataMap.get(EventJob.TEMPLATE_NAME)).isEqualTo(testName);
82 assertThat(actualJobDataMap.get(EventJob.VES_URL)).isEqualTo(vesUrl);
84 SimpleTrigger actualTrigger = triggerCaptor.getValue();
85 // repeat count adds 1 to given value
86 assertThat(actualTrigger.getRepeatCount()).isEqualTo(repeatCount - 1);
88 //getRepeatInterval returns interval in ms
89 assertThat(actualTrigger.getRepeatInterval()).isEqualTo(repeatInterval * 1000);
93 void shouldCancelAllEvents() throws SchedulerException {
95 List<JobKey> jobsKeys = Arrays.asList(new JobKey("jobName1"), new JobKey("jobName2"),
96 new JobKey("jobName3"), new JobKey("jobName4"));
97 List<JobExecutionContext> jobExecutionContexts = createExecutionContextWithKeys(jobsKeys);
98 when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
99 when(quartzScheduler.deleteJobs(jobsKeys)).thenReturn(true);
102 boolean isCancelled = eventScheduler.cancelAllEvents();
105 assertThat(isCancelled).isTrue();
109 void shouldCancelSingleEvent() throws SchedulerException {
111 JobKey jobToRemove = new JobKey("jobName3");
112 List<JobKey> jobsKeys = Arrays.asList(new JobKey("jobName1"), new JobKey("jobName2"),
113 jobToRemove, new JobKey("jobName4"));
114 List<JobExecutionContext> jobExecutionContexts = createExecutionContextWithKeys(jobsKeys);
116 when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
117 when(quartzScheduler.deleteJob(jobToRemove)).thenReturn(true);
120 boolean isCancelled = eventScheduler.cancelEvent("jobName3");
123 assertThat(isCancelled).isTrue();
126 private List<JobExecutionContext> createExecutionContextWithKeys(List<JobKey> jobsKeys) {
127 List<JobExecutionContext> contexts = new ArrayList<>();
128 for (JobKey key : jobsKeys) {
129 contexts.add(createExecutionContextFromKey(key));
134 private JobExecutionContext createExecutionContextFromKey(JobKey key) {
135 JobExecutionContext context = mock(JobExecutionContext.class);
136 JobDetail jobDetail = mock(JobDetail.class);
137 when(context.getJobDetail()).thenReturn(jobDetail);
138 when(jobDetail.getKey()).thenReturn(key);