Merge from ECOMP's repository
[vid.git] / vid-app-common / src / test / java / org / onap / vid / job / command / CommandUtilsTest.java
1 package org.onap.vid.job.command;
2
3 import com.google.common.collect.ImmutableMap;
4 import org.mockito.InjectMocks;
5 import org.mockito.Mock;
6 import org.mockito.MockitoAnnotations;
7 import org.onap.vid.asdc.AsdcCatalogException;
8 import org.onap.vid.model.GroupProperties;
9 import org.onap.vid.model.ServiceModel;
10 import org.onap.vid.model.VfModule;
11 import org.onap.vid.services.VidService;
12 import org.testng.annotations.AfterMethod;
13 import org.testng.annotations.BeforeClass;
14 import org.testng.annotations.DataProvider;
15 import org.testng.annotations.Test;
16
17 import java.util.Collections;
18 import java.util.Map;
19 import java.util.UUID;
20
21 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
22 import static org.hamcrest.MatcherAssert.assertThat;
23 import static org.hamcrest.Matchers.equalTo;
24 import static org.mockito.Mockito.*;
25
26 public class CommandUtilsTest {
27
28     @InjectMocks
29     CommandUtils commandUtils;
30
31     @Mock
32     VidService vidService;
33
34     @BeforeClass
35     public void initMocks() {
36         MockitoAnnotations.initMocks(this);
37     }
38
39     @AfterMethod
40     public void resetVidService() {
41         reset(vidService);
42     }
43
44     @DataProvider
45     public static Object[][] trueAndFalse() {
46         return new Object[][]{ {true}, {false} };
47     }
48
49     @Test(dataProvider="trueAndFalse")
50     void testIsVfModelIsBaseModule(boolean isBase) throws AsdcCatalogException {
51         final String serviceModelUuid = UUID.randomUUID().toString();
52         final String vfModuleUuid = UUID.randomUUID().toString();
53
54         ServiceModel mockedServiceModel = mock(ServiceModel.class);
55         VfModule mockedVfModule = mock(VfModule.class);
56         GroupProperties mockedGroupProperties = mock(GroupProperties.class);
57         Map<String, VfModule> vfModulesMap = ImmutableMap.of(randomAlphanumeric(10), mockedVfModule);
58
59         when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
60         when(mockedServiceModel.getVfModules()).thenReturn(vfModulesMap);
61         when(mockedVfModule.getUuid()).thenReturn(vfModuleUuid);
62         when(mockedVfModule.getProperties()).thenReturn(mockedGroupProperties);
63         when(mockedGroupProperties.getBaseModule()).thenReturn(isBase);
64
65         assertThat(commandUtils.isVfModuleBaseModule(serviceModelUuid, vfModuleUuid), equalTo(isBase));
66     }
67
68     @Test(expectedExceptions = AsdcCatalogException.class)
69     void whenCantFindModelInSdc_thenExceptionIsThrown() throws AsdcCatalogException {
70         String serviceModelUuid = UUID.randomUUID().toString();
71         when(vidService.getService(serviceModelUuid)).thenReturn(null);
72         commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
73     }
74
75     @Test(expectedExceptions = AsdcCatalogException.class)
76     void whenCantFindVfModuleInModel_thenExceptionIsThrown() throws AsdcCatalogException {
77
78         String serviceModelUuid = UUID.randomUUID().toString();
79
80         ServiceModel mockedServiceModel = mock(ServiceModel.class);
81         Map<String, VfModule> emptyMap = Collections.emptyMap();
82
83         when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
84         when(mockedServiceModel.getVfModules()).thenReturn(emptyMap);
85
86         commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
87     }
88 }