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 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;
38 import java.util.LinkedList;
39 import java.util.List;
41 import static org.assertj.core.api.Assertions.assertThat;
42 import static org.assertj.core.api.Assertions.catchThrowable;
44 public class UserValidationsTest {
47 UserValidations testSubject;
50 UserBusinessLogic userAdmin;
53 ComponentsUtils componentsUtils;
57 //TestUtilsSdc.setFinalStatic(UserValidations.class, "log", LoggerFactory.getLogger(UserValidations.class));
58 MockitoAnnotations.initMocks(this);
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);
69 testSubject.validateUserExists(userId);
73 public void testValidateNonExistingUser2() {
74 String userId = "mock";
75 String ecompErrorContext = "mock";
76 boolean inTransaction = false;
80 Mockito.when(userAdmin.getUser(Mockito.anyString())).thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
82 Throwable thrown = catchThrowable(() -> testSubject.validateUserExists(userId) );
83 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.USER_NOT_FOUND);
88 public void testValidateUserRole() {
89 User user = new User();
90 List<Role> roles = new LinkedList<>();
91 roles.add(Role.DESIGNER);
93 user.setRole(Role.DESIGNER.name());
96 testSubject.validateUserRole(user, roles);
100 public void testValidateUserExistsActionStatus() {
101 String userId = "mock";
102 String ecompErrorContext = "mock";
104 User usr = new User();
106 Mockito.when(userAdmin.getUser(Mockito.anyString())).thenReturn(usr);
109 result = testSubject.validateUserExistsActionStatus(userId);
113 public void testValidateUserExistsActionStatus2() {
114 String userId = "mock";
115 String ecompErrorContext = "mock";
117 User usr = new User();
119 Mockito.when(userAdmin.getUser(Mockito.anyString())).thenThrow(new ByActionStatusComponentException((ActionStatus.USER_NOT_FOUND)));
122 result = testSubject.validateUserExistsActionStatus(userId);
126 public void testValidateUserNotEmpty() {
127 User user = new User();
128 user.setUserId("userId");
129 String ecompErrorContext = "mock";
133 result = testSubject.validateUserNotEmpty(user, ecompErrorContext);
137 public void testValidateNonExistingUser() {
139 String ecompErrorContext = "";
141 Mockito.when(userAdmin.getUser(Mockito.anyString())).thenThrow(new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND));
144 Throwable thrown = catchThrowable(() -> testSubject.validateUserExists(userId) );
145 assertThat(thrown).isInstanceOf(ComponentException.class).hasFieldOrPropertyWithValue("actionStatus" , ActionStatus.USER_NOT_FOUND);