package com.emonster.taroaichat.web.rest;

import static com.emonster.taroaichat.domain.DonationAsserts.*;
import static com.emonster.taroaichat.web.rest.TestUtil.createUpdateProxyForBean;
import static com.emonster.taroaichat.web.rest.TestUtil.sameNumber;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.emonster.taroaichat.IntegrationTest;
import com.emonster.taroaichat.domain.Donation;
import com.emonster.taroaichat.domain.TarotSession;
import com.emonster.taroaichat.domain.UserProfile;
import com.emonster.taroaichat.domain.enumeration.PaymentStatus;
import com.emonster.taroaichat.repository.DonationRepository;
import com.emonster.taroaichat.service.DonationService;
import com.emonster.taroaichat.service.dto.DonationDTO;
import com.emonster.taroaichat.service.mapper.DonationMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManager;
import java.math.BigDecimal;
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 DonationResource} REST controller.
 */
@IntegrationTest
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@WithMockUser
class DonationResourceIT {

    private static final BigDecimal DEFAULT_AMOUNT = new BigDecimal(0);
    private static final BigDecimal UPDATED_AMOUNT = new BigDecimal(1);
    private static final BigDecimal SMALLER_AMOUNT = new BigDecimal(0 - 1);

    private static final String DEFAULT_CURRENCY = "AAA";
    private static final String UPDATED_CURRENCY = "BBB";

    private static final String DEFAULT_STRIPE_PAYMENT_INTENT_ID = "AAAAAAAAAA";
    private static final String UPDATED_STRIPE_PAYMENT_INTENT_ID = "BBBBBBBBBB";

    private static final PaymentStatus DEFAULT_STATUS = PaymentStatus.PENDING;
    private static final PaymentStatus UPDATED_STATUS = PaymentStatus.COMPLETED;

    private static final String ENTITY_API_URL = "/api/donations";
    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() + (2 * Integer.MAX_VALUE));

    @Autowired
    private ObjectMapper om;

    @Autowired
    private DonationRepository donationRepository;

    @Mock
    private DonationRepository donationRepositoryMock;

    @Autowired
    private DonationMapper donationMapper;

    @Mock
    private DonationService donationServiceMock;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restDonationMockMvc;

    private Donation donation;

    private Donation insertedDonation;

    /**
     * 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 Donation createEntity(EntityManager em) {
        Donation donation = new Donation()
            .amount(DEFAULT_AMOUNT)
            .currency(DEFAULT_CURRENCY)
            .stripePaymentIntentId(DEFAULT_STRIPE_PAYMENT_INTENT_ID)
            .status(DEFAULT_STATUS);
        // Add required entity
        UserProfile userProfile;
        if (TestUtil.findAll(em, UserProfile.class).isEmpty()) {
            userProfile = UserProfileResourceIT.createEntity(em);
            em.persist(userProfile);
            em.flush();
        } else {
            userProfile = TestUtil.findAll(em, UserProfile.class).get(0);
        }
        donation.setUserProfile(userProfile);
        return donation;
    }

    /**
     * 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 Donation createUpdatedEntity(EntityManager em) {
        Donation updatedDonation = new Donation()
            .amount(UPDATED_AMOUNT)
            .currency(UPDATED_CURRENCY)
            .stripePaymentIntentId(UPDATED_STRIPE_PAYMENT_INTENT_ID)
            .status(UPDATED_STATUS);
        // Add required entity
        UserProfile userProfile;
        if (TestUtil.findAll(em, UserProfile.class).isEmpty()) {
            userProfile = UserProfileResourceIT.createUpdatedEntity(em);
            em.persist(userProfile);
            em.flush();
        } else {
            userProfile = TestUtil.findAll(em, UserProfile.class).get(0);
        }
        updatedDonation.setUserProfile(userProfile);
        return updatedDonation;
    }

    @BeforeEach
    void initTest() {
        donation = createEntity(em);
    }

    @AfterEach
    void cleanup() {
        if (insertedDonation != null) {
            donationRepository.delete(insertedDonation);
            insertedDonation = null;
        }
    }

    @Test
    @Transactional
    void createDonation() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);
        var returnedDonationDTO = om.readValue(
            restDonationMockMvc
                .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(donationDTO)))
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            DonationDTO.class
        );

        // Validate the Donation in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedDonation = donationMapper.toEntity(returnedDonationDTO);
        assertDonationUpdatableFieldsEquals(returnedDonation, getPersistedDonation(returnedDonation));

        insertedDonation = returnedDonation;
    }

    @Test
    @Transactional
    void createDonationWithExistingId() throws Exception {
        // Create the Donation with an existing ID
        donation.setId(1L);
        DonationDTO donationDTO = donationMapper.toDto(donation);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

    @Test
    @Transactional
    void checkAmountIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        donation.setAmount(null);

        // Create the Donation, which fails.
        DonationDTO donationDTO = donationMapper.toDto(donation);

        restDonationMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(donationDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkCurrencyIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        donation.setCurrency(null);

        // Create the Donation, which fails.
        DonationDTO donationDTO = donationMapper.toDto(donation);

        restDonationMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(donationDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkStripePaymentIntentIdIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        donation.setStripePaymentIntentId(null);

        // Create the Donation, which fails.
        DonationDTO donationDTO = donationMapper.toDto(donation);

        restDonationMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(donationDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkStatusIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        donation.setStatus(null);

        // Create the Donation, which fails.
        DonationDTO donationDTO = donationMapper.toDto(donation);

        restDonationMockMvc
            .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(donationDTO)))
            .andExpect(status().isBadRequest());

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllDonations() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList
        restDonationMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(donation.getId().intValue())))
            .andExpect(jsonPath("$.[*].amount").value(hasItem(sameNumber(DEFAULT_AMOUNT))))
            .andExpect(jsonPath("$.[*].currency").value(hasItem(DEFAULT_CURRENCY)))
            .andExpect(jsonPath("$.[*].stripePaymentIntentId").value(hasItem(DEFAULT_STRIPE_PAYMENT_INTENT_ID)))
            .andExpect(jsonPath("$.[*].status").value(hasItem(DEFAULT_STATUS.toString())));
    }

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

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

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

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

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

    @Test
    @Transactional
    void getDonation() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get the donation
        restDonationMockMvc
            .perform(get(ENTITY_API_URL_ID, donation.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(donation.getId().intValue()))
            .andExpect(jsonPath("$.amount").value(sameNumber(DEFAULT_AMOUNT)))
            .andExpect(jsonPath("$.currency").value(DEFAULT_CURRENCY))
            .andExpect(jsonPath("$.stripePaymentIntentId").value(DEFAULT_STRIPE_PAYMENT_INTENT_ID))
            .andExpect(jsonPath("$.status").value(DEFAULT_STATUS.toString()));
    }

    @Test
    @Transactional
    void getDonationsByIdFiltering() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        Long id = donation.getId();

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

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

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

    @Test
    @Transactional
    void getAllDonationsByAmountIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount equals to
        defaultDonationFiltering("amount.equals=" + DEFAULT_AMOUNT, "amount.equals=" + UPDATED_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsInShouldWork() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount in
        defaultDonationFiltering("amount.in=" + DEFAULT_AMOUNT + "," + UPDATED_AMOUNT, "amount.in=" + UPDATED_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount is not null
        defaultDonationFiltering("amount.specified=true", "amount.specified=false");
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount is greater than or equal to
        defaultDonationFiltering("amount.greaterThanOrEqual=" + DEFAULT_AMOUNT, "amount.greaterThanOrEqual=" + UPDATED_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount is less than or equal to
        defaultDonationFiltering("amount.lessThanOrEqual=" + DEFAULT_AMOUNT, "amount.lessThanOrEqual=" + SMALLER_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsLessThanSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount is less than
        defaultDonationFiltering("amount.lessThan=" + UPDATED_AMOUNT, "amount.lessThan=" + DEFAULT_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByAmountIsGreaterThanSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where amount is greater than
        defaultDonationFiltering("amount.greaterThan=" + SMALLER_AMOUNT, "amount.greaterThan=" + DEFAULT_AMOUNT);
    }

    @Test
    @Transactional
    void getAllDonationsByCurrencyIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where currency equals to
        defaultDonationFiltering("currency.equals=" + DEFAULT_CURRENCY, "currency.equals=" + UPDATED_CURRENCY);
    }

    @Test
    @Transactional
    void getAllDonationsByCurrencyIsInShouldWork() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where currency in
        defaultDonationFiltering("currency.in=" + DEFAULT_CURRENCY + "," + UPDATED_CURRENCY, "currency.in=" + UPDATED_CURRENCY);
    }

    @Test
    @Transactional
    void getAllDonationsByCurrencyIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where currency is not null
        defaultDonationFiltering("currency.specified=true", "currency.specified=false");
    }

    @Test
    @Transactional
    void getAllDonationsByCurrencyContainsSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where currency contains
        defaultDonationFiltering("currency.contains=" + DEFAULT_CURRENCY, "currency.contains=" + UPDATED_CURRENCY);
    }

    @Test
    @Transactional
    void getAllDonationsByCurrencyNotContainsSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where currency does not contain
        defaultDonationFiltering("currency.doesNotContain=" + UPDATED_CURRENCY, "currency.doesNotContain=" + DEFAULT_CURRENCY);
    }

    @Test
    @Transactional
    void getAllDonationsByStripePaymentIntentIdIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where stripePaymentIntentId equals to
        defaultDonationFiltering(
            "stripePaymentIntentId.equals=" + DEFAULT_STRIPE_PAYMENT_INTENT_ID,
            "stripePaymentIntentId.equals=" + UPDATED_STRIPE_PAYMENT_INTENT_ID
        );
    }

    @Test
    @Transactional
    void getAllDonationsByStripePaymentIntentIdIsInShouldWork() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where stripePaymentIntentId in
        defaultDonationFiltering(
            "stripePaymentIntentId.in=" + DEFAULT_STRIPE_PAYMENT_INTENT_ID + "," + UPDATED_STRIPE_PAYMENT_INTENT_ID,
            "stripePaymentIntentId.in=" + UPDATED_STRIPE_PAYMENT_INTENT_ID
        );
    }

    @Test
    @Transactional
    void getAllDonationsByStripePaymentIntentIdIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where stripePaymentIntentId is not null
        defaultDonationFiltering("stripePaymentIntentId.specified=true", "stripePaymentIntentId.specified=false");
    }

    @Test
    @Transactional
    void getAllDonationsByStripePaymentIntentIdContainsSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where stripePaymentIntentId contains
        defaultDonationFiltering(
            "stripePaymentIntentId.contains=" + DEFAULT_STRIPE_PAYMENT_INTENT_ID,
            "stripePaymentIntentId.contains=" + UPDATED_STRIPE_PAYMENT_INTENT_ID
        );
    }

    @Test
    @Transactional
    void getAllDonationsByStripePaymentIntentIdNotContainsSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where stripePaymentIntentId does not contain
        defaultDonationFiltering(
            "stripePaymentIntentId.doesNotContain=" + UPDATED_STRIPE_PAYMENT_INTENT_ID,
            "stripePaymentIntentId.doesNotContain=" + DEFAULT_STRIPE_PAYMENT_INTENT_ID
        );
    }

    @Test
    @Transactional
    void getAllDonationsByStatusIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where status equals to
        defaultDonationFiltering("status.equals=" + DEFAULT_STATUS, "status.equals=" + UPDATED_STATUS);
    }

    @Test
    @Transactional
    void getAllDonationsByStatusIsInShouldWork() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where status in
        defaultDonationFiltering("status.in=" + DEFAULT_STATUS + "," + UPDATED_STATUS, "status.in=" + UPDATED_STATUS);
    }

    @Test
    @Transactional
    void getAllDonationsByStatusIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        // Get all the donationList where status is not null
        defaultDonationFiltering("status.specified=true", "status.specified=false");
    }

    @Test
    @Transactional
    void getAllDonationsBySessionIsEqualToSomething() throws Exception {
        TarotSession session;
        if (TestUtil.findAll(em, TarotSession.class).isEmpty()) {
            donationRepository.saveAndFlush(donation);
            session = TarotSessionResourceIT.createEntity(em);
        } else {
            session = TestUtil.findAll(em, TarotSession.class).get(0);
        }
        em.persist(session);
        em.flush();
        donation.setSession(session);
        donationRepository.saveAndFlush(donation);
        Long sessionId = session.getId();
        // Get all the donationList where session equals to sessionId
        defaultDonationShouldBeFound("sessionId.equals=" + sessionId);

        // Get all the donationList where session equals to (sessionId + 1)
        defaultDonationShouldNotBeFound("sessionId.equals=" + (sessionId + 1));
    }

    @Test
    @Transactional
    void getAllDonationsByUserProfileIsEqualToSomething() throws Exception {
        UserProfile userProfile;
        if (TestUtil.findAll(em, UserProfile.class).isEmpty()) {
            donationRepository.saveAndFlush(donation);
            userProfile = UserProfileResourceIT.createEntity(em);
        } else {
            userProfile = TestUtil.findAll(em, UserProfile.class).get(0);
        }
        em.persist(userProfile);
        em.flush();
        donation.setUserProfile(userProfile);
        donationRepository.saveAndFlush(donation);
        Long userProfileId = userProfile.getId();
        // Get all the donationList where userProfile equals to userProfileId
        defaultDonationShouldBeFound("userProfileId.equals=" + userProfileId);

        // Get all the donationList where userProfile equals to (userProfileId + 1)
        defaultDonationShouldNotBeFound("userProfileId.equals=" + (userProfileId + 1));
    }

    private void defaultDonationFiltering(String shouldBeFound, String shouldNotBeFound) throws Exception {
        defaultDonationShouldBeFound(shouldBeFound);
        defaultDonationShouldNotBeFound(shouldNotBeFound);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultDonationShouldBeFound(String filter) throws Exception {
        restDonationMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(donation.getId().intValue())))
            .andExpect(jsonPath("$.[*].amount").value(hasItem(sameNumber(DEFAULT_AMOUNT))))
            .andExpect(jsonPath("$.[*].currency").value(hasItem(DEFAULT_CURRENCY)))
            .andExpect(jsonPath("$.[*].stripePaymentIntentId").value(hasItem(DEFAULT_STRIPE_PAYMENT_INTENT_ID)))
            .andExpect(jsonPath("$.[*].status").value(hasItem(DEFAULT_STATUS.toString())));

        // Check, that the count call also returns 1
        restDonationMockMvc
            .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 defaultDonationShouldNotBeFound(String filter) throws Exception {
        restDonationMockMvc
            .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
        restDonationMockMvc
            .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 getNonExistingDonation() throws Exception {
        // Get the donation
        restDonationMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
    }

    @Test
    @Transactional
    void putExistingDonation() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the donation
        Donation updatedDonation = donationRepository.findById(donation.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedDonation are not directly saved in db
        em.detach(updatedDonation);
        updatedDonation
            .amount(UPDATED_AMOUNT)
            .currency(UPDATED_CURRENCY)
            .stripePaymentIntentId(UPDATED_STRIPE_PAYMENT_INTENT_ID)
            .status(UPDATED_STATUS);
        DonationDTO donationDTO = donationMapper.toDto(updatedDonation);

        restDonationMockMvc
            .perform(
                put(ENTITY_API_URL_ID, donationDTO.getId())
                    .contentType(MediaType.APPLICATION_JSON)
                    .content(om.writeValueAsBytes(donationDTO))
            )
            .andExpect(status().isOk());

        // Validate the Donation in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedDonationToMatchAllProperties(updatedDonation);
    }

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

    @Test
    @Transactional
    void partialUpdateDonationWithPatch() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the donation using partial update
        Donation partialUpdatedDonation = new Donation();
        partialUpdatedDonation.setId(donation.getId());

        partialUpdatedDonation.amount(UPDATED_AMOUNT);

        restDonationMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedDonation.getId())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(partialUpdatedDonation))
            )
            .andExpect(status().isOk());

        // Validate the Donation in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertDonationUpdatableFieldsEquals(createUpdateProxyForBean(partialUpdatedDonation, donation), getPersistedDonation(donation));
    }

    @Test
    @Transactional
    void fullUpdateDonationWithPatch() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the donation using partial update
        Donation partialUpdatedDonation = new Donation();
        partialUpdatedDonation.setId(donation.getId());

        partialUpdatedDonation
            .amount(UPDATED_AMOUNT)
            .currency(UPDATED_CURRENCY)
            .stripePaymentIntentId(UPDATED_STRIPE_PAYMENT_INTENT_ID)
            .status(UPDATED_STATUS);

        restDonationMockMvc
            .perform(
                patch(ENTITY_API_URL_ID, partialUpdatedDonation.getId())
                    .contentType("application/merge-patch+json")
                    .content(om.writeValueAsBytes(partialUpdatedDonation))
            )
            .andExpect(status().isOk());

        // Validate the Donation in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertDonationUpdatableFieldsEquals(partialUpdatedDonation, getPersistedDonation(partialUpdatedDonation));
    }

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

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

        // Create the Donation
        DonationDTO donationDTO = donationMapper.toDto(donation);

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

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

    @Test
    @Transactional
    void deleteDonation() throws Exception {
        // Initialize the database
        insertedDonation = donationRepository.saveAndFlush(donation);

        long databaseSizeBeforeDelete = getRepositoryCount();

        // Delete the donation
        restDonationMockMvc
            .perform(delete(ENTITY_API_URL_ID, donation.getId()).accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isNoContent());

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

    protected long getRepositoryCount() {
        return donationRepository.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 Donation getPersistedDonation(Donation donation) {
        return donationRepository.findById(donation.getId()).orElseThrow();
    }

    protected void assertPersistedDonationToMatchAllProperties(Donation expectedDonation) {
        assertDonationAllPropertiesEquals(expectedDonation, getPersistedDonation(expectedDonation));
    }

    protected void assertPersistedDonationToMatchUpdatableProperties(Donation expectedDonation) {
        assertDonationAllUpdatablePropertiesEquals(expectedDonation, getPersistedDonation(expectedDonation));
    }
}
