2 * ============LICENSE_START=======================================================
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
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.so.svnfm.simulator.services;
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;
46 public class SvnfmServiceTest {
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;
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);
68 testedObject = new SvnfmService(vnfmRepositoryMock, vnfOperationRepositoryMock, vnfmHelperMock,
69 applicationConfigMock, cacheManagerMock, vnfdsMock, subscriptionServiceMock);
73 public void getVnfSuccessful() {
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));
81 InlineResponse201 result = testedObject.getVnf(VNF_INSTANCE_ID);
83 assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
84 assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
88 public void getVnf_ifCacheNullThenReturnNull() {
90 when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(null);
92 assertThat(testedObject.getVnf("any")).isNull();
96 public void getVnf_ifWrapperNullThenReturnNull() {
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);
102 assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
106 public void getVnf_ifResultIsNullThenReturnNull() {
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);
114 assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
118 public void test_createVnf_usingValidCreateVnfRequest_vnfInstanceCreatedSuccessfully() throws Exception {
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);
127 final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
129 assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
130 assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
134 public void test_createVnf_illegalAccessExceptionThrown_returnsNull() throws Exception {
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());
142 final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
148 public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() {
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);
156 final InlineResponse200 result = testedObject.getOperationStatus(OPERATION_ID);
158 assertThat(result.getId()).isEqualTo(OPERATION_ID);
159 assertThat(result.getOperation()).isEqualTo(operationEnum);
163 public void test_getOperationStatus_usingInvalidOperationId_returnsNull() {
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);
171 InlineResponse200 result = testedObject.getOperationStatus("invalidOperationId");
176 private InlineResponse201 getInlineResponse(String vnfdId, String vnfInstanceName) {
177 InlineResponse201 response201 = new InlineResponse201();
178 response201.setVnfdId(vnfdId);
179 response201.vnfInstanceName(vnfInstanceName);
183 private CreateVnfRequest getCreateVnfRequest() {
184 final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
185 createVnfRequest.setVnfdId(VNFD_ID);
186 createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME);
187 return createVnfRequest;
190 private VnfInstance getVnfInstance() {
191 final VnfInstance vnfInstance = new VnfInstance();
192 vnfInstance.setVnfdId(VNFD_ID);
193 vnfInstance.setVnfInstanceName(VNF_INSTANCE_NAME);
197 private VnfOperation getVnfOperation(String operationId, OperationEnum operationEnum) {
198 final VnfOperation vnfOperation = new VnfOperation();
199 vnfOperation.setId(operationId);
200 vnfOperation.setOperation(operationEnum);