e58b6ba030d9612704746fe5477706ff10470d9e
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / impl / JobWorkerTest.java
1 package org.onap.vid.job.impl;
2
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;
16
17 import java.util.Map;
18 import java.util.UUID;
19
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;
25
26 public class JobWorkerTest {
27
28     @InjectMocks
29     private JobWorker jobWorker = new JobWorker();
30
31     @Mock
32     private JobCommandFactory jobCommandFactory;
33
34     private final JobCommand jobCommand = mock(JobCommand.class);
35     private Job jobUnderTest;
36     private JobAdapter.AsyncJobRequest originalData;
37     private JobType originalType;
38
39     @BeforeMethod
40     public void initMocks() {
41         MockitoAnnotations.initMocks(this);
42
43         when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
44
45         originalData = new JobAdapter.AsyncJobRequest() {
46             public final Map datum = ImmutableMap.of("some", "data");
47             public final String foobar = "aux";
48         };
49
50         originalType = JobType.ServiceInstantiation;
51         jobUnderTest = new JobAdapterImpl().createJob(
52                 originalType,
53                 originalData,
54                 UUID.randomUUID(),
55                 "my user id",
56                 RandomUtils.nextInt()
57         );
58     }
59
60     @Test
61     public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
62
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)))
67         );
68     }
69
70     @Test
71     public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
72
73         final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
74
75         final UUID jobUuid = UUID.randomUUID();
76         final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
77
78         String[] excludedFields = {"status", "data", "type"};
79
80         assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
81                 hasProperty("status", is(nextStatus)),
82                 hasProperty("data", is(nextCommand.getCommand().getData())),
83                 hasProperty("type", is(nextCommand.getCommand().getType())))
84         );
85     }
86
87     private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
88         when(jobCommand.call()).thenReturn(nextCommand);
89
90         String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
91
92         ////// FUNCTION UNDER TEST /////
93         Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
94         ////////////////////////////////
95
96         String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
97
98         assertThat(nextJob, jobMatcher);
99         assertThat(jobAfter, equalTo(jobBefore));
100     }
101 }