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 / TerminateOperationProgressorTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
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.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.mockito.Mock;
31 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.grant.model.GrantsAddResources;
32 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201;
33 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse200.OperationEnum;
34 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfo;
35 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoResourceHandle;
36 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.InlineResponse201InstantiatedVnfInfoVnfcResourceInfo;
37 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.model.LccnSubscriptionRequest;
38 import org.onap.so.svnfm.simulator.model.Vnfds;
39 import org.onap.so.svnfm.simulator.model.Vnfds.Vnfc;
40 import org.onap.so.svnfm.simulator.model.Vnfds.Vnfd;
41 import org.onap.so.svnfm.simulator.config.ApplicationConfig;
42 import org.onap.so.svnfm.simulator.model.VnfOperation;
43
44 public class TerminateOperationProgressorTest {
45
46     private static final String VNFD_ID = "vnfdId";
47     private static final String VNFC_TYPE = "COMPUTE";
48     private static final String VDU_ID = "vduId";
49     private static final String VNF_INSTANCE_ID = "vnfInstanceId";
50
51     @Mock
52     private SvnfmService svnfmService;
53
54     private TerminateOperationProgressor testedObject;
55
56     @Before
57     public void setup() {
58         svnfmService = mock(SvnfmService.class);
59         VnfOperation vnfOperation = new VnfOperation();
60         vnfOperation.setVnfInstanceId(VNF_INSTANCE_ID);
61         vnfOperation.setOperation(OperationEnum.OPERATE);
62         testedObject = new TerminateOperationProgressor(vnfOperation, svnfmService, null, new ApplicationConfig(),
63                 createVnfds(), createSubscriptionService());
64     }
65
66     @Test
67     public void test_getAddResources_usingValidVnfdId_returnsEmptyList() {
68         List<GrantsAddResources> results = testedObject.getAddResources(VNFD_ID);
69         assertThat(results.isEmpty());
70     }
71
72     @Test
73     public void test_getRemoveResources_usingValidVnfdId_retrievesGrantsAddResourcesSuccessfully() {
74         // given
75         InlineResponse201 inlineResponse201 = createInlineResponse201();
76         InlineResponse201InstantiatedVnfInfo inlineResponse201InstantiatedVnfInfo =
77                 setupInlineResponseInstantiatedVnfInfo();
78         inlineResponse201.setInstantiatedVnfInfo(inlineResponse201InstantiatedVnfInfo);
79
80         when(svnfmService.getVnf(VNF_INSTANCE_ID)).thenReturn(inlineResponse201);
81
82         // when
83         List<GrantsAddResources> result = testedObject.getRemoveResources(VNFD_ID);
84
85         // then
86         assertThat(result).hasSize(1);
87         GrantsAddResources grantsAddResourcesResult = result.get(0);
88         assertThat(grantsAddResourcesResult.getType()).hasToString(VNFC_TYPE);
89         assertThat(grantsAddResourcesResult.getVduId()).isEqualTo(VDU_ID);
90     }
91
92     @Test
93     public void test_getVnfcChangeType_isEnumRemoved() {
94         assertThat(testedObject.getVnfcChangeType().getValue()).isEqualTo("REMOVED");
95     }
96
97     private Vnfds createVnfds() {
98         Vnfd vnfd = new Vnfd();
99         vnfd.setVnfdId(VNFD_ID);
100         List<Vnfc> vnfcList = new ArrayList<>();
101         vnfcList.add(createVnfc());
102         vnfd.setVnfcList(vnfcList);
103         List<Vnfd> vnfdList = new ArrayList<>();
104         vnfdList.add(vnfd);
105
106         Vnfds vnfds = new Vnfds();
107         vnfds.setVnfdList(vnfdList);
108         return vnfds;
109     }
110
111     private Vnfc createVnfc() {
112         Vnfc vnfc = new Vnfc();
113         String vnfcId = "vnfcId";
114         vnfc.setVnfcId(vnfcId);
115         vnfc.setType(VNFC_TYPE);
116         String resourceTemplateId = "resTempId";
117         vnfc.setResourceTemplateId(resourceTemplateId);
118         vnfc.setVduId(VDU_ID);
119         String resourceDefinitionId = "resourceDefinitionId";
120         vnfc.setGrantResourceId(resourceDefinitionId);
121         return vnfc;
122     }
123
124     private SubscriptionService createSubscriptionService() {
125         SubscriptionService subscriptionService = new SubscriptionService();
126         LccnSubscriptionRequest lccnSubscriptionRequest = new LccnSubscriptionRequest();
127         String callbackUri = "/lcn/uriest";
128         lccnSubscriptionRequest.setCallbackUri(callbackUri);
129         subscriptionService.registerSubscription(lccnSubscriptionRequest);
130         return subscriptionService;
131     }
132
133     private InlineResponse201 createInlineResponse201() {
134         InlineResponse201 inlineResponse201 = new InlineResponse201();
135         inlineResponse201.setVnfdId(VNFD_ID);
136         return inlineResponse201;
137     }
138
139     private InlineResponse201InstantiatedVnfInfo setupInlineResponseInstantiatedVnfInfo() {
140         InlineResponse201InstantiatedVnfInfo inlineResponse201InstantiatedVnfInfo =
141                 new InlineResponse201InstantiatedVnfInfo();
142         List<InlineResponse201InstantiatedVnfInfoVnfcResourceInfo> resultList = new ArrayList<>();
143         InlineResponse201InstantiatedVnfInfoVnfcResourceInfo resourceInfo =
144                 new InlineResponse201InstantiatedVnfInfoVnfcResourceInfo();
145         String resourceInfoId = "resourceInfoId";
146         resourceInfo.setId(resourceInfoId);
147         resourceInfo.setVduId((VDU_ID));
148         InlineResponse201InstantiatedVnfInfoResourceHandle resourceHandle =
149                 new InlineResponse201InstantiatedVnfInfoResourceHandle();
150         resourceInfo.setComputeResource(resourceHandle);
151         resultList.add(resourceInfo);
152         inlineResponse201InstantiatedVnfInfo.setVnfcResourceInfo(resultList);
153         String flavourId = "flavourId";
154         inlineResponse201InstantiatedVnfInfo.setFlavourId(flavourId);
155         return inlineResponse201InstantiatedVnfInfo;
156     }
157 }