package com.oguerreiro.resilient.web.rest;

import static com.oguerreiro.resilient.domain.UnitTypeAsserts.*;
import static com.oguerreiro.resilient.web.rest.TestUtil.createUpdateProxyForBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oguerreiro.resilient.IntegrationTest;
import com.oguerreiro.resilient.domain.UnitType;
import com.oguerreiro.resilient.domain.enumeration.UnitValueType;
import com.oguerreiro.resilient.repository.UnitTypeRepository;
import com.oguerreiro.resilient.service.dto.UnitTypeDTO;
import com.oguerreiro.resilient.service.mapper.UnitTypeMapper;
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 UnitTypeResource} REST controller.
 */
@IntegrationTest
@AutoConfigureMockMvc
@WithMockUser
class UnitTypeResourceIT {

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

    private static final String DEFAULT_DESCRIPTION = "AAAAAAAAAA";
    private static final String UPDATED_DESCRIPTION = "BBBBBBBBBB";

    private static final UnitValueType DEFAULT_VALUE_TYPE = UnitValueType.DECIMAL;
    private static final UnitValueType UPDATED_VALUE_TYPE = UnitValueType.BOOLEAN;

    private static final Integer DEFAULT_VERSION = 1;
    private static final Integer UPDATED_VERSION = 2;

    private static final String ENTITY_API_URL = "/api/unit-types";
    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 UnitTypeRepository unitTypeRepository;

    @Autowired
    private UnitTypeMapper unitTypeMapper;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restUnitTypeMockMvc;

    private UnitType unitType;

    private UnitType insertedUnitType;

    /**
     * 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 UnitType createEntity(EntityManager em) {
        UnitType unitType = new UnitType()
            .name(DEFAULT_NAME)
            .description(DEFAULT_DESCRIPTION)
            .valueType(DEFAULT_VALUE_TYPE)
            .version(DEFAULT_VERSION);
        return unitType;
    }

    /**
     * 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 UnitType createUpdatedEntity(EntityManager em) {
        UnitType unitType = new UnitType()
            .name(UPDATED_NAME)
            .description(UPDATED_DESCRIPTION)
            .valueType(UPDATED_VALUE_TYPE)
            .version(UPDATED_VERSION);
        return unitType;
    }

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

    @AfterEach
    public void cleanup() {
        if (insertedUnitType != null) {
            unitTypeRepository.delete(insertedUnitType);
            insertedUnitType = null;
        }
    }

    @Test
    @Transactional
    void createUnitType() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);
        var returnedUnitTypeDTO = om.readValue(
            restUnitTypeMockMvc
                .perform(
                    post(ENTITY_API_URL).with(csrf()).contentType(MediaType.APPLICATION_JSON).content(om.writeValueAsBytes(unitTypeDTO))
                )
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            UnitTypeDTO.class
        );

        // Validate the UnitType in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedUnitType = unitTypeMapper.toEntity(returnedUnitTypeDTO);
        assertUnitTypeUpdatableFieldsEquals(returnedUnitType, getPersistedUnitType(returnedUnitType));

        insertedUnitType = returnedUnitType;
    }

    @Test
    @Transactional
    void createUnitTypeWithExistingId() throws Exception {
        // Create the UnitType with an existing ID
        unitType.setId(1L);
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

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

        // Create the UnitType, which fails.
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkDescriptionIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        unitType.setDescription(null);

        // Create the UnitType, which fails.
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkValueTypeIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        unitType.setValueType(null);

        // Create the UnitType, which fails.
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllUnitTypes() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        // Get all the unitTypeList
        restUnitTypeMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(unitType.getId().intValue())))
            .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
            .andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION)))
            .andExpect(jsonPath("$.[*].valueType").value(hasItem(DEFAULT_VALUE_TYPE.toString())))
            .andExpect(jsonPath("$.[*].version").value(hasItem(DEFAULT_VERSION)));
    }

    @Test
    @Transactional
    void getUnitType() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        // Get the unitType
        restUnitTypeMockMvc
            .perform(get(ENTITY_API_URL_ID, unitType.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(unitType.getId().intValue()))
            .andExpect(jsonPath("$.name").value(DEFAULT_NAME))
            .andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION))
            .andExpect(jsonPath("$.valueType").value(DEFAULT_VALUE_TYPE.toString()))
            .andExpect(jsonPath("$.version").value(DEFAULT_VERSION));
    }

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

    @Test
    @Transactional
    void putExistingUnitType() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the unitType
        UnitType updatedUnitType = unitTypeRepository.findById(unitType.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedUnitType are not directly saved in db
        em.detach(updatedUnitType);
        updatedUnitType.name(UPDATED_NAME).description(UPDATED_DESCRIPTION).valueType(UPDATED_VALUE_TYPE).version(UPDATED_VERSION);
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(updatedUnitType);

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

        // Validate the UnitType in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedUnitTypeToMatchAllProperties(updatedUnitType);
    }

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

    @Test
    @Transactional
    void partialUpdateUnitTypeWithPatch() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the unitType using partial update
        UnitType partialUpdatedUnitType = new UnitType();
        partialUpdatedUnitType.setId(unitType.getId());

        partialUpdatedUnitType.version(UPDATED_VERSION);

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

        // Validate the UnitType in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertUnitTypeUpdatableFieldsEquals(createUpdateProxyForBean(partialUpdatedUnitType, unitType), getPersistedUnitType(unitType));
    }

    @Test
    @Transactional
    void fullUpdateUnitTypeWithPatch() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the unitType using partial update
        UnitType partialUpdatedUnitType = new UnitType();
        partialUpdatedUnitType.setId(unitType.getId());

        partialUpdatedUnitType.name(UPDATED_NAME).description(UPDATED_DESCRIPTION).valueType(UPDATED_VALUE_TYPE).version(UPDATED_VERSION);

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

        // Validate the UnitType in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertUnitTypeUpdatableFieldsEquals(partialUpdatedUnitType, getPersistedUnitType(partialUpdatedUnitType));
    }

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

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

        // Create the UnitType
        UnitTypeDTO unitTypeDTO = unitTypeMapper.toDto(unitType);

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

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

    @Test
    @Transactional
    void deleteUnitType() throws Exception {
        // Initialize the database
        insertedUnitType = unitTypeRepository.saveAndFlush(unitType);

        long databaseSizeBeforeDelete = getRepositoryCount();

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

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

    protected long getRepositoryCount() {
        return unitTypeRepository.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 UnitType getPersistedUnitType(UnitType unitType) {
        return unitTypeRepository.findById(unitType.getId()).orElseThrow();
    }

    protected void assertPersistedUnitTypeToMatchAllProperties(UnitType expectedUnitType) {
        assertUnitTypeAllPropertiesEquals(expectedUnitType, getPersistedUnitType(expectedUnitType));
    }

    protected void assertPersistedUnitTypeToMatchUpdatableProperties(UnitType expectedUnitType) {
        assertUnitTypeAllUpdatablePropertiesEquals(expectedUnitType, getPersistedUnitType(expectedUnitType));
    }
}
