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

import static it.sw.pa.comune.artegna.domain.NotificaAsserts.*;
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.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.Conferma;
import it.sw.pa.comune.artegna.domain.Notifica;
import it.sw.pa.comune.artegna.domain.enumeration.TipoCanaleNotifica;
import it.sw.pa.comune.artegna.domain.enumeration.TipoEventoNotifica;
import it.sw.pa.comune.artegna.repository.NotificaRepository;
import it.sw.pa.comune.artegna.service.dto.NotificaDTO;
import it.sw.pa.comune.artegna.service.mapper.NotificaMapper;
import jakarta.persistence.EntityManager;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
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.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
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 NotificaResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class NotificaResourceIT {

    private static final TipoCanaleNotifica DEFAULT_TIPO_CANALE = TipoCanaleNotifica.EMAIL;
    private static final TipoCanaleNotifica UPDATED_TIPO_CANALE = TipoCanaleNotifica.WHATSAPP;

    private static final TipoEventoNotifica DEFAULT_TIPO_EVENTO = TipoEventoNotifica.RICHIESTA_CREATA;
    private static final TipoEventoNotifica UPDATED_TIPO_EVENTO = TipoEventoNotifica.PRENOTAZIONE_CONFERMATA;

    private static final String DEFAULT_MESSAGGIO = "AAAAAAAAAA";
    private static final String UPDATED_MESSAGGIO = "BBBBBBBBBB";

    private static final Boolean DEFAULT_INVIATA = false;
    private static final Boolean UPDATED_INVIATA = true;

    private static final Instant DEFAULT_INVIATA_AT = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_INVIATA_AT = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    private static final String DEFAULT_ERRORE = "AAAAAAAAAA";
    private static final String UPDATED_ERRORE = "BBBBBBBBBB";

    private static final Instant DEFAULT_CREATED_AT = Instant.ofEpochMilli(0L);
    private static final Instant UPDATED_CREATED_AT = Instant.now().truncatedTo(ChronoUnit.MILLIS);

    private static final String ENTITY_API_URL = "/api/notificas";
    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 NotificaRepository notificaRepository;

    @Autowired
    private NotificaMapper notificaMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restNotificaMockMvc;

    private Notifica notifica;

    private Notifica insertedNotifica;

    /**
     * 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 Notifica createEntity() {
        return new Notifica()
            .tipoCanale(DEFAULT_TIPO_CANALE)
            .tipoEvento(DEFAULT_TIPO_EVENTO)
            .messaggio(DEFAULT_MESSAGGIO)
            .inviata(DEFAULT_INVIATA)
            .inviataAt(DEFAULT_INVIATA_AT)
            .errore(DEFAULT_ERRORE)
            .createdAt(DEFAULT_CREATED_AT);
    }

    /**
     * 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 Notifica createUpdatedEntity() {
        return new Notifica()
            .tipoCanale(UPDATED_TIPO_CANALE)
            .tipoEvento(UPDATED_TIPO_EVENTO)
            .messaggio(UPDATED_MESSAGGIO)
            .inviata(UPDATED_INVIATA)
            .inviataAt(UPDATED_INVIATA_AT)
            .errore(UPDATED_ERRORE)
            .createdAt(UPDATED_CREATED_AT);
    }

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

    @AfterEach
    void cleanup() {
        if (insertedNotifica != null) {
            notificaRepository.delete(insertedNotifica);
            insertedNotifica = null;
        }
    }

    @Test
    @Transactional
    void createNotifica() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);
        var returnedNotificaDTO = om.readValue(
            restNotificaMockMvc
                .perform(
                    post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(notificaDTO))
                )
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            NotificaDTO.class
        );

        // Validate the Notifica in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedNotifica = notificaMapper.toEntity(returnedNotificaDTO);
        assertNotificaUpdatableFieldsEquals(returnedNotifica, getPersistedNotifica(returnedNotifica));

        insertedNotifica = returnedNotifica;
    }

    @Test
    @Transactional
    void createNotificaWithExistingId() throws Exception {
        // Create the Notifica with an existing ID
        notifica.setId(1L);
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

    @Test
    @Transactional
    void getAllNotificas() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(notifica.getId().intValue())))
            .andExpect(jsonPath("$.[*].tipoCanale").value(hasItem(DEFAULT_TIPO_CANALE.toString())))
            .andExpect(jsonPath("$.[*].tipoEvento").value(hasItem(DEFAULT_TIPO_EVENTO.toString())))
            .andExpect(jsonPath("$.[*].messaggio").value(hasItem(DEFAULT_MESSAGGIO)))
            .andExpect(jsonPath("$.[*].inviata").value(hasItem(DEFAULT_INVIATA)))
            .andExpect(jsonPath("$.[*].inviataAt").value(hasItem(DEFAULT_INVIATA_AT.toString())))
            .andExpect(jsonPath("$.[*].errore").value(hasItem(DEFAULT_ERRORE)))
            .andExpect(jsonPath("$.[*].createdAt").value(hasItem(DEFAULT_CREATED_AT.toString())));
    }

    @Test
    @Transactional
    void getNotifica() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get the notifica
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL_ID, notifica.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(notifica.getId().intValue()))
            .andExpect(jsonPath("$.tipoCanale").value(DEFAULT_TIPO_CANALE.toString()))
            .andExpect(jsonPath("$.tipoEvento").value(DEFAULT_TIPO_EVENTO.toString()))
            .andExpect(jsonPath("$.messaggio").value(DEFAULT_MESSAGGIO))
            .andExpect(jsonPath("$.inviata").value(DEFAULT_INVIATA))
            .andExpect(jsonPath("$.inviataAt").value(DEFAULT_INVIATA_AT.toString()))
            .andExpect(jsonPath("$.errore").value(DEFAULT_ERRORE))
            .andExpect(jsonPath("$.createdAt").value(DEFAULT_CREATED_AT.toString()));
    }

    @Test
    @Transactional
    void getNotificasByIdFiltering() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        Long id = notifica.getId();

        defaultNotificaFiltering("id.equals=" + id, "id.notEquals=" + id);

        defaultNotificaFiltering("id.greaterThanOrEqual=" + id, "id.greaterThan=" + id);

        defaultNotificaFiltering("id.lessThanOrEqual=" + id, "id.lessThan=" + id);
    }

    @Test
    @Transactional
    void getAllNotificasByTipoCanaleIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoCanale equals to
        defaultNotificaFiltering("tipoCanale.equals=" + DEFAULT_TIPO_CANALE, "tipoCanale.equals=" + UPDATED_TIPO_CANALE);
    }

    @Test
    @Transactional
    void getAllNotificasByTipoCanaleIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoCanale in
        defaultNotificaFiltering(
            "tipoCanale.in=" + DEFAULT_TIPO_CANALE + "," + UPDATED_TIPO_CANALE,
            "tipoCanale.in=" + UPDATED_TIPO_CANALE
        );
    }

    @Test
    @Transactional
    void getAllNotificasByTipoCanaleIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoCanale is not null
        defaultNotificaFiltering("tipoCanale.specified=true", "tipoCanale.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByTipoEventoIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoEvento equals to
        defaultNotificaFiltering("tipoEvento.equals=" + DEFAULT_TIPO_EVENTO, "tipoEvento.equals=" + UPDATED_TIPO_EVENTO);
    }

    @Test
    @Transactional
    void getAllNotificasByTipoEventoIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoEvento in
        defaultNotificaFiltering(
            "tipoEvento.in=" + DEFAULT_TIPO_EVENTO + "," + UPDATED_TIPO_EVENTO,
            "tipoEvento.in=" + UPDATED_TIPO_EVENTO
        );
    }

    @Test
    @Transactional
    void getAllNotificasByTipoEventoIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where tipoEvento is not null
        defaultNotificaFiltering("tipoEvento.specified=true", "tipoEvento.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByMessaggioIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where messaggio equals to
        defaultNotificaFiltering("messaggio.equals=" + DEFAULT_MESSAGGIO, "messaggio.equals=" + UPDATED_MESSAGGIO);
    }

    @Test
    @Transactional
    void getAllNotificasByMessaggioIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where messaggio in
        defaultNotificaFiltering("messaggio.in=" + DEFAULT_MESSAGGIO + "," + UPDATED_MESSAGGIO, "messaggio.in=" + UPDATED_MESSAGGIO);
    }

    @Test
    @Transactional
    void getAllNotificasByMessaggioIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where messaggio is not null
        defaultNotificaFiltering("messaggio.specified=true", "messaggio.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByMessaggioContainsSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where messaggio contains
        defaultNotificaFiltering("messaggio.contains=" + DEFAULT_MESSAGGIO, "messaggio.contains=" + UPDATED_MESSAGGIO);
    }

    @Test
    @Transactional
    void getAllNotificasByMessaggioNotContainsSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where messaggio does not contain
        defaultNotificaFiltering("messaggio.doesNotContain=" + UPDATED_MESSAGGIO, "messaggio.doesNotContain=" + DEFAULT_MESSAGGIO);
    }

    @Test
    @Transactional
    void getAllNotificasByInviataIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviata equals to
        defaultNotificaFiltering("inviata.equals=" + DEFAULT_INVIATA, "inviata.equals=" + UPDATED_INVIATA);
    }

    @Test
    @Transactional
    void getAllNotificasByInviataIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviata in
        defaultNotificaFiltering("inviata.in=" + DEFAULT_INVIATA + "," + UPDATED_INVIATA, "inviata.in=" + UPDATED_INVIATA);
    }

    @Test
    @Transactional
    void getAllNotificasByInviataIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviata is not null
        defaultNotificaFiltering("inviata.specified=true", "inviata.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByInviataAtIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviataAt equals to
        defaultNotificaFiltering("inviataAt.equals=" + DEFAULT_INVIATA_AT, "inviataAt.equals=" + UPDATED_INVIATA_AT);
    }

    @Test
    @Transactional
    void getAllNotificasByInviataAtIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviataAt in
        defaultNotificaFiltering("inviataAt.in=" + DEFAULT_INVIATA_AT + "," + UPDATED_INVIATA_AT, "inviataAt.in=" + UPDATED_INVIATA_AT);
    }

    @Test
    @Transactional
    void getAllNotificasByInviataAtIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where inviataAt is not null
        defaultNotificaFiltering("inviataAt.specified=true", "inviataAt.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByErroreIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where errore equals to
        defaultNotificaFiltering("errore.equals=" + DEFAULT_ERRORE, "errore.equals=" + UPDATED_ERRORE);
    }

    @Test
    @Transactional
    void getAllNotificasByErroreIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where errore in
        defaultNotificaFiltering("errore.in=" + DEFAULT_ERRORE + "," + UPDATED_ERRORE, "errore.in=" + UPDATED_ERRORE);
    }

    @Test
    @Transactional
    void getAllNotificasByErroreIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where errore is not null
        defaultNotificaFiltering("errore.specified=true", "errore.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByErroreContainsSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where errore contains
        defaultNotificaFiltering("errore.contains=" + DEFAULT_ERRORE, "errore.contains=" + UPDATED_ERRORE);
    }

    @Test
    @Transactional
    void getAllNotificasByErroreNotContainsSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where errore does not contain
        defaultNotificaFiltering("errore.doesNotContain=" + UPDATED_ERRORE, "errore.doesNotContain=" + DEFAULT_ERRORE);
    }

    @Test
    @Transactional
    void getAllNotificasByCreatedAtIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where createdAt equals to
        defaultNotificaFiltering("createdAt.equals=" + DEFAULT_CREATED_AT, "createdAt.equals=" + UPDATED_CREATED_AT);
    }

    @Test
    @Transactional
    void getAllNotificasByCreatedAtIsInShouldWork() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where createdAt in
        defaultNotificaFiltering("createdAt.in=" + DEFAULT_CREATED_AT + "," + UPDATED_CREATED_AT, "createdAt.in=" + UPDATED_CREATED_AT);
    }

    @Test
    @Transactional
    void getAllNotificasByCreatedAtIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        // Get all the notificaList where createdAt is not null
        defaultNotificaFiltering("createdAt.specified=true", "createdAt.specified=false");
    }

    @Test
    @Transactional
    void getAllNotificasByConfermaIsEqualToSomething() throws Exception {
        Conferma conferma;
        if (TestUtil.findAll(em, Conferma.class).isEmpty()) {
            notificaRepository.saveAndFlush(notifica);
            conferma = ConfermaResourceIT.createEntity();
        } else {
            conferma = TestUtil.findAll(em, Conferma.class).get(0);
        }
        em.persist(conferma);
        em.flush();
        notifica.setConferma(conferma);
        notificaRepository.saveAndFlush(notifica);
        Long confermaId = conferma.getId();
        // Get all the notificaList where conferma equals to confermaId
        defaultNotificaShouldBeFound("confermaId.equals=" + confermaId);

        // Get all the notificaList where conferma equals to (confermaId + 1)
        defaultNotificaShouldNotBeFound("confermaId.equals=" + (confermaId + 1));
    }

    private void defaultNotificaFiltering(String shouldBeFound, String shouldNotBeFound) throws Exception {
        defaultNotificaShouldBeFound(shouldBeFound);
        defaultNotificaShouldNotBeFound(shouldNotBeFound);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultNotificaShouldBeFound(String filter) throws Exception {
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(notifica.getId().intValue())))
            .andExpect(jsonPath("$.[*].tipoCanale").value(hasItem(DEFAULT_TIPO_CANALE.toString())))
            .andExpect(jsonPath("$.[*].tipoEvento").value(hasItem(DEFAULT_TIPO_EVENTO.toString())))
            .andExpect(jsonPath("$.[*].messaggio").value(hasItem(DEFAULT_MESSAGGIO)))
            .andExpect(jsonPath("$.[*].inviata").value(hasItem(DEFAULT_INVIATA)))
            .andExpect(jsonPath("$.[*].inviataAt").value(hasItem(DEFAULT_INVIATA_AT.toString())))
            .andExpect(jsonPath("$.[*].errore").value(hasItem(DEFAULT_ERRORE)))
            .andExpect(jsonPath("$.[*].createdAt").value(hasItem(DEFAULT_CREATED_AT.toString())));

        // Check, that the count call also returns 1
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL + "/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("1"));
    }

    /**
     * Executes the search, and checks that the default entity is not returned.
     */
    private void defaultNotificaShouldNotBeFound(String filter) throws Exception {
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$").isArray())
            .andExpect(jsonPath("$").isEmpty());

        // Check, that the count call also returns 0
        restNotificaMockMvc
            .perform(get(ENTITY_API_URL + "/count?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(content().string("0"));
    }

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

    @Test
    @Transactional
    void putExistingNotifica() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the notifica
        Notifica updatedNotifica = notificaRepository.findById(notifica.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedNotifica are not directly saved in db
        em.detach(updatedNotifica);
        updatedNotifica
            .tipoCanale(UPDATED_TIPO_CANALE)
            .tipoEvento(UPDATED_TIPO_EVENTO)
            .messaggio(UPDATED_MESSAGGIO)
            .inviata(UPDATED_INVIATA)
            .inviataAt(UPDATED_INVIATA_AT)
            .errore(UPDATED_ERRORE)
            .createdAt(UPDATED_CREATED_AT);
        NotificaDTO notificaDTO = notificaMapper.toDto(updatedNotifica);

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

        // Validate the Notifica in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedNotificaToMatchAllProperties(updatedNotifica);
    }

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

    @Test
    @Transactional
    void partialUpdateNotificaWithPatch() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the notifica using partial update
        Notifica partialUpdatedNotifica = new Notifica();
        partialUpdatedNotifica.setId(notifica.getId());

        partialUpdatedNotifica.tipoCanale(UPDATED_TIPO_CANALE).messaggio(UPDATED_MESSAGGIO);

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

        // Validate the Notifica in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertNotificaUpdatableFieldsEquals(createUpdateProxyForBean(partialUpdatedNotifica, notifica), getPersistedNotifica(notifica));
    }

    @Test
    @Transactional
    void fullUpdateNotificaWithPatch() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the notifica using partial update
        Notifica partialUpdatedNotifica = new Notifica();
        partialUpdatedNotifica.setId(notifica.getId());

        partialUpdatedNotifica
            .tipoCanale(UPDATED_TIPO_CANALE)
            .tipoEvento(UPDATED_TIPO_EVENTO)
            .messaggio(UPDATED_MESSAGGIO)
            .inviata(UPDATED_INVIATA)
            .inviataAt(UPDATED_INVIATA_AT)
            .errore(UPDATED_ERRORE)
            .createdAt(UPDATED_CREATED_AT);

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

        // Validate the Notifica in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertNotificaUpdatableFieldsEquals(partialUpdatedNotifica, getPersistedNotifica(partialUpdatedNotifica));
    }

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

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

        // Create the Notifica
        NotificaDTO notificaDTO = notificaMapper.toDto(notifica);

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

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

    @Test
    @Transactional
    void deleteNotifica() throws Exception {
        // Initialize the database
        insertedNotifica = notificaRepository.saveAndFlush(notifica);

        long databaseSizeBeforeDelete = getRepositoryCount();

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

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

    protected long getRepositoryCount() {
        return notificaRepository.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 Notifica getPersistedNotifica(Notifica notifica) {
        return notificaRepository.findById(notifica.getId()).orElseThrow();
    }

    protected void assertPersistedNotificaToMatchAllProperties(Notifica expectedNotifica) {
        assertNotificaAllPropertiesEquals(expectedNotifica, getPersistedNotifica(expectedNotifica));
    }

    protected void assertPersistedNotificaToMatchUpdatableProperties(Notifica expectedNotifica) {
        assertNotificaAllUpdatablePropertiesEquals(expectedNotifica, getPersistedNotifica(expectedNotifica));
    }
}
