46030e94d3760419cc6964c79ff6c933bef1e325
[so.git] /
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.svnfm.simulator.services;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
26 import java.util.ArrayList;
27 import java.util.List;
28 import org.junit.Before;
29 import org.junit.Test;
30 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201AddResources;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.InlineResponse201VimConnections;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
36 import org.onap.svnfm.simulator.config.ApplicationConfig;
37 import org.onap.svnfm.simulator.model.VnfOperation;
38 import org.onap.svnfm.simulator.model.Vnfds;
39 import org.onap.svnfm.simulator.model.Vnfds.Vnfc;
40 import org.onap.svnfm.simulator.model.Vnfds.Vnfd;
41
42 public class InstantiateOperatorProgressorTest {
43
44     private static final String VNF_ID = "vnfTestId";
45     private static final String CALLBACK_URI = "/lcn/uritest";
46     private static final String VNFC_TYPE = "COMPUTE";
47     private static final String RESOURCE_TEMPLATE_ID = "resTempIdTest";
48     private static final String VDU_ID = "vduIdTest";
49     private static final String VNF_INSTANCE_ID = "vnfInstanceId";
50     private static final String VNFC_ID = "vnfcIdTest";
51     private static final String RESOURCE_DEFINITION_ID = "resDefTestId";
52     private static final String VIM_CONNECTION_ID = "vimConnTestId";
53
54     private SvnfmService svnfmServiceMock;
55
56     private InstantiateOperationProgressor testedObject;
57
58     @Before
59     public void setup() {
60         svnfmServiceMock = mock(SvnfmService.class);
61         VnfOperation vnfOperation = new VnfOperation();
62         vnfOperation.setVnfInstanceId(VNF_INSTANCE_ID);
63         testedObject = new InstantiateOperationProgressor(vnfOperation, svnfmServiceMock, null, new ApplicationConfig(),
64                 createVnfds(), createSubscriptionService());
65     }
66
67     @Test
68     public void getAddResources_vnfIdFound() {
69         List<GrantsAddResources> result = testedObject.getAddResources(VNF_ID);
70         assertThat(result).hasSize(1);
71         GrantsAddResources grantsAddResourceResult = result.get(0);
72         assertThat(grantsAddResourceResult.getType()).hasToString(VNFC_TYPE);
73         assertThat(grantsAddResourceResult.getResourceTemplateId()).isEqualTo(RESOURCE_TEMPLATE_ID);
74         assertThat(grantsAddResourceResult.getVduId()).isEqualTo(VDU_ID);
75     }
76
77     @Test
78     public void getAddResources_vnfIdNotFound() {
79         List<GrantsAddResources> result = testedObject.getAddResources("otherVnfId");
80         assertThat(result).isEmpty();
81     }
82
83     @Test
84     public void handleGrantResponse_VnfdObjectsAvailable() {
85         when(svnfmServiceMock.getVnf(VNF_INSTANCE_ID)).thenReturn(createInlineResponse201());
86
87         InlineResponse201VimConnections inlineResponse201VimConnections = new InlineResponse201VimConnections();
88         List<InlineResponse201VimConnections> listOfVimConnection = new ArrayList<>();
89         listOfVimConnection.add(inlineResponse201VimConnections);
90
91         InlineResponse201AddResources inlineResponse201AddResources = new InlineResponse201AddResources();
92         inlineResponse201AddResources.setResourceDefinitionId(RESOURCE_DEFINITION_ID);
93         inlineResponse201AddResources.setVimConnectionId(VIM_CONNECTION_ID);
94         List<InlineResponse201AddResources> listOfResources = new ArrayList<>();
95         listOfResources.add(inlineResponse201AddResources);
96
97         InlineResponse201 inlineResponse201 = new InlineResponse201();
98         inlineResponse201.setVimConnections(listOfVimConnection);
99         inlineResponse201.setAddResources(listOfResources);
100         List<InlineResponse201InstantiatedVnfInfoVnfcResourceInfo> resultList =
101                 testedObject.handleGrantResponse(inlineResponse201);
102
103         assertThat(resultList).hasSize(1);
104         InlineResponse201InstantiatedVnfInfoVnfcResourceInfo resultObject = resultList.get(0);
105         assertThat(resultObject.getId()).isEqualTo(VNFC_ID);
106         assertThat(resultObject.getVduId()).isEqualTo(VDU_ID);
107         assertThat(resultObject.getComputeResource().getVimConnectionId()).isEqualTo(VIM_CONNECTION_ID);
108     }
109
110     @Test
111     public void getVnfcChangeType_enumAdded() {
112         assertThat(testedObject.getVnfcChangeType().getValue()).isEqualTo("ADDED");
113     }
114
115     @Test
116     public void getRemoveResourcesShouldReturnEmptyList() {
117         assertThat(testedObject.getRemoveResources("anyVnfId")).isEmpty();
118     }
119
120     private Vnfds createVnfds() {
121         Vnfd vnfd = new Vnfd();
122         vnfd.setVnfdId(VNF_ID);
123         List<Vnfc> vnfcList = new ArrayList<>();
124         vnfcList.add(createVnfc());
125         vnfd.setVnfcList(vnfcList);
126         List<Vnfd> vnfdList = new ArrayList<>();
127         vnfdList.add(vnfd);
128
129         Vnfds vnfds = new Vnfds();
130         vnfds.setVnfdList(vnfdList);
131         return vnfds;
132     }
133
134     private Vnfc createVnfc() {
135         Vnfc vnfc = new Vnfc();
136         vnfc.setVnfcId(VNFC_ID);
137         vnfc.setType(VNFC_TYPE);
138         vnfc.setResourceTemplateId(RESOURCE_TEMPLATE_ID);
139         vnfc.setVduId(VDU_ID);
140         vnfc.setGrantResourceId(RESOURCE_DEFINITION_ID);
141         return vnfc;
142     }
143
144     private SubscriptionService createSubscriptionService() {
145         SubscriptionService subscriptionService = new SubscriptionService();
146         LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest();
147         lccnSubscriptionRequest.setCallbackUri(CALLBACK_URI);
148         subscriptionService.registerSubscription(lccnSubscriptionRequest);
149         return subscriptionService;
150     }
151
152     private org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 createInlineResponse201() {
153         org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201 inlineResponse201 =
154                 new org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201();
155         inlineResponse201.setVnfdId(VNF_ID);
156         return inlineResponse201;
157     }
158 }