aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-be/src/test/java/org/openecomp/sdc/be/components/impl/generic/GenericTypeBusinessLogicTest.java
blob: a37feb950e7840508167b07e2f5d603ce42221fc (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
package org.openecomp.sdc.be.components.impl.generic;

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.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.InputDefinition;
import org.openecomp.sdc.be.model.PropertyDefinition;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
import org.openecomp.sdc.exception.ResponseFormat;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

public class GenericTypeBusinessLogicTest {

    @InjectMocks
    private GenericTypeBusinessLogic testInstance;

    @Mock
    private ToscaOperationFacade toscaOperationFacadeMock;

    @Before
    public void setUp() throws Exception {
        testInstance = new GenericTypeBusinessLogic();
        MockitoAnnotations.initMocks(this);

    }

    @Test
    public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
        Resource cvfc = new Resource();
        cvfc.setResourceType(ResourceTypeEnum.CVFC);
        cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
        cvfc.setDerivedFromGenericType("genericType");
        Resource genericResource = new Resource();
        when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
        Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
        assertEquals(genericResource, fetchedGenericType.left().value());
    }

    @Test
    public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
        Resource resource = Mockito.mock(Resource.class);
        when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
        when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
        Resource genericResource = new Resource();
        when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
        Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
        assertEquals(genericResource, fetchedGenericType.left().value());
    }

    @Test
    public void generateInputsFromGenericTypeProperties() throws Exception {
        Resource genericNodeType = new Resource();
        genericNodeType.setUniqueId("genericUid");
        PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
        PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");

        genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));

        List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
        assertEquals(2, genericInputs.size());
        assertInput(genericInputs.get(0), propertyDefinition);
        assertInput(genericInputs.get(1), propertyDefinition2);
    }

    @Test
    public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
        Resource genericNodeType = new Resource();
        assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
    }

    private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
        assertEquals(inputDefinition.getOwnerId(), "genericUid");
        assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
        assertEquals(inputDefinition.getName(), propertyDefinition.getName());
    }

    private PropertyDefinition generatePropDefinition(String name) {
        PropertyDefinition propertyDefinition = new PropertyDefinition();
        propertyDefinition.setName(name);
        propertyDefinition.setValue(name + "value");
        return propertyDefinition;
    }


}