2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2018 Nokia Intellectual Property. 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.vid.job.impl;
24 import org.mockito.ArgumentCaptor;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.onap.vid.job.JobsBrokerService;
28 import org.onap.vid.job.command.JobCommandFactory;
29 import org.onap.vid.properties.Features;
30 import org.quartz.JobDetail;
31 import org.quartz.Scheduler;
32 import org.quartz.SchedulerException;
33 import org.quartz.Trigger;
34 import org.springframework.scheduling.quartz.SchedulerFactoryBean;
35 import org.testng.annotations.BeforeMethod;
36 import org.testng.annotations.Test;
37 import org.togglz.core.manager.FeatureManager;
39 import java.util.List;
40 import java.util.stream.Collectors;
42 import static org.hamcrest.MatcherAssert.assertThat;
43 import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
44 import static org.mockito.Mockito.times;
45 import static org.mockito.Mockito.verify;
46 import static org.mockito.Mockito.verifyZeroInteractions;
47 import static org.mockito.Mockito.when;
48 import static org.mockito.MockitoAnnotations.initMocks;
50 public class JobSchedulerInitializerTest {
53 private JobsBrokerService brokerService;
56 private SchedulerFactoryBean schedulerFactoryBean;
59 private FeatureManager featureManager;
62 private JobCommandFactory commandFactory;
65 private Scheduler scheduler;
68 private JobSchedulerInitializer jobSchedulerInitializer;
77 public void shouldNotInitializeSchedulerWhenAsyncJobsAreDisabled() {
78 when(featureManager.isActive(Features.FLAG_ASYNC_JOBS)).thenReturn(false);
80 jobSchedulerInitializer.init();
82 verifyZeroInteractions(schedulerFactoryBean);
87 public void shouldInitializeSchedulerWhenAsyncJobsAreEnabled() throws SchedulerException {
88 ArgumentCaptor<JobDetail> jobDetailArgumentCaptor = ArgumentCaptor.forClass(JobDetail.class);
89 ArgumentCaptor<Trigger> triggerArgumentCaptor = ArgumentCaptor.forClass(Trigger.class);
90 when(featureManager.isActive(Features.FLAG_ASYNC_JOBS)).thenReturn(true);
91 when(schedulerFactoryBean.getScheduler()).thenReturn(scheduler);
93 jobSchedulerInitializer.init();
95 verify(scheduler, times(2)).scheduleJob(jobDetailArgumentCaptor.capture(), triggerArgumentCaptor.capture());
97 List<Object> topics = extractTopics(jobDetailArgumentCaptor);
99 List<String> descriptions = extractDescription(triggerArgumentCaptor);
101 assertThat(topics, containsInAnyOrder(org.onap.vid.job.Job.JobStatus.IN_PROGRESS, org.onap.vid.job.Job.JobStatus.PENDING));
102 assertThat(descriptions, containsInAnyOrder("Trigger to run async worker for PENDING", "Trigger to run async worker for IN_PROGRESS"));
105 private List<Object> extractTopics(ArgumentCaptor<JobDetail> jobDetailArgumentCaptor) {
106 return jobDetailArgumentCaptor
109 .map(JobDetail::getJobDataMap)
110 .map(x -> x.get("topic"))
111 .collect(Collectors.toList());
114 private List<String> extractDescription(ArgumentCaptor<Trigger> triggerArgumentCaptor) {
115 return triggerArgumentCaptor
118 .map(Trigger::getDescription)
119 .collect(Collectors.toList());