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