11b4fa0556699b383f1ece09e08810d4c2bc0251
[sdc.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 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  * Modifications copyright (c) 2019 Nokia
20  * ================================================================================
21  */
22 package org.openecomp.sdc.be.components.impl.generic;
23
24 import fj.data.Either;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.mockito.InjectMocks;
28 import org.mockito.Mock;
29 import org.mockito.Mockito;
30 import org.mockito.MockitoAnnotations;
31 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
32 import org.openecomp.sdc.be.impl.ComponentsUtils;
33 import org.openecomp.sdc.be.model.InputDefinition;
34 import org.openecomp.sdc.be.model.PropertyDefinition;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.exception.ResponseFormat;
38
39 import java.util.Arrays;
40 import java.util.List;
41
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.Mockito.when;
45
46 public class GenericTypeBusinessLogicTest {
47
48     @InjectMocks
49     private GenericTypeBusinessLogic testInstance;
50
51     @Mock
52     private ToscaOperationFacade toscaOperationFacadeMock;
53
54     @Mock
55     private ComponentsUtils componentsUtils;
56
57     @Before
58     public void setUp() throws Exception {
59         MockitoAnnotations.initMocks(this);
60         testInstance = new GenericTypeBusinessLogic(componentsUtils, toscaOperationFacadeMock);
61     }
62
63     @Test
64     public void fetchDerivedFromGenericType_cvfv_getGenericResourceTypeFromDerivedFrom() throws Exception {
65         Resource cvfc = new Resource();
66         cvfc.setResourceType(ResourceTypeEnum.CVFC);
67         cvfc.setDerivedFrom(Arrays.asList("genericType", "someOtherType"));
68         cvfc.setDerivedFromGenericType("genericType");
69         Resource genericResource = new Resource();
70         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
71         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(cvfc);
72         assertEquals(genericResource, fetchedGenericType.left().value());
73     }
74
75     @Test
76     public void fetchDerivedFromGenericType_getGenericResourceTypeFromConfiguration() throws Exception {
77         Resource resource = Mockito.mock(Resource.class);
78         when(resource.getResourceType()).thenReturn(ResourceTypeEnum.VF);
79         when(resource.fetchGenericTypeToscaNameFromConfig()).thenReturn("genericType");
80         Resource genericResource = new Resource();
81         when(toscaOperationFacadeMock.getLatestCertifiedNodeTypeByToscaResourceName("genericType")).thenReturn(Either.left(genericResource));
82         Either<Resource, ResponseFormat> fetchedGenericType = testInstance.fetchDerivedFromGenericType(resource);
83         assertEquals(genericResource, fetchedGenericType.left().value());
84     }
85
86     @Test
87     public void generateInputsFromGenericTypeProperties() throws Exception {
88         Resource genericNodeType = new Resource();
89         genericNodeType.setUniqueId("genericUid");
90         PropertyDefinition propertyDefinition = generatePropDefinition("prop1");
91         PropertyDefinition propertyDefinition2 = generatePropDefinition("prop2");
92
93         genericNodeType.setProperties(Arrays.asList(propertyDefinition, propertyDefinition2));
94
95         List<InputDefinition> genericInputs = testInstance.generateInputsFromGenericTypeProperties(genericNodeType);
96         assertEquals(2, genericInputs.size());
97         assertInput(genericInputs.get(0), propertyDefinition);
98         assertInput(genericInputs.get(1), propertyDefinition2);
99     }
100
101     @Test
102     public void generateInputsFromGenericTypeProperties_genericHasNoProps() throws Exception {
103         Resource genericNodeType = new Resource();
104         assertTrue(testInstance.generateInputsFromGenericTypeProperties(genericNodeType).isEmpty());
105     }
106
107     private void assertInput(InputDefinition inputDefinition, PropertyDefinition propertyDefinition) {
108         assertEquals(inputDefinition.getOwnerId(), "genericUid");
109         assertEquals(inputDefinition.getValue(), propertyDefinition.getValue());
110         assertEquals(inputDefinition.getName(), propertyDefinition.getName());
111     }
112
113     private PropertyDefinition generatePropDefinition(String name) {
114         PropertyDefinition propertyDefinition = new PropertyDefinition();
115         propertyDefinition.setName(name);
116         propertyDefinition.setValue(name + "value");
117         return propertyDefinition;
118     }
119
120
121 }