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 net.javacrumbs.jsonunit.JsonMatchers.jsonEquals;
24 import static org.hamcrest.MatcherAssert.assertThat;
25 import static org.hamcrest.Matchers.anEmptyMap;
26 import static org.hamcrest.Matchers.hasProperty;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import com.google.common.collect.ImmutableMap;
36 import java.util.UUID;
37 import org.mockito.InjectMocks;
38 import org.mockito.Mock;
39 import org.onap.vid.dal.AsyncInstantiationRepository;
40 import org.onap.vid.model.ModelUtil;
41 import org.onap.vid.model.serviceInstantiation.ServiceInstantiation;
42 import org.onap.vid.model.serviceInstantiation.ServiceInstantiationTemplate;
43 import org.onap.vid.model.serviceInstantiation.Vnf;
44 import org.onap.vid.testUtils.TestUtils;
45 import org.testng.annotations.BeforeMethod;
46 import org.testng.annotations.Test;
48 public class InstantiationTemplatesServiceTest {
51 private AsyncInstantiationRepository asyncInstantiationRepository;
54 private ModelUtil modelUtil;
57 private InstantiationTemplatesService instantiationTemplatesService;
60 public void initMocks() {
61 TestUtils.initMockitoMocks(this);
65 public void getJobRequestAsTemplate_whenIsCalled_asyncInstantiationRepositoryGetJobRequestIsInvoked() {
66 UUID jobId = UUID.randomUUID();
67 ServiceInstantiation serviceInstantiationMock = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
68 doReturn(serviceInstantiationMock).when(asyncInstantiationRepository).getJobRequest(jobId);
71 instantiationTemplatesService.getJobRequestAsTemplate(jobId);
73 verify(asyncInstantiationRepository).getJobRequest(jobId);
77 public void getJobRequestAsTemplate_givenModelUtilReturnsValue_thenVnfCounterMapIsPopulatedWithThatValue() {
78 Map<String, Integer> dummyNonEmptyMap = ImmutableMap.of("dummyKey", 9);
79 ServiceInstantiation serviceInstantiation = mock(ServiceInstantiation.class, RETURNS_DEEP_STUBS);
80 doReturn(serviceInstantiation).when(asyncInstantiationRepository).getJobRequest(any());
83 when(modelUtil.getExistingCounterMap(any(), any())).thenAnswer(
84 // return empty counterMap if argument is an empty map; otherwise return a mocked response
85 invocation -> ((Map)invocation.getArgument(0)).size() == 0 // isEmpty() does not work on mocks
90 // only vnf will have a non-empty value
91 when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("1", mock(Vnf.class)));
94 ServiceInstantiationTemplate result = instantiationTemplatesService.getJobRequestAsTemplate(UUID.randomUUID());
96 assertThat(result, hasProperty("existingVNFCounterMap", jsonEquals(dummyNonEmptyMap)));
97 assertThat(result, hasProperty("existingNetworksCounterMap", anEmptyMap()));
98 assertThat(result, hasProperty("existingVnfGroupCounterMap", anEmptyMap()));
99 assertThat(result, hasProperty("existingVRFCounterMap", anEmptyMap()));