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.Matchers.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.MacroServiceInstantiation;
51 jobUnderTest = new JobAdapterImpl().createServiceInstantiationJob(
56 "optimisticUniqueServiceInstanceName",
62 public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
64 assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
65 hasProperty("status", is(Job.JobStatus.STOPPED)),
66 hasProperty("sharedData", hasProperty("request", is(originalData))),
67 hasProperty("type", is(originalType)))
72 public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
74 final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
76 final UUID jobUuid = UUID.randomUUID();
77 final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
79 String[] excludedFields = {"status", "data", "type"};
81 assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
82 hasProperty("status", is(nextStatus)),
83 hasProperty("data", is(nextCommand.getCommand().getData())),
84 hasProperty("type", is(nextCommand.getCommand().getType())))
88 private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
89 when(jobCommand.call()).thenReturn(nextCommand);
91 String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
93 ////// FUNCTION UNDER TEST /////
94 Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
95 ////////////////////////////////
97 String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
99 assertThat(nextJob, jobMatcher);
100 assertThat(jobAfter, equalTo(jobBefore));