169a844e3a52c94834362229205773f576039870
[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 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;
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.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;
45
46 import java.util.Iterator;
47
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;
53
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";
60     @Mock
61     private JanusGraphVertex janusGraphVertex;
62
63     @javax.annotation.Resource(name = "janusgraph-generic-dao")
64     private JanusGraphGenericDao janusGraphDao;
65
66     @javax.annotation.Resource(name = "additional-information-operation")
67     private AdditionalInformationOperation additionalInformationOperation;
68
69     @Before
70     public void createUserAndCategory() {
71         deleteAndCreateCategory(CATEGORY_NAME);
72         deleteAndCreateUser(USER_ID, "first_" + USER_ID, "last_" + USER_ID);
73
74     }
75
76     @BeforeClass
77     public static void setupBeforeClass() {
78
79         ModelTestBase.init();
80
81     }
82
83     @Test
84     public void testDummy() {
85
86         assertNotNull(additionalInformationOperation);
87
88     }
89
90     @Test
91     public void testAddInfoParameter_InvalidId(){
92         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
93         String uid = "uid";
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());
99     }
100
101     @Test
102     public void testUpdateInfoParameter_InvalidId(){
103         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
104         String uid = "uid";
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());
110     }
111
112     @Test
113     public void testDelAdditionalInfoParam_InvalidId() {
114         Either<AdditionalInformationDefinition, JanusGraphOperationStatus> result;
115         String id = "uid";
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();
122         v2.property(id,id);
123
124         JanusGraphEdge addEdge = v1.addEdge(GraphEdgeLabels.ADDITIONAL_INFORMATION.getProperty(), v2);
125         addEdge.property("edgeProp", "resource");
126         graph.tx().commit();
127
128         result = additionalInformationOperation.deleteAdditionalInformationParameter(NodeTypeEnum.Resource, componentId, id);
129         clearGraph();
130         assertTrue(result.isRight());
131     }
132
133     private void clearGraph() {
134         Either<JanusGraph, JanusGraphOperationStatus> graphResult = janusGraphDao.getGraph();
135         JanusGraph graph = graphResult.left().value();
136
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();
142                 vertex.remove();
143             }
144         }
145         janusGraphDao.commit();
146     }
147
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);
153
154         janusGraphDao.deleteNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class);
155         janusGraphDao.createNode(userData, UserData.class);
156         janusGraphDao.commit();
157
158         return userData;
159     }
160
161     private void deleteAndCreateCategory(String category) {
162         String[] names = category.split("/");
163         OperationTestsUtil.deleteAndCreateResourceCategory(names[0], names[1], janusGraphDao);
164     }
165
166 }