UT-AdditionalInfoOperation2
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / AdditionalInformationOperationTest.java
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  */
20
21 package org.openecomp.sdc.be.model.operations.impl;
22
23 import com.thinkaurelius.titan.core.TitanEdge;
24 import com.thinkaurelius.titan.core.TitanGraph;
25 import com.thinkaurelius.titan.core.TitanVertex;
26 import fj.data.Either;
27 import static org.assertj.core.api.Assertions.assertThat;
28
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.Mock;
34 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
35 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
36 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
37 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
38 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
39 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
40 import org.openecomp.sdc.be.model.ModelTestBase;
41 import org.openecomp.sdc.be.model.operations.api.IAdditionalInformationOperation;
42 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
43 import org.openecomp.sdc.be.resources.data.UserData;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46
47 import java.util.Iterator;
48
49 import static org.junit.Assert.assertNotNull;
50 import static org.junit.Assert.assertTrue;
51 import static org.mockito.ArgumentMatchers.eq;
52 import static org.mockito.Mockito.mock;
53 import static org.mockito.Mockito.when;
54
55 @RunWith(SpringJUnit4ClassRunner.class)
56 @ContextConfiguration("classpath:application-context-test.xml")
57 public class AdditionalInformationOperationTest extends ModelTestBase {
58     private static final TitanGenericDao titanGenericDao = mock(TitanGenericDao.class);
59     private static String USER_ID = "muUserId";
60     private static String CATEGORY_NAME = "category/mycategory";
61     @Mock
62     private TitanVertex titanVertex;
63
64     @javax.annotation.Resource(name = "titan-generic-dao")
65     private TitanGenericDao titanDao;
66
67     @javax.annotation.Resource(name = "additional-information-operation")
68     private IAdditionalInformationOperation additionalInformationOperation;
69
70     @Before
71     public void createUserAndCategory() {
72         deleteAndCreateCategory(CATEGORY_NAME);
73         deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
74
75     }
76
77     @BeforeClass
78     public static void setupBeforeClass() {
79
80         ModelTestBase.init();
81
82     }
83
84     @Test
85     public void testDummy() {
86
87         assertNotNull(additionalInformationOperation);
88
89     }
90
91     @Test
92     public void testAddInfoParameter_InvalidId(){
93         Either<AdditionalInformationDefinition, TitanOperationStatus> result;
94         String uid = "uid";
95         String componentId = "componentId";
96         when(titanGenericDao.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(titanVertex));
97         result = additionalInformationOperation.addAdditionalInformationParameter
98                 (NodeTypeEnum.Resource,componentId,"key","value");
99         assertThat(result.isRight());
100     }
101
102     @Test
103     public void testUpdateInfoParameter_InvalidId(){
104         Either<AdditionalInformationDefinition, TitanOperationStatus> result;
105         String uid = "uid";
106         String componentId = "componentId";
107         when(titanGenericDao.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(titanVertex));
108         result = additionalInformationOperation.updateAdditionalInformationParameter
109                 (NodeTypeEnum.Resource,componentId,"id","key","value");
110         assertTrue(result.isRight());
111     }
112
113     @Test
114     public void testDelAdditionalInfoParam_InvalidId() {
115         Either<AdditionalInformationDefinition, TitanOperationStatus> result;
116         String id = "uid";
117         String componentId = "componentId";
118         TitanGraph graph = titanDao.getGraph().left().value();
119         TitanVertex v1 = graph.addVertex();
120         v1.property("uid", componentId);
121         v1.property(GraphPropertiesDictionary.LABEL.getProperty(), "resource");
122         TitanVertex v2 = graph.addVertex();
123         v2.property(id,id);
124
125         TitanEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
126         addEdge.property("edgeProp", "resource");
127         graph.tx().commit();
128
129         result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
130         clearGraph();
131         assertTrue(result.isRight());
132     }
133
134     private void clearGraph() {
135         Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
136         TitanGraph graph = graphResult.left().value();
137
138         Iterable<TitanVertex> vertices = graph.query().vertices();
139         if (vertices != null) {
140             Iterator<TitanVertex> iterator = vertices.iterator();
141             while (iterator.hasNext()) {
142                 TitanVertex vertex = iterator.next();
143                 vertex.remove();
144             }
145         }
146         titanDao.commit();
147     }
148
149     private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
150         UserData userData = new UserData();
151         userData.setUserId(userId);
152         userData.setFirstName(firstName);
153         userData.setLastName(lastName);
154
155         titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
156         titanDao.createNode(userData, UserData.class);
157         titanDao.commit();
158
159         return userData;
160     }
161
162     private void deleteAndCreateCategory(String category) {
163         String[] names = category.split("/");
164         OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);
165     }
166
167 }