ee43d1f72f8419a6d285ddb1c75836d3a5dce234
[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 com.google.common.collect.ImmutableMap;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.MockitoAnnotations;
27 import org.onap.vid.asdc.AsdcCatalogException;
28 import org.onap.vid.model.GroupProperties;
29 import org.onap.vid.model.ServiceModel;
30 import org.onap.vid.model.VfModule;
31 import org.onap.vid.services.VidService;
32 import org.testng.annotations.AfterMethod;
33 import org.testng.annotations.BeforeClass;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import java.util.Collections;
38 import java.util.Map;
39 import java.util.UUID;
40
41 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
42 import static org.hamcrest.MatcherAssert.assertThat;
43 import static org.hamcrest.Matchers.equalTo;
44 import static org.mockito.Mockito.*;
45
46 public class CommandUtilsTest {
47
48     @InjectMocks
49     CommandUtils commandUtils;
50
51     @Mock
52     VidService vidService;
53
54     @BeforeClass
55     public void initMocks() {
56         MockitoAnnotations.initMocks(this);
57     }
58
59     @AfterMethod
60     public void resetVidService() {
61         reset(vidService);
62     }
63
64     @DataProvider
65     public static Object[][] trueAndFalse() {
66         return new Object[][]{ {true}, {false} };
67     }
68
69     @Test(dataProvider="trueAndFalse")
70     void testIsVfModelIsBaseModule(boolean isBase) throws AsdcCatalogException {
71         final String serviceModelUuid = UUID.randomUUID().toString();
72         final String vfModuleUuid = UUID.randomUUID().toString();
73
74         ServiceModel mockedServiceModel = mock(ServiceModel.class);
75         VfModule mockedVfModule = mock(VfModule.class);
76         GroupProperties mockedGroupProperties = mock(GroupProperties.class);
77         Map<String, VfModule> vfModulesMap = ImmutableMap.of(randomAlphanumeric(10), mockedVfModule);
78
79         when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
80         when(mockedServiceModel.getVfModules()).thenReturn(vfModulesMap);
81         when(mockedVfModule.getUuid()).thenReturn(vfModuleUuid);
82         when(mockedVfModule.getProperties()).thenReturn(mockedGroupProperties);
83         when(mockedGroupProperties.getBaseModule()).thenReturn(isBase);
84
85         assertThat(commandUtils.isVfModuleBaseModule(serviceModelUuid, vfModuleUuid), equalTo(isBase));
86     }
87
88     @Test(expectedExceptions = AsdcCatalogException.class)
89     void whenCantFindModelInSdc_thenExceptionIsThrown() throws AsdcCatalogException {
90         String serviceModelUuid = UUID.randomUUID().toString();
91         when(vidService.getService(serviceModelUuid)).thenReturn(null);
92         commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
93     }
94
95     @Test(expectedExceptions = AsdcCatalogException.class)
96     void whenCantFindVfModuleInModel_thenExceptionIsThrown() throws AsdcCatalogException {
97
98         String serviceModelUuid = UUID.randomUUID().toString();
99
100         ServiceModel mockedServiceModel = mock(ServiceModel.class);
101         Map<String, VfModule> emptyMap = Collections.emptyMap();
102
103         when(vidService.getService(serviceModelUuid)).thenReturn(mockedServiceModel);
104         when(mockedServiceModel.getVfModules()).thenReturn(emptyMap);
105
106         commandUtils.isVfModuleBaseModule(serviceModelUuid, "abc");
107     }
108 }