1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
package org.onap.vid.job.impl;
import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.hamcrest.Matcher;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.onap.vid.job.*;
import org.onap.vid.job.command.HttpCallCommand;
import org.onap.vid.job.command.JobCommandFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import java.util.Map;
import java.util.UUID;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class JobWorkerTest {
@InjectMocks
private JobWorker jobWorker = new JobWorker();
@Mock
private JobCommandFactory jobCommandFactory;
private final JobCommand jobCommand = mock(JobCommand.class);
private Job jobUnderTest;
private JobAdapter.AsyncJobRequest originalData;
private JobType originalType;
@BeforeMethod
public void initMocks() {
MockitoAnnotations.initMocks(this);
when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
originalData = new JobAdapter.AsyncJobRequest() {
public final Map datum = ImmutableMap.of("some", "data");
public final String foobar = "aux";
};
originalType = JobType.MacroServiceInstantiation;
jobUnderTest = new JobAdapterImpl().createServiceInstantiationJob(
originalType,
originalData,
UUID.randomUUID(),
"my user id",
"optimisticUniqueServiceInstanceName",
RandomUtils.nextInt()
);
}
@Test
public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
hasProperty("status", is(Job.JobStatus.STOPPED)),
hasProperty("sharedData", hasProperty("request", is(originalData))),
hasProperty("type", is(originalType)))
);
}
@Test
public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
final UUID jobUuid = UUID.randomUUID();
final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
String[] excludedFields = {"status", "data", "type"};
assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
hasProperty("status", is(nextStatus)),
hasProperty("data", is(nextCommand.getCommand().getData())),
hasProperty("type", is(nextCommand.getCommand().getType())))
);
}
private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
when(jobCommand.call()).thenReturn(nextCommand);
String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
////// FUNCTION UNDER TEST /////
Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
////////////////////////////////
String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
assertThat(nextJob, jobMatcher);
assertThat(jobAfter, equalTo(jobBefore));
}
}
|