isVfModuleBaseModule() will not throw on model mismatch
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / CommandUtilsTest.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.command;
22
23 import static java.util.Collections.emptyMap;
24 import static java.util.Collections.singletonMap;
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.hamcrest.MatcherAssert.assertThat;
27 import static org.hamcrest.Matchers.equalTo;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.reset;
30 import static org.mockito.Mockito.when;
31 import static org.onap.vid.testUtils.TestUtils.setStringsInStringFields;
32
33 import java.util.UUID;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.vid.model.GroupProperties;
38 import org.onap.vid.model.ServiceModel;
39 import org.onap.vid.model.VfModule;
40 import org.onap.vid.mso.model.ModelInfo;
41 import org.onap.vid.services.VidService;
42 import org.testng.annotations.AfterMethod;
43 import org.testng.annotations.BeforeClass;
44 import org.testng.annotations.DataProvider;
45 import org.testng.annotations.Test;
46
47 public class CommandUtilsTest {
48
49     @InjectMocks
50     CommandUtils commandUtils;
51
52     @Mock
53     VidService vidService;
54
55     @BeforeClass
56     public void initMocks() {
57         MockitoAnnotations.initMocks(this);
58     }
59
60     @AfterMethod
61     public void resetVidService() {
62         reset(vidService);
63     }
64
65     @DataProvider
66     public static Object[][] vfModuleModelInfos() {
67         ModelInfo modelInfoMatchByUuid = setStringsInStringFields(new ModelInfo());
68         modelInfoMatchByUuid.setModelCustomizationId("toscaCustomizationUuid");
69
70         ModelInfo modelInfoMatchByName = setStringsInStringFields(new ModelInfo());
71         modelInfoMatchByName.setModelCustomizationName("toscaCustomizationName");
72
73         ModelInfo modelInfoDontMatch = setStringsInStringFields(new ModelInfo());
74
75         return new Object[][]{
76             {true, modelInfoMatchByUuid, true},
77             {false, modelInfoMatchByUuid, false},
78
79             {true, modelInfoMatchByName, true},
80             {false, modelInfoMatchByName, false},
81
82             {true, modelInfoDontMatch, false},
83             {false, modelInfoDontMatch, false},
84         };
85     }
86
87     @Test(dataProvider="vfModuleModelInfos")
88     void isVfModuleBaseModule_vfModuleIsMatchedByEitherNameOrUuid(boolean isBaseInTosca, ModelInfo instanceModelInfo, boolean expected) {
89         GroupProperties mockedGroupProperties = mock(GroupProperties.class);
90         when(mockedGroupProperties.getBaseModule()).thenReturn(isBaseInTosca);
91
92         VfModule toscaVfModuleModelInfo = mock(VfModule.class);
93         when(toscaVfModuleModelInfo.getCustomizationUuid()).thenReturn("toscaCustomizationUuid");
94         when(toscaVfModuleModelInfo.getModelCustomizationName()).thenReturn("toscaCustomizationName");
95         when(toscaVfModuleModelInfo.getProperties()).thenReturn(mockedGroupProperties);
96
97
98         ServiceModel mockedServiceModel = mock(ServiceModel.class);
99         when(mockedServiceModel.getVfModules()).thenReturn(singletonMap("some-name", toscaVfModuleModelInfo));
100
101         String serviceModelUuid = UUID.randomUUID().toString();
102         when(vidService.getServiceModelOrThrow(serviceModelUuid)).thenReturn(mockedServiceModel);
103
104         assertThat(commandUtils.isVfModuleBaseModule(serviceModelUuid, instanceModelInfo), equalTo(expected));
105     }
106
107     @Test
108     void isVfModuleBaseModule_whenCantFindVfModulesInModel_resultIsFalse() {
109         String serviceModelUuid = UUID.randomUUID().toString();
110
111         ServiceModel mockedServiceModel = mock(ServiceModel.class);
112
113         when(vidService.getServiceModelOrThrow(serviceModelUuid)).thenReturn(mockedServiceModel);
114         when(mockedServiceModel.getVfModules()).thenReturn(emptyMap());
115
116         assertThat(
117             commandUtils.isVfModuleBaseModule(serviceModelUuid, mock(ModelInfo.class)), is(false));
118     }
119 }