<%#
 Copyright 2013-2023 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 manyListName = ormTool === 'hibernate' ? 'Set' : 'List';
const manyListType = ormTool === 'hibernate' ? 'LinkedHashSet' : 'ArrayList';
 %>
package <%= packageName %>.service.mapper;

import <%= user.entityAbsoluteClass %>;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.service.dto.AuthorityDTO;
import <%= packageName %>.service.dto.<%= user.dtoClass %>;
<%_ if (!authenticationTypeOauth2) { _%>
import org.apache.commons.lang3.RandomStringUtils;
<%_ } _%>
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
<%_ if (user.primaryKey.hasUUID) { _%>
import java.util.UUID;
<%_ } _%>

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

/**
 * Unit tests for {@link UserMapper}.
 */
class UserMapperTest {

    private static final String DEFAULT_LOGIN = "johndoe";
<%_ if (databaseTypeCouchbase) { _%>
    private static final String DEFAULT_ID = DEFAULT_LOGIN;
<%_ } else { _%>
    private static final <%= user.primaryKey.type %> DEFAULT_ID = <%- this.getPrimaryKeyValue(user.primaryKey.type) %>;
<%_ } _%>

    private UserMapper userMapper;
    private <%= user.persistClass %> user;
    private <%= user.adminUserDto %> userDto;

    @BeforeEach
    public void init() {
        userMapper = new UserMapperImpl();
        user = new <%= user.persistClass %>();
        user.setLogin(DEFAULT_LOGIN);
<%_ if (!authenticationTypeOauth2) { _%>
        user.setPassword(RandomStringUtils.randomAlphanumeric(60));
<%_ } _%>
        user.setActivated(true);
        user.setEmail("johndoe@localhost");
        user.setFirstName("john");
        user.setLastName("doe");
<%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl("image_url");
<%_ } _%>
        user.setLangKey("en");

        userDto = userMapper.userTo<%= user.adminUserDto %>(user);
    }

    @Test
    void usersToUserDTOsShouldMapOnlyNonNullUsers() {
        List<<%= user.persistClass %>> users = new ArrayList<>();
        users.add(user);
        users.add(null);

        List<<%= user.dtoClass %>> userDTOS = userMapper.usersToUserDTOs(users);

        assertThat(userDTOS)
          .isNotEmpty()
          .size().isEqualTo(1);
    }

    @Test
    void userDTOsToUsersShouldMapOnlyNonNullUsers() {
        List<<%= user.adminUserDto %>> usersDto = new ArrayList<>();
        usersDto.add(userDto);
        usersDto.add(null);

        List<<%= user.persistClass %>> users = userMapper.userDTOsToUsers(usersDto);

        assertThat(users)
          .isNotEmpty()
          .size().isEqualTo(1);
    }

    @Test
    void userDTOsToUsersWithAuthoritiesStringShouldMapToUsersWithAuthoritiesDomain() {
        <%= manyListName %><AuthorityDTO> authoritiesAsString = new <%= manyListType %><>();
        authoritiesAsString.add(new AuthorityDTO().code("ADMIN"));
        userDto.setAuthorities(authoritiesAsString);

        List<<%= user.adminUserDto %>> usersDto = new ArrayList<>();
        usersDto.add(userDto);

        List<<%= user.persistClass %>> users = userMapper.userDTOsToUsers(usersDto);

        assertThat(users)
          .isNotEmpty()
          .size().isEqualTo(1);
        assertThat(users.get(0).getAuthorities()).isNotNull();
        assertThat(users.get(0).getAuthorities()).isNotEmpty();
<%_ if (databaseTypeSql || databaseTypeMongodb) { _%>
        assertThat(users.get(0).getAuthorities().iterator().next().getName()).isEqualTo("ADMIN");
<%_ } _%>
<%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%>
        assertThat(users.get(0).getAuthorities().iterator().next()).isEqualTo("ADMIN");
<%_ } _%>
    }

    @Test
    void userDTOsToUsersMapWithNullAuthoritiesStringShouldReturnUserWithEmptyAuthorities() {
        userDto.setAuthorities(null);

        List<<%= user.adminUserDto %>> usersDto = new ArrayList<>();
        usersDto.add(userDto);

        List<<%= user.persistClass %>> users = userMapper.userDTOsToUsers(usersDto);

        assertThat(users)
          .isNotEmpty()
          .size().isEqualTo(1);
        assertThat(users.get(0).getAuthorities()).isNotNull();
        assertThat(users.get(0).getAuthorities()).isEmpty();
    }

    @Test
    void userDTOToUserMapWithAuthoritiesStringShouldReturnUserWithAuthorities() {
        <%= manyListName %><AuthorityDTO> authoritiesAsString = new <%= manyListType %><>();
        authoritiesAsString.add(new AuthorityDTO().code("ADMIN"));
        userDto.setAuthorities(authoritiesAsString);

        <%= user.persistClass %> user = userMapper.userDTOToUser(userDto);

        assertThat(user).isNotNull();
        assertThat(user.getAuthorities()).isNotNull();
        assertThat(user.getAuthorities()).isNotEmpty();
<%_ if (databaseTypeSql || databaseTypeMongodb) { _%>
        assertThat(user.getAuthorities().iterator().next().getName()).isEqualTo("ADMIN");
<%_ } _%>
<%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%>
        assertThat(user.getAuthorities().iterator().next()).isEqualTo("ADMIN");
<%_ } _%>
    }

    @Test
    void userDTOToUserMapWithNullAuthoritiesStringShouldReturnUserWithEmptyAuthorities() {
        userDto.setAuthorities(null);

        <%= user.persistClass %> user = userMapper.userDTOToUser(userDto);

        assertThat(user).isNotNull();
        assertThat(user.getAuthorities()).isNotNull();
        assertThat(user.getAuthorities()).isEmpty();
    }

    @Test
    void userDTOToUserMapWithNullUserShouldReturnNull() {
        assertThat(userMapper.userDTOToUser(null)).isNull();
    }

    @Test
    void testUserFromId() {
        assertThat(userMapper.userFromId(DEFAULT_ID).getId()).isEqualTo(DEFAULT_ID);
        assertThat(userMapper.userFromId(null)).isNull();
    }
}
