Move pauseInstantiation from VfModule to BaseResource
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / VnfCommandTest.kt
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2020 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.command
22
23 import net.javacrumbs.jsonunit.JsonMatchers.jsonPartEquals
24 import org.hamcrest.MatcherAssert.assertThat
25 import org.hamcrest.core.AllOf.allOf
26 import org.mockito.Answers
27 import org.mockito.InjectMocks
28 import org.mockito.Mock
29 import org.mockito.Mockito.mock
30 import org.onap.vid.job.JobAdapter
31 import org.onap.vid.job.JobsBrokerService
32 import org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator
33 import org.onap.vid.job.impl.JobSharedData
34 import org.onap.vid.model.Action
35 import org.onap.vid.model.serviceInstantiation.BaseResource.PauseInstantiation.afterCompletion
36 import org.onap.vid.model.serviceInstantiation.VfModule
37 import org.onap.vid.mso.RestMsoImplementation
38 import org.onap.vid.properties.Features
39 import org.onap.vid.services.AsyncInstantiationBusinessLogic
40 import org.onap.vid.testUtils.TestUtils
41 import org.onap.vid.testUtils.TestUtils.initMockitoMocks
42 import org.testng.AssertJUnit
43 import org.testng.annotations.BeforeMethod
44 import org.testng.annotations.DataProvider
45 import org.testng.annotations.Test
46 import org.togglz.core.manager.FeatureManager
47 import org.mockito.Mockito.`when` as _when
48
49 class VnfCommandTest {
50
51     @Mock lateinit var asyncInstantiationBL: AsyncInstantiationBusinessLogic
52     @Mock lateinit var restMso: RestMsoImplementation
53     @Mock lateinit var msoRequestBuilder: MsoRequestBuilder
54     @Mock lateinit var msoResultHandlerService: MsoResultHandlerService
55     @Mock lateinit var inProgressStatusService:InProgressStatusService
56     @Mock lateinit var watchChildrenJobsBL: WatchChildrenJobsBL
57     @Mock lateinit var jobsBrokerService: JobsBrokerService
58     @Mock lateinit var jobAdapter: JobAdapter
59     @Mock lateinit var featureManager: FeatureManager
60
61     @Mock lateinit var jobSharedData: JobSharedData
62     @Mock(answer = Answers.RETURNS_MOCKS) lateinit var vnfJobRequest: org.onap.vid.model.serviceInstantiation.Vnf
63
64     @InjectMocks lateinit var vnfCommand: VnfCommand;
65
66     @BeforeMethod
67     fun initMocks() {
68         initMockitoMocks(this)
69     }
70
71     @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils::class)
72     fun `childVfModuleWithVnfRegionAndTenant -- given vfmodule -- tenant and region are copied from vnf`(featureToggleOn: Boolean) {
73         runChildVfModuleWithVnfRegionAndTenant(featureToggleOn, Action.Create, featureToggleOn)
74     }
75
76     @DataProvider
77     fun allPossibleActions(): Array<Array<out Any?>> {
78         return Action.values().map { arrayOf(it) }.toTypedArray()
79     }
80
81     @Test(dataProvider = "allPossibleActions")
82     fun `childVfModuleWithVnfRegionAndTenant -- given vfmodule in different actions -- only "action_Create" copies tenant and region from vnf`(vfModuleAction: Action) {
83         runChildVfModuleWithVnfRegionAndTenant(true, vfModuleAction, vfModuleAction == Action.Create)
84     }
85
86     private fun runChildVfModuleWithVnfRegionAndTenant(featureToggleOn: Boolean, vfModuleAction: Action, isCopyVnfToVfmoduleExpected: Boolean) {
87
88         val vfModule = FakeResourceCreator.createVfModule(vfModuleAction)
89                         .cloneWith("vfmodule-lcp-cloud-region-id", "vfmodule-tenant-id")
90
91         _when(featureManager.isActive(Features.FLAG_2006_VFMODULE_TAKES_TENANT_AND_REGION_FROM_VNF)).thenReturn(featureToggleOn)
92
93         _when(vnfJobRequest.lcpCloudRegionId).thenReturn("vnf-lcp-cloud-region-id")
94         _when(vnfJobRequest.tenantId).thenReturn("vnf-tenant-id")
95         _when(jobSharedData.request).thenReturn(vnfJobRequest)
96
97         vnfCommand.init(jobSharedData, mapOf())
98
99         val expectedSource = if (isCopyVnfToVfmoduleExpected) "vnf" else "vfmodule"
100
101         assertThat(vnfCommand.childVfModuleWithVnfRegionAndTenant(vfModule),
102                 allOf(
103                         jsonPartEquals("lcpCloudRegionId", "${expectedSource}-lcp-cloud-region-id"),
104                         jsonPartEquals("tenantId", "${expectedSource}-tenant-id")
105                 )
106         )
107     }
108
109     @DataProvider
110     fun shoudlPause(): Array<Array<out Any?>> {
111         return arrayOf(
112                 arrayOf("action create, pause in module and flag active", Action.Create, true, true, true),
113                 arrayOf("action create, pause in module and flag  not active", Action.Create, true, false, false),
114                 arrayOf("action not create, pause in module and flag active", Action.None, true, true, false),
115                 arrayOf("action create, no pause in module and flag active", Action.Create, false, true, false),
116                 arrayOf("action create, no pause in module and flag not active", Action.Create, false, false, false))
117     }
118
119     @Test(dataProvider = "shoudlPause")
120     fun `shouldPauseAfterInstantiation -- given different flag and pause conditions`(desc: String, vfModuleAction: Action, isPause: Boolean, flag: Boolean,
121                                                                                      expectedResult: Boolean) {
122         val vfModule = mock(VfModule::class.java);
123
124         _when(featureManager.isActive(Features.FLAG_2006_PAUSE_VFMODULE_INSTANTIATION_CREATION)).thenReturn(flag)
125         _when(vfModule.action).thenReturn(vfModuleAction)
126
127         if (isPause) {
128             _when(vfModule.pauseInstantiation).thenReturn(afterCompletion)
129         }
130
131         AssertJUnit.assertEquals(desc, expectedResult, vnfCommand.shoudlPauseAfterInstantiation(vfModule))
132     }
133 }