Merge from ECOMP's repository
[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.Matchers.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.MacroServiceInstantiation;
51         jobUnderTest = new JobAdapterImpl().createServiceInstantiationJob(
52                 originalType,
53                 originalData,
54                 UUID.randomUUID(),
55                 "my user id",
56                 "optimisticUniqueServiceInstanceName",
57                 RandomUtils.nextInt()
58         );
59     }
60
61     @Test
62     public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
63
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)))
68         );
69     }
70
71     @Test
72     public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
73
74         final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
75
76         final UUID jobUuid = UUID.randomUUID();
77         final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
78
79         String[] excludedFields = {"status", "data", "type"};
80
81         assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
82                 hasProperty("status", is(nextStatus)),
83                 hasProperty("data", is(nextCommand.getCommand().getData())),
84                 hasProperty("type", is(nextCommand.getCommand().getType())))
85         );
86     }
87
88     private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
89         when(jobCommand.call()).thenReturn(nextCommand);
90
91         String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
92
93         ////// FUNCTION UNDER TEST /////
94         Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
95         ////////////////////////////////
96
97         String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
98
99         assertThat(nextJob, jobMatcher);
100         assertThat(jobAfter, equalTo(jobBefore));
101     }
102 }