<%#
 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.
-%>
package <%= packageName %>.web.rest;
<%_
const singletonName = ormTool !== 'mybatis' ? 'singleton' : 'singletonList'
let saveMethod = 'save';
if (!reactive && databaseTypeSql) {
    saveMethod = 'saveAndFlush';
}
_%>
import <%= packageName %>.IntegrationTest;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.config.Constants;
<%_ } _%>
<%_ if (!databaseTypeCassandra && !databaseTypeCouchbase) { _%>
import <%= packageName %>.domain.Authority;
<%_ } _%>
import <%= user.entityAbsoluteClass %>;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.repository.AuthorityRepository;
<%_ } _%>
import <%= packageName %>.repository.UserRepository;
<%_ if (searchEngineElasticsearch) { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.service.dto.<%= user.adminUserDto %>;
import <%= packageName %>.service.dto.<%= user.dtoClass %>;
import <%= packageName %>.service.AuthorityService;
<%_ if (databaseTypeSql && reactive) { _%>
import <%= packageName %>.repository.EntityManager;
<%_ } _%>
import <%= packageName %>.service.mapper.UserMapper;
import org.apache.commons.lang3.RandomStringUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } _%>
<%_ if (cacheProviderAny) { _%>
import org.springframework.cache.CacheManager;
<%_ } _%>
<%_ if (cacheProviderMemcached) { _%>
import org.springframework.cache.support.NoOpCacheManager;
<%_ } _%>
import org.springframework.http.MediaType;
<%_ if (databaseTypeCouchbase) { _%>
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.test.context.TestSecurityContextHolder;
<%_ } _%>
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (reactive) { _%>
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } else { _%>
import org.springframework.test.web.servlet.MockMvc;
<%_ } _%>
<%_ if (databaseTypeSql && !reactive) { _%>
import org.springframework.transaction.annotation.Transactional;
<%_ } _%>
<%_ if (reactive && searchEngineElasticsearch) { _%>
import reactor.core.publisher.Mono;
<%_ } _%>

<%_ if (databaseTypeSql && !reactive && ormTool === 'hibernate') { _%>
import <%= javaVersion === '17' ? 'jakarta' : 'javax'%>.persistence.EntityManager;
<%_ } _%>
<%_ if (!databaseTypeCassandra) { _%>
import java.time.ZonedDateTime;
<%_ } _%>
import java.util.*;
import java.util.function.Consumer;
<%_ if (databaseTypeCassandra || databaseTypeCouchbase) { _%>
import java.util.stream.Collectors;
import java.util.stream.Stream;
<%_ } _%>

import static org.assertj.core.api.Assertions.assertThat;
<%_ if (reactive && searchEngineElasticsearch) { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
<%_ if (reactive && authenticationUsesCsrf) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } else if (!reactive) { _%>
import static org.hamcrest.Matchers.hasItem;
  <%_ if (authenticationUsesCsrf) { _%>
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
  <%_ } _%>
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } _%>

/**
 * Integration tests for the {@link UserResource} REST controller.
 */
<%_ if (reactive) { _%>
@AutoConfigureWebTestClient(timeout = IntegrationTest.DEFAULT_TIMEOUT)
<%_ } else { _%>
@AutoConfigureMockMvc
<%_ } _%>
@WithMockUser(authorities = AuthoritiesConstants.ADMIN)
@IntegrationTest
public class UserResourceIT {

    private static final String DEFAULT_LOGIN = "johndoe";
    private static final <%= userIdTypeName %> DEFAULT_LOGIN_ID = <% if (userIdTypeName === 'Long') { %>1L<% } else if (userIdTypeName === 'String') { %>"johndoe"<% } else { %>UUID.randomUUID()<% } %>;
<%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_LOGIN = "jhipster";
    private static final <%= userIdTypeName %> UPDATED_LOGIN_ID = <% if (userIdTypeName === 'Long') { %>1L<% } else if (userIdTypeName === 'String') { %>"jhipster"<% } else { %>UUID.randomUUID()<% } %>;
<%_ } _%>

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

    private static final String DEFAULT_PASSWORD = "passjohndoe";
    private static final String UPDATED_PASSWORD = "passjhipster";
<%_ } _%>

    private static final String DEFAULT_EMAIL = "johndoe@localhost";
<%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_EMAIL = "jhipster@localhost";
<%_ } _%>

    private static final String DEFAULT_FIRSTNAME = "john";
<%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_FIRSTNAME = "jhipsterFirstName";
<%_ } _%>

    private static final String DEFAULT_LASTNAME = "doe";
<%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_LASTNAME = "jhipsterLastName";
<%_ } _%>

<%_ if (!databaseTypeCassandra) { _%>
    private static final String DEFAULT_IMAGEURL = "http://placehold.it/50x50";
  <%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_IMAGEURL = "http://placehold.it/40x40";
  <%_ } _%>

<%_ } _%>
    private static final String DEFAULT_LANGKEY = "en";
<%_ if (!authenticationTypeOauth2) { _%>
    private static final String UPDATED_LANGKEY = "fr";
<%_ } _%>

    @Autowired
    private UserRepository userRepository;
<%_ if (databaseTypeSql && reactive) { _%>

    @Autowired
    private AuthorityRepository authorityRepository;
<%_ } _%>
<%_ if (searchEngineElasticsearch) { _%>

    @Autowired
    private UserSearchRepository userSearchRepository;
<%_ } _%>

    @Autowired
    private AuthorityService authorityService;

    @Autowired
    private UserMapper userMapper;
<%_ if (databaseType === 'sql' && ormTool === 'hibernate') { _%>

    @Autowired
    private EntityManager em;
<%_ } _%>
<%_ if (cacheProviderAny) { _%>

    @Autowired
    private CacheManager cacheManager;
<%_ } _%>

    @Autowired
<%_ if (reactive) { _%>
    private WebTestClient webTestClient;
<%_ } else { _%>
    private MockMvc restUserMockMvc;
<%_ } _%>

    private <%= user.persistClass %> user;

<%_ if (cacheProviderAny) { _%>
    @BeforeEach
    public void setup() {
        cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).clear();
        cacheManager.getCache(UserRepository.USERS_BY_EMAIL_CACHE).clear();
    }

<%_ } _%>
<%_ if (reactive && authenticationUsesCsrf) { _%>
    @BeforeEach
    public void setupCsrf() {
        webTestClient = webTestClient.mutateWith(csrf());
    }

<%_ } _%>
    /**
     * Create a User.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which has a required relationship to the User entity.
     */
    public static <%= user.persistClass %> createEntity(<% if (databaseTypeSql && ormTool === 'hibernate') { %>EntityManager em<% } %>) {
        <%= user.persistClass %> user = new <%= user.persistClass %>();
<%_ if (databaseTypeCassandra || (authenticationTypeOauth2 && !databaseTypeCouchbase)) { _%>
        user.setId(UUID.randomUUID().toString());
<%_ } _%>
        user.setLogin(DEFAULT_LOGIN<% if (databaseTypeSql) { %> + RandomStringUtils.randomAlphabetic(5)<% } %>);
<%_ if (!authenticationTypeOauth2) { _%>
        user.setPassword(RandomStringUtils.randomAlphanumeric(60));
<%_ } _%>
        user.setActivated(true);
        user.setEmail(<% if (databaseTypeSql) { %>RandomStringUtils.randomAlphabetic(5) + <% } %>DEFAULT_EMAIL);
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
<%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
<%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
<%_ if (databaseTypeSql && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM);
<%_ } _%>
        return user;
    }
<%_ if (databaseTypeSql && reactive) { _%>

    /**
     * Delete all the users from the database.
     */
    public static void deleteEntities(EntityManager em) {
        try {
            em.deleteAll("<%= jhiTablePrefix %>_user_authority").block();
            em.deleteAll(<%= user.persistClass %>.class).block();
        } catch (Exception e) {
            // It can fail, if other entities are still referring this - it will be removed later.
        }
    }
<%_ } _%>

    /**
     * Setups the database with one user.
     */
    public static <%= user.persistClass %> initTestUser(UserRepository userRepository<% if (databaseTypeSql && ormTool === 'hibernate') { %>, EntityManager em<% } _%>) {
<%_ if (databaseTypeSql && reactive) { _%>
        userRepository.deleteAllUserAuthorities().block();
<%_ } _%>
        userRepository.deleteAll()<% if (reactive) { %>.block()<% } %>;
        <%= user.persistClass %> user = createEntity(<% if (databaseTypeSql && ormTool === 'hibernate' ) { %>em<% } _%>);
<%_ if (databaseTypeSql) { _%>
        user.setLogin(DEFAULT_LOGIN);
        user.setEmail(DEFAULT_EMAIL);
<%_ } _%>
        return user;
    }

    @BeforeEach
    public void initTest() {
        user = initTestUser(userRepository<% if (databaseTypeSql && ormTool === 'hibernate') { %>, em<% } _%>);
    }
<%_ if (!authenticationTypeOauth2) { _%>

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void createUser() throws Exception {
        int databaseSizeBeforeCreate = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Create the User
        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setLogin(DEFAULT_LOGIN);
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
        user.setEmail(DEFAULT_EMAIL);
        user.setActivated(true);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
  <%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

  <%_ if (reactive) { _%>
        webTestClient.post().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isCreated();
  <%_ } else { _%>
        restUserMockMvc.perform(post("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isCreated());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeCreate + 1);
            <%= user.persistClass %> testUser = users.get(users.size() - 1);
            assertThat(testUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
            assertThat(testUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
  <%_ if (!databaseTypeCassandra) { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
  <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
        });
  <%_ if (!databaseTypeSql) {_%>
            userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
            userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void createUserWithExistingId() throws Exception {
        int databaseSizeBeforeCreate = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
  <%_ if (databaseTypeCassandra) { _%>
        user.setId(UUID.randomUUID().toString());
  <%_ } else if (databaseTypeMongodb || databaseTypeNeo4j || databaseTypeCouchbase) { _%>
        user.setId("1L");
  <%_ } else { _%>
        user.setId(DEFAULT_ID);
  <%_ } _%>
        user.setLogin(DEFAULT_LOGIN);
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
        user.setEmail(DEFAULT_EMAIL);
        user.setActivated(true);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
  <%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

        // An entity with an existing ID cannot be created, so this API call must fail
  <%_ if (reactive) { _%>
        webTestClient.post().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isBadRequest();
  <%_ } else { _%>
        restUserMockMvc.perform(post("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void createUserWithExistingLogin() throws Exception {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
    userRepository.insert(user);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } _%>
        int databaseSizeBeforeCreate = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setLogin(DEFAULT_LOGIN);// this login should already be used
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
        user.setEmail("anothermail@localhost");
        user.setActivated(true);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
  <%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

        // Create the User
  <%_ if (reactive) { _%>
        webTestClient.post().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isBadRequest();
  <%_ } else { _%>
        restUserMockMvc.perform(post("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void createUserWithExistingEmail() throws Exception {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
    userRepository.insert(user);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } _%>
        int databaseSizeBeforeCreate = userRepository.findAll()<% if (reactive) { %>
            .collectList().block()<% } %>.size();

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setLogin("anotherlogin");
        user.setFirstName(DEFAULT_FIRSTNAME);
        user.setLastName(DEFAULT_LASTNAME);
        user.setEmail(DEFAULT_EMAIL);// this email should already be used
        user.setActivated(true);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(DEFAULT_IMAGEURL);
  <%_ } _%>
        user.setLangKey(DEFAULT_LANGKEY);
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

        // Create the User
  <%_ if (reactive) { _%>
        webTestClient.post().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isBadRequest();
  <%_ } else { _%>
        restUserMockMvc.perform(post("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeCreate));
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void getAllUsers()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
    <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<% if (databaseTypeSql && reactive && authenticationTypeOauth2) { %>create<% } else { %><%= saveMethod %><% } %>(user)<% if (reactive) { %>.block()<% } %>;
    <%_ } else { _%>
        userRepository.insert(user);
    <%_ } _%>
  <%_ if (databaseTypeSql && reactive && !applicationTypeMicroservice) { _%>
        authorityRepository
            .findById(AuthoritiesConstants.USER)
            .flatMap(authority -> userRepository.saveUserAuthority(user.getId(), authority.getName()))
            .block();
  <%_ } _%>

        // Get all the users
  <%_ if (reactive) { _%>
        <%= user.adminUserDto %> foundUser = webTestClient.get().uri("/api/admin/users?sort=id,desc")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .returnResult(<%= user.adminUserDto %>.class).getResponseBody().blockFirst();

        assertThat(foundUser.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(foundUser.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(foundUser.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(foundUser.getEmail()).isEqualTo(DEFAULT_EMAIL);
    <%_ if (!databaseTypeCassandra) { _%>
        assertThat(foundUser.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
    <%_ } _%>
        assertThat(foundUser.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
    <%_ if (databaseTypeSql && !applicationTypeMicroservice) { _%>
        assertThat(foundUser.getAuthorities()).containsExactly(AuthoritiesConstants.USER);
    <%_ } _%>
  <%_ } else { _%>
        restUserMockMvc.perform(get("/api/admin/users<% if (databaseTypeSql) { %>?sort=id,desc<% } %>")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].login").value(hasItem(DEFAULT_LOGIN)))
            .andExpect(jsonPath("$.[*].firstName").value(hasItem(DEFAULT_FIRSTNAME)))
            .andExpect(jsonPath("$.[*].lastName").value(hasItem(DEFAULT_LASTNAME)))
            .andExpect(jsonPath("$.[*].email").value(hasItem(DEFAULT_EMAIL)))
    <%_ if (!databaseTypeCassandra) { _%>
            .andExpect(jsonPath("$.[*].imageUrl").value(hasItem(DEFAULT_IMAGEURL)))
    <%_ } _%>
            .andExpect(jsonPath("$.[*].langKey").value(hasItem(DEFAULT_LANGKEY)));
  <%_ } _%>
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void getUser()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<% if (databaseTypeSql && reactive && authenticationTypeOauth2) { %>create<% } else { %><%= saveMethod %><% } %>(user)<% if (reactive) { %>.block()<% } %>;
    <%_ } else { _%>
        userRepository.insert(user);
    <%_ } _%>
  <%_ if (databaseTypeSql && reactive && !applicationTypeMicroservice) { _%>
        authorityRepository
            .findById(AuthoritiesConstants.USER)
            .flatMap(authority -> userRepository.saveUserAuthority(user.getId(), authority.getName()))
            .block();
  <%_ } _%>

  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(user);
  <%_ } _%>
  <%_ if (cacheProviderAny) { _%>

        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNull();
  <%_ } _%>

        // Get the user
  <%_ if (reactive) { _%>
        webTestClient.get().uri("/api/admin/users/{login}", user.getLogin())
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.login").isEqualTo(user.getLogin())
            .jsonPath("$.firstName").isEqualTo(DEFAULT_FIRSTNAME)
            .jsonPath("$.lastName").isEqualTo(DEFAULT_LASTNAME)
            .jsonPath("$.email").isEqualTo(DEFAULT_EMAIL)
    <%_ if (!databaseTypeCassandra) { _%>
            .jsonPath("$.imageUrl").isEqualTo(DEFAULT_IMAGEURL)
    <%_ } _%>
            .jsonPath("$.langKey").isEqualTo(DEFAULT_LANGKEY)<%
                if (!databaseTypeSql || applicationTypeMicroservice) { %>;
            <%_ } else { _%>
            .jsonPath("$.authorities")
            .isEqualTo(AuthoritiesConstants.USER);
            <% } %>
  <%_ } else { _%>
        restUserMockMvc.perform(get("/api/admin/users/{login}", user.getLogin()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(user.getLogin()))
            .andExpect(jsonPath("$.firstName").value(DEFAULT_FIRSTNAME))
            .andExpect(jsonPath("$.lastName").value(DEFAULT_LASTNAME))
            .andExpect(jsonPath("$.email").value(DEFAULT_EMAIL))
    <%_ if (!databaseTypeCassandra) { _%>
            .andExpect(jsonPath("$.imageUrl").value(DEFAULT_IMAGEURL))
    <%_ } _%>
            .andExpect(jsonPath("$.langKey").value(DEFAULT_LANGKEY));
  <%_ } _%>

  <%_ if (cacheProviderMemcached) { _%>
        if (!(cacheManager instanceof NoOpCacheManager)) {
            assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNotNull();
        }
  <%_ } else if (cacheProviderAny) { _%>
        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNotNull();
  <%_ } _%>
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void getNonExistingUser()<% if (!reactive) { %> throws Exception<% } %> {
  <%_ if (reactive) { _%>
        webTestClient.get().uri("/api/admin/users/unknown")
            .exchange()
            .expectStatus().isNotFound();
  <%_ } else { _%>
        restUserMockMvc.perform(get("/api/admin/users/unknown"))
            .andExpect(status().isNotFound());
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void updateUser() throws Exception {
        // Initialize the database
    <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
    <%_ } else { _%>
        userRepository.insert(user);
    <%_ } _%>
        int databaseSizeBeforeUpdate = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Update the user
        <%= user.persistClass %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>orElseThrow<% } %>(<% if (javaVersion === '8' && !reactive) { %>() -> new NoSuchElementException("No value present")<% } %>);

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setId(updatedUser.getId());
        user.setLogin(updatedUser.getLogin());
        user.setFirstName(UPDATED_FIRSTNAME);
        user.setLastName(UPDATED_LASTNAME);
        user.setEmail(UPDATED_EMAIL);
        user.setActivated(updatedUser.isActivated());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(UPDATED_IMAGEURL);
  <%_ } _%>
        user.setLangKey(UPDATED_LANGKEY);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setCreatedBy(updatedUser.getCreatedBy());
        user.setCreatedDate(updatedUser.getCreatedDate());
        user.setLastModifiedBy(updatedUser.getLastModifiedBy());
        user.setLastModifiedDate(updatedUser.getLastModifiedDate());
  <%_ } _%>
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

  <%_ if (reactive) { _%>
        webTestClient.put().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isOk();
  <%_ } else { _%>
        restUserMockMvc.perform(put("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isOk());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeUpdate);
            <%= user.persistClass %> testUser = users.stream().filter(usr -> usr.getId().equals(updatedUser.getId())).findFirst().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>);
            assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
  <%_ if (!databaseTypeCassandra) { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
  <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
        });
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void updateUserLogin() throws Exception {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
    userRepository.insert(user);
  <%_ } _%>
        int databaseSizeBeforeUpdate = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Update the user
        <%= user.persistClass %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>orElseThrow<% } %>(<% if (javaVersion === '8' && !reactive) { %>() -> new NoSuchElementException("No value present")<% } %>);

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setId(updatedUser.getId());
        user.setLogin(UPDATED_LOGIN);
        user.setFirstName(UPDATED_FIRSTNAME);
        user.setLastName(UPDATED_LASTNAME);
        user.setEmail(UPDATED_EMAIL);
        user.setActivated(updatedUser.isActivated());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(UPDATED_IMAGEURL);
  <%_ } _%>
        user.setLangKey(UPDATED_LANGKEY);
  <%_ if (!databaseTypeCassandra) { _%>
        user.setCreatedBy(updatedUser.getCreatedBy());
        user.setCreatedDate(updatedUser.getCreatedDate());
        user.setLastModifiedBy(updatedUser.getLastModifiedBy());
        user.setLastModifiedDate(updatedUser.getLastModifiedDate());
  <%_ } _%>
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

  <%_ if (reactive) { _%>
        webTestClient.put().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isOk();
  <%_ } else { _%>
        restUserMockMvc.perform(put("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isOk());
  <%_ } _%>

        // Validate the User in the database
        assertPersistedUsers(users -> {
            assertThat(users).hasSize(databaseSizeBeforeUpdate);
            <%= user.persistClass %> testUser = users.stream().filter(usr -> usr.getId().equals(updatedUser.getId())).findFirst().orElseThrow(<% if (javaVersion === '8') { %>() -> new NoSuchElementException("No value present")<% } %>);
            assertThat(testUser.getLogin()).isEqualTo(UPDATED_LOGIN);
            assertThat(testUser.getFirstName()).isEqualTo(UPDATED_FIRSTNAME);
            assertThat(testUser.getLastName()).isEqualTo(UPDATED_LASTNAME);
            assertThat(testUser.getEmail()).isEqualTo(UPDATED_EMAIL);
  <%_ if (!databaseTypeCassandra) { _%>
            assertThat(testUser.getImageUrl()).isEqualTo(UPDATED_IMAGEURL);
  <%_ } _%>
            assertThat(testUser.getLangKey()).isEqualTo(UPDATED_LANGKEY);
        });
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void updateUserExistingEmail() throws Exception {
        // Initialize the database with 2 users
    <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
    <%_ } else { _%>
        userRepository.insert(user);
    <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(user);
  <%_ } _%>

        <%= user.persistClass %> anotherUser = new <%= user.persistClass %>();
  <%_ if (databaseTypeCassandra) { _%>
        anotherUser.setId(UUID.randomUUID().toString());
  <%_ } _%>
        anotherUser.setLogin("jhipster");
        anotherUser.setPassword(RandomStringUtils.randomAlphanumeric(60));
        anotherUser.setActivated(true);
        anotherUser.setEmail("jhipster@localhost");
        anotherUser.setFirstName("java");
        anotherUser.setLastName("hipster");
  <%_ if (!databaseTypeCassandra) { _%>
        anotherUser.setImageUrl("");
  <%_ } _%>
        anotherUser.setLangKey("en");
  <%_ if (databaseTypeSql && reactive) { _%>
        anotherUser.setCreatedBy(Constants.SYSTEM);
  <%_ } _%>
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.save<% if (databaseTypeSql && !reactive) { %>AndFlush<% } %>(anotherUser)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
        userRepository.insert(anotherUser);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(anotherUser);
  <%_ } _%>

        // Update the user
        <%= user.persistClass %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>orElseThrow<% } %>(<% if (javaVersion === '8' && !reactive) { %>() -> new NoSuchElementException("No value present")<% } %>);

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setId(updatedUser.getId());
        user.setLogin(updatedUser.getLogin());
        user.setFirstName(updatedUser.getFirstName());
        user.setLastName(updatedUser.getLastName());
        user.setEmail("jhipster@localhost");// this email should already be used by anotherUser
        user.setActivated(updatedUser.isActivated());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(updatedUser.getImageUrl());
  <%_ } _%>
        user.setLangKey(updatedUser.getLangKey());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setCreatedBy(updatedUser.getCreatedBy());
        user.setCreatedDate(updatedUser.getCreatedDate());
        user.setLastModifiedBy(updatedUser.getLastModifiedBy());
        user.setLastModifiedDate(updatedUser.getLastModifiedDate());
  <%_ } _%>
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

  <%_ if (reactive) { _%>
        webTestClient.put().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isBadRequest();
  <%_ } else { _%>
        restUserMockMvc.perform(put("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
  <%_ } _%>
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void updateUserExistingLogin() throws Exception {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
        userRepository.insert(user);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(user);
  <%_ } _%>

        <%= user.persistClass %> anotherUser = new <%= user.persistClass %>();
  <%_ if (databaseTypeCassandra) { _%>
        anotherUser.setId(UUID.randomUUID().toString());
  <%_ } _%>
        anotherUser.setLogin("jhipster");
        anotherUser.setPassword(RandomStringUtils.randomAlphanumeric(60));
        anotherUser.setActivated(true);
        anotherUser.setEmail("jhipster@localhost");
        anotherUser.setFirstName("java");
        anotherUser.setLastName("hipster");
  <%_ if (!databaseTypeCassandra) { _%>
        anotherUser.setImageUrl("");
  <%_ } _%>
        anotherUser.setLangKey("en");
  <%_ if (databaseTypeSql && reactive) { _%>
        anotherUser.setCreatedBy(Constants.SYSTEM);
  <%_ } _%>
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.save<% if (databaseTypeSql && !reactive) { %>AndFlush<% } %>(anotherUser)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
        userRepository.insert(anotherUser);
  <%_ } _%>
  <%_ if (searchEngineElasticsearch) { _%>
        userSearchRepository.save(anotherUser);
  <%_ } _%>

        // Update the user
        <%= user.persistClass %> updatedUser = userRepository.findById(user.getId()).<% if (reactive) { %>block<% } else { %>orElseThrow<% } %>(<% if (javaVersion === '8' && !reactive) { %>() -> new NoSuchElementException("No value present")<% } %>);

        <%= user.adminUserDto %> user = new <%= user.adminUserDto %>();
        user.setId(updatedUser.getId());
        user.setLogin("jhipster");// this login should already be used by anotherUser
        user.setFirstName(updatedUser.getFirstName());
        user.setLastName(updatedUser.getLastName());
        user.setEmail(updatedUser.getEmail());
        user.setActivated(updatedUser.isActivated());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setImageUrl(updatedUser.getImageUrl());
  <%_ } _%>
        user.setLangKey(updatedUser.getLangKey());
  <%_ if (!databaseTypeCassandra) { _%>
        user.setCreatedBy(updatedUser.getCreatedBy());
        user.setCreatedDate(updatedUser.getCreatedDate());
        user.setLastModifiedBy(updatedUser.getLastModifiedBy());
        user.setLastModifiedDate(updatedUser.getLastModifiedDate());
  <%_ } _%>
        user.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

  <%_ if (reactive) { _%>
        webTestClient.put().uri("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(user))
            .exchange()
            .expectStatus().isBadRequest();
  <%_ } else { _%>
        restUserMockMvc.perform(put("/api/admin/users")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(user))<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
  <%_ } _%>
  <%_ if (!databaseTypeSql) {_%>
        userRepository.deleteAll();
  <% } else if (searchEngineElasticsearch) { %>
        userSearchRepository.deleteAll();
  <%_ } _%>
    }

    @Test
  <%_ if (databaseTypeSql && !reactive) { _%>
    @Transactional
  <%_ } _%>
    void deleteUser()<% if (!reactive) { %> throws Exception<% } %> {
        // Initialize the database
  <%_ if (ormTool !== 'mybatis') { _%>
        userRepository.<%= saveMethod %>(user)<% if (reactive) { %>.block()<% } %>;
  <%_ } else { _%>
        userRepository.insert(user);
  <%_ } _%>
        int databaseSizeBeforeDelete = userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>
            .collectList().block()<% } %>.size();

        // Delete the user
  <%_ if (reactive) { _%>
        webTestClient.delete().uri("/api/admin/users/{login}", user.getLogin())
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isNoContent();
  <%_ } else { _%>
        restUserMockMvc.perform(delete("/api/admin/users/{login}", user.getLogin())
            .accept(MediaType.APPLICATION_JSON)<% if (authenticationUsesCsrf) { %>
            .with(csrf())<% } %>)
            .andExpect(status().isNoContent());
  <%_ } _%>
  <%_ if (cacheProviderAny) { _%>

        assertThat(cacheManager.getCache(UserRepository.USERS_BY_LOGIN_CACHE).get(user.getLogin())).isNull();
  <%_ } _%>

        // Validate the database is empty
        assertPersistedUsers(users -> assertThat(users).hasSize(databaseSizeBeforeDelete - 1));
    }
<%_ } _%>

    @Test
    void testUserEquals() throws Exception {
        TestUtil.equalsVerifier(<%= user.persistClass %>.class);
        <%= user.persistClass %> user1 = new <%= user.persistClass %>();
        user1.setId(DEFAULT_ID);
        <%= user.persistClass %> user2 = new <%= user.persistClass %>();
        user2.setId(user1.getId());
        assertThat(user1).isEqualTo(user2);
        user2.setId(<%- this.getPrimaryKeyValue(user.primaryKey.type, databaseType, 2) %>);
        assertThat(user1).isNotEqualTo(user2);
        user1.setId(null);
        assertThat(user1).isNotEqualTo(user2);
    }

    @Test
    void testUserDTOtoUser() {
        <%= user.adminUserDto %> userDTO = new <%= user.adminUserDto %>();
        userDTO.setId(DEFAULT_ID);
        userDTO.setLogin(DEFAULT_LOGIN);
        userDTO.setFirstName(DEFAULT_FIRSTNAME);
        userDTO.setLastName(DEFAULT_LASTNAME);
        userDTO.setEmail(DEFAULT_EMAIL);
        userDTO.setActivated(true);
<%_ if (!databaseTypeCassandra) { _%>
        userDTO.setImageUrl(DEFAULT_IMAGEURL);
<%_ } _%>
        userDTO.setLangKey(DEFAULT_LANGKEY);
<%_ if (!databaseTypeCassandra) { _%>
        userDTO.setCreatedBy(DEFAULT_LOGIN_ID);
        userDTO.setLastModifiedBy(DEFAULT_LOGIN_ID);
<%_ } _%>
        userDTO.setAuthorities(authorityService.findFirstByCode(AuthoritiesConstants.USER).map(Collections::<%= singletonName%>)<% if (reactive) { %>.blockOptional()<% } %>.orElse(null));

        <%= user.persistClass %> user = userMapper.userDTOToUser(userDTO);
        assertThat(user.getId()).isEqualTo(DEFAULT_ID);
        assertThat(user.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(user.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(user.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(user.getEmail()).isEqualTo(DEFAULT_EMAIL);
        assertThat(user.isActivated()).isTrue();
<%_ if (!databaseTypeCassandra) { _%>
        assertThat(user.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
<%_ } _%>
        assertThat(user.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
<%_ if (!databaseTypeCassandra) { _%>
        assertThat(user.getCreatedBy()).isNull();
        assertThat(user.getCreatedDate()).isNotNull();
        assertThat(user.getLastModifiedBy()).isNull();
        assertThat(user.getLastModifiedDate()).isNotNull();
<%_ } _%>
        assertThat(user.getAuthorities())<% if (!databaseTypeCassandra && !databaseTypeCouchbase) { %>.extracting("name")<%_ } _%>.containsExactly(AuthoritiesConstants.USER);
    }

    @Test
    void testUserToUserDTO() {
        user.setId(DEFAULT_ID);
<%_ if (!databaseTypeCassandra) { _%>
        user.setCreatedBy(DEFAULT_ID);
        user.setCreatedDate(ZonedDateTime.now());
        user.setLastModifiedBy(DEFAULT_ID);
        user.setLastModifiedDate(ZonedDateTime.now());
<%_ } _%>
<%_ if (!databaseTypeCassandra && !databaseTypeCouchbase) { _%>
        <%= ormTool !== 'mybatis' ? 'Set' : 'List'%><Authority> authorities = new <%= ormTool !== 'mybatis' ? 'LinkedHashSet' : 'ArrayList'%><>();
        Authority authority = new Authority();
        authority.setName(AuthoritiesConstants.USER);
        authorities.add(authority);
        user.setAuthorities(authorities);
<%_ } else { _%>
        user.setAuthorities(Stream.of(AuthoritiesConstants.USER).collect(Collectors.toSet()));
<%_ } _%>

        <%= user.adminUserDto %> userDTO = userMapper.userToAdminUserDTO(user);

        assertThat(userDTO.getId()).isEqualTo(DEFAULT_ID);
        assertThat(userDTO.getLogin()).isEqualTo(DEFAULT_LOGIN);
        assertThat(userDTO.getFirstName()).isEqualTo(DEFAULT_FIRSTNAME);
        assertThat(userDTO.getLastName()).isEqualTo(DEFAULT_LASTNAME);
        assertThat(userDTO.getEmail()).isEqualTo(DEFAULT_EMAIL);
        assertThat(userDTO.isActivated()).isTrue();
<%_ if (!databaseTypeCassandra) { _%>
        assertThat(userDTO.getImageUrl()).isEqualTo(DEFAULT_IMAGEURL);
<%_ } _%>
        assertThat(userDTO.getLangKey()).isEqualTo(DEFAULT_LANGKEY);
<%_ if (!databaseTypeCassandra) { _%>
        assertThat(userDTO.getCreatedBy()).isEqualTo(DEFAULT_LOGIN_ID);
        assertThat(userDTO.getCreatedDate()).isEqualTo(user.getCreatedDate());
        assertThat(userDTO.getLastModifiedBy()).isEqualTo(DEFAULT_LOGIN_ID);
        assertThat(userDTO.getLastModifiedDate()).isEqualTo(user.getLastModifiedDate());
<%_ } _%>
        assertThat(userDTO.getAuthorities()).containsExactly(authorityService.findFirstByCode(AuthoritiesConstants.USER).orElse(null));
        assertThat(userDTO.toString()).isNotNull();
    }
<%_ if (databaseTypeSql || databaseTypeMongodb || databaseTypeNeo4j) { _%>

    @Test
    void testAuthorityEquals() {
        Authority authorityA = new Authority();
        assertThat(authorityA)
          .isNotEqualTo(null)
          .isNotEqualTo(new Object());
        assertThat(authorityA.hashCode()).isZero();
        assertThat(authorityA.toString()).isNotNull();

        Authority authorityB = new Authority();
        assertThat(authorityA).isEqualTo(authorityB);

        authorityB.setName(AuthoritiesConstants.ADMIN);
        assertThat(authorityA).isNotEqualTo(authorityB);

        authorityA.setName(AuthoritiesConstants.USER);
        assertThat(authorityA).isNotEqualTo(authorityB);

        authorityB.setName(AuthoritiesConstants.USER);
        assertThat(authorityA)
          .isEqualTo(authorityB)
          .hasSameHashCodeAs(authorityB);
    }
<%_ } _%>

    private void assertPersistedUsers(Consumer<List<<%= user.persistClass %>>> userAssertion) {
<%_ if (databaseTypeCouchbase) { _%>
        // The security filter chain clears the security context after remote calls
        SecurityContextHolder.setContext(TestSecurityContextHolder.getContext());
<%_ } _%>
        userAssertion.accept(userRepository.<% if (ormTool !== 'mybatis') { %>findAll()<% } else { %>selectList(null)<% } %><% if (reactive) { %>.collectList().block()<% } %>);
    }
}
