Implant vid-app-common org.onap.vid.job (main and test)
[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 import org.togglz.core.manager.FeatureManager;
37
38 import java.util.Map;
39 import java.util.UUID;
40
41 import static org.hamcrest.MatcherAssert.assertThat;
42 import static org.hamcrest.Matchers.*;
43 import static org.mockito.Matchers.any;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.when;
46
47 public class JobWorkerTest {
48
49     @InjectMocks
50     private JobWorker jobWorker = new JobWorker();
51
52     @Mock
53     private JobCommandFactory jobCommandFactory;
54
55     @Mock
56     private FeatureManager featureManager;
57
58     private final JobCommand jobCommand = mock(JobCommand.class);
59     private Job jobUnderTest;
60     private JobAdapter.AsyncJobRequest originalData;
61     private JobType originalType;
62
63     @BeforeMethod
64     public void initMocks() {
65         MockitoAnnotations.initMocks(this);
66
67         when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
68
69         originalData = new JobAdapter.AsyncJobRequest() {
70             public final Map datum = ImmutableMap.of("some", "data");
71             public final String foobar = "aux";
72         };
73
74         originalType = JobType.MacroServiceInstantiation;
75         jobUnderTest = new JobAdapterImpl(featureManager).createServiceInstantiationJob(
76                 originalType,
77                 originalData,
78                 UUID.randomUUID(),
79                 "my user id",
80                 "VNF_API",
81                 "optimisticUniqueServiceInstanceName",
82                 RandomUtils.nextInt()
83         );
84     }
85
86     @Test
87     public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
88
89         assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
90                 hasProperty("status", is(Job.JobStatus.STOPPED)),
91                 hasProperty("sharedData", hasProperty("request", is(originalData))),
92                 hasProperty("type", is(originalType)))
93         );
94     }
95
96     @Test
97     public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
98
99         final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
100
101         final UUID jobUuid = UUID.randomUUID();
102         final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
103
104         String[] excludedFields = {"status", "data", "type"};
105
106         assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
107                 hasProperty("status", is(nextStatus)),
108                 hasProperty("data", is(nextCommand.getCommand().getData())),
109                 hasProperty("type", is(nextCommand.getCommand().getType())))
110         );
111     }
112
113     private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
114         when(jobCommand.call()).thenReturn(nextCommand);
115
116         String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
117
118         ////// FUNCTION UNDER TEST /////
119         Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
120         ////////////////////////////////
121
122         String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
123
124         assertThat(nextJob, jobMatcher);
125         assertThat(jobAfter, equalTo(jobBefore));
126     }
127 }