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.io.IOException;
31 import java.net.MalformedURLException;
32 import java.security.GeneralSecurityException;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.InjectMocks;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.pnfsimulator.simulator.client.utils.ssl.SSLAuthenticationHelper;
43 import org.quartz.JobDataMap;
44 import org.quartz.JobDetail;
45 import org.quartz.JobExecutionContext;
46 import org.quartz.JobKey;
47 import org.quartz.Scheduler;
48 import org.quartz.SchedulerException;
49 import org.quartz.SimpleTrigger;
51 class EventSchedulerTest {
54 EventScheduler eventScheduler;
57 Scheduler quartzScheduler;
60 SSLAuthenticationHelper sslAuthenticationHelper;
64 MockitoAnnotations.initMocks(this);
68 void shouldTriggerEventWithGivenConfiguration() throws SchedulerException, IOException, GeneralSecurityException {
70 ArgumentCaptor<JobDetail> jobDetailCaptor = ArgumentCaptor.forClass(JobDetail.class);
71 ArgumentCaptor<SimpleTrigger> triggerCaptor = ArgumentCaptor.forClass(SimpleTrigger.class);
73 String vesUrl = "http://some:80/";
74 int repeatInterval = 1;
76 String testName = "testName";
78 JsonObject body = new JsonObject();
81 eventScheduler.scheduleEvent(vesUrl, repeatInterval, repeatCount, testName, eventId, body);
84 verify(quartzScheduler).scheduleJob(jobDetailCaptor.capture(), triggerCaptor.capture());
85 JobDataMap actualJobDataMap = jobDetailCaptor.getValue().getJobDataMap();
86 assertThat(actualJobDataMap.get(EventJob.BODY)).isEqualTo(body);
87 assertThat(actualJobDataMap.get(EventJob.TEMPLATE_NAME)).isEqualTo(testName);
88 assertThat(actualJobDataMap.get(EventJob.VES_URL)).isEqualTo(vesUrl);
90 SimpleTrigger actualTrigger = triggerCaptor.getValue();
91 // repeat count adds 1 to given value
92 assertThat(actualTrigger.getRepeatCount()).isEqualTo(repeatCount - 1);
94 //getRepeatInterval returns interval in ms
95 assertThat(actualTrigger.getRepeatInterval()).isEqualTo(repeatInterval * 1000);
99 void shouldCancelAllEvents() throws SchedulerException {
101 List<JobKey> jobsKeys = Arrays.asList(new JobKey("jobName1"), new JobKey("jobName2"),
102 new JobKey("jobName3"), new JobKey("jobName4"));
103 List<JobExecutionContext> jobExecutionContexts = createExecutionContextWithKeys(jobsKeys);
104 when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
105 when(quartzScheduler.deleteJobs(jobsKeys)).thenReturn(true);
108 boolean isCancelled = eventScheduler.cancelAllEvents();
111 assertThat(isCancelled).isTrue();
115 void shouldCancelSingleEvent() throws SchedulerException {
117 JobKey jobToRemove = new JobKey("jobName3");
118 List<JobKey> jobsKeys = Arrays.asList(new JobKey("jobName1"), new JobKey("jobName2"),
119 jobToRemove, new JobKey("jobName4"));
120 List<JobExecutionContext> jobExecutionContexts = createExecutionContextWithKeys(jobsKeys);
122 when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
123 when(quartzScheduler.deleteJob(jobToRemove)).thenReturn(true);
126 boolean isCancelled = eventScheduler.cancelEvent("jobName3");
129 assertThat(isCancelled).isTrue();
132 private List<JobExecutionContext> createExecutionContextWithKeys(List<JobKey> jobsKeys) {
133 List<JobExecutionContext> contexts = new ArrayList<>();
134 for (JobKey key : jobsKeys) {
135 contexts.add(createExecutionContextFromKey(key));
140 private JobExecutionContext createExecutionContextFromKey(JobKey key) {
141 JobExecutionContext context = mock(JobExecutionContext.class);
142 JobDetail jobDetail = mock(JobDetail.class);
143 when(context.getJobDetail()).thenReturn(jobDetail);
144 when(jobDetail.getKey()).thenReturn(key);