Sync Integ to Master
[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.jsontitan.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         Resource genericResource = new Resource();
45         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
46         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
47         assertEquals(genericResource, fetchedGenericType.left().value());
48     }
49
50     @Test
51     public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
52         Resource resource = Mockito.mock(Resource.class);
53         when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
54         when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
55         Resource genericResource = new Resource();
56         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
57         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
58         assertEquals(genericResource, fetchedGenericType.left().value());
59     }
60
61     @Test
62     public void generateInputsFromGenericTypeProperties() throws Exception {
63         Resource genericNodeType = new Resource();
64         genericNodeType.setUniqueId("genericUid");
65         PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
66         PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");
67
68         genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
69
70         List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
71         assertEquals(2, genericInputs.size());
72         assertInput(genericInputs.get(0), propertyDefinition);
73         assertInput(genericInputs.get(1), propertyDefinition2);
74     }
75
76     @Test
77     public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
78         Resource genericNodeType = new Resource();
79         assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
80     }
81
82     private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
83         assertEquals(inputDefinition.getOwnerId(), "genericUid");
84         assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
85         assertEquals(inputDefinition.getName(), propertyDefinition.getName());
86     }
87
88     private PropertyDefinition generatePropDefinition(String name) {
89         PropertyDefinition propertyDefinition = new PropertyDefinition();
90         propertyDefinition.setName(name);
91         propertyDefinition.setValue(name + "value");
92         return propertyDefinition;
93     }
94
95
96 }