2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.job.command
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.onap.vid.job.JobAdapter
30 import org.onap.vid.job.JobsBrokerService
31 import org.onap.vid.job.command.ResourceCommandTest.FakeResourceCreator
32 import org.onap.vid.job.impl.JobSharedData
33 import org.onap.vid.model.Action
34 import org.onap.vid.mso.RestMsoImplementation
35 import org.onap.vid.properties.Features
36 import org.onap.vid.services.AsyncInstantiationBusinessLogic
37 import org.onap.vid.testUtils.TestUtils
38 import org.onap.vid.testUtils.TestUtils.initMockitoMocks
39 import org.testng.annotations.BeforeMethod
40 import org.testng.annotations.DataProvider
41 import org.testng.annotations.Test
42 import org.togglz.core.manager.FeatureManager
43 import org.mockito.Mockito.`when` as _when
45 class VnfCommandTest {
47 @Mock lateinit var asyncInstantiationBL: AsyncInstantiationBusinessLogic
48 @Mock lateinit var restMso: RestMsoImplementation
49 @Mock lateinit var msoRequestBuilder: MsoRequestBuilder
50 @Mock lateinit var msoResultHandlerService: MsoResultHandlerService
51 @Mock lateinit var inProgressStatusService:InProgressStatusService
52 @Mock lateinit var watchChildrenJobsBL: WatchChildrenJobsBL
53 @Mock lateinit var jobsBrokerService: JobsBrokerService
54 @Mock lateinit var jobAdapter: JobAdapter
55 @Mock lateinit var featureManager: FeatureManager
57 @Mock lateinit var jobSharedData: JobSharedData
58 @Mock(answer = Answers.RETURNS_MOCKS) lateinit var vnfJobRequest: org.onap.vid.model.serviceInstantiation.Vnf
60 @InjectMocks lateinit var vnfCommand: VnfCommand;
64 initMockitoMocks(this)
67 @Test(dataProvider = "trueAndFalse", dataProviderClass = TestUtils::class)
68 fun `childVfModuleWithVnfRegionAndTenant -- given vfmodule -- tenant and region are copied from vnf`(featureToggleOn: Boolean) {
69 runChildVfModuleWithVnfRegionAndTenant(featureToggleOn, Action.Create, featureToggleOn)
73 fun allPossibleActions(): Array<Array<out Any?>> {
74 return Action.values().map { arrayOf(it) }.toTypedArray()
77 @Test(dataProvider = "allPossibleActions")
78 fun `childVfModuleWithVnfRegionAndTenant -- given vfmodule in different actions -- only "action_Create" copies tenant and region from vnf`(vfModuleAction: Action) {
79 runChildVfModuleWithVnfRegionAndTenant(true, vfModuleAction, vfModuleAction == Action.Create)
82 private fun runChildVfModuleWithVnfRegionAndTenant(featureToggleOn: Boolean, vfModuleAction: Action, isCopyVnfToVfmoduleExpected: Boolean) {
84 val vfModule = FakeResourceCreator.createVfModule(vfModuleAction)
85 .cloneWith("vfmodule-lcp-cloud-region-id", "vfmodule-tenant-id")
87 _when(featureManager.isActive(Features.FLAG_2006_VFMODULE_TAKES_TENANT_AND_REGION_FROM_VNF)).thenReturn(featureToggleOn)
89 _when(vnfJobRequest.lcpCloudRegionId).thenReturn("vnf-lcp-cloud-region-id")
90 _when(vnfJobRequest.tenantId).thenReturn("vnf-tenant-id")
91 _when(jobSharedData.request).thenReturn(vnfJobRequest)
93 vnfCommand.init(jobSharedData, mapOf())
95 val expectedSource = if (isCopyVnfToVfmoduleExpected) "vnf" else "vfmodule"
97 assertThat(vnfCommand.childVfModuleWithVnfRegionAndTenant(vfModule),
99 jsonPartEquals("lcpCloudRegionId", "${expectedSource}-lcp-cloud-region-id"),
100 jsonPartEquals("tenantId", "${expectedSource}-tenant-id")