blob: 0c4dc2dbb82ab79f48f293c7aff19a492e725e78 [file] [log] [blame]
Aharoni, Pavel (pa0916)ca3cb012018-10-22 15:29:57 +03001/*-
2 * ============LICENSE_START=======================================================
3 * OSAM
4 * ================================================================================
5 * Copyright (C) 2018 AT&T
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
22
23package org.onap.osam.job.impl;
24
25import com.google.common.collect.ImmutableMap;
26import org.apache.commons.lang3.RandomUtils;
27import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
28import org.apache.commons.lang3.builder.ToStringStyle;
29import org.hamcrest.Matcher;
30import org.mockito.InjectMocks;
31import org.mockito.Mock;
32import org.mockito.MockitoAnnotations;
33import org.onap.osam.job.*;
34import org.onap.osam.job.command.HttpCallCommand;
35import org.onap.osam.job.command.JobCommandFactory;
36import org.testng.annotations.BeforeMethod;
37import org.testng.annotations.Test;
38
39import java.util.Map;
40import java.util.UUID;
41
42import static org.hamcrest.MatcherAssert.assertThat;
43import static org.hamcrest.Matchers.*;
44import static org.mockito.Matchers.any;
45import static org.mockito.Mockito.mock;
46import static org.mockito.Mockito.when;
47
48public class JobWorkerTest {
49
50 @InjectMocks
51 private JobWorker jobWorker = new JobWorker();
52
53 @Mock
54 private JobCommandFactory jobCommandFactory;
55
56 private final JobCommand jobCommand = mock(JobCommand.class);
57 private Job jobUnderTest;
58 private JobAdapter.AsyncJobRequest originalData;
59 private JobType originalType;
60
61 @BeforeMethod
62 public void initMocks() {
63 MockitoAnnotations.initMocks(this);
64
65 when(jobCommandFactory.toCommand(any())).thenReturn(jobCommand);
66
67 originalData = new JobAdapter.AsyncJobRequest() {
68 public final Map datum = ImmutableMap.of("some", "data");
69 public final String foobar = "aux";
70 };
71
72 originalType = JobType.ServiceInstantiation;
73 jobUnderTest = new JobAdapterImpl().createJob(
74 originalType,
75 originalData,
76 UUID.randomUUID(),
77 "my user id",
78 RandomUtils.nextInt()
79 );
80 }
81
82 @Test
83 public void executeJobAndStepToNext_givenNull_onlyStatusModified() {
84
85 assertNextJobAfterExecuteJob(null, new String[]{"status"}, allOf(
86 hasProperty("status", is(Job.JobStatus.STOPPED)),
87 hasProperty("data", hasEntry("request", originalData)),
88 hasProperty("type", is(originalType)))
89 );
90 }
91
92 @Test
93 public void executeJobAndStepToNext_givenNextJob_jobDataIsModified() {
94
95 final Job.JobStatus nextStatus = Job.JobStatus.IN_PROGRESS;
96
97 final UUID jobUuid = UUID.randomUUID();
98 final NextCommand nextCommand = new NextCommand(nextStatus, new HttpCallCommand("my strange url", jobUuid));
99
100 String[] excludedFields = {"status", "data", "type"};
101
102 assertNextJobAfterExecuteJob(nextCommand, excludedFields, allOf(
103 hasProperty("status", is(nextStatus)),
104 hasProperty("data", is(nextCommand.getCommand().getData())),
105 hasProperty("type", is(nextCommand.getCommand().getType())))
106 );
107 }
108
109 private void assertNextJobAfterExecuteJob(NextCommand nextCommand, String[] excludedFields, Matcher<Job> jobMatcher) {
110 when(jobCommand.call()).thenReturn(nextCommand);
111
112 String jobBefore = new ReflectionToStringBuilder(jobUnderTest, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
113
114 ////// FUNCTION UNDER TEST /////
115 Job nextJob = jobWorker.executeJobAndGetNext(jobUnderTest);
116 ////////////////////////////////
117
118 String jobAfter = new ReflectionToStringBuilder(nextJob, ToStringStyle.SHORT_PREFIX_STYLE).setExcludeFieldNames(excludedFields).toString();
119
120 assertThat(nextJob, jobMatcher);
121 assertThat(jobAfter, equalTo(jobBefore));
122 }
123}