84df5e9e066b6f281acbccbac754b561cba96fdf
[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.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;
50
51 class EventSchedulerTest {
52
53     @InjectMocks
54     EventScheduler eventScheduler;
55
56     @Mock
57     Scheduler quartzScheduler;
58     
59     @Mock
60     SSLAuthenticationHelper sslAuthenticationHelper;
61
62     @BeforeEach
63     void setUp() {
64         MockitoAnnotations.initMocks(this);
65     }
66
67     @Test
68     void shouldTriggerEventWithGivenConfiguration() throws SchedulerException, IOException, GeneralSecurityException {
69         //given
70         ArgumentCaptor<JobDetail> jobDetailCaptor = ArgumentCaptor.forClass(JobDetail.class);
71         ArgumentCaptor<SimpleTrigger> triggerCaptor = ArgumentCaptor.forClass(SimpleTrigger.class);
72
73         String vesUrl = "http://some:80/";
74         int repeatInterval = 1;
75         int repeatCount = 4;
76         String testName = "testName";
77         String eventId = "1";
78         JsonObject body = new JsonObject();
79
80         //when
81         eventScheduler.scheduleEvent(vesUrl, repeatInterval, repeatCount, testName, eventId, body);
82
83         //then
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);
89
90         SimpleTrigger actualTrigger = triggerCaptor.getValue();
91         // repeat count adds 1 to given value
92         assertThat(actualTrigger.getRepeatCount()).isEqualTo(repeatCount - 1);
93
94         //getRepeatInterval returns interval in ms
95         assertThat(actualTrigger.getRepeatInterval()).isEqualTo(repeatInterval * 1000);
96     }
97
98     @Test
99     void shouldCancelAllEvents() throws SchedulerException {
100         //given
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);
106
107         //when
108         boolean isCancelled = eventScheduler.cancelAllEvents();
109
110         //then
111         assertThat(isCancelled).isTrue();
112     }
113
114     @Test
115     void shouldCancelSingleEvent() throws SchedulerException {
116         //given
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);
121
122         when(quartzScheduler.getCurrentlyExecutingJobs()).thenReturn(jobExecutionContexts);
123         when(quartzScheduler.deleteJob(jobToRemove)).thenReturn(true);
124
125         //when
126         boolean isCancelled = eventScheduler.cancelEvent("jobName3");
127
128         //then
129         assertThat(isCancelled).isTrue();
130     }
131
132     private List<JobExecutionContext> createExecutionContextWithKeys(List<JobKey> jobsKeys) {
133         List<JobExecutionContext> contexts = new ArrayList<>();
134         for (JobKey key : jobsKeys) {
135             contexts.add(createExecutionContextFromKey(key));
136         }
137         return contexts;
138     }
139
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);
145         return context;
146     }
147
148
149 }