2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.impl;
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;
38 import java.util.UUID;
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;
46 public class JobWorkerTest {
49 private JobWorker jobWorker = new JobWorker();
52 private JobCommandFactory jobCommandFactory;
54 private final JobCommand jobCommand = mock(JobCommand.class);
55 private Job jobUnderTest;
56 private JobAdapter.AsyncJobRequest originalData;
57 private JobType originalType;
60 public void initMocks() {
61 MockitoAnnotations.initMocks(this);
63 when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
65 originalData = new JobAdapter.AsyncJobRequest() {
66 public final Map datum = ImmutableMap.of("some", "data");
67 public final String foobar = "aux";
70 originalType = JobType.MacroServiceInstantiation;
71 jobUnderTest = new JobAdapterImpl().createServiceInstantiationJob(
76 "optimisticUniqueServiceInstanceName",
82 public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
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)))
92 public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
94 final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
96 final UUID jobUuid = UUID.randomUUID();
97 final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
99 String[] excludedFields = {"status", "data", "type"};
101 assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
102 hasProperty("status", is(nextStatus)),
103 hasProperty("data", is(nextCommand.getCommand().getData())),
104 hasProperty("type", is(nextCommand.getCommand().getType())))
108 private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
109 when(jobCommand.call()).thenReturn(nextCommand);
111 String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
113 ////// FUNCTION UNDER TEST /////
114 Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
115 ////////////////////////////////
117 String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
119 assertThat(nextJob, jobMatcher);
120 assertThat(jobAfter, equalTo(jobBefore));