dc44d1520dc8836679b03b11ae03f4f984c8e474
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / impl / JobWorkerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.vid.job.impl;
22
23 import com.google.common.collect.ImmutableMap;
24 import org.apache.commons.lang3.RandomUtils;
25 import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
26 import org.apache.commons.lang3.builder.ToStringStyle;
27 import org.hamcrest.Matcher;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.MockitoAnnotations;
31 import org.onap.vid.job.*;
32 import org.onap.vid.job.command.HttpCallCommand;
33 import org.onap.vid.job.command.JobCommandFactory;
34 import org.testng.annotations.BeforeMethod;
35 import org.testng.annotations.Test;
36
37 import java.util.Map;
38 import java.util.UUID;
39
40 import static org.hamcrest.MatcherAssert.assertThat;
41 import static org.hamcrest.Matchers.*;
42 import static org.mockito.Matchers.any;
43 import static org.mockito.Mockito.mock;
44 import static org.mockito.Mockito.when;
45
46 public class JobWorkerTest {
47
48     @InjectMocks
49     private JobWorker jobWorker = new JobWorker();
50
51     @Mock
52     private JobCommandFactory jobCommandFactory;
53
54     private final JobCommand jobCommand = mock(JobCommand.class);
55     private Job jobUnderTest;
56     private JobAdapter.AsyncJobRequest originalData;
57     private JobType originalType;
58
59     @BeforeMethod
60     public void initMocks() {
61         MockitoAnnotations.initMocks(this);
62
63         when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
64
65         originalData = new JobAdapter.AsyncJobRequest() {
66             public final Map datum = ImmutableMap.of("some", "data");
67             public final String foobar = "aux";
68         };
69
70         originalType = JobType.MacroServiceInstantiation;
71         jobUnderTest = new JobAdapterImpl().createServiceInstantiationJob(
72                 originalType,
73                 originalData,
74                 UUID.randomUUID(),
75                 "my user id",
76                 "optimisticUniqueServiceInstanceName",
77                 RandomUtils.nextInt()
78         );
79     }
80
81     @Test
82     public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
83
84         assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
85                 hasProperty("status", is(Job.JobStatus.STOPPED)),
86                 hasProperty("sharedData", hasProperty("request", is(originalData))),
87                 hasProperty("type", is(originalType)))
88         );
89     }
90
91     @Test
92     public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
93
94         final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
95
96         final UUID jobUuid = UUID.randomUUID();
97         final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
98
99         String[] excludedFields = {"status", "data", "type"};
100
101         assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
102                 hasProperty("status", is(nextStatus)),
103                 hasProperty("data", is(nextCommand.getCommand().getData())),
104                 hasProperty("type", is(nextCommand.getCommand().getType())))
105         );
106     }
107
108     private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
109         when(jobCommand.call()).thenReturn(nextCommand);
110
111         String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
112
113         ////// FUNCTION UNDER TEST /////
114         Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
115         ////////////////////////////////
116
117         String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
118
119         assertThat(nextJob, jobMatcher);
120         assertThat(jobAfter, equalTo(jobBefore));
121     }
122 }