1 package org.openecomp.sdc.be.components.impl.generic;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertTrue;
5 import static org.mockito.Mockito.when;
7 import java.util.Arrays;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.mockito.InjectMocks;
13 import org.mockito.Mock;
14 import org.mockito.Mockito;
15 import org.mockito.MockitoAnnotations;
16 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
17 import org.openecomp.sdc.be.model.InputDefinition;
18 import org.openecomp.sdc.be.model.PropertyDefinition;
19 import org.openecomp.sdc.be.model.Resource;
20 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
21 import org.openecomp.sdc.exception.ResponseFormat;
23 import fj.data.Either;
25 public class GenericTypeBusinessLogicTest {
28 private GenericTypeBusinessLogic testInstance;
31 private ToscaOperationFacade toscaOperationFacadeMock;
34 public void setUp() throws Exception {
35 testInstance = new GenericTypeBusinessLogic();
36 MockitoAnnotations.initMocks(this);
41 public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
42 Resource cvfc = new Resource();
43 cvfc.setResourceType(ResourceTypeEnum.CVFC);
44 cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
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;