package com.emonster.taroaichat.web.rest;

import static com.emonster.taroaichat.domain.TarotSessionAsserts.*;
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.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.TarotSession;
import com.emonster.taroaichat.domain.UserProfile;
import com.emonster.taroaichat.domain.enumeration.Feedback;
import com.emonster.taroaichat.domain.enumeration.SessionStatus;
import com.emonster.taroaichat.repository.TarotSessionRepository;
import com.emonster.taroaichat.service.TarotSessionService;
import com.emonster.taroaichat.service.dto.TarotSessionDTO;
import com.emonster.taroaichat.service.mapper.TarotSessionMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.persistence.EntityManager;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
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 TarotSessionResource} REST controller.
 */
@IntegrationTest
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@WithMockUser
class TarotSessionResourceIT {

    private static final String DEFAULT_CARDS = "AAAAAAAAAA";
    private static final String UPDATED_CARDS = "BBBBBBBBBB";

    private static final String DEFAULT_INTERPRETATION = "AAAAAAAAAA";
    private static final String UPDATED_INTERPRETATION = "BBBBBBBBBB";

    private static final SessionStatus DEFAULT_STATUS = SessionStatus.STARTED;
    private static final SessionStatus UPDATED_STATUS = SessionStatus.COMPLETED;

    private static final Feedback DEFAULT_FEEDBACK = Feedback.POSITIVE;
    private static final Feedback UPDATED_FEEDBACK = Feedback.NEGATIVE;

    private static final Integer DEFAULT_RATING = 1;
    private static final Integer UPDATED_RATING = 2;
    private static final Integer SMALLER_RATING = 1 - 1;

    private static final Boolean DEFAULT_SAVED = false;
    private static final Boolean UPDATED_SAVED = true;

    private static final String DEFAULT_SUMMARY = "AAAAAAAAAA";
    private static final String UPDATED_SUMMARY = "BBBBBBBBBB";

    private static final String DEFAULT_SCREENSHOT_URL = "AAAAAAAAAA";
    private static final String UPDATED_SCREENSHOT_URL = "BBBBBBBBBB";

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

    private static final String ENTITY_API_URL = "/api/tarot-sessions";
    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 TarotSessionRepository tarotSessionRepository;

    @Mock
    private TarotSessionRepository tarotSessionRepositoryMock;

    @Autowired
    private TarotSessionMapper tarotSessionMapper;

    @Mock
    private TarotSessionService tarotSessionServiceMock;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restTarotSessionMockMvc;

    private TarotSession tarotSession;

    private TarotSession insertedTarotSession;

    /**
     * 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 TarotSession createEntity(EntityManager em) {
        TarotSession tarotSession = new TarotSession()
            .cards(DEFAULT_CARDS)
            .interpretation(DEFAULT_INTERPRETATION)
            .status(DEFAULT_STATUS)
            .feedback(DEFAULT_FEEDBACK)
            .rating(DEFAULT_RATING)
            .saved(DEFAULT_SAVED)
            .summary(DEFAULT_SUMMARY)
            .screenshotUrl(DEFAULT_SCREENSHOT_URL)
            .completedAt(DEFAULT_COMPLETED_AT);
        // 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);
        }
        tarotSession.setUserProfile(userProfile);
        return tarotSession;
    }

    /**
     * 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 TarotSession createUpdatedEntity(EntityManager em) {
        TarotSession updatedTarotSession = new TarotSession()
            .cards(UPDATED_CARDS)
            .interpretation(UPDATED_INTERPRETATION)
            .status(UPDATED_STATUS)
            .feedback(UPDATED_FEEDBACK)
            .rating(UPDATED_RATING)
            .saved(UPDATED_SAVED)
            .summary(UPDATED_SUMMARY)
            .screenshotUrl(UPDATED_SCREENSHOT_URL)
            .completedAt(UPDATED_COMPLETED_AT);
        // 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);
        }
        updatedTarotSession.setUserProfile(userProfile);
        return updatedTarotSession;
    }

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

    @AfterEach
    void cleanup() {
        if (insertedTarotSession != null) {
            tarotSessionRepository.delete(insertedTarotSession);
            insertedTarotSession = null;
        }
    }

    @Test
    @Transactional
    void createTarotSession() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);
        var returnedTarotSessionDTO = om.readValue(
            restTarotSessionMockMvc
                .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(tarotSessionDTO)))
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            TarotSessionDTO.class
        );

        // Validate the TarotSession in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedTarotSession = tarotSessionMapper.toEntity(returnedTarotSessionDTO);
        assertTarotSessionUpdatableFieldsEquals(returnedTarotSession, getPersistedTarotSession(returnedTarotSession));

        insertedTarotSession = returnedTarotSession;
    }

    @Test
    @Transactional
    void createTarotSessionWithExistingId() throws Exception {
        // Create the TarotSession with an existing ID
        tarotSession.setId(1L);
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

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

        // Create the TarotSession, which fails.
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkSavedIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        tarotSession.setSaved(null);

        // Create the TarotSession, which fails.
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllTarotSessions() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList
        restTarotSessionMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tarotSession.getId().intValue())))
            .andExpect(jsonPath("$.[*].cards").value(hasItem(DEFAULT_CARDS)))
            .andExpect(jsonPath("$.[*].interpretation").value(hasItem(DEFAULT_INTERPRETATION)))
            .andExpect(jsonPath("$.[*].status").value(hasItem(DEFAULT_STATUS.toString())))
            .andExpect(jsonPath("$.[*].feedback").value(hasItem(DEFAULT_FEEDBACK.toString())))
            .andExpect(jsonPath("$.[*].rating").value(hasItem(DEFAULT_RATING)))
            .andExpect(jsonPath("$.[*].saved").value(hasItem(DEFAULT_SAVED)))
            .andExpect(jsonPath("$.[*].summary").value(hasItem(DEFAULT_SUMMARY)))
            .andExpect(jsonPath("$.[*].screenshotUrl").value(hasItem(DEFAULT_SCREENSHOT_URL)))
            .andExpect(jsonPath("$.[*].completedAt").value(hasItem(DEFAULT_COMPLETED_AT.toString())));
    }

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

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

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

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

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

    @Test
    @Transactional
    void getTarotSession() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get the tarotSession
        restTarotSessionMockMvc
            .perform(get(ENTITY_API_URL_ID, tarotSession.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(tarotSession.getId().intValue()))
            .andExpect(jsonPath("$.cards").value(DEFAULT_CARDS))
            .andExpect(jsonPath("$.interpretation").value(DEFAULT_INTERPRETATION))
            .andExpect(jsonPath("$.status").value(DEFAULT_STATUS.toString()))
            .andExpect(jsonPath("$.feedback").value(DEFAULT_FEEDBACK.toString()))
            .andExpect(jsonPath("$.rating").value(DEFAULT_RATING))
            .andExpect(jsonPath("$.saved").value(DEFAULT_SAVED))
            .andExpect(jsonPath("$.summary").value(DEFAULT_SUMMARY))
            .andExpect(jsonPath("$.screenshotUrl").value(DEFAULT_SCREENSHOT_URL))
            .andExpect(jsonPath("$.completedAt").value(DEFAULT_COMPLETED_AT.toString()));
    }

    @Test
    @Transactional
    void getTarotSessionsByIdFiltering() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        Long id = tarotSession.getId();

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

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

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

    @Test
    @Transactional
    void getAllTarotSessionsByStatusIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

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

    @Test
    @Transactional
    void getAllTarotSessionsByStatusIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

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

    @Test
    @Transactional
    void getAllTarotSessionsByStatusIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

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

    @Test
    @Transactional
    void getAllTarotSessionsByFeedbackIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where feedback equals to
        defaultTarotSessionFiltering("feedback.equals=" + DEFAULT_FEEDBACK, "feedback.equals=" + UPDATED_FEEDBACK);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByFeedbackIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where feedback in
        defaultTarotSessionFiltering("feedback.in=" + DEFAULT_FEEDBACK + "," + UPDATED_FEEDBACK, "feedback.in=" + UPDATED_FEEDBACK);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByFeedbackIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where feedback is not null
        defaultTarotSessionFiltering("feedback.specified=true", "feedback.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating equals to
        defaultTarotSessionFiltering("rating.equals=" + DEFAULT_RATING, "rating.equals=" + UPDATED_RATING);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating in
        defaultTarotSessionFiltering("rating.in=" + DEFAULT_RATING + "," + UPDATED_RATING, "rating.in=" + UPDATED_RATING);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating is not null
        defaultTarotSessionFiltering("rating.specified=true", "rating.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsGreaterThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating is greater than or equal to
        defaultTarotSessionFiltering("rating.greaterThanOrEqual=" + DEFAULT_RATING, "rating.greaterThanOrEqual=" + (DEFAULT_RATING + 1));
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsLessThanOrEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating is less than or equal to
        defaultTarotSessionFiltering("rating.lessThanOrEqual=" + DEFAULT_RATING, "rating.lessThanOrEqual=" + SMALLER_RATING);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsLessThanSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating is less than
        defaultTarotSessionFiltering("rating.lessThan=" + (DEFAULT_RATING + 1), "rating.lessThan=" + DEFAULT_RATING);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByRatingIsGreaterThanSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where rating is greater than
        defaultTarotSessionFiltering("rating.greaterThan=" + SMALLER_RATING, "rating.greaterThan=" + DEFAULT_RATING);
    }

    @Test
    @Transactional
    void getAllTarotSessionsBySavedIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where saved equals to
        defaultTarotSessionFiltering("saved.equals=" + DEFAULT_SAVED, "saved.equals=" + UPDATED_SAVED);
    }

    @Test
    @Transactional
    void getAllTarotSessionsBySavedIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where saved in
        defaultTarotSessionFiltering("saved.in=" + DEFAULT_SAVED + "," + UPDATED_SAVED, "saved.in=" + UPDATED_SAVED);
    }

    @Test
    @Transactional
    void getAllTarotSessionsBySavedIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where saved is not null
        defaultTarotSessionFiltering("saved.specified=true", "saved.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotSessionsByScreenshotUrlIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where screenshotUrl equals to
        defaultTarotSessionFiltering("screenshotUrl.equals=" + DEFAULT_SCREENSHOT_URL, "screenshotUrl.equals=" + UPDATED_SCREENSHOT_URL);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByScreenshotUrlIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where screenshotUrl in
        defaultTarotSessionFiltering(
            "screenshotUrl.in=" + DEFAULT_SCREENSHOT_URL + "," + UPDATED_SCREENSHOT_URL,
            "screenshotUrl.in=" + UPDATED_SCREENSHOT_URL
        );
    }

    @Test
    @Transactional
    void getAllTarotSessionsByScreenshotUrlIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where screenshotUrl is not null
        defaultTarotSessionFiltering("screenshotUrl.specified=true", "screenshotUrl.specified=false");
    }

    @Test
    @Transactional
    void getAllTarotSessionsByScreenshotUrlContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where screenshotUrl contains
        defaultTarotSessionFiltering(
            "screenshotUrl.contains=" + DEFAULT_SCREENSHOT_URL,
            "screenshotUrl.contains=" + UPDATED_SCREENSHOT_URL
        );
    }

    @Test
    @Transactional
    void getAllTarotSessionsByScreenshotUrlNotContainsSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where screenshotUrl does not contain
        defaultTarotSessionFiltering(
            "screenshotUrl.doesNotContain=" + UPDATED_SCREENSHOT_URL,
            "screenshotUrl.doesNotContain=" + DEFAULT_SCREENSHOT_URL
        );
    }

    @Test
    @Transactional
    void getAllTarotSessionsByCompletedAtIsEqualToSomething() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where completedAt equals to
        defaultTarotSessionFiltering("completedAt.equals=" + DEFAULT_COMPLETED_AT, "completedAt.equals=" + UPDATED_COMPLETED_AT);
    }

    @Test
    @Transactional
    void getAllTarotSessionsByCompletedAtIsInShouldWork() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where completedAt in
        defaultTarotSessionFiltering(
            "completedAt.in=" + DEFAULT_COMPLETED_AT + "," + UPDATED_COMPLETED_AT,
            "completedAt.in=" + UPDATED_COMPLETED_AT
        );
    }

    @Test
    @Transactional
    void getAllTarotSessionsByCompletedAtIsNullOrNotNull() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        // Get all the tarotSessionList where completedAt is not null
        defaultTarotSessionFiltering("completedAt.specified=true", "completedAt.specified=false");
    }

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

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

    private void defaultTarotSessionFiltering(String shouldBeFound, String shouldNotBeFound) throws Exception {
        defaultTarotSessionShouldBeFound(shouldBeFound);
        defaultTarotSessionShouldNotBeFound(shouldNotBeFound);
    }

    /**
     * Executes the search, and checks that the default entity is returned.
     */
    private void defaultTarotSessionShouldBeFound(String filter) throws Exception {
        restTarotSessionMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc&" + filter))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(tarotSession.getId().intValue())))
            .andExpect(jsonPath("$.[*].cards").value(hasItem(DEFAULT_CARDS)))
            .andExpect(jsonPath("$.[*].interpretation").value(hasItem(DEFAULT_INTERPRETATION)))
            .andExpect(jsonPath("$.[*].status").value(hasItem(DEFAULT_STATUS.toString())))
            .andExpect(jsonPath("$.[*].feedback").value(hasItem(DEFAULT_FEEDBACK.toString())))
            .andExpect(jsonPath("$.[*].rating").value(hasItem(DEFAULT_RATING)))
            .andExpect(jsonPath("$.[*].saved").value(hasItem(DEFAULT_SAVED)))
            .andExpect(jsonPath("$.[*].summary").value(hasItem(DEFAULT_SUMMARY)))
            .andExpect(jsonPath("$.[*].screenshotUrl").value(hasItem(DEFAULT_SCREENSHOT_URL)))
            .andExpect(jsonPath("$.[*].completedAt").value(hasItem(DEFAULT_COMPLETED_AT.toString())));

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

    @Test
    @Transactional
    void putExistingTarotSession() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotSession
        TarotSession updatedTarotSession = tarotSessionRepository.findById(tarotSession.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedTarotSession are not directly saved in db
        em.detach(updatedTarotSession);
        updatedTarotSession
            .cards(UPDATED_CARDS)
            .interpretation(UPDATED_INTERPRETATION)
            .status(UPDATED_STATUS)
            .feedback(UPDATED_FEEDBACK)
            .rating(UPDATED_RATING)
            .saved(UPDATED_SAVED)
            .summary(UPDATED_SUMMARY)
            .screenshotUrl(UPDATED_SCREENSHOT_URL)
            .completedAt(UPDATED_COMPLETED_AT);
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(updatedTarotSession);

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

        // Validate the TarotSession in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedTarotSessionToMatchAllProperties(updatedTarotSession);
    }

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

    @Test
    @Transactional
    void partialUpdateTarotSessionWithPatch() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotSession using partial update
        TarotSession partialUpdatedTarotSession = new TarotSession();
        partialUpdatedTarotSession.setId(tarotSession.getId());

        partialUpdatedTarotSession
            .status(UPDATED_STATUS)
            .rating(UPDATED_RATING)
            .saved(UPDATED_SAVED)
            .summary(UPDATED_SUMMARY)
            .completedAt(UPDATED_COMPLETED_AT);

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

        // Validate the TarotSession in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertTarotSessionUpdatableFieldsEquals(
            createUpdateProxyForBean(partialUpdatedTarotSession, tarotSession),
            getPersistedTarotSession(tarotSession)
        );
    }

    @Test
    @Transactional
    void fullUpdateTarotSessionWithPatch() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the tarotSession using partial update
        TarotSession partialUpdatedTarotSession = new TarotSession();
        partialUpdatedTarotSession.setId(tarotSession.getId());

        partialUpdatedTarotSession
            .cards(UPDATED_CARDS)
            .interpretation(UPDATED_INTERPRETATION)
            .status(UPDATED_STATUS)
            .feedback(UPDATED_FEEDBACK)
            .rating(UPDATED_RATING)
            .saved(UPDATED_SAVED)
            .summary(UPDATED_SUMMARY)
            .screenshotUrl(UPDATED_SCREENSHOT_URL)
            .completedAt(UPDATED_COMPLETED_AT);

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

        // Validate the TarotSession in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertTarotSessionUpdatableFieldsEquals(partialUpdatedTarotSession, getPersistedTarotSession(partialUpdatedTarotSession));
    }

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

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

        // Create the TarotSession
        TarotSessionDTO tarotSessionDTO = tarotSessionMapper.toDto(tarotSession);

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

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

    @Test
    @Transactional
    void deleteTarotSession() throws Exception {
        // Initialize the database
        insertedTarotSession = tarotSessionRepository.saveAndFlush(tarotSession);

        long databaseSizeBeforeDelete = getRepositoryCount();

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

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

    protected long getRepositoryCount() {
        return tarotSessionRepository.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 TarotSession getPersistedTarotSession(TarotSession tarotSession) {
        return tarotSessionRepository.findById(tarotSession.getId()).orElseThrow();
    }

    protected void assertPersistedTarotSessionToMatchAllProperties(TarotSession expectedTarotSession) {
        assertTarotSessionAllPropertiesEquals(expectedTarotSession, getPersistedTarotSession(expectedTarotSession));
    }

    protected void assertPersistedTarotSessionToMatchUpdatableProperties(TarotSession expectedTarotSession) {
        assertTarotSessionAllUpdatablePropertiesEquals(expectedTarotSession, getPersistedTarotSession(expectedTarotSession));
    }
}
