Upgrade SDC from Titan to Janus Graph
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / generic / GenericTypeBusinessLogicTest.java
1 package org.openecomp.sdc.be.components.impl.generic;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.Mockito;
9 import org.mockito.MockitoAnnotations;
10 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
11 import org.openecomp.sdc.be.model.InputDefinition;
12 import org.openecomp.sdc.be.model.PropertyDefinition;
13 import org.openecomp.sdc.be.model.Resource;
14 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
15 import org.openecomp.sdc.exception.ResponseFormat;
16
17 import java.util.Arrays;
18 import java.util.List;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.when;
23
24 public class GenericTypeBusinessLogicTest {
25
26     @InjectMocks
27     private GenericTypeBusinessLogic testInstance;
28
29     @Mock
30     private ToscaOperationFacade toscaOperationFacadeMock;
31
32     @Before
33     public void setUp() throws Exception {
34         testInstance = new GenericTypeBusinessLogic();
35         MockitoAnnotations.initMocks(this);
36
37     }
38
39     @Test
40     public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
41         Resource cvfc = new Resource();
42         cvfc.setResourceType(ResourceTypeEnum.CVFC);
43         cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
44         cvfc.setDerivedFromGenericType("genericType");
45         Resource genericResource = new Resource();
46         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
47         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
48         assertEquals(genericResource, fetchedGenericType.left().value());
49     }
50
51     @Test
52     public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
53         Resource resource = Mockito.mock(Resource.class);
54         when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
55         when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
56         Resource genericResource = new Resource();
57         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
58         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
59         assertEquals(genericResource, fetchedGenericType.left().value());
60     }
61
62     @Test
63     public void generateInputsFromGenericTypeProperties() throws Exception {
64         Resource genericNodeType = new Resource();
65         genericNodeType.setUniqueId("genericUid");
66         PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
67         PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");
68
69         genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
70
71         List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
72         assertEquals(2, genericInputs.size());
73         assertInput(genericInputs.get(0), propertyDefinition);
74         assertInput(genericInputs.get(1), propertyDefinition2);
75     }
76
77     @Test
78     public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
79         Resource genericNodeType = new Resource();
80         assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
81     }
82
83     private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
84         assertEquals(inputDefinition.getOwnerId(), "genericUid");
85         assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
86         assertEquals(inputDefinition.getName(), propertyDefinition.getName());
87     }
88
89     private PropertyDefinition generatePropDefinition(String name) {
90         PropertyDefinition propertyDefinition = new PropertyDefinition();
91         propertyDefinition.setName(name);
92         propertyDefinition.setValue(name + "value");
93         return propertyDefinition;
94     }
95
96
97 }