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.jsontitan.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 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());
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());
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");
68 genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
70 List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
71 assertEquals(2, genericInputs.size());
72 assertInput(genericInputs.get(0), propertyDefinition);
73 assertInput(genericInputs.get(1), propertyDefinition2);
77 public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
78 Resource genericNodeType = new Resource();
79 assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
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());
88 private PropertyDefinition generatePropDefinition(String name) {
89 PropertyDefinition propertyDefinition = new PropertyDefinition();
90 propertyDefinition.setName(name);
91 propertyDefinition.setValue(name + "value");
92 return propertyDefinition;