isVfModuleBaseModule() will not throw on model mismatch
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / CommandUtilsTest.java
index ee43d1f..d1d9b5e 100644 (file)
 
 package org.onap.vid.job.command;
 
-import com.google.common.collect.ImmutableMap;
+import static java.util.Collections.emptyMap;
+import static java.util.Collections.singletonMap;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.equalTo;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.reset;
+import static org.mockito.Mockito.when;
+import static org.onap.vid.testUtils.TestUtils.setStringsInStringFields;
+
+import java.util.UUID;
 import org.mockito.InjectMocks;
 import org.mockito.Mock;
 import org.mockito.MockitoAnnotations;
-import org.onap.vid.asdc.AsdcCatalogException;
 import org.onap.vid.model.GroupProperties;
 import org.onap.vid.model.ServiceModel;
 import org.onap.vid.model.VfModule;
+import org.onap.vid.mso.model.ModelInfo;
 import org.onap.vid.services.VidService;
 import org.testng.annotations.AfterMethod;
 import org.testng.annotations.BeforeClass;
 import org.testng.annotations.DataProvider;
 import org.testng.annotations.Test;
 
-import java.util.Collections;
-import java.util.Map;
-import java.util.UUID;
-
-import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.hamcrest.Matchers.equalTo;
-import static org.mockito.Mockito.*;
-
 public class CommandUtilsTest {
 
     @InjectMocks
@@ -62,47 +63,57 @@ public class CommandUtilsTest {
     }
 
     @DataProvider
-    public static Object[][] trueAndFalse() {
-        return new Object[][]{ {true}, {false} };
-    }
+    public static Object[][] vfModuleModelInfos() {
+        ModelInfo modelInfoMatchByUuid = setStringsInStringFields(new ModelInfo());
+        modelInfoMatchByUuid.setModelCustomizationId("toscaCustomizationUuid");
 
-    @Test(dataProvider="trueAndFalse")
-    void testIsVfModelIsBaseModule(boolean isBase) throws AsdcCatalogException {
-        final String serviceModelUuid = UUID.randomUUID().toString();
-        final String vfModuleUuid = UUID.randomUUID().toString();
+        ModelInfo modelInfoMatchByName = setStringsInStringFields(new ModelInfo());
+        modelInfoMatchByName.setModelCustomizationName("toscaCustomizationName");
 
-        ServiceModel mockedServiceModel = mock(ServiceModel.class);
-        VfModule mockedVfModule = mock(VfModule.class);
-        GroupProperties mockedGroupProperties = mock(GroupProperties.class);
-        Map<String, VfModule> vfModulesMap = ImmutableMap.of(randomAlphanumeric(10), mockedVfModule);
+        ModelInfo modelInfoDontMatch = setStringsInStringFields(new ModelInfo());
+
+        return new Object[][]{
+            {true, modelInfoMatchByUuid, true},
+            {false, modelInfoMatchByUuid, false},
 
-        when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
-        when(mockedServiceModel.getVfModules()).thenReturn(vfModulesMap);
-        when(mockedVfModule.getUuid()).thenReturn(vfModuleUuid);
-        when(mockedVfModule.getProperties()).thenReturn(mockedGroupProperties);
-        when(mockedGroupProperties.getBaseModule()).thenReturn(isBase);
+            {true, modelInfoMatchByName, true},
+            {false, modelInfoMatchByName, false},
 
-        assertThat(commandUtils.isVfModuleBaseModule(serviceModelUuid, vfModuleUuid), equalTo(isBase));
+            {true, modelInfoDontMatch, false},
+            {false, modelInfoDontMatch, false},
+        };
     }
 
-    @Test(expectedExceptions = AsdcCatalogException.class)
-    void whenCantFindModelInSdc_thenExceptionIsThrown() throws AsdcCatalogException {
+    @Test(dataProvider="vfModuleModelInfos")
+    void isVfModuleBaseModule_vfModuleIsMatchedByEitherNameOrUuid(boolean isBaseInTosca, ModelInfo instanceModelInfo, boolean expected) {
+        GroupProperties mockedGroupProperties = mock(GroupProperties.class);
+        when(mockedGroupProperties.getBaseModule()).thenReturn(isBaseInTosca);
+
+        VfModule toscaVfModuleModelInfo = mock(VfModule.class);
+        when(toscaVfModuleModelInfo.getCustomizationUuid()).thenReturn("toscaCustomizationUuid");
+        when(toscaVfModuleModelInfo.getModelCustomizationName()).thenReturn("toscaCustomizationName");
+        when(toscaVfModuleModelInfo.getProperties()).thenReturn(mockedGroupProperties);
+
+
+        ServiceModel mockedServiceModel = mock(ServiceModel.class);
+        when(mockedServiceModel.getVfModules()).thenReturn(singletonMap("some-name", toscaVfModuleModelInfo));
+
         String serviceModelUuid = UUID.randomUUID().toString();
-        when(vidService.getService(serviceModelUuid)).thenReturn(null);
-        commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
-    }
+        when(vidService.getServiceModelOrThrow(serviceModelUuid)).thenReturn(mockedServiceModel);
 
-    @Test(expectedExceptions = AsdcCatalogException.class)
-    void whenCantFindVfModuleInModel_thenExceptionIsThrown() throws AsdcCatalogException {
+        assertThat(commandUtils.isVfModuleBaseModule(serviceModelUuid, instanceModelInfo), equalTo(expected));
+    }
 
+    @Test
+    void isVfModuleBaseModule_whenCantFindVfModulesInModel_resultIsFalse() {
         String serviceModelUuid = UUID.randomUUID().toString();
 
         ServiceModel mockedServiceModel = mock(ServiceModel.class);
-        Map<String, VfModule> emptyMap = Collections.emptyMap();
 
-        when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
-        when(mockedServiceModel.getVfModules()).thenReturn(emptyMap);
+        when(vidService.getServiceModelOrThrow(serviceModelUuid)).thenReturn(mockedServiceModel);
+        when(mockedServiceModel.getVfModules()).thenReturn(emptyMap());
 
-        commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
+        assertThat(
+            commandUtils.isVfModuleBaseModule(serviceModelUuid, mock(ModelInfo.class)), is(false));
     }
 }