aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/InputsBusinessLogicTest.java
blob: 4b021c357c03156ce9a99778f29a8394b5f601ce (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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
/*
 * Copyright © 2016-2019 European Support Limited
 *
 * 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.
 */


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

import fj.data.Either;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
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.janusgraph.JanusGraphOperationStatus;
import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
import org.openecomp.sdc.be.datatypes.elements.SchemaDefinition;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
import org.openecomp.sdc.be.impl.ComponentsUtils;
import org.openecomp.sdc.be.model.Component;
import org.openecomp.sdc.be.model.ComponentInstInputsMap;
import org.openecomp.sdc.be.model.ComponentInstListInput;
import org.openecomp.sdc.be.model.ComponentInstance;
import org.openecomp.sdc.be.model.ComponentInstanceInput;
import org.openecomp.sdc.be.model.ComponentInstancePropInput;
import org.openecomp.sdc.be.model.ComponentInstanceProperty;
import org.openecomp.sdc.be.model.ComponentParametersView;
import org.openecomp.sdc.be.model.DataTypeDefinition;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.LifecycleStateEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
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.StorageOperationStatus;
import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
import org.openecomp.sdc.be.user.UserBusinessLogic;
import org.openecomp.sdc.common.api.ConfigurationSource;
import org.openecomp.sdc.common.impl.ExternalConfiguration;
import org.openecomp.sdc.common.impl.FSConfigurationSource;
import org.openecomp.sdc.exception.ResponseFormat;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyMap;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;


public class InputsBusinessLogicTest {

    private static final String COMPONENT_INSTANCE_ID = "instanceId";
    private static final String COMPONENT_ID = "componentId";
    private static final String USER_ID = "userId";
    private static final String INPUT_ID = "inputId";
    private static final String INPUT_TYPE = "string";
    private static final String LISTINPUT_NAME = "listInput";
    private static final String LISTINPUT_SCHEMA_TYPE = "org.onap.datatypes.listinput";
    private static final String LISTINPUT_PROP1_NAME = "prop1";
    private static final String LISTINPUT_PROP1_TYPE = "string";
    private static final String LISTINPUT_PROP2_NAME = "prop2";
    private static final String LISTINPUT_PROP2_TYPE = "integer";
    private static final String OLD_VALUE = "old value";
    private static final String NEW_VALUE = "new value";
    static ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
    static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);

    @Mock
    private ComponentsUtils componentsUtilsMock;

    @Mock
    private UserBusinessLogic userAdminMock;

    @Mock
    private ToscaOperationFacade toscaOperationFacadeMock;

    @Mock
    private UserValidations userValidations;

    @Mock
    private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;

    @Mock
    private IGraphLockOperation graphLockOperation;

    @Mock
    private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;

    @Mock
    private ApplicationDataTypeCache applicationDataTypeCache;

    @Mock
    private PropertyOperation propertyOperation;

    @Mock
    private JanusGraphDao janusGraphDao;

    @Mock
    private DataTypeBusinessLogic dataTypeBusinessLogic;

    @InjectMocks
    private InputsBusinessLogic testInstance;

    private Service service;

    @Captor
    ArgumentCaptor<Map<String, DataTypeDefinition>> dataTypesMapCaptor;

    private Map<String, List<ComponentInstanceInput>> instanceInputMap;
    private List<ComponentInstanceInput> inputsList;

    @Before
    public void setUp() {
        MockitoAnnotations.initMocks(this);
        service = new Service();
        service.setUniqueId(COMPONENT_ID);
        service.setLastUpdaterUserId(USER_ID);
        service.setIsDeleted(false);
        service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);

        testInstance.setUserValidations(userValidations);
        testInstance.setToscaOperationFacade(toscaOperationFacadeMock);
        testInstance.setGraphLockOperation(graphLockOperation);
        testInstance.setComponentsUtils(componentsUtilsMock);
        testInstance.setJanusGraphDao(janusGraphDao);
        testInstance.setApplicationDataTypeCache(applicationDataTypeCache);
        testInstance.setPropertyOperation(propertyOperation);

        // add a ComponentInstance
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setUniqueId(COMPONENT_INSTANCE_ID);
        service.setComponentInstances(Collections.singletonList(componentInstance));

        instanceInputMap = new HashMap<>();
        ComponentInstanceInput componentInstanceInput = new ComponentInstanceInput();
        componentInstanceInput.setInputId(INPUT_ID);
        componentInstanceInput.setName(INPUT_ID);
        inputsList = Collections.singletonList(componentInstanceInput);
        instanceInputMap.put(COMPONENT_INSTANCE_ID, inputsList);
        instanceInputMap.put("someInputId", Collections.singletonList(new ComponentInstanceInput()));
        service.setComponentInstancesInputs(instanceInputMap);
        when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(new User());
        when(userAdminMock.getUser(USER_ID, false)).thenReturn(new User());
    }

    @Test
    public void getComponentInstanceInputs_ComponentInstanceNotExist() {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, "nonExisting");
        assertThat(componentInstanceInputs.isRight()).isTrue();
        verify(componentsUtilsMock).getResponseFormat(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND);
    }

    @Test
    public void getComponentInstanceInputs_emptyInputsMap() {
        service.setComponentInstancesInputs(Collections.emptyMap());
        getComponents_emptyInputs(service);
    }

    @Test
    public void getComponentInstanceInputs_nullInputsMap() {
        service.setComponentInstancesInputs(null);
        getComponents_emptyInputs(service);
    }

    @Test
    public void getComponentInstanceInputs_instanceHasNoInputs() {
        service.setComponentInstancesInputs(Collections.singletonMap("someInputId", new ArrayList<>()));
        getComponents_emptyInputs(service);
    }

    @Test
    public void getComponentInstanceInputs() {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
        assertEquals("inputId", componentInstanceInputs.left().value().get(0).getInputId());
    }

    @Test
    public void testGetInputs() {
        String userId = "userId";
        String componentId = "compId";
        Component component = new Resource();
        when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        testInstance.getInputs(userId, componentId);
        assertNull(component.getInputs());
    }

    @Test
    public void testGetCIPropertiesByInputId() {
        Either<List<ComponentInstanceProperty>, ResponseFormat> result;
        String userId = "userId";
        String componentId = "compId";
        Component component = new Resource();
        List<InputDefinition> listDef = new ArrayList<>();
        InputDefinition inputDef = new InputDefinition();
        inputDef.setUniqueId(componentId);
        listDef.add(inputDef);
        component.setInputs(listDef);
        when(toscaOperationFacadeMock.getToscaElement(any(String.class), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        result = testInstance.getComponentInstancePropertiesByInputId(userId, componentId, componentId, componentId);
        assertThat(result.isLeft()).isTrue();
    }

    @Test
    public void testDeclareProperties() {
        service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
        service.setLastUpdaterUserId(USER_ID);
        ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
        Map<String, List<ComponentInstancePropInput>> propertiesForDeclaration = new HashMap<>();
        propertiesForDeclaration.put(COMPONENT_ID, getPropertiesListForDeclaration());
        componentInstInputsMap.setServiceProperties(propertiesForDeclaration);

        List<InputDefinition> declaredPropertiesToInputs = getDeclaredProperties();
        initMockitoStubbings(declaredPropertiesToInputs);

        Either<List<InputDefinition>, ResponseFormat> declaredPropertiesEither =
                testInstance.declareProperties(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, componentInstInputsMap);

        assertThat(declaredPropertiesEither.isLeft()).isTrue();

        List<InputDefinition> declaredProperties = declaredPropertiesEither.left().value();
        assertThat(CollectionUtils.isNotEmpty(declaredProperties)).isTrue();
        assertEquals(1, declaredProperties.size());
        assertEquals(declaredProperties, declaredPropertiesToInputs);
    }

    private void initMockitoStubbings(List<InputDefinition> declaredPropertiesToInputs) {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(
                Either.left(service));
        when(propertyDeclarationOrchestrator.declarePropertiesToInputs(any(), any())).thenReturn(Either.left(
                declaredPropertiesToInputs));
        when(toscaOperationFacadeMock.addInputsToComponent(any(), any())).thenReturn(Either.left(declaredPropertiesToInputs));
        when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
        when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
        when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
        when(componentInstanceBusinessLogic.setInputConstraint(any())).thenReturn(Collections.emptyList());
    }

    private void getComponents_emptyInputs(Service service) {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        Either<List<ComponentInstanceInput>, ResponseFormat> componentInstanceInputs = testInstance.getComponentInstanceInputs(USER_ID, COMPONENT_ID, COMPONENT_INSTANCE_ID);
        assertEquals(Collections.emptyList(), componentInstanceInputs.left().value());
    }

    @Test
    public void testgetInputs_ARTIFACT_NOT_FOUND() throws Exception {

        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
       Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
        assertThat(responseFormatEither.isRight()).isTrue();

    }

    @Test
    public void testgetInputs_SUCCESS() throws Exception {
        Component component = new Service();
        InputDefinition input= new InputDefinition();
        List inputlist = new ArrayList<>();
        inputlist.add(input);
        component.setInputs(inputlist);
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.ARTIFACT_NOT_FOUND)).thenReturn(ActionStatus.ARTIFACT_NOT_FOUND);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        Either<List<InputDefinition>, ResponseFormat> responseFormatEither = testInstance.getInputs("USR01", COMPONENT_ID);
        assertEquals(inputlist,responseFormatEither.left().value());
    }

    @Test
    public void testgetComponentInstancePropertiesByInputId_Artifactnotfound() throws Exception
    {
        Component component = new Service();
        InputDefinition input= new InputDefinition();
        List inputlist = new ArrayList<>();
        inputlist.add(input);
        component.setInputs(inputlist);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
        Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
        assertThat(responseFormatEither.isRight()).isTrue();
    }

    @Test
    public void testgetComponentInstancePropertiesByInputId_PARENT_ARTIFACT_NOT_FOUND() throws Exception
    {
        Component component = new Service();
        InputDefinition input= new InputDefinition();
        List inputlist = new ArrayList<>();
        inputlist.add(input);
        component.setInputs(inputlist);
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setUniqueId("INST0.1");
        componentInstance.setComponentUid("RES0.1");
        List compinstancelist = new ArrayList<>();
        compinstancelist.add(componentInstance);
        component.setComponentInstances(compinstancelist);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
        Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
        assertThat(responseFormatEither.isRight()).isTrue();
    }

    @Test
    public void testgetComponentInstancePropertiesByInputId() throws Exception
    {
        Component component = new Service();
        InputDefinition input= new InputDefinition();
        input.setUniqueId("INPO1");
        List inputlist = new ArrayList<>();
        inputlist.add(input);
        component.setInputs(inputlist);
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setUniqueId("INST0.1");
        componentInstance.setComponentUid("RES0.1");
        List compinstancelist = new ArrayList<>();
        compinstancelist.add(componentInstance);
        component.setComponentInstances(compinstancelist);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
        //when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
        when(toscaOperationFacadeMock.getToscaElement(eq("RES0.1"), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        Either<List<ComponentInstanceProperty>, ResponseFormat> responseFormatEither = testInstance.getComponentInstancePropertiesByInputId("USR01", COMPONENT_ID,"INST0.1", "INPO1");
        assertEquals(compinstancelist,responseFormatEither.left().value());
    }

    @Test
    public void testgetInputsForComponentInput_ARTIFACT_NOT_FOUND() throws Exception
    {
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.ARTIFACT_NOT_FOUND));
        Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
        assertThat(result.isRight()).isTrue();
    }

    @Test
    public void testgetInputsForComponentInput() throws Exception
    {
        Component component = new Service();
        InputDefinition input= new InputDefinition();
        input.setUniqueId("INPO1");
        List inputlist = new ArrayList<>();
        inputlist.add(input);
        component.setInputs(inputlist);
        ComponentInstance componentInstance = new ComponentInstance();
        componentInstance.setUniqueId("INST0.1");
        componentInstance.setComponentUid("RES0.1");
        List compinstancelist = new ArrayList<>();
        compinstancelist.add(componentInstance);
        component.setComponentInstances(compinstancelist);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(component));
        when(componentInstanceBusinessLogic.getComponentInstancePropertiesByInputId(any(Component.class),eq("INPO1"))).thenReturn(compinstancelist);
        Either<List<ComponentInstanceInput>, ResponseFormat> result = testInstance.getInputsForComponentInput("USR01", COMPONENT_ID,"INPO1");
        assertThat(result.isLeft()).isTrue();
    }

    private List<ComponentInstancePropInput> getPropertiesListForDeclaration() {
        return inputsList.stream().map(this::getPropertyForDeclaration).collect(Collectors.toList());
    }

    private ComponentInstancePropInput getPropertyForDeclaration(ComponentInstanceInput componentInstanceInput) {
        ComponentInstancePropInput propInput = new ComponentInstancePropInput();
        propInput.setInput(componentInstanceInput);
        propInput.setPropertiesName(componentInstanceInput.getName());

        return propInput;
    }

    private List<InputDefinition> getDeclaredProperties() {
        return inputsList.stream().map(InputDefinition::new).collect(Collectors.toList());
    }


    private InputDefinition setUpListInput() {
        InputDefinition listInput = new InputDefinition();
        listInput.setName(LISTINPUT_NAME);
        listInput.setType("list");
        SchemaDefinition listInputSchema = new SchemaDefinition();
        listInputSchema.setProperty(new PropertyDataDefinitionBuilder()
                .setType(LISTINPUT_SCHEMA_TYPE)
                .setIsRequired(false)
                .build()
        );
        listInput.setSchema(listInputSchema);
        return listInput;
    }

    private ComponentInstListInput setUpCreateListInputParams() {
        ComponentInstListInput componentInstListInput = new ComponentInstListInput();

        // Create a "list input"
        InputDefinition listInput = setUpListInput();
        componentInstListInput.setListInput(listInput);

        // Create ComponentInstancePropInputs
        // for inputs in the ComponentInstance
        Map<String, List<ComponentInstancePropInput>> propInputsListMap = new HashMap<>();
        // Add 2 PropInputs. property owner is COMPONENT_INSTANCE_ID
        List<ComponentInstancePropInput> propInputsList = new ArrayList<>();
        ComponentInstancePropInput propInput = new ComponentInstancePropInput();
        propInput.setName(LISTINPUT_PROP1_NAME);
        propInput.setType(LISTINPUT_PROP1_TYPE);
        propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP1_NAME);
        propInputsList.add(propInput);
        propInput = new ComponentInstancePropInput();
        propInput.setName(LISTINPUT_PROP2_NAME);
        propInput.setType(LISTINPUT_PROP2_TYPE);
        propInput.setUniqueId(COMPONENT_INSTANCE_ID+"."+LISTINPUT_PROP2_NAME);
        propInputsList.add(propInput);
        propInputsListMap.put(COMPONENT_INSTANCE_ID, propInputsList);
        ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
        componentInstInputsMap.setComponentInstanceInputsMap(propInputsListMap);
        componentInstListInput.setComponentInstInputsMap(componentInstInputsMap);

        return componentInstListInput;
    }

    @Test
    public void test_createListInput_success() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
        List<ComponentInstancePropInput> propInputsList = componentInstInputsMap.getComponentInstanceInputsMap().get(COMPONENT_INSTANCE_ID);
        InputDefinition listInput = createListInputParams.getListInput();

        // set up mock returns
        // for get component object:
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        // for data type creation (use captor):
        when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
        when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
        when(propertyDeclarationOrchestrator.declarePropertiesToListInput(service, componentInstInputsMap, listInput)).thenReturn(Either.left(listInput));
        // for BaseOperation.getAllDataTypes:
        when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
        // for BaseOperation.validatePropertyDefaultValue:
        when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
        when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
        when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
        // for createListInputsInGraph:
        when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.left(Arrays.asList(listInput)));
        // for rollback/commit:
        when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
        // for unlock resource
        when(graphLockOperation.unlockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);

        Either<List<InputDefinition>, ResponseFormat> result =
                testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        // validate result
        assertThat(result.isLeft()).isTrue();
        List<InputDefinition> resultInputList = result.left().value();
        assertEquals(1, resultInputList.size());
        //InputDefinition resultInput = resultInputList.get(0);
        Map<String, DataTypeDefinition> captoredDataTypeMap = dataTypesMapCaptor.getValue();
        assertEquals(1, captoredDataTypeMap.size());
        assertThat(captoredDataTypeMap.containsKey(LISTINPUT_SCHEMA_TYPE)).isTrue();
        DataTypeDefinition captoredDataType = captoredDataTypeMap.get(LISTINPUT_SCHEMA_TYPE);
        assertEquals("tosca.datatypes.Root", captoredDataType.getDerivedFromName());
        assertEquals( propInputsList.size(), captoredDataType.getProperties().size());
        // confirm if corresponding property exists in data type
        captoredDataType.getProperties().forEach(dataTypeProp -> {
            Optional<ComponentInstancePropInput> find = propInputsList.stream()
                    .filter(propInput -> propInput.getName().equals(dataTypeProp.getName())).findAny();
            assertThat(find.isPresent()).isTrue();
        });
    }

    @Test
    public void test_createListInput_fail_getComponent() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);
        try{
            testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        } catch (ByActionStatusComponentException e) {
            assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
            return;
        }
        fail();
    }


    @Test
    public void test_createListInput_fail_lockComponent() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.COMPONENT_IN_USE);
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.FAILED_TO_LOCK_ELEMENT);
        try {
            Either<List<InputDefinition>, ResponseFormat> result =
                    testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        } catch (ByActionStatusComponentException e) {
            assertEquals(ActionStatus.COMPONENT_IN_USE, e.getActionStatus());
            return;
        }
        fail();
    }

    @Test
    public void test_createListInput_fail_getAllDataTypes() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
        when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
        when(applicationDataTypeCache.getAll()).thenReturn(Either.right(JanusGraphOperationStatus.NOT_FOUND));

        try {
            Either<List<InputDefinition>, ResponseFormat> result =
                    testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        } catch (ByActionStatusComponentException e) {
            assertEquals(ActionStatus.DATA_TYPE_CANNOT_BE_EMPTY, e.getActionStatus());
            verify(applicationDataTypeCache, times(1)).getAll();
            return;
        }
        fail();
    }

    @Test
    public void test_createListInput_fail_prepareAndValidateInput() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
        InputDefinition listInput = createListInputParams.getListInput();

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
        when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
        when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
        // for BaseOperation.validatePropertyDefaultValue:
        when(propertyOperation.isPropertyTypeValid(any())).thenReturn(false);

        Either<List<InputDefinition>, ResponseFormat> result =
                testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        assertThat(result.isRight()).isTrue();
        verify(propertyOperation, times(1)).isPropertyTypeValid(any());
    }

    @Test
    public void test_createListInput_fail_addInputsToComponent() throws Exception {
        ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        ComponentInstInputsMap componentInstInputsMap = createListInputParams.getComponentInstInputsMap();
        InputDefinition listInput = createListInputParams.getListInput();

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.addDataTypesToComponent(dataTypesMapCaptor.capture(), eq(COMPONENT_ID))).thenReturn(Either.left(new ArrayList<>()));
        when(propertyDeclarationOrchestrator.getPropOwnerId(componentInstInputsMap)).thenReturn(COMPONENT_INSTANCE_ID);
        when(applicationDataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>())); // don't use Collections.emptyMap
        // for BaseOperation.validatePropertyDefaultValue:
        when(propertyOperation.isPropertyTypeValid(any())).thenReturn(true);
        when(propertyOperation.isPropertyInnerTypeValid(any(),any())).thenReturn(new ImmutablePair<>(listInput.getSchemaType(), true));
        when(propertyOperation.isPropertyDefaultValueValid(any(), any())).thenReturn(true);
        when(toscaOperationFacadeMock.addInputsToComponent(anyMap(), eq(COMPONENT_ID))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));

        Either<List<InputDefinition>, ResponseFormat> result =
                testInstance.createListInput(USER_ID, COMPONENT_ID, ComponentTypeEnum.SERVICE, createListInputParams, true, false);
        assertThat(result.isRight()).isTrue();
        verify(toscaOperationFacadeMock, times(1)).addInputsToComponent(anyMap(), eq(COMPONENT_ID));
    }

    @Test
    public void test_deleteInput_listInput_fail_getComponent() throws Exception {
        //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND)).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);

        try {
            testInstance.deleteInput(COMPONENT_ID, USER_ID, LISTINPUT_NAME);
        } catch (ComponentException e) {
            assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
            verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
            return;
        }
        fail();
    }


    @Test
    public void test_deleteInput_listInput_fail_validateInput() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        service.setInputs(Collections.singletonList(listInput));
        //ComponentInstListInput createListInputParams = setUpCreateListInputParams();
        final String NONEXIST_INPUT_NAME = "myInput";

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));

        try {
            testInstance.deleteInput(COMPONENT_ID, USER_ID, NONEXIST_INPUT_NAME);
        } catch (ComponentException e) {
            assertEquals(ActionStatus.INPUT_IS_NOT_CHILD_OF_COMPONENT, e.getActionStatus());
            verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
            return;
        }
        fail();
    }


    @Test
    public void test_deleteInput_listInput_fail_lockComponent() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        service.setInputs(Collections.singletonList(listInput));

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.NOT_FOUND);
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.SERVICE)).thenReturn(ActionStatus.SERVICE_NOT_FOUND);

        try {
            testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
        } catch (ComponentException e) {
            assertEquals(ActionStatus.SERVICE_NOT_FOUND, e.getActionStatus());
            verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
            verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
            return;
        }
        fail();
    }


    @Test
    public void test_deleteInput_listInput_fail_deleteInput() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        service.setInputs(Collections.singletonList(listInput));

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.BAD_REQUEST);
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);

        try {
            testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
        } catch (ComponentException e) {
            assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
            verify(toscaOperationFacadeMock, times(1)).getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class));
            verify(graphLockOperation, times(1)).lockComponent(COMPONENT_ID, NodeTypeEnum.Service);
            verify(toscaOperationFacadeMock, times(1)).deleteInputOfResource(service, listInput.getName());
            return;
        }
        fail();
    }


    @Test
    public void test_deleteInput_listInput_success() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        listInput.setIsDeclaredListInput(true);
        service.setInputs(Collections.singletonList(listInput));
        ArgumentCaptor<String> schemaTypeCaptor = ArgumentCaptor.forClass(String.class);

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
        when(propertyDeclarationOrchestrator.unDeclarePropertiesAsListInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);
        when(dataTypeBusinessLogic.deletePrivateDataType(eq(service), schemaTypeCaptor.capture()))
                .thenReturn(Either.left(new DataTypeDefinition()));

        testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
        verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsListInputs(service, listInput);
        verify(dataTypeBusinessLogic, times(1)).deletePrivateDataType(service, listInput.getSchemaType());
        assertEquals(listInput.getSchemaType(), schemaTypeCaptor.getValue());
    }


    @Test
    public void test_deleteInput_input_fail_unDeclare() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        listInput.setIsDeclaredListInput(false);
        service.setInputs(Collections.singletonList(listInput));

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
        when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.BAD_REQUEST);
        when(componentsUtilsMock.convertFromStorageResponse(StorageOperationStatus.BAD_REQUEST)).thenReturn(ActionStatus.INVALID_CONTENT);

        try {
            testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
        } catch (ComponentException e) {
            assertEquals(ActionStatus.INVALID_CONTENT, e.getActionStatus());
            verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
            return;
        }
        fail();
    }


    @Test
    public void test_deleteInput_input_success() throws Exception {
        InputDefinition listInput = setUpListInput();
        String inputId = COMPONENT_ID + "." + listInput.getName();
        listInput.setUniqueId(inputId);
        listInput.setIsDeclaredListInput(false);
        service.setInputs(Collections.singletonList(listInput));

        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class)))
                .thenReturn(Either.left(service));
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        when(toscaOperationFacadeMock.deleteInputOfResource(service, listInput.getName())).thenReturn(StorageOperationStatus.OK);
        when(propertyDeclarationOrchestrator.unDeclarePropertiesAsInputs(service, listInput)).thenReturn(StorageOperationStatus.OK);

        testInstance.deleteInput(COMPONENT_ID, USER_ID, inputId);
        verify(propertyDeclarationOrchestrator, times(1)).unDeclarePropertiesAsInputs(service, listInput);
    }


    @Test
    public void test_updateInputsValue() throws Exception {
        List<InputDefinition> oldInputDefs = new ArrayList<>();
        InputDefinition oldInputDef = new InputDefinition();
        oldInputDef.setUniqueId(INPUT_ID);
        oldInputDef.setType(INPUT_TYPE);
        oldInputDef.setDefaultValue(OLD_VALUE);
        oldInputDef.setRequired(Boolean.FALSE);
        oldInputDefs.add(oldInputDef);
        service.setInputs(oldInputDefs);

        List<InputDefinition> newInputDefs = new ArrayList<>();
        InputDefinition inputDef = new InputDefinition();
        inputDef.setUniqueId(INPUT_ID);
        inputDef.setType(INPUT_TYPE);
        inputDef.setDefaultValue(NEW_VALUE); // update value
        inputDef.setRequired(Boolean.TRUE); // update value
        newInputDefs.add(inputDef);

        // used in validateComponentExists
        when(toscaOperationFacadeMock.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(Either.left(service));
        // used in lockComponent
        when(graphLockOperation.lockComponent(COMPONENT_ID, NodeTypeEnum.Service)).thenReturn(StorageOperationStatus.OK);
        // used in validateInputValueConstraint
        Map<String, DataTypeDefinition> dataTypeMap = new HashMap<>();
        when(applicationDataTypeCache.getAll()).thenReturn(Either.left(dataTypeMap)); // don't use Collections.emptyMap
        // used in updateInputObjectValue
        when(propertyOperation.validateAndUpdatePropertyValue(INPUT_TYPE, NEW_VALUE, true, null, dataTypeMap))
            .thenReturn(Either.left(NEW_VALUE));
        when(toscaOperationFacadeMock.updateInputOfComponent(service, oldInputDef))
            .thenReturn(Either.left(inputDef));

        Either<List<InputDefinition>, ResponseFormat> result =
            testInstance.updateInputsValue(service.getComponentType(), COMPONENT_ID, newInputDefs, USER_ID, true, false);
        assertThat(result.isLeft()).isTrue();
        // check if values are updated
        assertEquals(service.getInputs().get(0).getDefaultValue(), NEW_VALUE);
        assertEquals(service.getInputs().get(0).isRequired(), Boolean.TRUE);
    }

}