2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.vid.services;
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;
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;
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;
63 public class InstantiationTemplatesServiceTest {
66 private AsyncInstantiationRepository asyncInstantiationRepository;
69 private ModelUtil modelUtil;
72 private FeatureManager featureManager;
75 private InstantiationTemplatesService instantiationTemplatesService;
78 public void initMocks() {
79 TestUtils.initMockitoMocks(this);
83 public void resetMocks() {
84 reset(featureManager);
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);
94 instantiationTemplatesService.getJobRequestAsTemplate(jobId);
96 verify(asyncInstantiationRepository).getJobRequest(jobId);
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());
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
113 // only vnf will have a non-empty value
114 when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("1", mock(Vnf.class)));
117 ServiceInstantiationTemplate result = instantiationTemplatesService.getJobRequestAsTemplate(UUID.randomUUID());
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()));
126 public static Object[][] isTemplatesExistsByGivenServiceUuid() {
127 return new Object[][]{{"1",TRUE},
131 @Test(dataProvider = "isTemplatesExistsByGivenServiceUuid")
132 public void setInServicesTemplateValue_givenServiceWithServiceModelId_thenIsTemplateExistsIsEatherTrueOrFalse(String givenUuid, Boolean expectedTemplatesExist){
134 Service service = new Service();
135 service.setUuid(givenUuid);
137 Service newService = instantiationTemplatesService.setTemplateExistForService(service, ImmutableSet.of("1", "2"));
138 assertThat(newService.getIsInstantiationTemplateExists(), is(expectedTemplatesExist));
142 public void setTemplatesExistance_givenCollection__flagIsActive_thenSameCollectionReturnedWithTemplateExistsProperty(){
143 when(featureManager.isActive(Features.FLAG_2004_INSTANTIATION_TEMPLATES_POPUP)).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)))
153 public void setTemplatesExistance_givenCollection_flagIsNotActive_thenTemplatesExistNotAdded(){
154 when(featureManager.isActive(Features.FLAG_2004_INSTANTIATION_TEMPLATES_POPUP)).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)))
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);