9d0f7d84f0c8525e87468323f1e591c521e96021
[integration.git] /
1 /*
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.pnfsimulator.simulator.scheduler;
22
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;
27
28 import com.google.gson.JsonObject;
29
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;
47
48 class EventSchedulerTest {
49
50     @InjectMocks
51     EventScheduler eventScheduler;
52
53     @Mock
54     Scheduler quartzScheduler;
55
56     @BeforeEach
57     void setUp() {
58         MockitoAnnotations.initMocks(this);
59     }
60
61     @Test
62     void shouldTriggerEventWithGivenConfiguration() throws SchedulerException, MalformedURLException {
63         //given
64         ArgumentCaptor<JobDetail> jobDetailCaptor = ArgumentCaptor.forClass(JobDetail.class);
65         ArgumentCaptor<SimpleTrigger> triggerCaptor = ArgumentCaptor.forClass(SimpleTrigger.class);
66
67         String vesUrl = "http://some:80/";
68         int repeatInterval = 1;
69         int repeatCount = 4;
70         String testName = "testName";
71         String eventId = "1";
72         JsonObject body = new JsonObject();
73
74         //when
75         eventScheduler.scheduleEvent(vesUrl, repeatInterval, repeatCount, testName, eventId, body);
76
77         //then
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);
83
84         SimpleTrigger actualTrigger = triggerCaptor.getValue();
85         // repeat count adds 1 to given value
86         assertThat(actualTrigger.getRepeatCount()).isEqualTo(repeatCount - 1);
87
88         //getRepeatInterval returns interval in ms
89         assertThat(actualTrigger.getRepeatInterval()).isEqualTo(repeatInterval * 1000);
90     }
91
92     @Test
93     void shouldCancelAllEvents() throws SchedulerException {
94         //given
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);
100
101         //when
102         boolean isCancelled = eventScheduler.cancelAllEvents();
103
104         //then
105         assertThat(isCancelled).isTrue();
106     }
107
108     @Test
109     void shouldCancelSingleEvent() throws SchedulerException {
110         //given
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);
115
116         when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
117         when(quartzScheduler.deleteJob(jobToRemove)).thenReturn(true);
118
119         //when
120         boolean isCancelled = eventScheduler.cancelEvent("jobName3");
121
122         //then
123         assertThat(isCancelled).isTrue();
124     }
125
126     private List<JobExecutionContext> createExecutionContextWithKeys(List<JobKey> jobsKeys) {
127         List<JobExecutionContext> contexts = new ArrayList<>();
128         for (JobKey key : jobsKeys) {
129             contexts.add(createExecutionContextFromKey(key));
130         }
131         return contexts;
132     }
133
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);
139         return context;
140     }
141
142
143 }