2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.components.validation;
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;
37 import java.util.LinkedList;
38 import java.util.List;
40 import static org.assertj.core.api.Assertions.assertThat;
41 import static org.assertj.core.api.Assertions.catchThrowable;
43 public class UserValidationsTest {
46 UserValidations testSubject;
49 IUserBusinessLogic userAdmin;
52 ComponentsUtils componentsUtils;
55 public void setUp() throws Exception {
56 //TestUtilsSdc.setFinalStatic(UserValidations.class, "log", LoggerFactory.getLogger(UserValidations.class));
57 MockitoAnnotations.initMocks(this);
61 public void testValidateUserExists() throws Exception {
62 String userId = "mock";
63 String ecompErrorContext = "mock";
64 User usr = new User();
65 boolean inTransaction = false;
69 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
72 result = testSubject.validateUserExists(userId, ecompErrorContext, inTransaction);
76 public void testValidateNonExistingUser2() throws Exception {
77 String userId = "mock";
78 String ecompErrorContext = "mock";
79 boolean inTransaction = false;
83 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
85 Throwable thrown = catchThrowable(() -> testSubject.validateUserExists(userId, ecompErrorContext, inTransaction) );
86 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);
91 public void testValidateUserRole() throws Exception {
92 User user = new User();
93 List<Role> roles = new LinkedList<>();
94 roles.add(Role.DESIGNER);
96 user.setRole(Role.DESIGNER.name());
99 testSubject.validateUserRole(user, roles);
103 public void testValidateUserExistsActionStatus() throws Exception {
104 String userId = "mock";
105 String ecompErrorContext = "mock";
106 Either<User, ActionStatus> result;
107 User usr = new User();
109 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.left(usr));
112 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
116 public void testValidateUserExistsActionStatus2() throws Exception {
117 String userId = "mock";
118 String ecompErrorContext = "mock";
119 Either<User, ActionStatus> result;
120 User usr = new User();
122 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
125 result = testSubject.validateUserExistsActionStatus(userId, ecompErrorContext);
129 public void testValidateUserNotEmpty() throws Exception {
130 User user = new User();
131 user.setUserId("userId");
132 String ecompErrorContext = "mock";
136 result = testSubject.validateUserNotEmpty(user, ecompErrorContext);
140 public void testValidateNonExistingUser() throws Exception {
142 String ecompErrorContext = "";
144 Mockito.when(userAdmin.getUser(Mockito.anyString(), Mockito.anyBoolean())).thenReturn(Either.right(ActionStatus.USER_NOT_FOUND));
147 Throwable thrown = catchThrowable(() -> testSubject.validateUserExist(userId, ecompErrorContext) );
148 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.AUTH_FAILED);