re base code
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / validation / UserValidationsTest.java
1 package org.openecomp.sdc.be.components.validation;
2
3 import fj.data.Either;
4 import org.junit.Before;
5 import org.junit.Test;
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;
16
17 import java.util.LinkedList;
18 import java.util.List;
19
20 import static org.assertj.core.api.Assertions.assertThat;
21 import static org.assertj.core.api.Assertions.catchThrowable;
22
23 public class UserValidationsTest {
24
25         @InjectMocks
26         UserValidations testSubject;
27         
28         @Mock
29         IUserBusinessLogic userAdmin;
30         
31         @Mock
32     ComponentsUtils componentsUtils;
33         
34         @Before
35         public void setUp() throws Exception {
36                 //TestUtilsSdc.setFinalStatic(UserValidations.class, "log", LoggerFactory.getLogger(UserValidations.class));
37                 MockitoAnnotations.initMocks(this);
38         }
39
40         @Test
41         public void testValidateUserExists() throws Exception {
42                 String userId = "mock";
43                 String ecompErrorContext = "mock";
44                 User usr = new User();
45                 boolean inTransaction = false;
46                 User result;
47                 
48                 
49                 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
50                 
51                 // default test
52                 result = testSubject.validateUserExists(userId, ecompErrorContext, inTransaction);
53         }
54         
55         @Test
56         public void testValidateNonExistingUser2() throws Exception {
57                 String userId = "mock";
58                 String ecompErrorContext = "mock";
59                 boolean inTransaction = false;
60                 User result;
61                 
62                 
63                 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
64
65                 Throwable thrown = catchThrowable(() -> testSubject.validateUserExists(userId, ecompErrorContext, inTransaction) );
66                 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);
67
68         }
69
70         @Test
71         public void testValidateUserRole() throws Exception {
72                 User user = new User();
73                 List<Role> roles = new LinkedList<>();
74                 roles.add(Role.DESIGNER);
75                 
76                 user.setRole(Role.DESIGNER.name());
77                 
78                 // test 1
79                 testSubject.validateUserRole(user, roles);
80         }
81
82         @Test
83         public void testValidateUserExistsActionStatus() throws Exception {
84                 String userId = "mock";
85                 String ecompErrorContext = "mock";
86                 Either<User, ActionStatus> result;
87                 User usr = new User();
88                 
89                 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
90                 
91                 // default test
92                 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
93         }
94
95         @Test
96         public void testValidateUserExistsActionStatus2() throws Exception {
97                 String userId = "mock";
98                 String ecompErrorContext = "mock";
99                 Either<User, ActionStatus> result;
100                 User usr = new User();
101                 
102                 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
103                 
104                 // default test
105                 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
106         }
107         
108         @Test
109         public void testValidateUserNotEmpty() throws Exception {
110                 User user = new User();
111                 user.setUserId("userId");
112                 String ecompErrorContext = "mock";
113                 User result;
114
115                 // default test
116                 result = testSubject.validateUserNotEmpty(user, ecompErrorContext);
117         }
118
119         @Test
120         public void testValidateNonExistingUser() throws Exception {
121                 String userId = "";
122                 String ecompErrorContext = "";
123
124                 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
125                 
126                 // default test
127                 Throwable thrown = catchThrowable(() -> testSubject.validateUserExist(userId, ecompErrorContext) );
128                 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);
129         }
130 }