Fix test cases failing incorrectly
[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 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;
29
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;
49
50 @SpringJUnitConfig(locations = "classpath:application-context-test.xml")
51 public class AdditionalInformationOperationTest extends ModelTestBase {
52
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";
56     @Mock
57     private JanusGraphVertex janusGraphVertex;
58
59     @javax.annotation.Resource(name = "janusgraph-generic-dao")
60     private JanusGraphGenericDao janusGraphDao;
61
62     @javax.annotation.Resource(name = "additional-information-operation")
63     private AdditionalInformationOperation additionalInformationOperation;
64
65     @BeforeAll
66     public static void setupBeforeClass() {
67
68         ModelTestBase.init();
69
70     }
71
72     @BeforeEach
73     public void createUserAndCategory() {
74         deleteAndCreateCategory(CATEGORY_NAME);
75         deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
76
77     }
78
79     @Test
80     public void testDummy() {
81
82         assertNotNull(additionalInformationOperation);
83
84     }
85
86     @Test
87     public void testAddInfoParameter_InvalidId() {
88         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
89         String uid = "uid";
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());
95     }
96
97     @Test
98     public void testUpdateInfoParameter_InvalidId() {
99         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
100         String uid = "uid";
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());
106     }
107
108     @Test
109     public void testDelAdditionalInfoParam_InvalidId() {
110         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
111         String id = "uid";
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();
118         v2.property(id, id);
119
120         JanusGraphEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
121         addEdge.property("edgeProp", "resource");
122         graph.tx().commit();
123
124         result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
125         clearGraph();
126         assertTrue(result.isRight());
127     }
128
129     private void clearGraph() {
130         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
131         JanusGraph graph = graphResult.left().value();
132
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();
138                 vertex.remove();
139             }
140         }
141         janusGraphDao.commit();
142     }
143
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);
149
150         janusGraphDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
151         janusGraphDao.createNode(userData, UserData.class);
152         janusGraphDao.commit();
153
154         return userData;
155     }
156
157     private void deleteAndCreateCategory(String category) {
158         String[] names = category.split("/");
159         OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], janusGraphDao);
160     }
161
162 }