Aharoni, Pavel (pa0916) | 8c70f07 | 2018-11-18 00:07:12 +0200 | [diff] [blame] | 1 | /*-
|
| 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 | package org.onap.osam.job.impl;
|
| 21 |
|
| 22 | import com.google.common.collect.ImmutableMap;
|
| 23 | import org.apache.commons.lang3.RandomUtils;
|
| 24 | import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
|
| 25 | import org.apache.commons.lang3.builder.ToStringStyle;
|
| 26 | import org.hamcrest.Matcher;
|
| 27 | import org.mockito.InjectMocks;
|
| 28 | import org.mockito.Mock;
|
| 29 | import org.mockito.MockitoAnnotations;
|
| 30 | import org.onap.osam.job.dao.job.JobStatus;
|
| 31 | import org.onap.osam.job.dao.job.OsamJob;
|
| 32 | import org.onap.osam.job.IJobFactory;
|
| 33 | import org.onap.osam.job.IJobCommand;
|
| 34 | import org.onap.osam.job.JobType;
|
| 35 | import org.onap.osam.job.NextCommand;
|
| 36 | import org.onap.osam.job.command.HttpCallCommand;
|
| 37 | import org.onap.osam.job.command.JobCommandFactory;
|
| 38 | import org.onap.osam.job.impl.JobFactory;
|
| 39 | import org.onap.osam.job.impl.JobWorker;
|
| 40 | import org.testng.annotations.BeforeMethod;
|
| 41 | import org.testng.annotations.Test;
|
| 42 |
|
| 43 | import java.util.Map;
|
| 44 | import java.util.UUID;
|
| 45 |
|
| 46 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 47 | import static org.hamcrest.Matchers.*;
|
| 48 | import static org.hamcrest.core.Is.is;
|
| 49 | import static org.mockito.Matchers.any;
|
| 50 | import static org.mockito.Mockito.mock;
|
| 51 | import static org.mockito.Mockito.when;
|
| 52 |
|
| 53 | public 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 | }
|