aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/upgrade/UpgradeBusinessLogicTest.java
blob: ea9095fd114152eeb9d1e27d035938ff2a659825 (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
/*
 * -
 *  * ============LICENSE_START=======================================================
 *  *  Copyright (C) 2019  Nordix Foundation.
 *  * ================================================================================
 *  * 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.
 *  *
 *  * SPDX-License-Identifier: Apache-2.0
 *  * ============LICENSE_END=========================================================
 *
 */

package org.openecomp.sdc.be.components.upgrade;

import fj.data.Either;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
import org.openecomp.sdc.be.components.lifecycle.LifecycleChangeInfoWithAction;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
import org.openecomp.sdc.be.datatypes.components.ResourceMetadataDataDefinition;
import org.openecomp.sdc.be.datatypes.components.ServiceMetadataDataDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentDependency;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.LifeCycleTransitionEnum;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.ResourceMetadataDefinition;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.ServiceMetadataDefinition;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.jsontitan.operations.UpgradeOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.exception.ResponseFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;


public class UpgradeBusinessLogicTest {

    private static final String COMPONENT_ID = "componentId";
    private static final String USER_ID = "admin123";
    private static final String SERVICE_ID = "service01";
    private static final String RESOURCE_ID = "resource01";

    private User user;
    private Resource resource;
    private Service service;
    private ResourceMetadataDefinition resourceMetadataDefinition;
    private ResourceMetadataDataDefinition resourceMetadataDataDefinition;
    private ServiceMetadataDefinition serviceMetadataDefinition;
    private ServiceMetadataDataDefinition serviceMetadataDataDefinition;

    @Mock
    private LifecycleBusinessLogic lifecycleBusinessLogic;

    @Mock
    private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;

    @Mock
    private UserValidations userValidations;

    @Mock
    private ToscaOperationFacade toscaOperationFacade;

    @Mock
    private ComponentsUtils componentsUtils;

    @Mock
    private UpgradeOperation upgradeOperation;

    @Mock
    private TitanDao titanDao;

    @InjectMocks
    private UpgradeBusinessLogic upgradeBusinessLogic;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);

        upgradeBusinessLogic = new UpgradeBusinessLogic(lifecycleBusinessLogic, componentInstanceBusinessLogic,
                userValidations, toscaOperationFacade, componentsUtils, upgradeOperation, titanDao);

        user = new User();
        user.setRole(Role.ADMIN.name());
        user.setUserId(USER_ID);

        resourceMetadataDataDefinition = new ResourceMetadataDataDefinition();
        resourceMetadataDefinition = new ResourceMetadataDefinition(resourceMetadataDataDefinition);
        serviceMetadataDataDefinition = new ServiceMetadataDataDefinition();
        serviceMetadataDefinition = new ServiceMetadataDefinition(serviceMetadataDataDefinition);

        resource = new Resource(resourceMetadataDefinition);
        service = new Service(serviceMetadataDefinition);

        when(userValidations.validateUserExists(eq(user.getUserId()), anyString(), anyBoolean()))
                .thenReturn(user);
    }

    @Test
    public void testAutomatedUpgrade_givenValidResource_returnsSuccessful() {

        resourceMetadataDataDefinition.setHighestVersion(true);
        resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
        resourceMetadataDataDefinition.setComponentType(ComponentTypeEnum.RESOURCE);
        resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.VF);

        when(toscaOperationFacade.getToscaFullElement(anyString()))
                .thenReturn(Either.left(resource));

        UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
        assertEquals(ActionStatus.OK, automatedUpgradeStatus.getStatus());
    }

    @Test
    public void testAutomatedUpgrade_givenResourceIsNotVFType_returnsUnsuccessful() {
        resourceMetadataDataDefinition.setResourceType(ResourceTypeEnum.PNF);
        resourceMetadataDataDefinition.setHighestVersion(true);
        resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
        when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
                .thenReturn(Either.left(resource));

        UpgradeStatus automatedUpgradeStatus = upgradeBusinessLogic.automatedUpgrade(
                COMPONENT_ID, getRequests(), user.getUserId());

        assertEquals("Status should be GENERAL Error ", ActionStatus.GENERAL_ERROR,
                automatedUpgradeStatus.getStatus());
    }

    @Test
    public void testAutomatedUpgrade_givenResourceVersionIsNotHighest_thenReturnsError() {
        ResponseFormat responseFormat = new ResponseFormat();
        responseFormat.setStatus(001);

        resourceMetadataDataDefinition.setHighestVersion(false);

        when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
                .thenReturn(Either.left(resource));
        when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_NOT_HIHGEST_CERTIFIED), any()))
                .thenReturn(responseFormat);

        UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());

        assertEquals(responseFormat.getStatus(), status.getError().getStatus());
    }

    @Test
    public void testAutomatedUpgrade_givenInvalidResourceId_thenReturnsError() {

        String invalidResourceId = "invalidResourceId";
        ResponseFormat stubResponseFormat = new ResponseFormat();
        stubResponseFormat.setStatus(001);

        when(toscaOperationFacade.getToscaFullElement(invalidResourceId))
                .thenReturn(Either.right(StorageOperationStatus.GENERAL_ERROR));
        when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.GENERAL_ERROR))
                .thenReturn(ActionStatus.RESOURCE_NOT_FOUND);

        when(componentsUtils.getResponseFormatByResource(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString()))
                .thenReturn(stubResponseFormat);


        UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(invalidResourceId,
                null, user.getUserId());
        assertEquals(stubResponseFormat.getStatus(), status.getError().getStatus());
    }

    @Test
    public void testAutomatedUpgrade_givenResourceIsArchived_thenReturnsError() {
        ResponseFormat responseFormat = new ResponseFormat();
        responseFormat.setStatus(001);

        resourceMetadataDataDefinition.setHighestVersion(true);
        resourceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
        resourceMetadataDataDefinition.setArchived(true);

        when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID))
                .thenReturn(Either.left(resource));
        when(componentsUtils.getResponseFormat(eq(ActionStatus.COMPONENT_IS_ARCHIVED), any()))
                .thenReturn(responseFormat);

        UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, null, user.getUserId());

        assertEquals(responseFormat.getStatus(), status.getError().getStatus());
    }

    @Test
    public void testAutomatedUpgrade_givenService_thenReturnsSuccessful() {
        user.setRole(Role.TESTER.name());

        List<String> stubInstanceIdsList = new ArrayList<>();

        List<ComponentInstance> stubComponentInstanceList = new ArrayList<>();
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setToscaPresentationValue(JsonPresentationFields.CI_IS_PROXY, Boolean.TRUE);
        componentInstance.setSourceModelUid("sm1");
        stubComponentInstanceList.add(componentInstance);

        List<ComponentInstanceProperty> stubComponentPropertyList = new ArrayList<>();
        ComponentInstanceProperty componentInstanceProperty = new ComponentInstanceProperty();
        stubComponentPropertyList.add(componentInstanceProperty);

        Map<String, List<ComponentInstanceProperty>> stubComponentInstanceProperties = new HashMap<>();
        stubComponentInstanceProperties.put("prop1", stubComponentPropertyList);
        service.setComponentInstancesProperties(stubComponentInstanceProperties);
        service.setComponentInstances(stubComponentInstanceList);

        serviceMetadataDataDefinition.setHighestVersion(true);
        serviceMetadataDataDefinition.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
        serviceMetadataDataDefinition.setComponentType(ComponentTypeEnum.SERVICE);
        serviceMetadataDataDefinition.setUniqueId("sUniqueId1");
        serviceMetadataDataDefinition.setInvariantUUID("iid");
        serviceMetadataDataDefinition.setVersion("1.0");


        ServiceMetadataDataDefinition serviceMetadataDataDefinition2 = new ServiceMetadataDataDefinition();
        serviceMetadataDataDefinition2.setLifecycleState(LifecycleStateEnum.CERTIFIED.name());
        serviceMetadataDataDefinition2.setComponentType(ComponentTypeEnum.SERVICE);
        serviceMetadataDataDefinition2.setUniqueId("sUniqueId2");
        serviceMetadataDataDefinition2.setInvariantUUID("iid");
        serviceMetadataDataDefinition2.setVersion("2.0");

        Service service2 = new Service(new ServiceMetadataDefinition(serviceMetadataDataDefinition2));

        when(toscaOperationFacade.getToscaFullElement(anyString()))
               .thenReturn(Either.left(service));

        when(toscaOperationFacade.getToscaElement(RESOURCE_ID))
                .thenReturn(Either.left(service));
        when(toscaOperationFacade.getToscaElement(eq(componentInstance.getSourceModelUid()), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service2));

        doReturn(Either.left(service)).when(lifecycleBusinessLogic).changeComponentState(
                any(ComponentTypeEnum.class), any(), any(User.class),
                any(LifeCycleTransitionEnum.class), any(LifecycleChangeInfoWithAction.class), anyBoolean(), anyBoolean());

        when(upgradeOperation.getInstanceIdFromAllottedEdge(any(), any()))
                .thenReturn(stubInstanceIdsList);
        when(toscaOperationFacade.updateComponentInstancePropsToComponent(any(Map.class), any()))
                .thenReturn(Either.left(stubComponentInstanceProperties));
        when(componentInstanceBusinessLogic.changeInstanceVersion(any(Component.class), any(ComponentInstance.class), any(ComponentInstance.class), any(User.class), any(ComponentTypeEnum.class)))
                .thenReturn(Either.left(componentInstance));

        UpgradeStatus status = upgradeBusinessLogic.automatedUpgrade(COMPONENT_ID, getRequests(), user.getUserId());
        Assert.assertEquals(ActionStatus.OK, status.getStatus());
    }

    @Test
    public void testGetComponentDependencies_givenValidComponentId_thenReturnsSuccessful() {
        String componentId = "componentId";
        List<ComponentDependency> stubComponentDependencies
                = new ArrayList<>();
        stubComponentDependencies.add(new ComponentDependency());

        when(upgradeOperation.getComponentDependencies(componentId))
                .thenReturn(Either.left(stubComponentDependencies));

        assertEquals("should contain one component dependency", 1,
                upgradeBusinessLogic.getComponentDependencies(componentId, user.getUserId()).left().value().size());
    }

    private List<UpgradeRequest> getRequests() {
        List<UpgradeRequest> stubRequests = new ArrayList<>();
        UpgradeRequest request1 = new UpgradeRequest();
        request1.setServiceId(SERVICE_ID);
        request1.setResourceId(RESOURCE_ID);
        stubRequests.add(request1);
        return stubRequests;
    }
}