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.svnfm.simulator.services;
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.ArgumentMatchers.anyString;
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.mockito.Mockito;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantRequest;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201AddResources;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum;
38 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo;
39 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
40 import org.onap.svnfm.simulator.config.ApplicationConfig;
41 import org.onap.svnfm.simulator.model.VnfOperation;
42 import org.onap.svnfm.simulator.model.Vnfds;
43 import org.onap.svnfm.simulator.model.Vnfds.Vnfc;
44 import org.onap.svnfm.simulator.model.Vnfds.Vnfd;
46 public class InstantiateOperatorProgressorTest {
48 private static final String VNF_ID = "vnfTestId";
49 private static final String CALLBACK_URI = "/lcn/uritest";
50 private static final String VNFC_TYPE = "COMPUTE";
51 private static final String RESOURCE_TEMPLATE_ID = "resTempIdTest";
52 private static final String VDU_ID = "vduIdTest";
53 private static final String VNF_INSTANCE_ID = "vnfInstanceId";
54 private static final String VNFC_ID = "vnfcIdTest";
55 private static final String RESOURCE_DEFINITION_ID = "resDefTestId";
56 private static final String VIM_CONNECTION_ID = "vimConnTestId";
58 private SvnfmService svnfmServiceMock;
60 private InstantiateOperationProgressor testedObject;
64 svnfmServiceMock = mock(SvnfmService.class);
65 VnfOperation vnfOperation = new VnfOperation();
66 vnfOperation.setVnfInstanceId(VNF_INSTANCE_ID);
67 vnfOperation.setOperation(OperationEnum.OPERATE);
68 testedObject = new InstantiateOperationProgressor(vnfOperation, svnfmServiceMock, null, new ApplicationConfig(),
69 createVnfds(), createSubscriptionService());
73 public void getAddResources_vnfIdFound() {
74 List<GrantsAddResources> result = testedObject.getAddResources(VNF_ID);
75 assertThat(result).hasSize(1);
76 GrantsAddResources grantsAddResourceResult = result.get(0);
77 assertThat(grantsAddResourceResult.getType()).hasToString(VNFC_TYPE);
78 assertThat(grantsAddResourceResult.getResourceTemplateId()).isEqualTo(RESOURCE_TEMPLATE_ID);
79 assertThat(grantsAddResourceResult.getVduId()).isEqualTo(VDU_ID);
83 public void getAddResources_vnfIdNotFound() {
84 List<GrantsAddResources> result = testedObject.getAddResources("otherVnfId");
85 assertThat(result).isEmpty();
89 public void handleGrantResponse_VnfdObjectsAvailable() {
90 when(svnfmServiceMock.getVnf(VNF_INSTANCE_ID)).thenReturn(createInlineResponse201());
92 InlineResponse201VimConnections inlineResponse201VimConnections = new InlineResponse201VimConnections();
93 List<InlineResponse201VimConnections> listOfVimConnection = new ArrayList<>();
94 listOfVimConnection.add(inlineResponse201VimConnections);
96 InlineResponse201AddResources inlineResponse201AddResources = new InlineResponse201AddResources();
97 inlineResponse201AddResources.setResourceDefinitionId(RESOURCE_DEFINITION_ID);
98 inlineResponse201AddResources.setVimConnectionId(VIM_CONNECTION_ID);
99 List<InlineResponse201AddResources> listOfResources = new ArrayList<>();
100 listOfResources.add(inlineResponse201AddResources);
102 InlineResponse201 inlineResponse201 = new InlineResponse201();
103 inlineResponse201.setVimConnections(listOfVimConnection);
104 inlineResponse201.setAddResources(listOfResources);
105 List<InlineResponse201InstantiatedVnfInfoVnfcResourceInfo> resultList =
106 testedObject.handleGrantResponse(inlineResponse201);
108 assertThat(resultList).hasSize(1);
109 InlineResponse201InstantiatedVnfInfoVnfcResourceInfo resultObject = resultList.get(0);
110 assertThat(resultObject.getId()).isEqualTo(VNFC_ID);
111 assertThat(resultObject.getVduId()).isEqualTo(VDU_ID);
112 assertThat(resultObject.getComputeResource().getVimConnectionId()).isEqualTo(VIM_CONNECTION_ID);
116 public void getVnfcChangeType_enumAdded() {
117 assertThat(testedObject.getVnfcChangeType().getValue()).isEqualTo("ADDED");
121 public void getRemoveResourcesShouldReturnEmptyList() {
122 assertThat(testedObject.getRemoveResources("anyVnfId")).isEmpty();
126 public void test_buildGrantRequest_usingValidVnfInstanceId_grantRequestRetrievedSuccessfully() {
127 when(svnfmServiceMock.getVnf(Mockito.eq(VNF_INSTANCE_ID))).thenReturn(createInlineResponse201());
128 GrantRequest grantRequest = testedObject.buildGrantRequest();
129 assertThat(grantRequest.getVnfdId().equals(VNF_ID));
132 private Vnfds createVnfds() {
133 Vnfd vnfd = new Vnfd();
134 vnfd.setVnfdId(VNF_ID);
135 List<Vnfc> vnfcList = new ArrayList<>();
136 vnfcList.add(createVnfc());
137 vnfd.setVnfcList(vnfcList);
138 List<Vnfd> vnfdList = new ArrayList<>();
141 Vnfds vnfds = new Vnfds();
142 vnfds.setVnfdList(vnfdList);
146 private Vnfc createVnfc() {
147 Vnfc vnfc = new Vnfc();
148 vnfc.setVnfcId(VNFC_ID);
149 vnfc.setType(VNFC_TYPE);
150 vnfc.setResourceTemplateId(RESOURCE_TEMPLATE_ID);
151 vnfc.setVduId(VDU_ID);
152 vnfc.setGrantResourceId(RESOURCE_DEFINITION_ID);
156 private SubscriptionService createSubscriptionService() {
157 SubscriptionService subscriptionService = new SubscriptionService();
158 LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest();
159 lccnSubscriptionRequest.setCallbackUri(CALLBACK_URI);
160 subscriptionService.registerSubscription(lccnSubscriptionRequest);
161 return subscriptionService;
164 private org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 createInlineResponse201() {
165 org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 inlineResponse201 =
166 new org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201();
167 inlineResponse201.setVnfdId(VNF_ID);
168 return inlineResponse201;