Catalog alignment
[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 fj.data.Either;
20 import mockit.Deencapsulation;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
28 import org.openecomp.sdc.be.datatypes.elements.AdditionalInfoParameterDataDefinition;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.datatypes.enums.JsonPresentationFields;
31 import org.openecomp.sdc.be.datatypes.tosca.ToscaDataDefinition;
32 import org.openecomp.sdc.be.model.Component;
33 import org.openecomp.sdc.be.model.ComponentParametersView;
34 import org.openecomp.sdc.be.model.LifecycleStateEnum;
35 import org.openecomp.sdc.be.model.Resource;
36 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
37 import org.openecomp.sdc.be.model.operations.StorageException;
38 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
39 import org.openecomp.sdc.be.model.operations.impl.GraphLockOperation;
40
41 public class ComponentValidationsTest {
42
43         @InjectMocks
44         ComponentValidations testSubject;
45
46         @Mock
47         ToscaOperationFacade toscaOperationFacadeMock;
48
49         @Mock
50         GraphLockOperation graphLockOperationMock;
51
52         @Before
53         public void setUp() throws Exception {
54                 MockitoAnnotations.initMocks(this);
55         }
56
57         @Test
58         public void testValidateComponentInstanceExist() throws Exception {
59                 Component component = new Resource();
60                 String instanceId = "";
61                 boolean result;
62
63                 // default test
64                 result = ComponentValidations.validateComponentInstanceExist(component, instanceId);
65         }
66
67         @Test
68         public void testGetNormalizedName() throws Exception {
69                 ToscaDataDefinition toscaDataDefinition = new AdditionalInfoParameterDataDefinition();
70                 toscaDataDefinition.setToscaPresentationValue(JsonPresentationFields.NAME, "mock");
71                 String result;
72
73                 // default test
74                 result = ComponentValidations.getNormalizedName(toscaDataDefinition);
75         }
76
77         @Test
78         public void testValidateNameIsUniqueInComponent() throws Exception {
79                 String currentName = "";
80                 String newName = "";
81                 String newName2 = "mock";
82                 Component component = new Resource();
83                 boolean result;
84
85                 // default test
86                 result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName, component);
87                 result = ComponentValidations.validateNameIsUniqueInComponent(currentName, newName2, component);
88         }
89
90         @Test(expected=ComponentException.class)
91         public void testValidateComponentIsCheckedOutByUserAndLockIt() throws Exception {
92                 String componentId = "";
93                 String userId = "";
94                 Component result;
95                 Resource resource = new  Resource();
96                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
97                 
98                 Mockito.when(toscaOperationFacadeMock.getToscaElement(Mockito.anyString(), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(resource));
99                 
100                 // default test
101                 result = testSubject.validateComponentIsCheckedOutByUser("",ComponentTypeEnum.RESOURCE,
102                                 userId);
103         }
104
105         @Test
106         public void testGetComponent() throws Exception {
107                 String componentId = "mock";
108                 ComponentTypeEnum componentType = null;
109                 Component result;
110                 Component resource = new Resource();
111                 resource.setComponentType(ComponentTypeEnum.RESOURCE);
112                 Mockito.when(toscaOperationFacadeMock.getToscaElement(Mockito.anyString(), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(resource));
113                 
114                 // default test
115                 result = Deencapsulation.invoke(testSubject, "getComponent", componentId, ComponentTypeEnum.RESOURCE);
116         }
117
118         @Test(expected = StorageException.class)
119         public void testOnToscaOperationError() throws Exception {
120                 Component result;
121
122                 // default test
123                 result = Deencapsulation.invoke(testSubject, "onToscaOperationError",
124                                 StorageOperationStatus.ARTIFACT_NOT_FOUND,"");
125         }
126 }