Merge "Logging improvements"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / impl / JobAdapterImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2018 Nokia Intellectual Property. All rights reserved.
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 package org.onap.vid.job.impl;
22
23 import com.google.common.collect.ImmutableList;
24 import com.google.common.collect.ImmutableMap;
25 import org.mockito.Mock;
26 import org.onap.vid.job.Job;
27 import org.onap.vid.job.JobAdapter;
28 import org.onap.vid.job.JobType;
29 import org.onap.vid.model.JobBulk;
30 import org.onap.vid.model.JobModel;
31 import org.testng.annotations.BeforeMethod;
32 import org.testng.annotations.Test;
33
34 import java.util.List;
35 import java.util.UUID;
36 import java.util.stream.Stream;
37
38 import static org.assertj.core.api.Assertions.assertThat;
39 import static org.mockito.Mockito.when;
40 import static org.mockito.MockitoAnnotations.initMocks;
41
42 public class JobAdapterImplTest {
43
44
45     private static final int SAMPLE_INDEX = 10;
46     private static final String SAMPLE_USER_ID = "sampleUserId";
47
48     @Mock
49     private Job job;
50
51     @Mock
52     private JobAdapter.AsyncJobRequest asyncJobRequest;
53
54     private UUID sampleUuid=UUID.randomUUID();
55
56     private JobAdapterImpl jobAdapter = new JobAdapterImpl();
57
58     @BeforeMethod
59     public void setUp() {
60         initMocks(this);
61
62         when(job.getUuid()).thenReturn(sampleUuid);
63         when(job.getStatus()).thenReturn(Job.JobStatus.IN_PROGRESS);
64         when(job.getTemplateId()).thenReturn(sampleUuid);
65     }
66
67     @Test
68     public void shouldConvertJobToJobModel() {
69
70
71         JobModel convertedJob = jobAdapter.toModel(job);
72
73         assertThat(convertedJob.getUuid()).isEqualByComparingTo(sampleUuid);
74         assertThat(convertedJob.getStatus()).isEqualByComparingTo(Job.JobStatus.IN_PROGRESS);
75         assertThat(convertedJob.getTemplateId()).isEqualByComparingTo(sampleUuid);
76     }
77
78
79     @Test
80     public void shouldProperlyCreateJob() {
81         UUID uuid = UUID.randomUUID();
82
83         Job createdJob = jobAdapter.createJob(JobType.ServiceInstantiation, asyncJobRequest, uuid, SAMPLE_USER_ID, SAMPLE_INDEX);
84
85         assertThat(createdJob.getStatus()).isEqualByComparingTo(Job.JobStatus.PENDING);
86         assertThat(createdJob.getTemplateId()).isEqualByComparingTo(uuid);
87         assertThat(createdJob.getType()).isEqualByComparingTo(JobType.ServiceInstantiation);
88         assertThat(createdJob.getData()).isEqualTo(ImmutableMap.of("request", asyncJobRequest, "userId", SAMPLE_USER_ID));
89     }
90
91     @Test
92     public void shouldProperlyCreateBulkOfJobs(){
93         List<Job> bulkOfJobs = jobAdapter.createBulkOfJobs(ImmutableMap.of("count", 5, "type", JobType.InProgressStatus.name()));
94
95
96         assertThat(bulkOfJobs).hasSize(5);
97
98         Stream<Job> jobStream = bulkOfJobs.stream().filter(x -> JobType.InProgressStatus.equals(x.getType()) && Job.JobStatus.PENDING.equals(x.getStatus()));
99
100         assertThat(jobStream).hasSize(5);
101     }
102
103
104     @Test
105     public void shouldConvertListToBulkJob(){
106         JobBulk jobBulk = jobAdapter.toModelBulk(ImmutableList.of(job, job));
107
108         assertThat(jobBulk.getJobs()).hasSize(2);
109     }
110 }