Implant vid-app-common org.onap.vid.job (main and test)
[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.onap.vid.properties.Features;
30 import org.onap.vid.testUtils.TestUtils;
31 import org.testng.annotations.Test;
32 import org.togglz.core.manager.FeatureManager;
33
34 import java.util.UUID;
35
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.when;
38 import static org.testng.Assert.assertEquals;
39 import static org.testng.Assert.assertNotEquals;
40 import static org.testng.AssertJUnit.assertNotNull;
41
42 public class JobAdapterTest {
43
44     @Test
45     public void testCreateServiceInstantiationJob() {
46         FeatureManager featureManager = mock(FeatureManager.class);
47         JobAdapter jobAdapter = new JobAdapterImpl(featureManager);
48
49         JobType jobType = JobType.NoOp;
50         JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
51         UUID templateId = UUID.randomUUID();
52         String userId = "ou012t";
53         String testApi = "VNF_API";
54         String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
55         int indexInBulk = RandomUtils.nextInt();
56         Job job = jobAdapter.createServiceInstantiationJob(
57                 jobType,
58                 request,
59                 templateId,
60                 userId,
61                 testApi,
62                 optimisticUniqueServiceInstanceName,
63                 indexInBulk
64                 );
65
66         assertEquals(job.getType(), jobType);
67         assertEquals(job.getSharedData().getRequest(), request);
68         assertEquals(job.getSharedData().getRequestType(), request.getClass());
69         assertEquals(job.getSharedData().getUserId(), userId);
70         assertEquals(job.getSharedData().getTestApi(), testApi);
71         assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
72         assertEquals(job.getSharedData().getRootJobId(), job.getUuid());
73         assertNotNull(job.getUuid());
74         assertEquals(job.getTemplateId(), templateId);
75         assertEquals(job.getData().get("optimisticUniqueServiceInstanceName"), optimisticUniqueServiceInstanceName);
76         assertEquals((int)job.getIndexInBulk(), indexInBulk );
77         assertEquals(job.getStatus(), Job.JobStatus.PENDING);
78     }
79
80     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
81     public void testCreateChildJob(boolean isFlagExpCreateResourcesInParallel) {
82         FeatureManager featureManager = mock(FeatureManager.class);
83         when(featureManager.isActive(Features.FLAG_EXP_CREATE_RESOURCES_IN_PARALLEL)).thenReturn(isFlagExpCreateResourcesInParallel);
84         Job.JobStatus expectedJobStatus = isFlagExpCreateResourcesInParallel ? Job.JobStatus.CREATING : Job.JobStatus.PENDING_RESOURCE;
85         JobAdapter jobAdapter = new JobAdapterImpl(featureManager);
86
87         UUID templateId = UUID.randomUUID();
88         String userId = "ou012t";
89         String testApi = "VNF_API";
90         String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
91         int indexInBulk = RandomUtils.nextInt();
92         Job grandJob = jobAdapter.createServiceInstantiationJob(
93                 JobType.HttpCall,
94                 new JobCommandFactoryTest.MockedRequest(99, "anything"),
95                 templateId,
96                 userId,
97                 testApi,
98                 optimisticUniqueServiceInstanceName,
99                 indexInBulk
100         );
101
102         JobType jobType = JobType.NoOp;
103         JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
104         Job parentJob = jobAdapter.createChildJob(jobType, request, grandJob.getSharedData(), ImmutableMap.of(), 1);
105
106         assertEquals(parentJob.getType(), jobType);
107         assertEquals(parentJob.getSharedData().getRequest(), request);
108         assertEquals(parentJob.getSharedData().getRequestType(), request.getClass());
109         assertEquals(parentJob.getSharedData().getUserId(), userId);
110         assertEquals(parentJob.getSharedData().getTestApi(), testApi);
111         assertEquals(parentJob.getSharedData().getJobUuid(), parentJob.getUuid());
112         assertNotNull(parentJob.getUuid());
113         assertNotEquals(parentJob.getUuid(), grandJob.getUuid());
114         assertEquals(parentJob.getStatus(), expectedJobStatus);
115         assertEquals(parentJob.getTemplateId(), grandJob.getUuid());
116         assertEquals(parentJob.getIndexInBulk().intValue(), 1);
117         assertEquals(parentJob.getSharedData().getRootJobId(), grandJob.getUuid());
118
119         JobType jobType2 = JobType.AggregateState;
120         JobAdapter.AsyncJobRequest request2 = new JobCommandFactoryTest.MockedRequest(66,"abc");
121         Job job = jobAdapter.createChildJob(jobType2, request2, parentJob.getSharedData(), ImmutableMap.of(), 0);
122
123         assertEquals(job.getType(), jobType2);
124         assertEquals(job.getSharedData().getRequest(), request2);
125         assertEquals(job.getSharedData().getRequestType(), request2.getClass());
126         assertEquals(job.getSharedData().getUserId(), userId);
127         assertEquals(job.getSharedData().getTestApi(), testApi);
128         assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
129         assertNotNull(job.getUuid());
130         assertNotEquals(job.getUuid(), parentJob.getUuid());
131         assertEquals(job.getStatus(), expectedJobStatus);
132         assertEquals(job.getSharedData().getRootJobId(), grandJob.getUuid());
133         assertEquals(job.getTemplateId(), parentJob.getUuid());
134         assertEquals(job.getIndexInBulk().intValue(), 0);
135
136     }
137 }