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 static org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.eq;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.when;
30 import fj.data.Either;
31 import java.util.Iterator;
32 import org.janusgraph.core.JanusGraph;
33 import org.janusgraph.core.JanusGraphEdge;
34 import org.janusgraph.core.JanusGraphVertex;
35 import org.junit.jupiter.api.BeforeAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.Test;
38 import org.mockito.Mock;
39 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphGenericDao;
40 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
41 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
42 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
43 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
44 import org.openecomp.sdc.be.model.AdditionalInformationDefinition;
45 import org.openecomp.sdc.be.model.ModelTestBase;
46 import org.openecomp.sdc.be.model.operations.impl.util.OperationTestsUtil;
47 import org.openecomp.sdc.be.resources.data.UserData;
48 import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
50 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
51 public class AdditionalInformationOperationTest extends ModelTestBase {
53 private static final JanusGraphGenericDao JANUS_GRAPH_GENERIC_DAO = mock(JanusGraphGenericDao.class);
54 private static String USER_ID = "muUserId";
55 private static String CATEGORY_NAME = "category/mycategory";
57 private JanusGraphVertex janusGraphVertex;
59 @javax.annotation.Resource(name = "janusgraph-generic-dao")
60 private JanusGraphGenericDao janusGraphDao;
62 @javax.annotation.Resource(name = "additional-information-operation")
63 private AdditionalInformationOperation additionalInformationOperation;
66 public static void setupBeforeClass() {
73 public void createUserAndCategory() {
74 deleteAndCreateCategory(CATEGORY_NAME);
75 deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
80 public void testDummy() {
82 assertNotNull(additionalInformationOperation);
87 public void testAddInfoParameter_InvalidId() {
88 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
90 String componentId = "componentId";
91 when(JANUS_GRAPH_GENERIC_DAO.getVertexByProperty(eq(uid), eq(componentId))).thenReturn(Either.left(janusGraphVertex));
92 result = additionalInformationOperation.addAdditionalInformationParameter
93 (NodeTypeEnum.Resource, componentId, "key", "value");
94 assertThat(result.isRight());
98 public void testUpdateInfoParameter_InvalidId() {
99 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
101 String componentId = "componentId";
102 when(JANUS_GRAPH_GENERIC_DAO.getVertexByProperty(eq(uid), eq(componentId))).thenReturn(Either.left(janusGraphVertex));
103 result = additionalInformationOperation.updateAdditionalInformationParameter
104 (NodeTypeEnum.Resource, componentId, "id", "key", "value");
105 assertTrue(result.isRight());
109 public void testDelAdditionalInfoParam_InvalidId() {
110 Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
112 String componentId = "componentId";
113 JanusGraph graph = janusGraphDao.getGraph().left().value();
114 JanusGraphVertex v1 = graph.addVertex();
115 v1.property("uid", componentId);
116 v1.property(GraphPropertiesDictionary.LABEL.getProperty(), "resource");
117 JanusGraphVertex v2 = graph.addVertex();
120 JanusGraphEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
121 addEdge.property("edgeProp", "resource");
124 result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
126 assertTrue(result.isRight());
129 private void clearGraph() {
130 Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
131 JanusGraph graph = graphResult.left().value();
133 Iterable<JanusGraphVertex> vertices = graph.query().vertices();
134 if (vertices != null) {
135 Iterator<JanusGraphVertex> iterator = vertices.iterator();
136 while (iterator.hasNext()) {
137 JanusGraphVertex vertex = iterator.next();
141 janusGraphDao.commit();
144 private UserData deleteAndCreateUser(String userId, String firstName, String lastName) {
145 UserData userData = new UserData();
146 userData.setUserId(userId);
147 userData.setFirstName(firstName);
148 userData.setLastName(lastName);
150 janusGraphDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
151 janusGraphDao.createNode(userData, UserData.class);
152 janusGraphDao.commit();
157 private void deleteAndCreateCategory(String category) {
158 String[] names = category.split("/");
159 OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], janusGraphDao);