<%#
 Copyright 2013-2025 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 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

      https://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.
-%>
<%_
const callBlock = reactive ? '.block()' : '';
const callBlockOptional = reactive ? '.blockOptional()' : '';
_%>
package <%= packageName %>.security;

import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.repository.UserRepository;
import <%= packageName %>.service.UserService;

import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
<%_ if (reactive) { _%>
import org.springframework.security.core.userdetails.ReactiveUserDetailsService;
<%_ } _%>
import org.springframework.security.core.userdetails.UserDetails;
<%_ if (!reactive) { _%>
import org.springframework.security.core.userdetails.UserDetailsService;
<%_ } _%>
<%_ if (databaseTypeCouchbase) { _%>
import org.springframework.security.test.context.support.WithMockUser;
<%_ } _%>
<%_ if (databaseTypeSql && !reactive) { _%>
import org.springframework.transaction.annotation.Transactional;
<%_ } _%>

<%_ if (databaseTypeCassandra) { _%>
import java.util.UUID;
<%_ } _%>
import java.util.Locale;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
 * Integrations tests for {@link DomainUserDetailsService}.
 */
<%_ if (databaseTypeSql && !reactive) { _%>
@Transactional
<%_ } _%>
<%_ if (databaseTypeCouchbase) { _%>
@WithMockUser("test-user-one")
<%_ } _%>
@IntegrationTest
class DomainUserDetailsServiceIT {

    private static final String USER_ONE_LOGIN = "test-user-one";
    private static final String USER_ONE_EMAIL = "test-user-one@localhost";
    private static final String USER_TWO_LOGIN = "test-user-two";
    private static final String USER_TWO_EMAIL = "test-user-two@localhost";
    private static final String USER_THREE_LOGIN = "test-user-three";
    private static final String USER_THREE_EMAIL = "test-user-three@localhost";

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private UserService userService;

    @Autowired
    @Qualifier("userDetailsService")
    private <% if (reactive) { %>Reactive<% } %>UserDetailsService domainUserDetailsService;

    public <%= user.persistClass %> getUserOne() {
        <%= user.persistClass %> userOne = new <%= user.persistClass %>();
<%_ if (databaseTypeCassandra) { _%>
        userOne.setId(UUID.randomUUID().toString());
<%_ } _%>
        userOne.setLogin(USER_ONE_LOGIN);
        userOne.setPassword(RandomStringUtils.insecure().nextAlphanumeric(60));
        userOne.setActivated(true);
        userOne.setEmail(USER_ONE_EMAIL);
        userOne.setFirstName("userOne");
        userOne.setLastName("doe");
        userOne.setLangKey("en");
<%_ if (databaseTypeSql && reactive) { _%>
        userOne.setCreatedBy(Constants.SYSTEM);
<%_ } _%>
        return userOne;
    }

    public <%= user.persistClass %> getUserTwo() {
        <%= user.persistClass %> userTwo = new <%= user.persistClass %>();
<%_ if (databaseTypeCassandra) { _%>
        userTwo.setId(UUID.randomUUID().toString());
<%_ } _%>
        userTwo.setLogin(USER_TWO_LOGIN);
        userTwo.setPassword(RandomStringUtils.insecure().nextAlphanumeric(60));
        userTwo.setActivated(true);
        userTwo.setEmail(USER_TWO_EMAIL);
        userTwo.setFirstName("userTwo");
        userTwo.setLastName("doe");
        userTwo.setLangKey("en");
<%_ if (databaseTypeSql && reactive) { _%>
        userTwo.setCreatedBy(Constants.SYSTEM);
<%_ } _%>
        return userTwo;
    }

    public <%= user.persistClass %> getUserThree() {
        <%= user.persistClass %> userThree = new <%= user.persistClass %>();
<%_ if (databaseTypeCassandra) { _%>
        userThree.setId(UUID.randomUUID().toString());
<%_ } _%>
        userThree.setLogin(USER_THREE_LOGIN);
        userThree.setPassword(RandomStringUtils.insecure().nextAlphanumeric(60));
        userThree.setActivated(false);
        userThree.setEmail(USER_THREE_EMAIL);
        userThree.setFirstName("userThree");
        userThree.setLastName("doe");
        userThree.setLangKey("en");
<%_ if (databaseTypeSql && reactive) { _%>
        userThree.setCreatedBy(Constants.SYSTEM);
<%_ } _%>
        return userThree;
    }

    @BeforeEach
    public void init() {
        userRepository.save(getUserOne())<%- callBlock %>;
        userRepository.save(getUserTwo())<%- callBlock %>;
        userRepository.save(getUserThree())<%- callBlock %>;
    }

    @AfterEach
    public void cleanup () {
        userService.deleteUser(USER_ONE_LOGIN)<%- callBlock %>;
        userService.deleteUser(USER_TWO_LOGIN)<%- callBlock %>;
        userService.deleteUser(USER_THREE_LOGIN)<%- callBlock %>;
    }

    @Test
    void assertThatUserCanBeFoundByLogin() {
        UserDetails userDetails = domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_ONE_LOGIN)<% if (reactive) { %>.block()<% } %>;
        assertThat(userDetails).isNotNull();
        assertThat(userDetails.getUsername()).isEqualTo(USER_ONE_LOGIN);
    }

    @Test
    void assertThatUserCanBeFoundByLoginIgnoreCase() {
        UserDetails userDetails = domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_ONE_LOGIN.toUpperCase(Locale.ENGLISH))<% if (reactive) { %>.block()<% } %>;
        assertThat(userDetails).isNotNull();
        assertThat(userDetails.getUsername()).isEqualTo(USER_ONE_LOGIN);
    }

    @Test
    void assertThatUserCanBeFoundByEmail() {
        UserDetails userDetails = domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_TWO_EMAIL)<% if (reactive) { %>.block()<% } %>;
        assertThat(userDetails).isNotNull();
        assertThat(userDetails.getUsername()).isEqualTo(USER_TWO_LOGIN);
    }

    @Test
    void assertThatUserCanBeFoundByEmailIgnoreCase() {
        UserDetails userDetails = domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_TWO_EMAIL.toUpperCase(Locale.ENGLISH))<% if (reactive) { %>.block()<% } %>;
        assertThat(userDetails).isNotNull();
        assertThat(userDetails.getUsername()).isEqualTo(USER_TWO_LOGIN);
    }

    @Test
    void assertThatEmailIsPrioritizedOverLogin() {
        UserDetails userDetails = domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_ONE_EMAIL)<% if (reactive) { %>.block()<% } %>;
        assertThat(userDetails).isNotNull();
        assertThat(userDetails.getUsername()).isEqualTo(USER_ONE_LOGIN);
    }

    @Test
    void assertThatUserNotActivatedExceptionIsThrownForNotActivatedUsers() {
        assertThatExceptionOfType(UserNotActivatedException.class).isThrownBy(
            () -> domainUserDetailsService.<% if (reactive) { %>find<% } else { %>loadUser<% } %>ByUsername(USER_THREE_LOGIN)<% if (reactive) { %>.block()<% } %>);
    }
}

