aboutsummaryrefslogtreecommitdiffstats
path: root/vnfm-simulator/vnfm-service/src/test/java/org/onap/svnfm/simulator/services/SvnfmServiceTest.java
blob: 2eca37ba6cfdeccdf9a62c07adc0a6c0afa7732f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*-
 * ============LICENSE_START=======================================================
 * ONAP - SO
 * ================================================================================
 * Copyright (C) 2019 Nokia.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.svnfm.simulator.services;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.CreateVnfRequest;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum;
import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
import org.onap.svnfm.simulator.config.ApplicationConfig;
import org.onap.svnfm.simulator.constants.Constant;
import org.onap.svnfm.simulator.model.VnfInstance;
import org.onap.svnfm.simulator.model.VnfOperation;
import org.onap.svnfm.simulator.model.Vnfds;
import org.onap.svnfm.simulator.repository.VnfOperationRepository;
import org.onap.svnfm.simulator.repository.VnfmRepository;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.support.SimpleValueWrapper;

public class SvnfmServiceTest {

    private static final String VNFD_ID = "vnfdId";
    private static final String VNF_INSTANCE_NAME = "vnfInstanceName";
    private static final String VNF_INSTANCE_ID = "vnfInstanceId";
    private static final String OPERATION_ID = "operationId";
    private SvnfmService testedObject;
    private CacheManager cacheManagerMock;
    private VnfmHelper vnfmHelperMock;
    private VnfmRepository vnfmRepositoryMock;
    private VnfOperationRepository vnfOperationRepositoryMock;

    @Before
    public void setup() {
        vnfmRepositoryMock = mock(VnfmRepository.class);
        vnfOperationRepositoryMock = mock(VnfOperationRepository.class);
        vnfmHelperMock = mock(VnfmHelper.class);
        ApplicationConfig applicationConfigMock = mock(ApplicationConfig.class);
        cacheManagerMock = mock(CacheManager.class);
        Vnfds vnfdsMock = mock(Vnfds.class);
        SubscriptionService subscriptionServiceMock = mock(SubscriptionService.class);

        testedObject = new SvnfmService(vnfmRepositoryMock, vnfOperationRepositoryMock, vnfmHelperMock,
                applicationConfigMock, cacheManagerMock, vnfdsMock, subscriptionServiceMock);
    }

    @Test
    public void getVnfSuccessful() {
        // given
        Cache cacheMock = mock(Cache.class);
        SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class);
        when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
        when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock);
        when(simpleValueWrapperMock.get()).thenReturn(getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME));
        // when
        InlineResponse201 result = testedObject.getVnf(VNF_INSTANCE_ID);
        // then
        assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
        assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
    }

    @Test
    public void getVnf_ifCacheNullThenReturnNull() {
        // given
        when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(null);
        // then
        assertThat(testedObject.getVnf("any")).isNull();
    }

    @Test
    public void getVnf_ifWrapperNullThenReturnNull() {
        // given
        Cache cacheMock = mock(Cache.class);
        when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
        when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(null);
        // then
        assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
    }

    @Test
    public void getVnf_ifResultIsNullThenReturnNull() {
        // when
        Cache cacheMock = mock(Cache.class);
        SimpleValueWrapper simpleValueWrapperMock = mock(SimpleValueWrapper.class);
        when(cacheManagerMock.getCache(Constant.IN_LINE_RESPONSE_201_CACHE)).thenReturn(cacheMock);
        when(cacheMock.get(VNF_INSTANCE_ID)).thenReturn(simpleValueWrapperMock);
        when(simpleValueWrapperMock.get()).thenReturn(null);
        // then
        assertThat(testedObject.getVnf(VNF_INSTANCE_ID)).isNull();
    }

    @Test
    public void test_createVnf_usingValidCreateVnfRequest_vnfInstanceCreatedSuccessfully() throws Exception {
        // given
        final CreateVnfRequest createVnfRequest = getCreateVnfRequest();
        final VnfInstance vnfInstance = getVnfInstance();
        final InlineResponse201 inlineResponse201 = getInlineResponse(VNFD_ID, VNF_INSTANCE_NAME);
        when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance);
        when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance);
        when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenReturn(inlineResponse201);
        // when
        final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
        // then
        assertThat(result.getVnfdId()).isEqualTo(VNFD_ID);
        assertThat(result.getVnfInstanceName()).isEqualTo(VNF_INSTANCE_NAME);
    }

    @Test
    public void test_createVnf_illegalAccessExceptionThrown_returnsNull() throws Exception {
        // given
        final CreateVnfRequest createVnfRequest = getCreateVnfRequest();
        final VnfInstance vnfInstance = getVnfInstance();
        when(vnfmHelperMock.createVnfInstance(createVnfRequest, VNF_INSTANCE_ID)).thenReturn(vnfInstance);
        when(vnfmRepositoryMock.save(vnfInstance)).thenReturn(vnfInstance);
        when(vnfmHelperMock.getInlineResponse201(vnfInstance)).thenThrow(new IllegalAccessException());
        // when
        final InlineResponse201 result = testedObject.createVnf(createVnfRequest, VNF_INSTANCE_ID);
        // then
        assertNull(result);
    }

    @Test
    public void test_getOperationStatus_usingValidOperationId_operationStatusRetrievedSuccessfully() {
        // given
        final OperationEnum operationEnum = OperationEnum.OPERATE;
        final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum);
        final List<VnfOperation> vnfOperations = new ArrayList<>();
        vnfOperations.add(vnfOperation);
        when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations);
        // when
        final InlineResponse200 result = testedObject.getOperationStatus(OPERATION_ID);
        // then
        assertThat(result.getId()).isEqualTo(OPERATION_ID);
        assertThat(result.getOperation()).isEqualTo(operationEnum);
    }

    @Test
    public void test_getOperationStatus_usingInvalidOperationId_returnsNull() {
        // given
        final OperationEnum operationEnum = OperationEnum.OPERATE;
        final VnfOperation vnfOperation = getVnfOperation(OPERATION_ID, operationEnum);
        final List<VnfOperation> vnfOperations = new ArrayList<>();
        vnfOperations.add(vnfOperation);
        when(vnfOperationRepositoryMock.findAll()).thenReturn(vnfOperations);
        // when
        InlineResponse200 result = testedObject.getOperationStatus("invalidOperationId");
        // then
        assertNull(result);
    }

    private InlineResponse201 getInlineResponse(String vnfdId, String vnfInstanceName) {
        InlineResponse201 response201 = new InlineResponse201();
        response201.setVnfdId(vnfdId);
        response201.vnfInstanceName(vnfInstanceName);
        return response201;
    }

    private CreateVnfRequest getCreateVnfRequest() {
        final CreateVnfRequest createVnfRequest = new CreateVnfRequest();
        createVnfRequest.setVnfdId(VNFD_ID);
        createVnfRequest.setVnfInstanceName(VNF_INSTANCE_NAME);
        return createVnfRequest;
    }

    private VnfInstance getVnfInstance() {
        final VnfInstance vnfInstance = new VnfInstance();
        vnfInstance.setVnfdId(VNFD_ID);
        vnfInstance.setVnfInstanceName(VNF_INSTANCE_NAME);
        return vnfInstance;
    }

    private VnfOperation getVnfOperation(String operationId, OperationEnum operationEnum) {
        final VnfOperation vnfOperation = new VnfOperation();
        vnfOperation.setId(operationId);
        vnfOperation.setOperation(operationEnum);
        return vnfOperation;
    }
}