f6785c37d549ae07b108e5b6740e52c1cd32ec87
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / impl / JobAdapterTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 - 2019 AT&T 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.ImmutableMap;
24 import org.apache.commons.lang3.RandomUtils;
25 import org.onap.vid.job.Job;
26 import org.onap.vid.job.JobAdapter;
27 import org.onap.vid.job.JobType;
28 import org.onap.vid.job.command.JobCommandFactoryTest;
29 import org.testng.annotations.Test;
30
31 import java.util.UUID;
32
33 import static org.testng.Assert.assertEquals;
34 import static org.testng.Assert.assertNotEquals;
35 import static org.testng.AssertJUnit.assertNotNull;
36
37 public class JobAdapterTest {
38
39     @Test
40     public void testCreateServiceInstantiationJob() {
41         JobAdapter jobAdapter = new JobAdapterImpl();
42
43         JobType jobType = JobType.NoOp;
44         JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
45         UUID templateId = UUID.randomUUID();
46         String userId = "ou012t";
47         String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
48         int indexInBulk = RandomUtils.nextInt();
49         Job job = jobAdapter.createServiceInstantiationJob(
50                 jobType,
51                 request,
52                 templateId,
53                 userId,
54                 optimisticUniqueServiceInstanceName,
55                 indexInBulk
56                 );
57
58         assertEquals(job.getType(), jobType);
59         assertEquals(job.getSharedData().getRequest(), request);
60         assertEquals(job.getSharedData().getRequestType(), request.getClass());
61         assertEquals(job.getSharedData().getUserId(), userId);
62         assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
63         assertEquals(job.getSharedData().getRootJobId(), job.getUuid());
64         assertNotNull(job.getUuid());
65         assertEquals(job.getTemplateId(), templateId);
66         assertEquals(job.getData().get("optimisticUniqueServiceInstanceName"), optimisticUniqueServiceInstanceName);
67         assertEquals((int)job.getIndexInBulk(), indexInBulk );
68         assertEquals(job.getStatus(), Job.JobStatus.PENDING);
69     }
70
71     @Test
72     public void testCreateChildJob() {
73
74         JobAdapter jobAdapter = new JobAdapterImpl();
75
76         UUID templateId = UUID.randomUUID();
77         String userId = "ou012t";
78         String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
79         int indexInBulk = RandomUtils.nextInt();
80         Job grandJob = jobAdapter.createServiceInstantiationJob(
81                 JobType.HttpCall,
82                 new JobCommandFactoryTest.MockedRequest(99, "anything"),
83                 templateId,
84                 userId,
85                 optimisticUniqueServiceInstanceName,
86                 indexInBulk
87         );
88
89         Job.JobStatus jobStatus = Job.JobStatus.PAUSE;
90         JobType jobType = JobType.NoOp;
91         JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
92         Job parentJob = jobAdapter.createChildJob(jobType, jobStatus, request, grandJob.getSharedData(), ImmutableMap.of());
93
94         assertEquals(parentJob.getType(), jobType);
95         assertEquals(parentJob.getSharedData().getRequest(), request);
96         assertEquals(parentJob.getSharedData().getRequestType(), request.getClass());
97         assertEquals(parentJob.getSharedData().getUserId(), userId);
98         assertEquals(parentJob.getSharedData().getJobUuid(), parentJob.getUuid());
99         assertNotNull(parentJob.getUuid());
100         assertNotEquals(parentJob.getUuid(), grandJob.getUuid());
101         assertEquals(parentJob.getStatus(), jobStatus);
102         assertEquals(parentJob.getSharedData().getRootJobId(), grandJob.getUuid());
103
104         Job.JobStatus jobStatus2 = Job.JobStatus.IN_PROGRESS;
105         JobType jobType2 = JobType.AggregateState;
106         JobAdapter.AsyncJobRequest request2 = new JobCommandFactoryTest.MockedRequest(66,"abc");
107         Job job = jobAdapter.createChildJob(jobType2, jobStatus2, request2, parentJob.getSharedData(), ImmutableMap.of());
108
109         assertEquals(job.getType(), jobType2);
110         assertEquals(job.getSharedData().getRequest(), request2);
111         assertEquals(job.getSharedData().getRequestType(), request2.getClass());
112         assertEquals(job.getSharedData().getUserId(), userId);
113         assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
114         assertNotNull(job.getUuid());
115         assertNotEquals(job.getUuid(), parentJob.getUuid());
116         assertEquals(job.getStatus(), jobStatus2);
117         assertEquals(job.getSharedData().getRootJobId(), grandJob.getUuid());
118
119     }
120 }