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;
36 import org.togglz.core.manager.FeatureManager;
39 import java.util.UUID;
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;
47 public class JobWorkerTest {
50 private JobWorker jobWorker = new JobWorker();
53 private JobCommandFactory jobCommandFactory;
56 private FeatureManager featureManager;
58 private final JobCommand jobCommand = mock(JobCommand.class);
59 private Job jobUnderTest;
60 private JobAdapter.AsyncJobRequest originalData;
61 private JobType originalType;
64 public void initMocks() {
65 MockitoAnnotations.initMocks(this);
67 when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
69 originalData = new JobAdapter.AsyncJobRequest() {
70 public final Map datum = ImmutableMap.of("some", "data");
71 public final String foobar = "aux";
74 originalType = JobType.MacroServiceInstantiation;
75 jobUnderTest = new JobAdapterImpl(featureManager).createServiceInstantiationJob(
81 "optimisticUniqueServiceInstanceName",
87 public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
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)))
97 public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
99 final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
101 final UUID jobUuid = UUID.randomUUID();
102 final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
104 String[] excludedFields = {"status", "data", "type"};
106 assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
107 hasProperty("status", is(nextStatus)),
108 hasProperty("data", is(nextCommand.getCommand().getData())),
109 hasProperty("type", is(nextCommand.getCommand().getType())))
113 private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
114 when(jobCommand.call()).thenReturn(nextCommand);
116 String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
118 ////// FUNCTION UNDER TEST /////
119 Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
120 ////////////////////////////////
122 String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
124 assertThat(nextJob, jobMatcher);
125 assertThat(jobAfter, equalTo(jobBefore));