de9fc2bcb69fc3723e0a289a91372fe75ea5bb2a
[vid.git] / vid-app-common / src / test / java / org / onap / vid / services / InstantiationTemplatesServiceTest.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.services;
22
23 import static java.lang.Boolean.FALSE;
24 import static java.lang.Boolean.TRUE;
25 import static net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
26 import static org.hamcrest.CoreMatchers.is;
27 import static org.hamcrest.MatcherAssert.assertThat;
28 import static org.hamcrest.Matchers.allOf;
29 import static org.hamcrest.Matchers.anEmptyMap;
30 import static org.hamcrest.Matchers.containsInAnyOrder;
31 import static org.hamcrest.Matchers.hasProperty;
32 import static org.hamcrest.Matchers.nullValue;
33 import static org.mockito.ArgumentMatchers.any;
34 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
35 import static org.mockito.Mockito.doReturn;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.reset;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
40
41 import com.google.common.collect.ImmutableList;
42 import com.google.common.collect.ImmutableMap;
43 import com.google.common.collect.ImmutableSet;
44 import java.util.Collection;
45 import java.util.Map;
46 import java.util.UUID;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.onap.vid.asdc.beans.Service;
50 import org.onap.vid.dal.AsyncInstantiationRepository;
51 import org.onap.vid.model.ModelUtil;
52 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
53 import org.onap.vid.model.serviceInstantiation.ServiceInstantiationTemplate;
54 import org.onap.vid.model.serviceInstantiation.Vnf;
55 import org.onap.vid.properties.Features;
56 import org.onap.vid.testUtils.TestUtils;
57 import org.testng.annotations.AfterMethod;
58 import org.testng.annotations.BeforeMethod;
59 import org.testng.annotations.DataProvider;
60 import org.testng.annotations.Test;
61 import org.togglz.core.manager.FeatureManager;
62
63 public class InstantiationTemplatesServiceTest {
64
65     @Mock
66     private AsyncInstantiationRepository asyncInstantiationRepository;
67
68     @Mock
69     private ModelUtil modelUtil;
70
71     @Mock
72     private FeatureManager featureManager;
73
74     @InjectMocks
75     private InstantiationTemplatesService instantiationTemplatesService;
76
77     @BeforeMethod
78     public void initMocks() {
79         TestUtils.initMockitoMocks(this);
80     }
81
82     @AfterMethod
83     public void resetMocks() {
84         reset(featureManager);
85     }
86
87     @Test
88     public void getJobRequestAsTemplate_whenIsCalled_asyncInstantiationRepositoryGetJobRequestIsInvoked() {
89         UUID jobId = UUID.randomUUID();
90         ServiceInstantiation serviceInstantiationMock = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
91         doReturn(serviceInstantiationMock).when(asyncInstantiationRepository).getJobRequest(jobId);
92
93         // When...
94         instantiationTemplatesService.getJobRequestAsTemplate(jobId);
95
96         verify(asyncInstantiationRepository).getJobRequest(jobId);
97     }
98
99     @Test
100     public void getJobRequestAsTemplate_givenModelUtilReturnsValue_thenVnfCounterMapIsPopulatedWithThatValue() {
101         Map<String, Integer> dummyNonEmptyMap = ImmutableMap.of("dummyKey", 9);
102         ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
103         doReturn(serviceInstantiation).when(asyncInstantiationRepository).getJobRequest(any());
104
105         // Given...
106         when(modelUtil.getExistingCounterMap(any(), any())).thenAnswer(
107             // return empty counterMap if argument is an empty map; otherwise return a mocked response
108             invocation -> ((Map)invocation.getArgument(0)).size() == 0 // isEmpty() does not work on mocks
109                 ? ImmutableMap.of()
110                 : dummyNonEmptyMap
111         );
112
113         // only vnf will have a non-empty value
114         when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("1", mock(Vnf.class)));
115
116         // When...
117         ServiceInstantiationTemplate result = instantiationTemplatesService.getJobRequestAsTemplate(UUID.randomUUID());
118
119         assertThat(result, hasProperty("existingVNFCounterMap", jsonEquals(dummyNonEmptyMap)));
120         assertThat(result, hasProperty("existingNetworksCounterMap", anEmptyMap()));
121         assertThat(result, hasProperty("existingVnfGroupCounterMap", anEmptyMap()));
122         assertThat(result, hasProperty("existingVRFCounterMap", anEmptyMap()));
123     }
124
125     @DataProvider
126     public static Object[][] isTemplatesExistsByGivenServiceUuid() {
127         return new Object[][]{{"1",TRUE},
128                               {"3",FALSE}};
129     }
130
131     @Test(dataProvider = "isTemplatesExistsByGivenServiceUuid")
132     public void setInServicesTemplateValue_givenServiceWithServiceModelId_thenIsTemplateExistsIsEatherTrueOrFalse(String givenUuid, Boolean expectedTemplatesExist){
133
134         Service service = new Service();
135         service.setUuid(givenUuid);
136
137         Service newService = instantiationTemplatesService.setTemplateExistForService(service, ImmutableSet.of("1", "2"));
138         assertThat(newService.getIsInstantiationTemplateExists(), is(expectedTemplatesExist));
139     }
140
141     @Test
142     public void setTemplatesExistance_givenCollection__flagIsActive_thenSameCollectionReturnedWithTemplateExistsProperty(){
143         when(featureManager.isActive(Features.FLAG_2004_CREATE_ANOTHER_INSTANCE_FROM_TEMPLATE)).thenReturn(true);
144         when(asyncInstantiationRepository.getAllTemplatesServiceModelIds()).thenReturn(ImmutableSet.of("1", "2"));
145         Collection<Service> actualCollection = instantiationTemplatesService.setOnEachServiceIsTemplateExists(createGivenCollection());
146         assertThat(actualCollection, containsInAnyOrder(
147             allOf(hasProperty("uuid", is("1")), hasProperty("isInstantiationTemplateExists", is(true))),
148             allOf(hasProperty("uuid", is("3")), hasProperty("isInstantiationTemplateExists", is(false)))
149         ));
150     }
151
152     @Test
153     public void setTemplatesExistance_givenCollection_flagIsNotActive_thenTemplatesExistNotAdded(){
154         when(featureManager.isActive(Features.FLAG_2004_CREATE_ANOTHER_INSTANCE_FROM_TEMPLATE)).thenReturn(false);
155         Collection<Service> actualCollection = instantiationTemplatesService.setOnEachServiceIsTemplateExists(createGivenCollection());
156         assertThat("was " + actualCollection, actualCollection, containsInAnyOrder(
157             allOf(hasProperty("uuid", is("1")), hasProperty("isInstantiationTemplateExists", is(false))),
158             allOf(hasProperty("uuid", is("3")), hasProperty("isInstantiationTemplateExists", is(false)))
159         ));
160     }
161
162     private Collection<Service> createGivenCollection(){
163         Service service1 = new Service();
164         Service service2 = new Service();
165         service1.setUuid("1");
166         service2.setUuid("3");
167         return ImmutableList.of(service1, service2);
168     }
169
170 }