Merge "vfModule upgrade: don't fallback on mismatching newest model"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / VfmoduleCommandTest.kt
1 package org.onap.vid.job.command
2
3 import org.hamcrest.MatcherAssert.assertThat
4 import org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs
5 import org.mockito.InjectMocks
6 import org.mockito.Mock
7 import org.onap.vid.job.JobAdapter
8 import org.onap.vid.job.JobsBrokerService
9 import org.onap.vid.model.ServiceModel
10 import org.onap.vid.model.VfModule
11 import org.onap.vid.mso.RestMsoImplementation
12 import org.onap.vid.mso.model.ModelInfo
13 import org.onap.vid.services.AsyncInstantiationBusinessLogic
14 import org.onap.vid.testUtils.TestUtils.initMockitoMocks
15 import org.testng.annotations.BeforeMethod
16 import org.testng.annotations.Test
17
18 class VfmoduleCommandTest {
19
20     @Mock lateinit var asyncInstantiationBL: AsyncInstantiationBusinessLogic;
21     @Mock lateinit var restMso: RestMsoImplementation;
22     @Mock lateinit var msoRequestBuilder: MsoRequestBuilder;
23     @Mock lateinit var msoResultHandlerService: MsoResultHandlerService;
24     @Mock lateinit var inProgressStatusService: InProgressStatusService;
25     @Mock lateinit var watchChildrenJobsBL: WatchChildrenJobsBL;
26     @Mock lateinit var jobsBrokerService: JobsBrokerService;
27     @Mock lateinit var jobAdapter: JobAdapter;
28
29     @InjectMocks lateinit var vfModuleCommand: VfmoduleCommand;
30
31     private val uniqueCustomizationName = "my unique customization name"
32
33     @BeforeMethod
34     fun initMocks() {
35         initMockitoMocks(this)
36     }
37
38     @Test
39     fun `given correlated modelCustomizationName, selectVfms returns the vfm`() {
40         val newestModel = ServiceModel()
41         newestModel.vfModules = someVfModules()
42                 .plus("my vfm" to vfModuleWithCustomizationName())
43
44         val selectedVfm = vfModuleCommand.selectVfm(newestModel, modelInfoWithCustomizationName())
45
46         assertThat(selectedVfm, samePropertyValuesAs(vfModuleWithCustomizationName()))
47     }
48
49     @Test(
50             expectedExceptions = [IllegalStateException::class],
51             expectedExceptionsMessageRegExp =
52             """Cannot match vfModule for modelCustomizationName "my unique customization name": Collection contains no element matching the predicate.""")
53     fun `given no matching modelCustomizationName, selectVfms throws`() {
54         val newestModel = ServiceModel()
55         newestModel.vfModules = someVfModules()
56
57         vfModuleCommand.selectVfm(newestModel, modelInfoWithCustomizationName())
58     }
59
60     @Test(
61             expectedExceptions = [IllegalStateException::class],
62             expectedExceptionsMessageRegExp =
63             """Cannot match vfModule for modelCustomizationName "my unique customization name": Collection contains more than one matching element.""")
64     fun `given a few matching modelCustomizationName, selectVfms throws`() {
65
66         val newestModel = ServiceModel()
67         newestModel.vfModules = someVfModules()
68                 .plus("my vfm" to vfModuleWithCustomizationName())
69                 .plus("my vfm2" to vfModuleWithCustomizationName())
70
71         vfModuleCommand.selectVfm(newestModel, modelInfoWithCustomizationName())
72     }
73
74     private fun modelInfoWithCustomizationName(customizationName: String = uniqueCustomizationName) =
75             ModelInfo().also { it.modelCustomizationName = customizationName }
76
77     private fun someVfModules(): Map<String, VfModule> = mapOf(
78             "any vfm 1" to vfModuleWithCustomizationName("any customization name 1"),
79             "any vfm 2" to vfModuleWithCustomizationName("any customization name 2")
80     )
81
82     private fun vfModuleWithCustomizationName(customizationName: String = uniqueCustomizationName) =
83             VfModule().also { it.modelCustomizationName = customizationName }
84
85 }