blob: 0e595a6e6257a18526d86dbc76fc19178c8a3dfc [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.command;
21
22import org.mockito.InjectMocks;
23import org.mockito.Mock;
24import org.mockito.MockitoAnnotations;
25import org.onap.osam.job.dao.job.JobStatus;
26import org.onap.osam.job.dao.job.OsamJob;
27import org.onap.osam.job.IJobFactory;
28import org.onap.osam.job.NextCommand;
29import org.onap.osam.job.AsyncJobService;
30import org.onap.osam.job.command.WatchingCommand;
31import org.onap.osam.job.impl.JobSharedData;
32import org.onap.osam.job.repository.job.OsamJobRepository;
33import org.testng.annotations.BeforeMethod;
34import org.testng.annotations.DataProvider;
35import org.testng.annotations.Test;
36
37import java.util.ArrayList;
38import java.util.Arrays;
39import java.util.List;
40import java.util.UUID;
41import java.util.stream.Collectors;
42
43import static org.hamcrest.MatcherAssert.assertThat;
44import static org.hamcrest.core.Is.is;
45import static org.mockito.Matchers.any;
46import static org.mockito.Matchers.eq;
47import static org.mockito.Mockito.*;
48
49public 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}