2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.operations.impl;
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;
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;
47 import java.util.Iterator;
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;
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";
62 private TitanVertex titanVertex;
64 @javax.annotation.Resource(name = "titan-generic-dao")
65 private TitanGenericDao titanDao;
67 @javax.annotation.Resource(name = "additional-information-operation")
68 private IAdditionalInformationOperation additionalInformationOperation;
71 public void createUserAndCategory() {
72 deleteAndCreateCategory(CATEGORY_NAME);
73 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
78 public static void setupBeforeClass() {
85 public void testDummy() {
87 assertNotNull(additionalInformationOperation);
92 public void testAddInfoParameter_InvalidId(){
93 Either<AdditionalInformationDefinition, TitanOperationStatus> result;
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());
103 public void testUpdateInfoParameter_InvalidId(){
104 Either<AdditionalInformationDefinition, TitanOperationStatus> result;
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());
114 public void testDelAdditionalInfoParam_InvalidId() {
115 Either<AdditionalInformationDefinition, TitanOperationStatus> result;
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();
125 TitanEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
126 addEdge.property("edgeProp", "resource");
129 result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
131 assertTrue(result.isRight());
134 private void clearGraph() {
135 Either<TitanGraph, TitanOperationStatus> graphResult = titanDao.getGraph();
136 TitanGraph graph = graphResult.left().value();
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();
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);
155 titanDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
156 titanDao.createNode(userData, UserData.class);
162 private void deleteAndCreateCategory(String category) {
163 String[] names = category.split("/");
164 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], titanDao);