aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/PropertyBusinessLogicTest.java
blob: 95e6cceb78b7f3589b79ab2d05c57d960401619b (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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * 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=========================================================
 * Modifications copyright (c) 2019 Nokia
 * ================================================================================
 */

package org.openecomp.sdc.be.components;

import fj.data.Either;
import org.junit.Before;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
import org.openecomp.sdc.be.components.impl.BaseBusinessLogicMock;
import org.openecomp.sdc.be.components.impl.PropertyBusinessLogic;
import org.openecomp.sdc.be.components.validation.UserValidations;
import org.openecomp.sdc.be.config.ConfigurationManager;
import org.openecomp.sdc.be.dao.api.ActionStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.impl.WebAppContextWrapper;
import org.openecomp.sdc.be.model.*;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
import org.openecomp.sdc.be.model.operations.api.IPropertyOperation;
import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
import org.openecomp.sdc.be.user.Role;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.api.Constants;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.openecomp.sdc.exception.ResponseFormat;
import org.openecomp.sdc.test.utils.InterfaceOperationTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import java.lang.reflect.Field;
import java.util.*;

import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.when;

public class PropertyBusinessLogicTest extends BaseBusinessLogicMock {

    private static final Logger log = LoggerFactory.getLogger(PropertyBusinessLogicTest.class);
    @Mock
    private ServletContext servletContext;
    @Mock
    private IPropertyOperation propertyOperation;
    @Mock
    private WebAppContextWrapper webAppContextWrapper;
    @Mock
    private UserBusinessLogic mockUserAdmin;
    @Mock
    private WebApplicationContext webAppContext;
    @Mock
    private ComponentsUtils componentsUtils;
    @Mock
    private ToscaOperationFacade toscaOperationFacade;
    @Mock
    private UserValidations userValidations;
    @Mock
    IGraphLockOperation graphLockOperation;
    @Mock
    JanusGraphDao janusGraphDao;

    @InjectMocks
    private PropertyBusinessLogic bl = new PropertyBusinessLogic(elementDao, groupOperation, groupInstanceOperation,
        groupTypeOperation, interfaceOperation, interfaceLifecycleTypeOperation, artifactToscaOperation);
    private User user = null;
    private String resourceId = "resourceforproperty.0.1";
    private String serviceId = "serviceForProperty.0.1";
    private static final String interfaceType = "interfaceType";
    private static final String operationType = "operationType";
    private static final String operationId = "operationId";

    @Before
    public void setup() {
        MockitoAnnotations.initMocks(this);
        ExternalConfiguration.setAppName("catalog-be");

        // init Configuration
        String appConfigDir = "src/test/resources/config/catalog-be";
        ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
        ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);

        // User data and management
        user = new User();
        user.setUserId("jh003");
        user.setFirstName("Jimmi");
        user.setLastName("Hendrix");
        user.setRole(Role.ADMIN.name());

        Either<User, ActionStatus> eitherGetUser = Either.left(user);
        when(mockUserAdmin.getUser("jh003", false)).thenReturn(eitherGetUser);
        when(userValidations.validateUserExists(eq("jh003"), anyString(), eq(false))).thenReturn(user);

        // Servlet Context attributes
        when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
        when(servletContext.getAttribute(Constants.PROPERTY_OPERATION_MANAGER)).thenReturn(propertyOperation);
        when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
        when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webAppContext);


    }

    @Test
    public void getProperty_propertyNotFound() throws Exception {
        Resource resource = new Resource();
        PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
        PropertyDefinition property2 = createPropertyObject("someProperty2", "myResource");
        resource.setProperties(Arrays.asList(property1, property2));
        String resourceId = "myResource";
        resource.setUniqueId(resourceId);

        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
        Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> nonExistingProperty = bl.getComponentProperty(resourceId, "NonExistingProperty", user.getUserId());
        assertTrue(nonExistingProperty.isRight());
        Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
    }

    @Test
    public void getProperty_propertyNotBelongsToResource() throws Exception {
        Resource resource = new Resource();
        PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");
        resource.setProperties(Arrays.asList(property1));
        String resourceId = "myResource";
        resource.setUniqueId(resourceId);

        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
        Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> notFoundProperty = bl.getComponentProperty(resourceId, "invalidId", user.getUserId());
        assertTrue(notFoundProperty.isRight());
        Mockito.verify(componentsUtils).getResponseFormat(ActionStatus.PROPERTY_NOT_FOUND, "");
    }

    @Test
    public void getProperty() throws Exception {
        Resource resource = new Resource();
        resource.setUniqueId(resourceId);
        PropertyDefinition property1 = createPropertyObject("someProperty", null);
        resource.setProperties(Arrays.asList(property1));

        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));
        Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> foundProperty = bl.getComponentProperty(resourceId, property1.getUniqueId(), user.getUserId());
        assertTrue(foundProperty.isLeft());
        assertEquals(foundProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
    }

    @Test
    public void testGetPropertyFromService() {
        Service service = new Service();
        service.setUniqueId(serviceId);

        PropertyDefinition property1 = createPropertyObject("someProperty", null);
        service.setProperties(Arrays.asList(property1));

        Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
        Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
            bl.getComponentProperty(serviceId, property1.getUniqueId(), user.getUserId());

        assertTrue(serviceProperty.isLeft());
        assertEquals(serviceProperty.left().value().getValue().getUniqueId(), property1.getUniqueId());
    }

    @Test
    public void testPropertyNotFoundOnService() {
        Service service = new Service();
        service.setUniqueId(serviceId);

        PropertyDefinition property1 = createPropertyObject("someProperty", null);
        service.setProperties(Arrays.asList(property1));

        Mockito.when(toscaOperationFacade.getToscaElement(serviceId)).thenReturn(Either.left(service));
        Either<Map.Entry<String, PropertyDefinition>, ResponseFormat> serviceProperty =
            bl.getComponentProperty(serviceId, "notExistingPropId", user.getUserId());

        assertTrue(serviceProperty.isRight());
    }

    @Test
    public void isPropertyUsedByComponentInterface(){
        Service service = new Service();
        service.setUniqueId(serviceId);
        service.setInterfaces(InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType));

        PropertyDefinition propDef1 = new PropertyDefinition();
        propDef1.setUniqueId("ComponentInput1_uniqueId");
        assertTrue(bl.isPropertyUsedByOperation(service, propDef1));

        PropertyDefinition propDef2 = new PropertyDefinition();
        propDef1.setUniqueId("inputId2");
        Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
        assertFalse(bl.isPropertyUsedByOperation(service, propDef2));
    }

    @Test
    public void isPropertyUsedByComponentInstanceInterface(){
        Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
        ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));

        Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
        componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));

        Service service = new Service();
        service.setUniqueId(serviceId);
        service.setComponentInstancesInterfaces(componentInstanceInterfaces);

        PropertyDefinition propDef1 = new PropertyDefinition();
        propDef1.setUniqueId("ComponentInput1_uniqueId");
        assertTrue(bl.isPropertyUsedByOperation(service, propDef1));

        PropertyDefinition propDef2 = new PropertyDefinition();
        propDef1.setUniqueId("inputId2");
        Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
        assertFalse(bl.isPropertyUsedByOperation(service, propDef2));
    }

    @Test
    public void isPropertyUsedByComponentParentComponentInstanceInterface(){
        Map<String, InterfaceDefinition> newInterfaceDefinition = InterfaceOperationTestUtils.createMockInterfaceDefinitionMap(interfaceType, operationId, operationType);
        ComponentInstanceInterface componentInstanceInterface = new ComponentInstanceInterface(interfaceType, newInterfaceDefinition.get(interfaceType));

        Map<String, List<ComponentInstanceInterface>> componentInstanceInterfaces = new HashMap<>();
        componentInstanceInterfaces.put("Test", Arrays.asList(componentInstanceInterface));

        Service parentService = new Service();
        parentService.setComponentInstancesInterfaces(componentInstanceInterfaces);
        Service childService = new Service();
        childService.setUniqueId(serviceId);

        PropertyDefinition propDef1 = new PropertyDefinition();
        propDef1.setUniqueId("ComponentInput1_uniqueId");
        Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(Arrays.asList(parentService)));
        assertTrue(bl.isPropertyUsedByOperation(childService, propDef1));

        PropertyDefinition propDef2 = new PropertyDefinition();
        propDef1.setUniqueId("inputId2");
        Mockito.when(toscaOperationFacade.getParentComponents(serviceId)).thenReturn(Either.left(new ArrayList<>()));
        assertFalse(bl.isPropertyUsedByOperation(childService, propDef2));
    }


    private PropertyDefinition createPropertyObject(String propertyName, String resourceId) {
        PropertyDefinition pd = new PropertyDefinition();
        pd.setConstraints(null);
        pd.setDefaultValue("100");
        pd.setDescription("Size of thasdasdasdasde local disk, in Gigabytes (GB), available to applications running on the Compute node");
        pd.setPassword(false);
        pd.setRequired(true);
        pd.setType("Integer");
        pd.setOwnerId(resourceId);
        pd.setUniqueId(resourceId + "." + propertyName);
        return pd;
    }

    @Test
    public void deleteProperty_CONNECTION_FAILURE() {
        StorageOperationStatus lockResult = StorageOperationStatus.CONNECTION_FAILURE;
        when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
        when(toscaOperationFacade.getToscaElement(anyString())).thenReturn(Either.left(new Resource()));
        assertTrue(bl.deletePropertyFromComponent("resourceforproperty.0.1", "someProperty","i726").isRight());
    }

    @Test
    public void deleteProperty_RESOURCE_NOT_FOUND() throws Exception {

        Resource resource = new Resource();
        PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");

        resource.setProperties(Arrays.asList(property1));
        String resourceId = "myResource";
        resource.setUniqueId(resourceId);

        Field baseBusinessLogic3;
        baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
        baseBusinessLogic3.setAccessible(true);
        baseBusinessLogic3.set(bl, janusGraphDao);


        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));

        StorageOperationStatus lockResult = StorageOperationStatus.OK;
        when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
        //doNothing().when(janusGraphDao).commit();

        Either<PropertyDefinition, ResponseFormat> result;

        Component resourcereturn= new Resource();
        resourcereturn.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
        resourcereturn.setIsDeleted(false);
        resourcereturn.setLastUpdaterUserId("USR01");

        Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
        when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);


        assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
    }

    @Test
    public void deleteProperty_RESTRICTED_OPERATION() throws Exception {

        Resource resource = new Resource();
        PropertyDefinition property1 = createPropertyObject("someProperty", "someResource");

        resource.setProperties(Arrays.asList(property1));
        String resourceId = "myResource";
        resource.setUniqueId(resourceId);

        Field baseBusinessLogic3;
        baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
        baseBusinessLogic3.setAccessible(true);
        baseBusinessLogic3.set(bl, janusGraphDao);


        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));

        StorageOperationStatus lockResult = StorageOperationStatus.OK;
        when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
        //doNothing().when(janusGraphDao).commit();

        Either<PropertyDefinition, ResponseFormat> result;

        Component resourcereturn= new Resource();
        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
        resource.setIsDeleted(false);
        resource.setLastUpdaterUserId("USR01");

        Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
        when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);


        assertTrue(bl.deletePropertyFromComponent("RES01", "someProperty","i726").isRight());
    }

    @Test
    public void deleteProperty_RESTRICTED_() throws Exception {

        Resource resource = new Resource();
        PropertyDefinition property1 = createPropertyObject("PROP", "RES01");
        property1.setUniqueId("PROP");
        resource.setProperties(Arrays.asList(property1));
        String resourceId = "myResource";
        resource.setUniqueId(resourceId);

        Field baseBusinessLogic3;
        baseBusinessLogic3 = bl.getClass().getSuperclass().getDeclaredField("janusGraphDao");
        baseBusinessLogic3.setAccessible(true);
        baseBusinessLogic3.set(bl, janusGraphDao);


        Mockito.when(toscaOperationFacade.getToscaElement(resourceId)).thenReturn(Either.left(resource));

        StorageOperationStatus lockResult = StorageOperationStatus.OK;
        when(graphLockOperation.lockComponent(any(), any())).thenReturn(lockResult);
        //doNothing().when(janusGraphDao).commit();

        Either<PropertyDefinition, ResponseFormat> result;

        Component resourcereturn= new Resource();
        resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
        resource.setIsDeleted(false);
        resource.setLastUpdaterUserId("USR01");

        Either<Component, StorageOperationStatus> toscastatus=Either.left(resource);
        when(toscaOperationFacade.getToscaElement("RES01")).thenReturn(toscastatus);
        when(toscaOperationFacade.deletePropertyOfComponent(anyObject(),anyString())).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacade.getParentComponents(anyString())).thenReturn(Either.left(new ArrayList<>()));

        assertTrue(bl.deletePropertyFromComponent("RES01", "PROP","USR01").isRight());
    }
}