Move the VNFM Simulator into CSIT
[integration/csit.git] / plans / so / integration-etsi-testing / so-simulators / vnfm-simulator / vnfm-service / src / test / java / org / onap / so / svnfm / simulator / services / SvnfmServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP - SO
4  * ================================================================================
5  * Copyright (C) 2019 Nokia.
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.so.svnfm.simulator.services;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 import java.util.ArrayList;
28 import java.util.List;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
35 import org.onap.so.svnfm.simulator.model.Vnfds;
36 import org.onap.so.svnfm.simulator.config.ApplicationConfig;
37 import org.onap.so.svnfm.simulator.constants.Constant;
38 import org.onap.so.svnfm.simulator.model.VnfInstance;
39 import org.onap.so.svnfm.simulator.model.VnfOperation;
40 import org.onap.so.svnfm.simulator.repository.VnfOperationRepository;
41 import org.onap.so.svnfm.simulator.repository.VnfmRepository;
42 import org.springframework.cache.Cache;
43 import org.springframework.cache.CacheManager;
44 import org.springframework.cache.support.SimpleValueWrapper;
45
46 public class SvnfmServiceTest {
47
48     private static final String VNFD_ID = "vnfdId";
49     private static final String VNF_INSTANCE_NAME = "vnfInstanceName";
50     private static final String VNF_INSTANCE_ID = "vnfInstanceId";
51     private static final String OPERATION_ID = "operationId";
52     private SvnfmService testedObject;
53     private CacheManager cacheManagerMock;
54     private VnfmHelper vnfmHelperMock;
55     private VnfmRepository vnfmRepositoryMock;
56     private VnfOperationRepository vnfOperationRepositoryMock;
57
58     @Before
59     public void setup() {
60         vnfmRepositoryMock = mock(VnfmRepository.class);
61         vnfOperationRepositoryMock = mock(VnfOperationRepository.class);
62         vnfmHelperMock = mock(VnfmHelper.class);
63         ApplicationConfig applicationConfigMock = mock(ApplicationConfig.class);
64         cacheManagerMock = mock(CacheManager.class);
65         Vnfds vnfdsMock = mock(Vnfds.class);
66         SubscriptionService subscriptionServiceMock = mock(SubscriptionService.class);
67
68         testedObject = new SvnfmService(vnfmRepositoryMock, vnfOperationRepositoryMock, vnfmHelperMock,
69                 applicationConfigMock, cacheManagerMock, vnfdsMock, subscriptionServiceMock);
70     }
71
72     @Test
73     public void getVnfSuccessful() {
74         // given
75         Cache cacheMock = mock(Cache.class);
76         SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class);
77         when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
78         when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock);
79         when(simpleValueWrapperMock.get()).thenReturn(getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME));
80         // when
81         InlineResponse201 result = testedObject.getVnf(VNF_INSTANCE_ID);
82         // then
83         assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
84         assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
85     }
86
87     @Test
88     public void getVnf_ifCacheNullThenReturnNull() {
89         // given
90         when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(null);
91         // then
92         assertThat(testedObject.getVnf("any")).isNull();
93     }
94
95     @Test
96     public void getVnf_ifWrapperNullThenReturnNull() {
97         // given
98         Cache cacheMock = mock(Cache.class);
99         when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
100         when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(null);
101         // then
102         assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
103     }
104
105     @Test
106     public void getVnf_ifResultIsNullThenReturnNull() {
107         // when
108         Cache cacheMock = mock(Cache.class);
109         SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class);
110         when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
111         when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock);
112         when(simpleValueWrapperMock.get()).thenReturn(null);
113         // then
114         assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
115     }
116
117     @Test
118     public void test_createVnf_usingValidCreateVnfRequest_vnfInstanceCreatedSuccessfully() throws Exception {
119         // given
120         final CreateVnfRequest createVnfRequest = getCreateVnfRequest();
121         final VnfInstance vnfInstance = getVnfInstance();
122         final InlineResponse201 inlineResponse201 = getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME);
123         when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance);
124         when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance);
125         when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenReturn(inlineResponse201);
126         // when
127         final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
128         // then
129         assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
130         assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
131     }
132
133     @Test
134     public void test_createVnf_illegalAccessExceptionThrown_returnsNull() throws Exception {
135         // given
136         final CreateVnfRequest createVnfRequest = getCreateVnfRequest();
137         final VnfInstance vnfInstance = getVnfInstance();
138         when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance);
139         when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance);
140         when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenThrow(new IllegalAccessException());
141         // when
142         final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
143         // then
144         assertNull(result);
145     }
146
147     @Test
148     public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() {
149         // given
150         final OperationEnum operationEnum = OperationEnum.OPERATE;
151         final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum);
152         final List<VnfOperation> vnfOperations = new ArrayList<>();
153         vnfOperations.add(vnfOperation);
154         when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations);
155         // when
156         final InlineResponse200 result = testedObject.getOperationStatus(OPERATION_ID);
157         // then
158         assertThat(result.getId()).isEqualTo(OPERATION_ID);
159         assertThat(result.getOperation()).isEqualTo(operationEnum);
160     }
161
162     @Test
163     public void test_getOperationStatus_usingInvalidOperationId_returnsNull() {
164         // given
165         final OperationEnum operationEnum = OperationEnum.OPERATE;
166         final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum);
167         final List<VnfOperation> vnfOperations = new ArrayList<>();
168         vnfOperations.add(vnfOperation);
169         when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations);
170         // when
171         InlineResponse200 result = testedObject.getOperationStatus("invalidOperationId");
172         // then
173         assertNull(result);
174     }
175
176     private InlineResponse201 getInlineResponse(String vnfdId, String vnfInstanceName) {
177         InlineResponse201 response201 = new InlineResponse201();
178         response201.setVnfdId(vnfdId);
179         response201.vnfInstanceName(vnfInstanceName);
180         return response201;
181     }
182
183     private CreateVnfRequest getCreateVnfRequest() {
184         final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
185         createVnfRequest.setVnfdId(VNFD_ID);
186         createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME);
187         return createVnfRequest;
188     }
189
190     private VnfInstance getVnfInstance() {
191         final VnfInstance vnfInstance = new VnfInstance();
192         vnfInstance.setVnfdId(VNFD_ID);
193         vnfInstance.setVnfInstanceName(VNF_INSTANCE_NAME);
194         return vnfInstance;
195     }
196
197     private VnfOperation getVnfOperation(String operationId, OperationEnum operationEnum) {
198         final VnfOperation vnfOperation = new VnfOperation();
199         vnfOperation.setId(operationId);
200         vnfOperation.setOperation(operationEnum);
201         return vnfOperation;
202     }
203 }