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