Merge changes from topics "VID-45", "VID-44"
[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 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;
33
34 import com.google.common.collect.ImmutableMap;
35 import java.util.Map;
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;
47
48 public class InstantiationTemplatesServiceTest {
49
50     @Mock
51     private AsyncInstantiationRepository asyncInstantiationRepository;
52
53     @Mock
54     private ModelUtil modelUtil;
55
56     @InjectMocks
57     private InstantiationTemplatesService instantiationTemplatesService;
58
59     @BeforeMethod
60     public void initMocks() {
61         TestUtils.initMockitoMocks(this);
62     }
63
64     @Test
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);
69
70         // When...
71         instantiationTemplatesService.getJobRequestAsTemplate(jobId);
72
73         verify(asyncInstantiationRepository).getJobRequest(jobId);
74     }
75
76     @Test
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());
81
82         // Given...
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
86                 ? ImmutableMap.of()
87                 : dummyNonEmptyMap
88         );
89
90         // only vnf will have a non-empty value
91         when(serviceInstantiation.getVnfs()).thenReturn(ImmutableMap.of("1", mock(Vnf.class)));
92
93         // When...
94         ServiceInstantiationTemplate result = instantiationTemplatesService.getJobRequestAsTemplate(UUID.randomUUID());
95
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()));
100     }
101
102 }