package com.emonster.taroaichat.web.rest;

import static com.emonster.taroaichat.domain.TarotCardAsserts.*;
import static com.emonster.taroaichat.web.rest.TestUtil.createUpdateProxyForBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
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.TarotCard;
import com.emonster.taroaichat.domain.enumeration.ArcanaType;
import com.emonster.taroaichat.repository.TarotCardRepository;
import com.emonster.taroaichat.service.dto.TarotCardDTO;
import com.emonster.taroaichat.service.mapper.TarotCardMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManager;
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 TarotCardResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class TarotCardResourceIT {

    private static final String DEFAULT_NAME = "AAAAAAAAAA";
    private static final String UPDATED_NAME = "BBBBBBBBBB";

    private static final ArcanaType DEFAULT_ARCANA_TYPE = ArcanaType.MAJOR;
    private static final ArcanaType UPDATED_ARCANA_TYPE = ArcanaType.MINOR;

    private static final Integer DEFAULT_CARD_NUMBER = 0;
    private static final Integer UPDATED_CARD_NUMBER = 1;
    private static final Integer SMALLER_CARD_NUMBER = 0 - 1;

    private static final String DEFAULT_IMAGE_URL = "AAAAAAAAAA";
    private static final String UPDATED_IMAGE_URL = "BBBBBBBBBB";

    private static final String DEFAULT_UPRIGHT_MEANING = "AAAAAAAAAA";
    private static final String UPDATED_UPRIGHT_MEANING = "BBBBBBBBBB";

    private static final String DEFAULT_REVERSED_MEANING = "AAAAAAAAAA";
    private static final String UPDATED_REVERSED_MEANING = "BBBBBBBBBB";

    private static final String DEFAULT_KEYWORDS = "AAAAAAAAAA";
    private static final String UPDATED_KEYWORDS = "BBBBBBBBBB";

    private static final String ENTITY_API_URL = "/api/tarot-cards";
    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 TarotCardRepository tarotCardRepository;

    @Autowired
    private TarotCardMapper tarotCardMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restTarotCardMockMvc;

    private TarotCard tarotCard;

    private TarotCard insertedTarotCard;

    /**
     * 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 TarotCard createEntity() {
        return new TarotCard()
            .name(DEFAULT_NAME)
            .arcanaType(DEFAULT_ARCANA_TYPE)
            .cardNumber(DEFAULT_CARD_NUMBER)
            .imageUrl(DEFAULT_IMAGE_URL)
            .uprightMeaning(DEFAULT_UPRIGHT_MEANING)
            .reversedMeaning(DEFAULT_REVERSED_MEANING)
            .keywords(DEFAULT_KEYWORDS);
    }

    /**
     * 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 TarotCard createUpdatedEntity() {
        return new TarotCard()
            .name(UPDATED_NAME)
            .arcanaType(UPDATED_ARCANA_TYPE)
            .cardNumber(UPDATED_CARD_NUMBER)
            .imageUrl(UPDATED_IMAGE_URL)
            .uprightMeaning(UPDATED_UPRIGHT_MEANING)
            .reversedMeaning(UPDATED_REVERSED_MEANING)
            .keywords(UPDATED_KEYWORDS);
    }

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

    @AfterEach
    void cleanup() {
        if (insertedTarotCard != null) {
            tarotCardRepository.delete(insertedTarotCard);
            insertedTarotCard = null;
        }
    }

    @Test
    @Transactional
    void createTarotCard() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);
        var returnedTarotCardDTO = om.readValue(
            restTarotCardMockMvc
                .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(tarotCardDTO)))
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            TarotCardDTO.class
        );

        // Validate the TarotCard in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedTarotCard = tarotCardMapper.toEntity(returnedTarotCardDTO);
        assertTarotCardUpdatableFieldsEquals(returnedTarotCard, getPersistedTarotCard(returnedTarotCard));

        insertedTarotCard = returnedTarotCard;
    }

    @Test
    @Transactional
    void createTarotCardWithExistingId() throws Exception {
        // Create the TarotCard with an existing ID
        tarotCard.setId(1L);
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

    @Test
    @Transactional
    void checkNameIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        tarotCard.setName(null);

        // Create the TarotCard, which fails.
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkArcanaTypeIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        tarotCard.setArcanaType(null);

        // Create the TarotCard, which fails.
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkCardNumberIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        tarotCard.setCardNumber(null);

        // Create the TarotCard, which fails.
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkImageUrlIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        tarotCard.setImageUrl(null);

        // Create the TarotCard, which fails.
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllTarotCards() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList
        restTarotCardMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tarotCard.getId().intValue())))
            .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
            .andExpect(jsonPath("$.[*].arcanaType").value(hasItem(DEFAULT_ARCANA_TYPE.toString())))
            .andExpect(jsonPath("$.[*].cardNumber").value(hasItem(DEFAULT_CARD_NUMBER)))
            .andExpect(jsonPath("$.[*].imageUrl").value(hasItem(DEFAULT_IMAGE_URL)))
            .andExpect(jsonPath("$.[*].uprightMeaning").value(hasItem(DEFAULT_UPRIGHT_MEANING)))
            .andExpect(jsonPath("$.[*].reversedMeaning").value(hasItem(DEFAULT_REVERSED_MEANING)))
            .andExpect(jsonPath("$.[*].keywords").value(hasItem(DEFAULT_KEYWORDS)));
    }

    @Test
    @Transactional
    void getTarotCard() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get the tarotCard
        restTarotCardMockMvc
            .perform(get(ENTITY_API_URL_ID, tarotCard.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(tarotCard.getId().intValue()))
            .andExpect(jsonPath("$.name").value(DEFAULT_NAME))
            .andExpect(jsonPath("$.arcanaType").value(DEFAULT_ARCANA_TYPE.toString()))
            .andExpect(jsonPath("$.cardNumber").value(DEFAULT_CARD_NUMBER))
            .andExpect(jsonPath("$.imageUrl").value(DEFAULT_IMAGE_URL))
            .andExpect(jsonPath("$.uprightMeaning").value(DEFAULT_UPRIGHT_MEANING))
            .andExpect(jsonPath("$.reversedMeaning").value(DEFAULT_REVERSED_MEANING))
            .andExpect(jsonPath("$.keywords").value(DEFAULT_KEYWORDS));
    }

    @Test
    @Transactional
    void getTarotCardsByIdFiltering() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        Long id = tarotCard.getId();

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

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

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

    @Test
    @Transactional
    void getAllTarotCardsByNameIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where name equals to
        defaultTarotCardFiltering("name.equals=" + DEFAULT_NAME, "name.equals=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    void getAllTarotCardsByNameIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where name in
        defaultTarotCardFiltering("name.in=" + DEFAULT_NAME + "," + UPDATED_NAME, "name.in=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    void getAllTarotCardsByNameIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where name is not null
        defaultTarotCardFiltering("name.specified=true", "name.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotCardsByNameContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where name contains
        defaultTarotCardFiltering("name.contains=" + DEFAULT_NAME, "name.contains=" + UPDATED_NAME);
    }

    @Test
    @Transactional
    void getAllTarotCardsByNameNotContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where name does not contain
        defaultTarotCardFiltering("name.doesNotContain=" + UPDATED_NAME, "name.doesNotContain=" + DEFAULT_NAME);
    }

    @Test
    @Transactional
    void getAllTarotCardsByArcanaTypeIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where arcanaType equals to
        defaultTarotCardFiltering("arcanaType.equals=" + DEFAULT_ARCANA_TYPE, "arcanaType.equals=" + UPDATED_ARCANA_TYPE);
    }

    @Test
    @Transactional
    void getAllTarotCardsByArcanaTypeIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where arcanaType in
        defaultTarotCardFiltering(
            "arcanaType.in=" + DEFAULT_ARCANA_TYPE + "," + UPDATED_ARCANA_TYPE,
            "arcanaType.in=" + UPDATED_ARCANA_TYPE
        );
    }

    @Test
    @Transactional
    void getAllTarotCardsByArcanaTypeIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where arcanaType is not null
        defaultTarotCardFiltering("arcanaType.specified=true", "arcanaType.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber equals to
        defaultTarotCardFiltering("cardNumber.equals=" + DEFAULT_CARD_NUMBER, "cardNumber.equals=" + UPDATED_CARD_NUMBER);
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber in
        defaultTarotCardFiltering(
            "cardNumber.in=" + DEFAULT_CARD_NUMBER + "," + UPDATED_CARD_NUMBER,
            "cardNumber.in=" + UPDATED_CARD_NUMBER
        );
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber is not null
        defaultTarotCardFiltering("cardNumber.specified=true", "cardNumber.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber is greater than or equal to
        defaultTarotCardFiltering(
            "cardNumber.greaterThanOrEqual=" + DEFAULT_CARD_NUMBER,
            "cardNumber.greaterThanOrEqual=" + (DEFAULT_CARD_NUMBER + 1)
        );
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber is less than or equal to
        defaultTarotCardFiltering("cardNumber.lessThanOrEqual=" + DEFAULT_CARD_NUMBER, "cardNumber.lessThanOrEqual=" + SMALLER_CARD_NUMBER);
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsLessThanSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber is less than
        defaultTarotCardFiltering("cardNumber.lessThan=" + (DEFAULT_CARD_NUMBER + 1), "cardNumber.lessThan=" + DEFAULT_CARD_NUMBER);
    }

    @Test
    @Transactional
    void getAllTarotCardsByCardNumberIsGreaterThanSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where cardNumber is greater than
        defaultTarotCardFiltering("cardNumber.greaterThan=" + SMALLER_CARD_NUMBER, "cardNumber.greaterThan=" + DEFAULT_CARD_NUMBER);
    }

    @Test
    @Transactional
    void getAllTarotCardsByImageUrlIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where imageUrl equals to
        defaultTarotCardFiltering("imageUrl.equals=" + DEFAULT_IMAGE_URL, "imageUrl.equals=" + UPDATED_IMAGE_URL);
    }

    @Test
    @Transactional
    void getAllTarotCardsByImageUrlIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where imageUrl in
        defaultTarotCardFiltering("imageUrl.in=" + DEFAULT_IMAGE_URL + "," + UPDATED_IMAGE_URL, "imageUrl.in=" + UPDATED_IMAGE_URL);
    }

    @Test
    @Transactional
    void getAllTarotCardsByImageUrlIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where imageUrl is not null
        defaultTarotCardFiltering("imageUrl.specified=true", "imageUrl.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotCardsByImageUrlContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where imageUrl contains
        defaultTarotCardFiltering("imageUrl.contains=" + DEFAULT_IMAGE_URL, "imageUrl.contains=" + UPDATED_IMAGE_URL);
    }

    @Test
    @Transactional
    void getAllTarotCardsByImageUrlNotContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where imageUrl does not contain
        defaultTarotCardFiltering("imageUrl.doesNotContain=" + UPDATED_IMAGE_URL, "imageUrl.doesNotContain=" + DEFAULT_IMAGE_URL);
    }

    @Test
    @Transactional
    void getAllTarotCardsByKeywordsIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where keywords equals to
        defaultTarotCardFiltering("keywords.equals=" + DEFAULT_KEYWORDS, "keywords.equals=" + UPDATED_KEYWORDS);
    }

    @Test
    @Transactional
    void getAllTarotCardsByKeywordsIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where keywords in
        defaultTarotCardFiltering("keywords.in=" + DEFAULT_KEYWORDS + "," + UPDATED_KEYWORDS, "keywords.in=" + UPDATED_KEYWORDS);
    }

    @Test
    @Transactional
    void getAllTarotCardsByKeywordsIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where keywords is not null
        defaultTarotCardFiltering("keywords.specified=true", "keywords.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotCardsByKeywordsContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where keywords contains
        defaultTarotCardFiltering("keywords.contains=" + DEFAULT_KEYWORDS, "keywords.contains=" + UPDATED_KEYWORDS);
    }

    @Test
    @Transactional
    void getAllTarotCardsByKeywordsNotContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        // Get all the tarotCardList where keywords does not contain
        defaultTarotCardFiltering("keywords.doesNotContain=" + UPDATED_KEYWORDS, "keywords.doesNotContain=" + DEFAULT_KEYWORDS);
    }

    private void defaultTarotCardFiltering(String shouldBeFound, String shouldNotBeFound) throws Exception {
        defaultTarotCardShouldBeFound(shouldBeFound);
        defaultTarotCardShouldNotBeFound(shouldNotBeFound);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultTarotCardShouldBeFound(String filter) throws Exception {
        restTarotCardMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tarotCard.getId().intValue())))
            .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
            .andExpect(jsonPath("$.[*].arcanaType").value(hasItem(DEFAULT_ARCANA_TYPE.toString())))
            .andExpect(jsonPath("$.[*].cardNumber").value(hasItem(DEFAULT_CARD_NUMBER)))
            .andExpect(jsonPath("$.[*].imageUrl").value(hasItem(DEFAULT_IMAGE_URL)))
            .andExpect(jsonPath("$.[*].uprightMeaning").value(hasItem(DEFAULT_UPRIGHT_MEANING)))
            .andExpect(jsonPath("$.[*].reversedMeaning").value(hasItem(DEFAULT_REVERSED_MEANING)))
            .andExpect(jsonPath("$.[*].keywords").value(hasItem(DEFAULT_KEYWORDS)));

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

    @Test
    @Transactional
    void putExistingTarotCard() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotCard
        TarotCard updatedTarotCard = tarotCardRepository.findById(tarotCard.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedTarotCard are not directly saved in db
        em.detach(updatedTarotCard);
        updatedTarotCard
            .name(UPDATED_NAME)
            .arcanaType(UPDATED_ARCANA_TYPE)
            .cardNumber(UPDATED_CARD_NUMBER)
            .imageUrl(UPDATED_IMAGE_URL)
            .uprightMeaning(UPDATED_UPRIGHT_MEANING)
            .reversedMeaning(UPDATED_REVERSED_MEANING)
            .keywords(UPDATED_KEYWORDS);
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(updatedTarotCard);

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

        // Validate the TarotCard in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedTarotCardToMatchAllProperties(updatedTarotCard);
    }

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

    @Test
    @Transactional
    void partialUpdateTarotCardWithPatch() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotCard using partial update
        TarotCard partialUpdatedTarotCard = new TarotCard();
        partialUpdatedTarotCard.setId(tarotCard.getId());

        partialUpdatedTarotCard.name(UPDATED_NAME).uprightMeaning(UPDATED_UPRIGHT_MEANING);

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

        // Validate the TarotCard in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertTarotCardUpdatableFieldsEquals(
            createUpdateProxyForBean(partialUpdatedTarotCard, tarotCard),
            getPersistedTarotCard(tarotCard)
        );
    }

    @Test
    @Transactional
    void fullUpdateTarotCardWithPatch() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotCard using partial update
        TarotCard partialUpdatedTarotCard = new TarotCard();
        partialUpdatedTarotCard.setId(tarotCard.getId());

        partialUpdatedTarotCard
            .name(UPDATED_NAME)
            .arcanaType(UPDATED_ARCANA_TYPE)
            .cardNumber(UPDATED_CARD_NUMBER)
            .imageUrl(UPDATED_IMAGE_URL)
            .uprightMeaning(UPDATED_UPRIGHT_MEANING)
            .reversedMeaning(UPDATED_REVERSED_MEANING)
            .keywords(UPDATED_KEYWORDS);

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

        // Validate the TarotCard in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertTarotCardUpdatableFieldsEquals(partialUpdatedTarotCard, getPersistedTarotCard(partialUpdatedTarotCard));
    }

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

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

        // Create the TarotCard
        TarotCardDTO tarotCardDTO = tarotCardMapper.toDto(tarotCard);

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

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

    @Test
    @Transactional
    void deleteTarotCard() throws Exception {
        // Initialize the database
        insertedTarotCard = tarotCardRepository.saveAndFlush(tarotCard);

        long databaseSizeBeforeDelete = getRepositoryCount();

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

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

    protected long getRepositoryCount() {
        return tarotCardRepository.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 TarotCard getPersistedTarotCard(TarotCard tarotCard) {
        return tarotCardRepository.findById(tarotCard.getId()).orElseThrow();
    }

    protected void assertPersistedTarotCardToMatchAllProperties(TarotCard expectedTarotCard) {
        assertTarotCardAllPropertiesEquals(expectedTarotCard, getPersistedTarotCard(expectedTarotCard));
    }

    protected void assertPersistedTarotCardToMatchUpdatableProperties(TarotCard expectedTarotCard) {
        assertTarotCardAllUpdatablePropertiesEquals(expectedTarotCard, getPersistedTarotCard(expectedTarotCard));
    }
}
