1 package org.onap.vid.job.impl;
3 import com.google.common.collect.ImmutableMap;
4 import org.apache.commons.lang3.RandomUtils;
5 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
6 import org.apache.commons.lang3.builder.ToStringStyle;
7 import org.hamcrest.Matcher;
8 import org.mockito.InjectMocks;
9 import org.mockito.Mock;
10 import org.mockito.MockitoAnnotations;
11 import org.onap.vid.job.*;
12 import org.onap.vid.job.command.HttpCallCommand;
13 import org.onap.vid.job.command.JobCommandFactory;
14 import org.testng.annotations.BeforeMethod;
15 import org.testng.annotations.Test;
18 import java.util.UUID;
20 import static org.hamcrest.MatcherAssert.assertThat;
21 import static org.hamcrest.Matchers.*;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.when;
26 public class JobWorkerTest {
29 private JobWorker jobWorker = new JobWorker();
32 private JobCommandFactory jobCommandFactory;
34 private final JobCommand jobCommand = mock(JobCommand.class);
35 private Job jobUnderTest;
36 private JobAdapter.AsyncJobRequest originalData;
37 private JobType originalType;
40 public void initMocks() {
41 MockitoAnnotations.initMocks(this);
43 when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
45 originalData = new JobAdapter.AsyncJobRequest() {
46 public final Map datum = ImmutableMap.of("some", "data");
47 public final String foobar = "aux";
50 originalType = JobType.ServiceInstantiation;
51 jobUnderTest = new JobAdapterImpl().createJob(
61 public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
63 assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
64 hasProperty("status", is(Job.JobStatus.STOPPED)),
65 hasProperty("data", hasEntry("request", originalData)),
66 hasProperty("type", is(originalType)))
71 public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
73 final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
75 final UUID jobUuid = UUID.randomUUID();
76 final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
78 String[] excludedFields = {"status", "data", "type"};
80 assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
81 hasProperty("status", is(nextStatus)),
82 hasProperty("data", is(nextCommand.getCommand().getData())),
83 hasProperty("type", is(nextCommand.getCommand().getType())))
87 private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
88 when(jobCommand.call()).thenReturn(nextCommand);
90 String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
92 ////// FUNCTION UNDER TEST /////
93 Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
94 ////////////////////////////////
96 String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
98 assertThat(nextJob, jobMatcher);
99 assertThat(jobAfter, equalTo(jobBefore));