2 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.components.validation;
19 import static org.junit.Assert.assertFalse;
20 import static org.junit.Assert.assertTrue;
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;
48 class ComponentValidationsTest {
51 private ComponentValidations testSubject;
54 private ToscaOperationFacade toscaOperationFacadeMock;
57 private GraphLockOperation graphLockOperationMock;
60 public void setUp() throws Exception {
61 MockitoAnnotations.openMocks(this);
62 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
66 void testValidateComponentInstanceExist() throws Exception {
67 String instanceId = "test";
69 ComponentInstance instance = new ComponentInstance();
70 instance.setUniqueId(instanceId);
71 List<ComponentInstance> instances = new ArrayList<>();
72 instances.add(instance);
74 Component component = new Resource();
75 component.setComponentInstances(instances);
78 boolean result = ComponentValidations.validateComponentInstanceExist(component, instanceId);
83 void testValidateNameIsUniqueInComponent() throws Exception {
84 String currentName = "curr_name";
85 String newName = "curr_name";
86 String newName2 = "mock";
88 ComponentInstance instance = new ComponentInstance();
89 instance.setName(currentName);
90 instance.setNormalizedName(currentName);
91 List<ComponentInstance> instances = new ArrayList<>();
92 instances.add(instance);
94 Component component = new Resource();
95 component.setComponentInstances(instances);
98 boolean result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName, component);
101 result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName2, component);
104 final GroupDefinition groupDefinition = new GroupDefinition();
105 groupDefinition.setName(newName2);
106 component.setGroups(Arrays.asList(groupDefinition));
107 result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName2, component);
112 void testValidateComponentIsCheckedOutByUserAndLockIt() throws Exception {
113 String componentId = "";
115 Resource resource = new Resource();
116 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
118 Mockito.when(toscaOperationFacadeMock.getToscaElement(Mockito.anyString(), Mockito.any(ComponentParametersView.class)))
119 .thenReturn(Either.left(resource));
121 Assertions.assertThrows(ComponentException.class, () -> {
123 testSubject.validateComponentIsCheckedOutByUser("", ComponentTypeEnum.RESOURCE, userId);
128 void testGetComponentInstance() {
129 String instanceId = "test";
131 ComponentInstance instance = new ComponentInstance();
132 instance.setUniqueId(instanceId);
133 List<ComponentInstance> instances = new ArrayList<>();
134 instances.add(instance);
136 Component component = new Resource();
137 component.setComponentInstances(instances);
139 final Optional<ComponentInstance> result = testSubject.getComponentInstance(component, instanceId);
140 Assertions.assertNotNull(result);
141 Assertions.assertTrue(result.isPresent());
142 Assertions.assertNotNull(result.get());