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