re base code
[sdc.git] / catalog-model / src / test / java / org / openecomp / sdc / be / model / operations / impl / UserAdminOperationTest.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 com.thinkaurelius.titan.core.*;
24 import com.thinkaurelius.titan.graphdb.relations.StandardVertexProperty;
25 import com.thinkaurelius.titan.graphdb.types.system.EmptyVertex;
26 import com.thinkaurelius.titan.graphdb.types.system.ImplicitKey;
27 import fj.data.Either;
28 import org.apache.tinkerpop.gremlin.structure.*;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.stubbing.Answer;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
38 import org.openecomp.sdc.be.dao.titan.TitanGenericDao;
39 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
40 import org.openecomp.sdc.be.dao.utils.UserStatusEnum;
41 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
42 import org.openecomp.sdc.be.model.ModelTestBase;
43 import org.openecomp.sdc.be.model.User;
44 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
45 import org.openecomp.sdc.be.resources.data.UserData;
46 import org.openecomp.sdc.common.api.UserRoleEnum;
47
48 import java.time.Instant;
49 import java.util.ArrayList;
50 import java.util.HashMap;
51 import java.util.Iterator;
52 import java.util.List;
53
54 import static org.assertj.core.api.Assertions.assertThat;
55 import static org.junit.Assert.assertSame;
56 import static org.junit.Assert.assertTrue;
57 import static org.mockito.ArgumentMatchers.any;
58 import static org.mockito.ArgumentMatchers.eq;
59 import static org.mockito.Mockito.*;
60
61 public class UserAdminOperationTest extends ModelTestBase {
62     private static final TitanGenericDao titanGenericDao = mock(TitanGenericDao.class);
63     @InjectMocks
64     private static final UserAdminOperation userAdminOperation = new UserAdminOperation(titanGenericDao);
65     private static final String ADMIN = "admin";
66
67     @BeforeClass
68     public static void setup() {
69         ModelTestBase.init();
70     }
71
72     @Before
73     public void initMocks() {
74         MockitoAnnotations.initMocks(this);
75         Mockito.reset(titanGenericDao);
76         mockTitanUpdate();
77         mockTitanDelete();
78
79     }
80
81     @Test
82     public void testDeActivateUserDataSuccess() {
83         UserData userData = mockTitanGet(ADMIN, UserRoleEnum.ADMIN, true);
84
85         Either<User, StorageOperationStatus> eitherUser = userAdminOperation.deActivateUser(userAdminOperation.convertToUser(userData));
86
87         verify(titanGenericDao, times(1)).updateNode(eq(userData), eq(UserData.class));
88         verify(titanGenericDao, times(0)).deleteNode(any(UserData.class), eq(UserData.class));
89         assertTrue(eitherUser.isLeft());
90         User user = eitherUser.left().value();
91         assertSame(user.getStatus(), UserStatusEnum.INACTIVE);
92     }
93
94     @Test
95     public void testDeleteUserWithoutResources() {
96         UserData userData = mockTitanGet(ADMIN, UserRoleEnum.ADMIN, true);
97
98         List<Edge> edgesList = new ArrayList<>();
99
100         Either<List<Edge>, TitanOperationStatus> eitherResult = Either.left(edgesList);
101         when(titanGenericDao.getEdgesForNode(userData, Direction.BOTH)).thenReturn(eitherResult);
102
103         Either<User, ActionStatus> eitherUser = userAdminOperation.deleteUserData(ADMIN);
104         verify(titanGenericDao, times(0)).updateNode(any(UserData.class), eq(UserData.class));
105         verify(titanGenericDao, times(1)).deleteNode(userData, UserData.class);
106         assertTrue(eitherUser.isLeft());
107
108     }
109
110     @Test
111     public void testDeleteUserWithResources() {
112         UserData userData = mockTitanGet(ADMIN, UserRoleEnum.ADMIN, true);
113
114         List<Edge> edgesList = new ArrayList<>();
115         edgesList.add(getEmptyEdgeImpl());
116
117         Either<List<Edge>, TitanOperationStatus> eitherResult = Either.left(edgesList);
118         when(titanGenericDao.getEdgesForNode(userData, Direction.BOTH)).thenReturn(eitherResult);
119
120         Either<User, ActionStatus> eitherUser = userAdminOperation.deleteUserData(ADMIN);
121         verify(titanGenericDao, times(0)).updateNode(any(UserData.class), eq(UserData.class));
122         verify(titanGenericDao, times(0)).deleteNode(any(UserData.class), eq(UserData.class));
123         assertTrue(eitherUser.isRight());
124         assertSame(eitherUser.right().value(), ActionStatus.USER_HAS_ACTIVE_ELEMENTS);
125
126     }
127
128     @Test
129     public void getUserPendingTasks_shouldReturnNonDeleted() {
130         String userId = "abc123";
131         String userKey = UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User);
132         User user = new User();
133         user.setUserId(userId);
134         TitanVertex userVertex = null;
135         TestVertex component1 = new TestVertex(null);
136         TestVertex component2 = new TestVertex(true);
137         TestVertex component3 = new TestVertex(false);
138         List<Edge> edges = new ArrayList<>();
139         Edge edge1 = new TestEdge(component1, "1");
140         Edge edge2 = new TestEdge(component2, "2");
141         Edge edge3 = new TestEdge(component3, "3");
142         edges.add(edge1);
143         edges.add(edge2);
144         edges.add(edge3);
145         when(titanGenericDao.getVertexByProperty(userKey, userId)).thenReturn(Either.left(userVertex));
146         when(titanGenericDao.getOutgoingEdgesByCriteria(any(), any(), any())).thenReturn(Either.left(edges));
147         Either<List<Edge>, StorageOperationStatus> result = userAdminOperation.getUserPendingTasksList(user, new HashMap<>());
148         assertThat(result.isLeft()).isTrue();
149         List<Edge> pendingTasks = result.left().value();
150         assertThat(pendingTasks.size()).isEqualTo(2);
151         assertThat(((TestEdge)pendingTasks.get(0)).getName()).isNotEqualTo("2");
152         assertThat(((TestEdge)pendingTasks.get(1)).getName()).isNotEqualTo("2");
153     }
154
155     private class TestVertex implements TitanVertex {
156
157         private final Boolean isDeleted;
158
159         private TestVertex(Boolean isDeleted) {
160             this.isDeleted = isDeleted;
161         }
162
163         @Override
164         public TitanEdge addEdge(String s, Vertex vertex, Object... objects) {
165             return null;
166         }
167
168         @Override
169         public <V> TitanVertexProperty<V> property(String s, V v, Object... objects) {
170             return null;
171         }
172
173         @Override
174         public <V> VertexProperty<V> property(String key) {
175             if (key.equals(GraphPropertiesDictionary.IS_DELETED.getProperty())) {
176                 if (isDeleted==null)
177                     return VertexProperty.empty();
178                 return new StandardVertexProperty(1, ImplicitKey.ID, new EmptyVertex(), isDeleted, (byte)1);
179             }
180             return VertexProperty.empty();
181         }
182
183         @Override
184         public <V> TitanVertexProperty<V> property(VertexProperty.Cardinality cardinality, String s, V v, Object... objects) {
185             return null;
186         }
187
188         @Override
189         public Iterator<Edge> edges(Direction direction, String... strings) {
190             return null;
191         }
192
193         @Override
194         public Iterator<Vertex> vertices(Direction direction, String... strings) {
195             return null;
196         }
197
198         @Override
199         public Object id() {
200             return null;
201         }
202
203         @Override
204         public long longId() {
205             return 0;
206         }
207
208         @Override
209         public boolean hasId() {
210             return false;
211         }
212
213         @Override
214         public String label() {
215             return null;
216         }
217
218         @Override
219         public VertexLabel vertexLabel() {
220             return null;
221         }
222
223         @Override
224         public TitanVertexQuery<? extends TitanVertexQuery> query() {
225             return null;
226         }
227
228         @Override
229         public boolean isModified() {
230             return false;
231         }
232
233         @Override
234         public TitanTransaction graph() {
235             return null;
236         }
237
238         @Override
239         public void remove() {
240
241         }
242
243         @Override
244         public <V> V valueOrNull(PropertyKey propertyKey) {
245             return null;
246         }
247
248         @Override
249         public boolean isNew() {
250             return false;
251         }
252
253         @Override
254         public boolean isLoaded() {
255             return false;
256         }
257
258         @Override
259         public boolean isRemoved() {
260             return false;
261         }
262
263         @Override
264         public <V> Iterator<VertexProperty<V>> properties(String... strings) {
265             return null;
266         }
267     }
268
269     private class TestEdge implements Edge {
270
271         private final Vertex inVertx;
272         private final String name;
273
274         TestEdge(Vertex inVertx, String name) {
275             this.inVertx = inVertx;
276             this.name = name;
277         }
278
279         public String getName() {
280             return name;
281         }
282
283         @Override
284         public Iterator<Vertex> vertices(Direction direction) {
285             return null;
286         }
287
288         @Override
289         public Vertex inVertex() {
290             return inVertx;
291         }
292
293         @Override
294         public Object id() {
295             return null;
296         }
297
298         @Override
299         public String label() {
300             return null;
301         }
302
303         @Override
304         public Graph graph() {
305             return null;
306         }
307
308         @Override
309         public <V> Property<V> property(String s, V v) {
310             return null;
311         }
312
313         @Override
314         public void remove() {
315
316         }
317
318         @Override
319         public <V> Iterator<Property<V>> properties(String... strings) {
320             return null;
321         }
322     }
323
324     private Edge getEmptyEdgeImpl() {
325         return new Edge() {
326
327             @Override
328             public Object id() {
329                 // TODO Auto-generated method stub
330                 return null;
331             }
332
333             @Override
334             public String label() {
335                 // TODO Auto-generated method stub
336                 return null;
337             }
338
339             @Override
340             public Graph graph() {
341                 // TODO Auto-generated method stub
342                 return null;
343             }
344
345             @Override
346             public <V> Property<V> property(String key, V value) {
347                 // TODO Auto-generated method stub
348                 return null;
349             }
350
351             @Override
352             public void remove() {
353                 // TODO Auto-generated method stub
354
355             }
356
357             @Override
358             public Iterator<Vertex> vertices(Direction direction) {
359                 // TODO Auto-generated method stub
360                 return null;
361             }
362
363             @Override
364             public <V> Iterator<Property<V>> properties(String... propertyKeys) {
365                 // TODO Auto-generated method stub
366                 return null;
367             }
368
369         };
370     }
371
372     private UserData mockTitanGet(String userId, UserRoleEnum role, boolean isActive) {
373         UserData userData = buildUserData(userId, role, isActive);
374         Either<UserData, TitanOperationStatus> eitherUserData = Either.left(userData);
375         when(titanGenericDao.getNode(UniqueIdBuilder.getKeyByNodeType(NodeTypeEnum.User), userId, UserData.class)).thenReturn(eitherUserData);
376         return userData;
377     }
378
379     private static void mockTitanUpdate() {
380         doAnswer((Answer<Either<UserData, TitanOperationStatus>>) invocation -> {
381             Object[] args = invocation.getArguments();
382             UserData retValue = (UserData) args[0];
383             return Either.left(retValue);
384         }).when(titanGenericDao).updateNode(any(UserData.class), eq(UserData.class));
385     }
386
387     private static void mockTitanDelete() {
388         doAnswer((Answer<Either<UserData, TitanOperationStatus>>) invocation -> {
389             Object[] args = invocation.getArguments();
390             UserData retValue = (UserData) args[0];
391             return Either.left(retValue);
392         }).when(titanGenericDao).deleteNode(any(UserData.class), eq(UserData.class));
393     }
394
395     private static UserData buildUserData(String userId, UserRoleEnum role, boolean isActive) {
396         UserData userData = new UserData();
397         userData.setUserId(userId);
398         userData.setRole(role.getName());
399         userData.setEmail("someEmail@somePlace.com");
400         userData.setFirstName("israel");
401         userData.setLastName("israeli");
402         userData.setLastLoginTime(Instant.MIN.getEpochSecond());
403         userData.setStatus(isActive ? UserStatusEnum.ACTIVE.name() : UserStatusEnum.INACTIVE.name());
404         return userData;
405     }
406 }