Added oparent to sdc main
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / generic / GenericTypeBusinessLogicTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.impl.generic;
22
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
31 import org.openecomp.sdc.be.model.InputDefinition;
32 import org.openecomp.sdc.be.model.PropertyDefinition;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
35 import org.openecomp.sdc.exception.ResponseFormat;
36
37 import java.util.Arrays;
38 import java.util.List;
39
40 import static org.junit.Assert.assertEquals;
41 import static org.junit.Assert.assertTrue;
42 import static org.mockito.Mockito.when;
43
44 public class GenericTypeBusinessLogicTest {
45
46     @InjectMocks
47     private GenericTypeBusinessLogic testInstance;
48
49     @Mock
50     private ToscaOperationFacade toscaOperationFacadeMock;
51
52     @Before
53     public void setUp() throws Exception {
54         testInstance = new GenericTypeBusinessLogic();
55         MockitoAnnotations.initMocks(this);
56
57     }
58
59     @Test
60     public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
61         Resource cvfc = new Resource();
62         cvfc.setResourceType(ResourceTypeEnum.CVFC);
63         cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
64         cvfc.setDerivedFromGenericType("genericType");
65         Resource genericResource = new Resource();
66         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
67         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
68         assertEquals(genericResource, fetchedGenericType.left().value());
69     }
70
71     @Test
72     public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
73         Resource resource = Mockito.mock(Resource.class);
74         when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
75         when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
76         Resource genericResource = new Resource();
77         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
78         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
79         assertEquals(genericResource, fetchedGenericType.left().value());
80     }
81
82     @Test
83     public void generateInputsFromGenericTypeProperties() throws Exception {
84         Resource genericNodeType = new Resource();
85         genericNodeType.setUniqueId("genericUid");
86         PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
87         PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");
88
89         genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
90
91         List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
92         assertEquals(2, genericInputs.size());
93         assertInput(genericInputs.get(0), propertyDefinition);
94         assertInput(genericInputs.get(1), propertyDefinition2);
95     }
96
97     @Test
98     public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
99         Resource genericNodeType = new Resource();
100         assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
101     }
102
103     private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
104         assertEquals(inputDefinition.getOwnerId(), "genericUid");
105         assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
106         assertEquals(inputDefinition.getName(), propertyDefinition.getName());
107     }
108
109     private PropertyDefinition generatePropDefinition(String name) {
110         PropertyDefinition propertyDefinition = new PropertyDefinition();
111         propertyDefinition.setName(name);
112         propertyDefinition.setValue(name + "value");
113         return propertyDefinition;
114     }
115
116
117 }