aboutsummaryrefslogtreecommitdiffstats
path: root/vid-app-common/src/test/java/org/onap/vid/job/impl/JobAdapterTest.java
blob: 4e11b7e8bf83ccbd9afe73cdcb5c43057380420a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*-
 * ============LICENSE_START=======================================================
 * VID
 * ================================================================================
 * Copyright (C) 2017 - 2019 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.vid.job.impl;

import com.google.common.collect.ImmutableMap;
import org.apache.commons.lang3.RandomUtils;
import org.onap.vid.job.Job;
import org.onap.vid.job.JobAdapter;
import org.onap.vid.job.JobType;
import org.onap.vid.job.command.JobCommandFactoryTest;
import org.onap.vid.properties.Features;
import org.onap.vid.testUtils.TestUtils;
import org.testng.annotations.Test;
import org.togglz.core.manager.FeatureManager;

import java.util.UUID;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotEquals;
import static org.testng.AssertJUnit.assertNotNull;

public class JobAdapterTest {

    @Test
    public void testCreateServiceInstantiationJob() {
        FeatureManager featureManager = mock(FeatureManager.class);
        JobAdapter jobAdapter = new JobAdapterImpl(featureManager);

        JobType jobType = JobType.NoOp;
        JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
        UUID templateId = UUID.randomUUID();
        String userId = "ou012t";
        String testApi = "VNF_API";
        String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
        int indexInBulk = RandomUtils.nextInt();
        Job job = jobAdapter.createServiceInstantiationJob(
                jobType,
                request,
                templateId,
                userId,
                testApi,
                optimisticUniqueServiceInstanceName,
                indexInBulk
                );

        assertEquals(job.getType(), jobType);
        assertEquals(job.getSharedData().getRequest(), request);
        assertEquals(job.getSharedData().getRequestType(), request.getClass());
        assertEquals(job.getSharedData().getUserId(), userId);
        assertEquals(job.getSharedData().getTestApi(), testApi);
        assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
        assertEquals(job.getSharedData().getRootJobId(), job.getUuid());
        assertNotNull(job.getUuid());
        assertEquals(job.getTemplateId(), templateId);
        assertEquals(job.getData().get("optimisticUniqueServiceInstanceName"), optimisticUniqueServiceInstanceName);
        assertEquals((int)job.getIndexInBulk(), indexInBulk );
        assertEquals(job.getStatus(), Job.JobStatus.PENDING);
    }

    @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils.class)
    public void testCreateChildJob(boolean isFlagExpCreateResourcesInParallel) {
        FeatureManager featureManager = mock(FeatureManager.class);
        when(featureManager.isActive(Features.FLAG_EXP_CREATE_RESOURCES_IN_PARALLEL)).thenReturn(isFlagExpCreateResourcesInParallel);
        Job.JobStatus expectedJobStatus = isFlagExpCreateResourcesInParallel ? Job.JobStatus.CREATING : Job.JobStatus.PENDING_RESOURCE;
        JobAdapter jobAdapter = new JobAdapterImpl(featureManager);

        UUID templateId = UUID.randomUUID();
        String userId = "ou012t";
        String testApi = "VNF_API";
        String optimisticUniqueServiceInstanceName = "optimisticUniqueServiceInstanceName";
        int indexInBulk = RandomUtils.nextInt();
        Job grandJob = jobAdapter.createServiceInstantiationJob(
                JobType.HttpCall,
                new JobCommandFactoryTest.MockedRequest(99, "anything"),
                templateId,
                userId,
                testApi,
                optimisticUniqueServiceInstanceName,
                indexInBulk
        );

        JobType jobType = JobType.NoOp;
        JobAdapter.AsyncJobRequest request = new JobCommandFactoryTest.MockedRequest(42,"nothing");
        Job parentJob = jobAdapter.createChildJob(jobType, request, grandJob.getSharedData(), ImmutableMap.of(), 1);

        assertEquals(parentJob.getType(), jobType);
        assertEquals(parentJob.getSharedData().getRequest(), request);
        assertEquals(parentJob.getSharedData().getRequestType(), request.getClass());
        assertEquals(parentJob.getSharedData().getUserId(), userId);
        assertEquals(parentJob.getSharedData().getTestApi(), testApi);
        assertEquals(parentJob.getSharedData().getJobUuid(), parentJob.getUuid());
        assertNotNull(parentJob.getUuid());
        assertNotEquals(parentJob.getUuid(), grandJob.getUuid());
        assertEquals(parentJob.getStatus(), expectedJobStatus);
        assertEquals(parentJob.getTemplateId(), grandJob.getUuid());
        assertEquals(parentJob.getIndexInBulk().intValue(), 1);
        assertEquals(parentJob.getSharedData().getRootJobId(), grandJob.getUuid());

        JobType jobType2 = JobType.AggregateState;
        JobAdapter.AsyncJobRequest request2 = new JobCommandFactoryTest.MockedRequest(66,"abc");
        Job job = jobAdapter.createChildJob(jobType2, request2, parentJob.getSharedData(), ImmutableMap.of(), 0);

        assertEquals(job.getType(), jobType2);
        assertEquals(job.getSharedData().getRequest(), request2);
        assertEquals(job.getSharedData().getRequestType(), request2.getClass());
        assertEquals(job.getSharedData().getUserId(), userId);
        assertEquals(job.getSharedData().getTestApi(), testApi);
        assertEquals(job.getSharedData().getJobUuid(), job.getUuid());
        assertNotNull(job.getUuid());
        assertNotEquals(job.getUuid(), parentJob.getUuid());
        assertEquals(job.getStatus(), expectedJobStatus);
        assertEquals(job.getSharedData().getRootJobId(), grandJob.getUuid());
        assertEquals(job.getTemplateId(), parentJob.getUuid());
        assertEquals(job.getIndexInBulk().intValue(), 0);

    }
}