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 org.janusgraph.core.JanusGraphEdge;
24 import org.janusgraph.core.JanusGraph;
25 import org.janusgraph.core.JanusGraphVertex;
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.janusgraph.JanusGraphGenericDao;
35 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
36 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
37 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
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.impl.util.OperationTestsUtil;
42 import org.openecomp.sdc.be.resources.data.UserData;
43 import org.springframework.test.context.ContextConfiguration;
44 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
46 import java.util.Iterator;
48 import static org.junit.Assert.assertNotNull;
49 import static org.junit.Assert.assertTrue;
50 import static org.mockito.ArgumentMatchers.eq;
51 import static org.mockito.Mockito.mock;
52 import static org.mockito.Mockito.when;
54 @RunWith(SpringJUnit4ClassRunner.class)
55 @ContextConfiguration("classpath:application-context-test.xml")
56 public class AdditionalInformationOperationTest extends ModelTestBase {
57 private static final JanusGraphGenericDao JANUS_GRAPH_GENERIC_DAO = mock(JanusGraphGenericDao.class);
58 private static String USER_ID = "muUserId";
59 private static String CATEGORY_NAME = "category/mycategory";
61 private JanusGraphVertex janusGraphVertex;
63 @javax.annotation.Resource(name = "janusgraph-generic-dao")
64 private JanusGraphGenericDao janusGraphDao;
66 @javax.annotation.Resource(name = "additional-information-operation")
67 private AdditionalInformationOperation additionalInformationOperation;
70 public void createUserAndCategory() {
71 deleteAndCreateCategory(CATEGORY_NAME);
72 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
77 public static void setupBeforeClass() {
84 public void testDummy() {
86 assertNotNull(additionalInformationOperation);
91 public void testAddInfoParameter_InvalidId(){
92 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
94 String componentId = "componentId";
95 when(JANUS_GRAPH_GENERIC_DAO.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(janusGraphVertex));
96 result = additionalInformationOperation.addAdditionalInformationParameter
97 (NodeTypeEnum.Resource,componentId,"key","value");
98 assertThat(result.isRight());
102 public void testUpdateInfoParameter_InvalidId(){
103 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
105 String componentId = "componentId";
106 when(JANUS_GRAPH_GENERIC_DAO.getVertexByProperty(eq(uid),eq(componentId))).thenReturn(Either.left(janusGraphVertex));
107 result = additionalInformationOperation.updateAdditionalInformationParameter
108 (NodeTypeEnum.Resource,componentId,"id","key","value");
109 assertTrue(result.isRight());
113 public void testDelAdditionalInfoParam_InvalidId() {
114 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
116 String componentId = "componentId";
117 JanusGraph graph = janusGraphDao.getGraph().left().value();
118 JanusGraphVertex v1 = graph.addVertex();
119 v1.property("uid", componentId);
120 v1.property(GraphPropertiesDictionary.LABEL.getProperty(), "resource");
121 JanusGraphVertex v2 = graph.addVertex();
124 JanusGraphEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
125 addEdge.property("edgeProp", "resource");
128 result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
130 assertTrue(result.isRight());
133 private void clearGraph() {
134 Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
135 JanusGraph graph = graphResult.left().value();
137 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
138 if (vertices != null) {
139 Iterator<JanusGraphVertex> iterator = vertices.iterator();
140 while (iterator.hasNext()) {
141 JanusGraphVertex vertex = iterator.next();
145 janusGraphDao.commit();
148 private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
149 UserData userData = new UserData();
150 userData.setUserId(userId);
151 userData.setFirstName(firstName);
152 userData.setLastName(lastName);
154 janusGraphDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
155 janusGraphDao.createNode(userData, UserData.class);
156 janusGraphDao.commit();
161 private void deleteAndCreateCategory(String category) {
162 String[] names = category.split("/");
163 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], janusGraphDao);