1 package org.openecomp.sdc.be.components.impl.generic;
4 import org.junit.Before;
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;
17 import java.util.Arrays;
18 import java.util.List;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22 import static org.mockito.Mockito.when;
24 public class GenericTypeBusinessLogicTest {
27 private GenericTypeBusinessLogic testInstance;
30 private ToscaOperationFacade toscaOperationFacadeMock;
33 public void setUp() throws Exception {
34 testInstance = new GenericTypeBusinessLogic();
35 MockitoAnnotations.initMocks(this);
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());
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());
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");
69 genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
71 List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
72 assertEquals(2, genericInputs.size());
73 assertInput(genericInputs.get(0), propertyDefinition);
74 assertInput(genericInputs.get(1), propertyDefinition2);
78 public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
79 Resource genericNodeType = new Resource();
80 assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
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());
89 private PropertyDefinition generatePropDefinition(String name) {
90 PropertyDefinition propertyDefinition = new PropertyDefinition();
91 propertyDefinition.setName(name);
92 propertyDefinition.setValue(name + "value");
93 return propertyDefinition;