Improve testing stability
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / ComponentValidationsTest.java
1 /*
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.be.components.validation;
18
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 import fj.data.Either;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.Optional;
27 import org.junit.jupiter.api.Assertions;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.mockito.InjectMocks;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
35 import org.openecomp.sdc.be.config.ConfigurationManager;
36 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
37 import org.openecomp.sdc.be.model.Component;
38 import org.openecomp.sdc.be.model.ComponentInstance;
39 import org.openecomp.sdc.be.model.ComponentParametersView;
40 import org.openecomp.sdc.be.model.GroupDefinition;
41 import org.openecomp.sdc.be.model.LifecycleStateEnum;
42 import org.openecomp.sdc.be.model.Resource;
43 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
44 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
45 import org.openecomp.sdc.common.impl.ExternalConfiguration;
46 import org.openecomp.sdc.common.impl.FSConfigurationSource;
47
48 class ComponentValidationsTest {
49
50     @InjectMocks
51     private ComponentValidations testSubject;
52
53     @Mock
54     private ToscaOperationFacade toscaOperationFacadeMock;
55
56     @Mock
57     private GraphLockOperation graphLockOperationMock;
58
59     @BeforeEach
60     public void setUp() throws Exception {
61         MockitoAnnotations.openMocks(this);
62         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
63     }
64
65     @Test
66     void testValidateComponentInstanceExist() throws Exception {
67         String instanceId = "test";
68
69         ComponentInstance instance = new ComponentInstance();
70         instance.setUniqueId(instanceId);
71         List<ComponentInstance> instances = new ArrayList<>();
72         instances.add(instance);
73
74         Component component = new Resource();
75         component.setComponentInstances(instances);
76
77         // default test
78         boolean result = ComponentValidations.validateComponentInstanceExist(component, instanceId);
79         assertTrue(result);
80     }
81
82     @Test
83     void testValidateNameIsUniqueInComponent() throws Exception {
84         String currentName = "curr_name";
85         String newName = "curr_name";
86         String newName2 = "mock";
87
88         ComponentInstance instance = new ComponentInstance();
89         instance.setName(currentName);
90         instance.setNormalizedName(currentName);
91         List<ComponentInstance> instances = new ArrayList<>();
92         instances.add(instance);
93
94         Component component = new Resource();
95         component.setComponentInstances(instances);
96
97         // default test
98         boolean result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName, component);
99         assertTrue(result);
100
101         result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName2, component);
102         assertTrue(result);
103
104         final GroupDefinition groupDefinition = new GroupDefinition();
105         groupDefinition.setName(newName2);
106         component.setGroups(Arrays.asList(groupDefinition));
107         result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName2, component);
108         assertFalse(result);
109     }
110
111     @Test
112     void testValidateComponentIsCheckedOutByUserAndLockIt() throws Exception {
113         String componentId = "";
114         String userId = "";
115         Resource resource = new Resource();
116         resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
117
118         Mockito.when(toscaOperationFacadeMock.getToscaElement(Mockito.anyString(), Mockito.any(ComponentParametersView.class)))
119             .thenReturn(Either.left(resource));
120
121         Assertions.assertThrows(ComponentException.class, () -> {
122             // default test
123             testSubject.validateComponentIsCheckedOutByUser("", ComponentTypeEnum.RESOURCE, userId);
124         });
125     }
126
127     @Test
128     void testGetComponentInstance() {
129         String instanceId = "test";
130
131         ComponentInstance instance = new ComponentInstance();
132         instance.setUniqueId(instanceId);
133         List<ComponentInstance> instances = new ArrayList<>();
134         instances.add(instance);
135
136         Component component = new Resource();
137         component.setComponentInstances(instances);
138
139         final Optional<ComponentInstance> result = testSubject.getComponentInstance(component, instanceId);
140         Assertions.assertNotNull(result);
141         Assertions.assertTrue(result.isPresent());
142         Assertions.assertNotNull(result.get());
143     }
144
145 }