package it.sw.pa.comune.artegna.web.rest;

import static it.sw.pa.comune.artegna.domain.UtenteAppAsserts.*;
import static it.sw.pa.comune.artegna.web.rest.TestUtil.createUpdateProxyForBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.Mockito.*;
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.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import it.sw.pa.comune.artegna.IntegrationTest;
import it.sw.pa.comune.artegna.domain.UtenteApp;
import it.sw.pa.comune.artegna.domain.enumeration.Ruolo;
import it.sw.pa.comune.artegna.repository.UserRepository;
import it.sw.pa.comune.artegna.repository.UtenteAppRepository;
import it.sw.pa.comune.artegna.service.UtenteAppService;
import it.sw.pa.comune.artegna.service.dto.UtenteAppDTO;
import it.sw.pa.comune.artegna.service.mapper.UtenteAppMapper;
import jakarta.persistence.EntityManager;
import java.util.ArrayList;
import java.util.Random;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.transaction.annotation.Transactional;

/**
 * Integration tests for the {@link UtenteAppResource} REST controller.
 */
@IntegrationTest
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@WithMockUser
class UtenteAppResourceIT {

    private static final String DEFAULT_USERNAME = "AAAAAAAAAA";
    private static final String UPDATED_USERNAME = "BBBBBBBBBB";

    private static final String DEFAULT_EMAIL = "AAAAAAAAAA";
    private static final String UPDATED_EMAIL = "BBBBBBBBBB";

    private static final String DEFAULT_TELEFONO = "AAAAAAAAAA";
    private static final String UPDATED_TELEFONO = "BBBBBBBBBB";

    private static final Ruolo DEFAULT_RUOLO = Ruolo.USER;
    private static final Ruolo UPDATED_RUOLO = Ruolo.INCARICATO;

    private static final Boolean DEFAULT_ATTIVO = false;
    private static final Boolean UPDATED_ATTIVO = true;

    private static final String DEFAULT_NOME = "AAAAAAAAAA";
    private static final String UPDATED_NOME = "BBBBBBBBBB";

    private static final String DEFAULT_COGNOME = "AAAAAAAAAA";
    private static final String UPDATED_COGNOME = "BBBBBBBBBB";

    private static final String DEFAULT_LUOGO_NASCITA = "AAAAAAAAAA";
    private static final String UPDATED_LUOGO_NASCITA = "BBBBBBBBBB";

    private static final String DEFAULT_DATA_NASCITA = "AAAAAAAAAA";
    private static final String UPDATED_DATA_NASCITA = "BBBBBBBBBB";

    private static final String DEFAULT_RESIDENTE = "AAAAAAAAAA";
    private static final String UPDATED_RESIDENTE = "BBBBBBBBBB";

    private static final String DEFAULT_SOCIETA = "AAAAAAAAAA";
    private static final String UPDATED_SOCIETA = "BBBBBBBBBB";

    private static final String DEFAULT_SEDE = "AAAAAAAAAA";
    private static final String UPDATED_SEDE = "BBBBBBBBBB";

    private static final String DEFAULT_CODFISCALE = "AAAAAAAAAA";
    private static final String UPDATED_CODFISCALE = "BBBBBBBBBB";

    private static final String DEFAULT_TELEFONO_SOC = "AAAAAAAAAA";
    private static final String UPDATED_TELEFONO_SOC = "BBBBBBBBBB";

    private static final String DEFAULT_EMAIL_SOC = "AAAAAAAAAA";
    private static final String UPDATED_EMAIL_SOC = "BBBBBBBBBB";

    private static final String ENTITY_API_URL = "/api/utente-apps";
    private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";

    private static Random random = new Random();
    private static AtomicLong longCount = new AtomicLong(random.nextInt() + (2L * Integer.MAX_VALUE));

    @Autowired
    private ObjectMapper om;

    @Autowired
    private UtenteAppRepository utenteAppRepository;

    @Autowired
    private UserRepository userRepository;

    @Mock
    private UtenteAppRepository utenteAppRepositoryMock;

    @Autowired
    private UtenteAppMapper utenteAppMapper;

    @Mock
    private UtenteAppService utenteAppServiceMock;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restUtenteAppMockMvc;

    private UtenteApp utenteApp;

    private UtenteApp insertedUtenteApp;

    /**
     * Create an entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UtenteApp createEntity() {
        return new UtenteApp()
            .username(DEFAULT_USERNAME)
            .email(DEFAULT_EMAIL)
            .telefono(DEFAULT_TELEFONO)
            .ruolo(DEFAULT_RUOLO)
            .attivo(DEFAULT_ATTIVO)
            .nome(DEFAULT_NOME)
            .cognome(DEFAULT_COGNOME)
            .luogoNascita(DEFAULT_LUOGO_NASCITA)
            .dataNascita(DEFAULT_DATA_NASCITA)
            .residente(DEFAULT_RESIDENTE)
            .societa(DEFAULT_SOCIETA)
            .sede(DEFAULT_SEDE)
            .codfiscale(DEFAULT_CODFISCALE)
            .telefonoSoc(DEFAULT_TELEFONO_SOC)
            .emailSoc(DEFAULT_EMAIL_SOC);
    }

    /**
     * Create an updated entity for this test.
     *
     * This is a static method, as tests for other entities might also need it,
     * if they test an entity which requires the current entity.
     */
    public static UtenteApp createUpdatedEntity() {
        return new UtenteApp()
            .username(UPDATED_USERNAME)
            .email(UPDATED_EMAIL)
            .telefono(UPDATED_TELEFONO)
            .ruolo(UPDATED_RUOLO)
            .attivo(UPDATED_ATTIVO)
            .nome(UPDATED_NOME)
            .cognome(UPDATED_COGNOME)
            .luogoNascita(UPDATED_LUOGO_NASCITA)
            .dataNascita(UPDATED_DATA_NASCITA)
            .residente(UPDATED_RESIDENTE)
            .societa(UPDATED_SOCIETA)
            .sede(UPDATED_SEDE)
            .codfiscale(UPDATED_CODFISCALE)
            .telefonoSoc(UPDATED_TELEFONO_SOC)
            .emailSoc(UPDATED_EMAIL_SOC);
    }

    @BeforeEach
    void initTest() {
        utenteApp = createEntity();
    }

    @AfterEach
    void cleanup() {
        if (insertedUtenteApp != null) {
            utenteAppRepository.delete(insertedUtenteApp);
            insertedUtenteApp = null;
        }
    }

    @Test
    @Transactional
    void createUtenteApp() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);
        var returnedUtenteAppDTO = om.readValue(
            restUtenteAppMockMvc
                .perform(
                    post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO))
                )
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            UtenteAppDTO.class
        );

        // Validate the UtenteApp in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedUtenteApp = utenteAppMapper.toEntity(returnedUtenteAppDTO);
        assertUtenteAppUpdatableFieldsEquals(returnedUtenteApp, getPersistedUtenteApp(returnedUtenteApp));

        insertedUtenteApp = returnedUtenteApp;
    }

    @Test
    @Transactional
    void createUtenteAppWithExistingId() throws Exception {
        // Create the UtenteApp with an existing ID
        utenteApp.setId(1L);
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        long databaseSizeBeforeCreate = getRepositoryCount();

        // An entity with an existing ID cannot be created, so this API call must fail
        restUtenteAppMockMvc
            .perform(post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isBadRequest());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeCreate);
    }

    @Test
    @Transactional
    void checkUsernameIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        utenteApp.setUsername(null);

        // Create the UtenteApp, which fails.
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        restUtenteAppMockMvc
            .perform(post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkEmailIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        utenteApp.setEmail(null);

        // Create the UtenteApp, which fails.
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        restUtenteAppMockMvc
            .perform(post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkRuoloIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        utenteApp.setRuolo(null);

        // Create the UtenteApp, which fails.
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        restUtenteAppMockMvc
            .perform(post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkAttivoIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        utenteApp.setAttivo(null);

        // Create the UtenteApp, which fails.
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        restUtenteAppMockMvc
            .perform(post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllUtenteApps() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        // Get all the utenteAppList
        restUtenteAppMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(utenteApp.getId().intValue())))
            .andExpect(jsonPath("$.[*].username").value(hasItem(DEFAULT_USERNAME)))
            .andExpect(jsonPath("$.[*].email").value(hasItem(DEFAULT_EMAIL)))
            .andExpect(jsonPath("$.[*].telefono").value(hasItem(DEFAULT_TELEFONO)))
            .andExpect(jsonPath("$.[*].ruolo").value(hasItem(DEFAULT_RUOLO.toString())))
            .andExpect(jsonPath("$.[*].attivo").value(hasItem(DEFAULT_ATTIVO)))
            .andExpect(jsonPath("$.[*].nome").value(hasItem(DEFAULT_NOME)))
            .andExpect(jsonPath("$.[*].cognome").value(hasItem(DEFAULT_COGNOME)))
            .andExpect(jsonPath("$.[*].luogoNascita").value(hasItem(DEFAULT_LUOGO_NASCITA)))
            .andExpect(jsonPath("$.[*].dataNascita").value(hasItem(DEFAULT_DATA_NASCITA)))
            .andExpect(jsonPath("$.[*].residente").value(hasItem(DEFAULT_RESIDENTE)))
            .andExpect(jsonPath("$.[*].societa").value(hasItem(DEFAULT_SOCIETA)))
            .andExpect(jsonPath("$.[*].sede").value(hasItem(DEFAULT_SEDE)))
            .andExpect(jsonPath("$.[*].codfiscale").value(hasItem(DEFAULT_CODFISCALE)))
            .andExpect(jsonPath("$.[*].telefonoSoc").value(hasItem(DEFAULT_TELEFONO_SOC)))
            .andExpect(jsonPath("$.[*].emailSoc").value(hasItem(DEFAULT_EMAIL_SOC)));
    }

    @SuppressWarnings({ "unchecked" })
    void getAllUtenteAppsWithEagerRelationshipsIsEnabled() throws Exception {
        when(utenteAppServiceMock.findAllWithEagerRelationships(any())).thenReturn(new PageImpl(new ArrayList<>()));

        restUtenteAppMockMvc.perform(get(ENTITY_API_URL + "?eagerload=true")).andExpect(status().isOk());

        verify(utenteAppServiceMock, times(1)).findAllWithEagerRelationships(any());
    }

    @SuppressWarnings({ "unchecked" })
    void getAllUtenteAppsWithEagerRelationshipsIsNotEnabled() throws Exception {
        when(utenteAppServiceMock.findAllWithEagerRelationships(any())).thenReturn(new PageImpl(new ArrayList<>()));

        restUtenteAppMockMvc.perform(get(ENTITY_API_URL + "?eagerload=false")).andExpect(status().isOk());
        verify(utenteAppRepositoryMock, times(1)).findAll(any(Pageable.class));
    }

    @Test
    @Transactional
    void getUtenteApp() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        // Get the utenteApp
        restUtenteAppMockMvc
            .perform(get(ENTITY_API_URL_ID, utenteApp.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(utenteApp.getId().intValue()))
            .andExpect(jsonPath("$.username").value(DEFAULT_USERNAME))
            .andExpect(jsonPath("$.email").value(DEFAULT_EMAIL))
            .andExpect(jsonPath("$.telefono").value(DEFAULT_TELEFONO))
            .andExpect(jsonPath("$.ruolo").value(DEFAULT_RUOLO.toString()))
            .andExpect(jsonPath("$.attivo").value(DEFAULT_ATTIVO))
            .andExpect(jsonPath("$.nome").value(DEFAULT_NOME))
            .andExpect(jsonPath("$.cognome").value(DEFAULT_COGNOME))
            .andExpect(jsonPath("$.luogoNascita").value(DEFAULT_LUOGO_NASCITA))
            .andExpect(jsonPath("$.dataNascita").value(DEFAULT_DATA_NASCITA))
            .andExpect(jsonPath("$.residente").value(DEFAULT_RESIDENTE))
            .andExpect(jsonPath("$.societa").value(DEFAULT_SOCIETA))
            .andExpect(jsonPath("$.sede").value(DEFAULT_SEDE))
            .andExpect(jsonPath("$.codfiscale").value(DEFAULT_CODFISCALE))
            .andExpect(jsonPath("$.telefonoSoc").value(DEFAULT_TELEFONO_SOC))
            .andExpect(jsonPath("$.emailSoc").value(DEFAULT_EMAIL_SOC));
    }

    @Test
    @Transactional
    void getNonExistingUtenteApp() throws Exception {
        // Get the utenteApp
        restUtenteAppMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putExistingUtenteApp() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the utenteApp
        UtenteApp updatedUtenteApp = utenteAppRepository.findById(utenteApp.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedUtenteApp are not directly saved in db
        em.detach(updatedUtenteApp);
        updatedUtenteApp
            .username(UPDATED_USERNAME)
            .email(UPDATED_EMAIL)
            .telefono(UPDATED_TELEFONO)
            .ruolo(UPDATED_RUOLO)
            .attivo(UPDATED_ATTIVO)
            .nome(UPDATED_NOME)
            .cognome(UPDATED_COGNOME)
            .luogoNascita(UPDATED_LUOGO_NASCITA)
            .dataNascita(UPDATED_DATA_NASCITA)
            .residente(UPDATED_RESIDENTE)
            .societa(UPDATED_SOCIETA)
            .sede(UPDATED_SEDE)
            .codfiscale(UPDATED_CODFISCALE)
            .telefonoSoc(UPDATED_TELEFONO_SOC)
            .emailSoc(UPDATED_EMAIL_SOC);
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(updatedUtenteApp);

        restUtenteAppMockMvc
            .perform(
                put(ENTITY_API_URL_ID, utenteAppDTO.getId())
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isOk());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedUtenteAppToMatchAllProperties(updatedUtenteApp);
    }

    @Test
    @Transactional
    void putNonExistingUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(
                put(ENTITY_API_URL_ID, utenteAppDTO.getId())
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithIdMismatchUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(
                put(ENTITY_API_URL_ID, longCount.incrementAndGet())
                    .with(csrf())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void putWithMissingIdPathParamUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(put(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(utenteAppDTO)))
            .andExpect(status().isMethodNotAllowed());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void partialUpdateUtenteAppWithPatch() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the utenteApp using partial update
        UtenteApp partialUpdatedUtenteApp = new UtenteApp();
        partialUpdatedUtenteApp.setId(utenteApp.getId());

        partialUpdatedUtenteApp
            .email(UPDATED_EMAIL)
            .ruolo(UPDATED_RUOLO)
            .attivo(UPDATED_ATTIVO)
            .nome(UPDATED_NOME)
            .cognome(UPDATED_COGNOME)
            .luogoNascita(UPDATED_LUOGO_NASCITA)
            .residente(UPDATED_RESIDENTE)
            .sede(UPDATED_SEDE)
            .telefonoSoc(UPDATED_TELEFONO_SOC);

        restUtenteAppMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedUtenteApp.getId())
                    .with(csrf())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(partialUpdatedUtenteApp))
            )
            .andExpect(status().isOk());

        // Validate the UtenteApp in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertUtenteAppUpdatableFieldsEquals(
            createUpdateProxyForBean(partialUpdatedUtenteApp, utenteApp),
            getPersistedUtenteApp(utenteApp)
        );
    }

    @Test
    @Transactional
    void fullUpdateUtenteAppWithPatch() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the utenteApp using partial update
        UtenteApp partialUpdatedUtenteApp = new UtenteApp();
        partialUpdatedUtenteApp.setId(utenteApp.getId());

        partialUpdatedUtenteApp
            .username(UPDATED_USERNAME)
            .email(UPDATED_EMAIL)
            .telefono(UPDATED_TELEFONO)
            .ruolo(UPDATED_RUOLO)
            .attivo(UPDATED_ATTIVO)
            .nome(UPDATED_NOME)
            .cognome(UPDATED_COGNOME)
            .luogoNascita(UPDATED_LUOGO_NASCITA)
            .dataNascita(UPDATED_DATA_NASCITA)
            .residente(UPDATED_RESIDENTE)
            .societa(UPDATED_SOCIETA)
            .sede(UPDATED_SEDE)
            .codfiscale(UPDATED_CODFISCALE)
            .telefonoSoc(UPDATED_TELEFONO_SOC)
            .emailSoc(UPDATED_EMAIL_SOC);

        restUtenteAppMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedUtenteApp.getId())
                    .with(csrf())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(partialUpdatedUtenteApp))
            )
            .andExpect(status().isOk());

        // Validate the UtenteApp in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertUtenteAppUpdatableFieldsEquals(partialUpdatedUtenteApp, getPersistedUtenteApp(partialUpdatedUtenteApp));
    }

    @Test
    @Transactional
    void patchNonExistingUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If the entity doesn't have an ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, utenteAppDTO.getId())
                    .with(csrf())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithIdMismatchUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, longCount.incrementAndGet())
                    .with(csrf())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isBadRequest());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void patchWithMissingIdPathParamUtenteApp() throws Exception {
        long databaseSizeBeforeUpdate = getRepositoryCount();
        utenteApp.setId(longCount.incrementAndGet());

        // Create the UtenteApp
        UtenteAppDTO utenteAppDTO = utenteAppMapper.toDto(utenteApp);

        // If url ID doesn't match entity ID, it will throw BadRequestAlertException
        restUtenteAppMockMvc
            .perform(
                patch(ENTITY_API_URL).with(csrf()).contentType("application/merge-patch+json").content(om.writeValueAsBytes(utenteAppDTO))
            )
            .andExpect(status().isMethodNotAllowed());

        // Validate the UtenteApp in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
    }

    @Test
    @Transactional
    void deleteUtenteApp() throws Exception {
        // Initialize the database
        insertedUtenteApp = utenteAppRepository.saveAndFlush(utenteApp);

        long databaseSizeBeforeDelete = getRepositoryCount();

        // Delete the utenteApp
        restUtenteAppMockMvc
            .perform(delete(ENTITY_API_URL_ID, utenteApp.getId()).with(csrf()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

        // Validate the database contains one less item
        assertDecrementedRepositoryCount(databaseSizeBeforeDelete);
    }

    protected long getRepositoryCount() {
        return utenteAppRepository.count();
    }

    protected void assertIncrementedRepositoryCount(long countBefore) {
        assertThat(countBefore + 1).isEqualTo(getRepositoryCount());
    }

    protected void assertDecrementedRepositoryCount(long countBefore) {
        assertThat(countBefore - 1).isEqualTo(getRepositoryCount());
    }

    protected void assertSameRepositoryCount(long countBefore) {
        assertThat(countBefore).isEqualTo(getRepositoryCount());
    }

    protected UtenteApp getPersistedUtenteApp(UtenteApp utenteApp) {
        return utenteAppRepository.findById(utenteApp.getId()).orElseThrow();
    }

    protected void assertPersistedUtenteAppToMatchAllProperties(UtenteApp expectedUtenteApp) {
        assertUtenteAppAllPropertiesEquals(expectedUtenteApp, getPersistedUtenteApp(expectedUtenteApp));
    }

    protected void assertPersistedUtenteAppToMatchUpdatableProperties(UtenteApp expectedUtenteApp) {
        assertUtenteAppAllUpdatablePropertiesEquals(expectedUtenteApp, getPersistedUtenteApp(expectedUtenteApp));
    }
}
