1 package org.openecomp.sdc.be.components.validation;
4 import org.junit.Before;
6 import org.mockito.InjectMocks;
7 import org.mockito.Mock;
8 import org.mockito.Mockito;
9 import org.mockito.MockitoAnnotations;
10 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
11 import org.openecomp.sdc.be.dao.api.ActionStatus;
12 import org.openecomp.sdc.be.impl.ComponentsUtils;
13 import org.openecomp.sdc.be.model.User;
14 import org.openecomp.sdc.be.user.IUserBusinessLogic;
15 import org.openecomp.sdc.be.user.Role;
17 import java.util.LinkedList;
18 import java.util.List;
20 import static org.assertj.core.api.Assertions.assertThat;
21 import static org.assertj.core.api.Assertions.catchThrowable;
23 public class UserValidationsTest {
26 UserValidations testSubject;
29 IUserBusinessLogic userAdmin;
32 ComponentsUtils componentsUtils;
35 public void setUp() throws Exception {
36 //TestUtilsSdc.setFinalStatic(UserValidations.class, "log", LoggerFactory.getLogger(UserValidations.class));
37 MockitoAnnotations.initMocks(this);
41 public void testValidateUserExists() throws Exception {
42 String userId = "mock";
43 String ecompErrorContext = "mock";
44 User usr = new User();
45 boolean inTransaction = false;
49 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
52 result = testSubject.validateUserExists(userId, ecompErrorContext, inTransaction);
56 public void testValidateNonExistingUser2() throws Exception {
57 String userId = "mock";
58 String ecompErrorContext = "mock";
59 boolean inTransaction = false;
63 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
65 Throwable thrown = catchThrowable(() -> testSubject.validateUserExists(userId, ecompErrorContext, inTransaction) );
66 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);
71 public void testValidateUserRole() throws Exception {
72 User user = new User();
73 List<Role> roles = new LinkedList<>();
74 roles.add(Role.DESIGNER);
76 user.setRole(Role.DESIGNER.name());
79 testSubject.validateUserRole(user, roles);
83 public void testValidateUserExistsActionStatus() throws Exception {
84 String userId = "mock";
85 String ecompErrorContext = "mock";
86 Either<User, ActionStatus> result;
87 User usr = new User();
89 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
92 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
96 public void testValidateUserExistsActionStatus2() throws Exception {
97 String userId = "mock";
98 String ecompErrorContext = "mock";
99 Either<User, ActionStatus> result;
100 User usr = new User();
102 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
105 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
109 public void testValidateUserNotEmpty() throws Exception {
110 User user = new User();
111 user.setUserId("userId");
112 String ecompErrorContext = "mock";
116 result = testSubject.validateUserNotEmpty(user, ecompErrorContext);
120 public void testValidateNonExistingUser() throws Exception {
122 String ecompErrorContext = "";
124 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
127 Throwable thrown = catchThrowable(() -> testSubject.validateUserExist(userId, ecompErrorContext) );
128 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);