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.command;
|
| 21 |
|
| 22 | import org.mockito.InjectMocks;
|
| 23 | import org.mockito.Mock;
|
| 24 | import org.mockito.MockitoAnnotations;
|
| 25 | import org.onap.osam.job.dao.job.JobStatus;
|
| 26 | import org.onap.osam.job.dao.job.OsamJob;
|
| 27 | import org.onap.osam.job.IJobFactory;
|
| 28 | import org.onap.osam.job.NextCommand;
|
| 29 | import org.onap.osam.job.AsyncJobService;
|
| 30 | import org.onap.osam.job.command.WatchingCommand;
|
| 31 | import org.onap.osam.job.impl.JobSharedData;
|
| 32 | import org.onap.osam.job.repository.job.OsamJobRepository;
|
| 33 | import org.testng.annotations.BeforeMethod;
|
| 34 | import org.testng.annotations.DataProvider;
|
| 35 | import org.testng.annotations.Test;
|
| 36 |
|
| 37 | import java.util.ArrayList;
|
| 38 | import java.util.Arrays;
|
| 39 | import java.util.List;
|
| 40 | import java.util.UUID;
|
| 41 | import java.util.stream.Collectors;
|
| 42 |
|
| 43 | import static org.hamcrest.MatcherAssert.assertThat;
|
| 44 | import static org.hamcrest.core.Is.is;
|
| 45 | import static org.mockito.Matchers.any;
|
| 46 | import static org.mockito.Matchers.eq;
|
| 47 | import static org.mockito.Mockito.*;
|
| 48 |
|
| 49 | public class WatchingCommandTest {
|
| 50 |
|
| 51 | @Mock
|
| 52 | private AsyncJobService asyncInstantiationBL;
|
| 53 |
|
| 54 | @Mock
|
| 55 | private OsamJobRepository osamJobRepository;
|
| 56 |
|
| 57 | @InjectMocks
|
| 58 | private WatchingCommand watchingCommand = new WatchingCommand();
|
| 59 |
|
| 60 | @DataProvider
|
| 61 | public static Object[][] expectedJobStatusDataProvider() {
|
| 62 | return new Object[][]{
|
| 63 | {Arrays.asList(JobStatus.COMPLETED, JobStatus.COMPLETED), JobStatus.COMPLETED, true},
|
| 64 | {Arrays.asList(JobStatus.FAILED, JobStatus.COMPLETED), JobStatus.COMPLETED_WITH_ERRORS, true},
|
| 65 | {Arrays.asList(JobStatus.STOPPED, JobStatus.COMPLETED), JobStatus.COMPLETED_WITH_ERRORS, false},
|
| 66 | {Arrays.asList(JobStatus.IN_PROGRESS, JobStatus.FAILED), JobStatus.IN_PROGRESS, true},
|
| 67 | {Arrays.asList(JobStatus.IN_PROGRESS, JobStatus.COMPLETED), JobStatus.IN_PROGRESS, true},
|
| 68 | {Arrays.asList(JobStatus.IN_PROGRESS, JobStatus.IN_PROGRESS), JobStatus.IN_PROGRESS, true},
|
| 69 | {Arrays.asList(null, JobStatus.COMPLETED), JobStatus.COMPLETED_WITH_ERRORS, true},
|
| 70 | {Arrays.asList(null, JobStatus.IN_PROGRESS), JobStatus.IN_PROGRESS, true},
|
| 71 | {Arrays.asList(null, JobStatus.FAILED), JobStatus.COMPLETED_WITH_ERRORS, false},
|
| 72 | {new ArrayList<>(), JobStatus.COMPLETED, true}
|
| 73 | };
|
| 74 | }
|
| 75 |
|
| 76 | @BeforeMethod
|
| 77 | public void initMocks() {
|
| 78 | MockitoAnnotations.initMocks(this);
|
| 79 | }
|
| 80 |
|
| 81 | @Test(dataProvider = "expectedJobStatusDataProvider")
|
| 82 | public void testAssertNextCommandIsValid(List<JobStatus> childJobs, JobStatus expectedCommandStatus, boolean isService) {
|
| 83 | //init sql result mock
|
| 84 | List<OsamJob> mockChildren = childJobs.stream().map(st -> {
|
| 85 | OsamJob job = new OsamJob();
|
| 86 | job.setUuid(UUID.randomUUID());
|
| 87 | job.setStatus(st);
|
| 88 | return job;
|
| 89 | }).collect(Collectors.toList());
|
| 90 | when(osamJobRepository.findAllByUuid(any()))
|
| 91 | .thenReturn(mockChildren);
|
| 92 |
|
| 93 | //init job data for watching command
|
| 94 | UUID jobUUID = UUID.randomUUID();
|
| 95 | JobSharedData sharedData = new JobSharedData(jobUUID, "mockedUserID", mock(TestRequest.class));
|
| 96 | List<UUID> uuids = mockChildren.stream().map(job -> job.getUuid()).collect(Collectors.toList());
|
| 97 | watchingCommand.init(sharedData, uuids, isService);
|
| 98 |
|
| 99 | //execute command and verify
|
| 100 | NextCommand nextCommand = watchingCommand.call();
|
| 101 | assertThat(nextCommand.getStatus(), is(expectedCommandStatus));
|
| 102 | }
|
| 103 |
|
| 104 | public static class TestRequest implements IJobFactory.AsyncJobRequest{}
|
| 105 | }
|