/*
 * Copyright 2019 EPAM Systems
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.epam.ta.reportportal.auth.basic;

import com.epam.ta.reportportal.dao.UserRepository;
import com.epam.ta.reportportal.entity.user.User;
import com.epam.ta.reportportal.entity.user.UserRole;
import com.epam.ta.reportportal.entity.user.UserType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.when;

/**
 * @author <a href="mailto:ihar_kahadouski@epam.com">Ihar Kahadouski</a>
 */
@ExtendWith(MockitoExtension.class)
class DatabaseUserDetailsServiceTest {

	@Mock
	private UserRepository userRepository;

	@InjectMocks
	private DatabaseUserDetailsService userDetailsService;

	@Test
	void userNotFoundTest() {
		when(userRepository.findByLogin("not_exist")).thenReturn(Optional.empty());

		UsernameNotFoundException exception = assertThrows(UsernameNotFoundException.class,
				() -> userDetailsService.loadUserByUsername("not_exist")
		);

		assertEquals("User not found", exception.getMessage());
	}

	@Test
	void loadUserWithEmptyPassword() {
		User user = new User();
		user.setLogin("user");
		user.setId(1L);
		user.setEmail("email@domain.com");
		user.setExpired(false);
		user.setUserType(UserType.INTERNAL);
		user.setRole(UserRole.USER);
		when(userRepository.findByLogin("user")).thenReturn(Optional.of(user));

		UserDetails userDetails = userDetailsService.loadUserByUsername("user");

		assertEquals(user.getLogin(), userDetails.getUsername());
		assertTrue(userDetails.getPassword().isEmpty());
		assertEquals(!user.isExpired(), userDetails.isAccountNonExpired());
	}
}
@@ -688,166 +688,8 @@
 ry;%0A
-import com.epam.ta.reportportal.entity.user.User;%0Aimport com.epam.ta.reportportal.entity.user.UserRole;%0Aimport com.epam.ta.reportportal.entity.user.UserType;%0A
 impo
@@ -719,16 +719,16 @@
 i.Test;%0A
+
 import o
@@ -882,74 +882,8 @@
 on;%0A
-import org.springframework.security.core.userdetails.UserDetails;%0A
 impo
@@ -1034,17 +1034,89 @@
 ertions.
-*
+assertEquals;%0Aimport static org.junit.jupiter.api.Assertions.assertThrows
 ;%0Aimport
@@ -1486,23 +1486,27 @@
 ory.find
-ByLogin
+UserDetails
 (%22not_ex
@@ -1692,24 +1692,24 @@
 ist%22)%0A%09%09);%0A%0A
+
 %09%09assertEqua
@@ -1760,587 +1760,6 @@
 ;%0A%09%7D
-%0A%0A%09@Test%0A%09void loadUserWithEmptyPassword() %7B%0A%09%09User user = new User();%0A%09%09user.setLogin(%22user%22);%0A%09%09user.setId(1L);%0A%09%09user.setEmail(%22email@domain.com%22);%0A%09%09user.setExpired(false);%0A%09%09user.setUserType(UserType.INTERNAL);%0A%09%09user.setRole(UserRole.USER);%0A%09%09when(userRepository.findByLogin(%22user%22)).thenReturn(Optional.of(user));%0A%0A%09%09UserDetails userDetails = userDetailsService.loadUserByUsername(%22user%22);%0A%0A%09%09assertEquals(user.getLogin(), userDetails.getUsername());%0A%09%09assertTrue(userDetails.getPassword().isEmpty());%0A%09%09assertEquals(!user.isExpired(), userDetails.isAccountNonExpired());%0A%09%7D
 %0A%7D

fix tests