package com.oguerreiro.resilient.web.rest;

import static com.oguerreiro.resilient.domain.InputDataUploadAsserts.*;
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.mockito.Mockito.*;
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.InputDataUpload;
import com.oguerreiro.resilient.domain.enumeration.UploadStatus;
import com.oguerreiro.resilient.domain.enumeration.UploadType;
import com.oguerreiro.resilient.repository.InputDataUploadRepository;
import com.oguerreiro.resilient.service.InputDataUploadService;
import com.oguerreiro.resilient.service.dto.InputDataUploadDTO;
import com.oguerreiro.resilient.service.mapper.InputDataUploadMapper;
import jakarta.persistence.EntityManager;
import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Base64;
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 InputDataUploadResource} REST controller.
 */
@IntegrationTest
@ExtendWith(MockitoExtension.class)
@AutoConfigureMockMvc
@WithMockUser
class InputDataUploadResourceIT {

    private static final String DEFAULT_TITLE = "AAAAAAAAAA";
    private static final String UPDATED_TITLE = "BBBBBBBBBB";

    private static final byte[] DEFAULT_DATA_FILE = TestUtil.createByteArray(1, "0");
    private static final byte[] UPDATED_DATA_FILE = TestUtil.createByteArray(1, "1");
    private static final String DEFAULT_DATA_FILE_CONTENT_TYPE = "image/jpg";
    private static final String UPDATED_DATA_FILE_CONTENT_TYPE = "image/png";

    private static final String DEFAULT_UPLOAD_FILE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_UPLOAD_FILE_NAME = "BBBBBBBBBB";

    private static final String DEFAULT_DISK_FILE_NAME = "AAAAAAAAAA";
    private static final String UPDATED_DISK_FILE_NAME = "BBBBBBBBBB";

    private static final UploadType DEFAULT_TYPE = UploadType.INVENTORY;
    private static final UploadType UPDATED_TYPE = UploadType.ACCOUNTING;

    private static final UploadStatus DEFAULT_STATE = UploadStatus.UPLOADED;
    private static final UploadStatus UPDATED_STATE = UploadStatus.PROCESSING;

    private static final String DEFAULT_COMMENTS = "AAAAAAAAAA";
    private static final String UPDATED_COMMENTS = "BBBBBBBBBB";

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

    private static final String DEFAULT_CREATION_USERNAME = "AAAAAAAAAA";
    private static final String UPDATED_CREATION_USERNAME = "BBBBBBBBBB";

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

    private static final String ENTITY_API_URL = "/api/input-data-uploads";
    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 InputDataUploadRepository inputDataUploadRepository;

    @Mock
    private InputDataUploadRepository inputDataUploadRepositoryMock;

    @Autowired
    private InputDataUploadMapper inputDataUploadMapper;

    @Mock
    private InputDataUploadService inputDataUploadServiceMock;

    @Autowired
    private EntityManager em;

    @Autowired
    private MockMvc restInputDataUploadMockMvc;

    private InputDataUpload inputDataUpload;

    private InputDataUpload insertedInputDataUpload;

    /**
     * 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 InputDataUpload createEntity(EntityManager em) {
        InputDataUpload inputDataUpload = new InputDataUpload()
            .title(DEFAULT_TITLE)
            .dataFile(DEFAULT_DATA_FILE)
            .dataFileContentType(DEFAULT_DATA_FILE_CONTENT_TYPE)
            .uploadFileName(DEFAULT_UPLOAD_FILE_NAME)
            .diskFileName(DEFAULT_DISK_FILE_NAME)
            .type(DEFAULT_TYPE)
            .state(DEFAULT_STATE)
            .comments(DEFAULT_COMMENTS)
            .creationDate(DEFAULT_CREATION_DATE)
            .creationUsername(DEFAULT_CREATION_USERNAME)
            .version(DEFAULT_VERSION);
        return inputDataUpload;
    }

    /**
     * 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 InputDataUpload createUpdatedEntity(EntityManager em) {
        InputDataUpload inputDataUpload = new InputDataUpload()
            .title(UPDATED_TITLE)
            .dataFile(UPDATED_DATA_FILE)
            .dataFileContentType(UPDATED_DATA_FILE_CONTENT_TYPE)
            .uploadFileName(UPDATED_UPLOAD_FILE_NAME)
            .diskFileName(UPDATED_DISK_FILE_NAME)
            .type(UPDATED_TYPE)
            .state(UPDATED_STATE)
            .comments(UPDATED_COMMENTS)
            .creationDate(UPDATED_CREATION_DATE)
            .creationUsername(UPDATED_CREATION_USERNAME)
            .version(UPDATED_VERSION);
        return inputDataUpload;
    }

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

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

    @Test
    @Transactional
    void createInputDataUpload() throws Exception {
        long databaseSizeBeforeCreate = getRepositoryCount();
        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);
        var returnedInputDataUploadDTO = om.readValue(
            restInputDataUploadMockMvc
                .perform(
                    post(ENTITY_API_URL)
                        .with(csrf())
                        .contentType(MediaType.APPLICATION_JSON)
                        .content(om.writeValueAsBytes(inputDataUploadDTO))
                )
                .andExpect(status().isCreated())
                .andReturn()
                .getResponse()
                .getContentAsString(),
            InputDataUploadDTO.class
        );

        // Validate the InputDataUpload in the database
        assertIncrementedRepositoryCount(databaseSizeBeforeCreate);
        var returnedInputDataUpload = inputDataUploadMapper.toEntity(returnedInputDataUploadDTO);
        assertInputDataUploadUpdatableFieldsEquals(returnedInputDataUpload, getPersistedInputDataUpload(returnedInputDataUpload));

        insertedInputDataUpload = returnedInputDataUpload;
    }

    @Test
    @Transactional
    void createInputDataUploadWithExistingId() throws Exception {
        // Create the InputDataUpload with an existing ID
        inputDataUpload.setId(1L);
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

        long databaseSizeBeforeCreate = getRepositoryCount();

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

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

    @Test
    @Transactional
    void checkTitleIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        inputDataUpload.setTitle(null);

        // Create the InputDataUpload, which fails.
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkTypeIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        inputDataUpload.setType(null);

        // Create the InputDataUpload, which fails.
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkStateIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        inputDataUpload.setState(null);

        // Create the InputDataUpload, which fails.
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkCreationDateIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        inputDataUpload.setCreationDate(null);

        // Create the InputDataUpload, which fails.
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void checkCreationUsernameIsRequired() throws Exception {
        long databaseSizeBeforeTest = getRepositoryCount();
        // set the field null
        inputDataUpload.setCreationUsername(null);

        // Create the InputDataUpload, which fails.
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

        assertSameRepositoryCount(databaseSizeBeforeTest);
    }

    @Test
    @Transactional
    void getAllInputDataUploads() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        // Get all the inputDataUploadList
        restInputDataUploadMockMvc
            .perform(get(ENTITY_API_URL + "?sort=id,desc"))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.[*].id").value(hasItem(inputDataUpload.getId().intValue())))
            .andExpect(jsonPath("$.[*].title").value(hasItem(DEFAULT_TITLE)))
            .andExpect(jsonPath("$.[*].dataFileContentType").value(hasItem(DEFAULT_DATA_FILE_CONTENT_TYPE)))
            .andExpect(jsonPath("$.[*].dataFile").value(hasItem(Base64.getEncoder().encodeToString(DEFAULT_DATA_FILE))))
            .andExpect(jsonPath("$.[*].uploadFileName").value(hasItem(DEFAULT_UPLOAD_FILE_NAME)))
            .andExpect(jsonPath("$.[*].diskFileName").value(hasItem(DEFAULT_DISK_FILE_NAME)))
            .andExpect(jsonPath("$.[*].type").value(hasItem(DEFAULT_TYPE.toString())))
            .andExpect(jsonPath("$.[*].state").value(hasItem(DEFAULT_STATE.toString())))
            .andExpect(jsonPath("$.[*].comments").value(hasItem(DEFAULT_COMMENTS)))
            .andExpect(jsonPath("$.[*].creationDate").value(hasItem(DEFAULT_CREATION_DATE.toString())))
            .andExpect(jsonPath("$.[*].creationUsername").value(hasItem(DEFAULT_CREATION_USERNAME)))
            .andExpect(jsonPath("$.[*].version").value(hasItem(DEFAULT_VERSION)));
    }

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

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

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

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

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

    @Test
    @Transactional
    void getInputDataUpload() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        // Get the inputDataUpload
        restInputDataUploadMockMvc
            .perform(get(ENTITY_API_URL_ID, inputDataUpload.getId()))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.id").value(inputDataUpload.getId().intValue()))
            .andExpect(jsonPath("$.title").value(DEFAULT_TITLE))
            .andExpect(jsonPath("$.dataFileContentType").value(DEFAULT_DATA_FILE_CONTENT_TYPE))
            .andExpect(jsonPath("$.dataFile").value(Base64.getEncoder().encodeToString(DEFAULT_DATA_FILE)))
            .andExpect(jsonPath("$.uploadFileName").value(DEFAULT_UPLOAD_FILE_NAME))
            .andExpect(jsonPath("$.diskFileName").value(DEFAULT_DISK_FILE_NAME))
            .andExpect(jsonPath("$.type").value(DEFAULT_TYPE.toString()))
            .andExpect(jsonPath("$.state").value(DEFAULT_STATE.toString()))
            .andExpect(jsonPath("$.comments").value(DEFAULT_COMMENTS))
            .andExpect(jsonPath("$.creationDate").value(DEFAULT_CREATION_DATE.toString()))
            .andExpect(jsonPath("$.creationUsername").value(DEFAULT_CREATION_USERNAME))
            .andExpect(jsonPath("$.version").value(DEFAULT_VERSION));
    }

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

    @Test
    @Transactional
    void putExistingInputDataUpload() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the inputDataUpload
        InputDataUpload updatedInputDataUpload = inputDataUploadRepository.findById(inputDataUpload.getId()).orElseThrow();
        // Disconnect from session so that the updates on updatedInputDataUpload are not directly saved in db
        em.detach(updatedInputDataUpload);
        updatedInputDataUpload
            .title(UPDATED_TITLE)
            .dataFile(UPDATED_DATA_FILE)
            .dataFileContentType(UPDATED_DATA_FILE_CONTENT_TYPE)
            .uploadFileName(UPDATED_UPLOAD_FILE_NAME)
            .diskFileName(UPDATED_DISK_FILE_NAME)
            .type(UPDATED_TYPE)
            .state(UPDATED_STATE)
            .comments(UPDATED_COMMENTS)
            .creationDate(UPDATED_CREATION_DATE)
            .creationUsername(UPDATED_CREATION_USERNAME)
            .version(UPDATED_VERSION);
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(updatedInputDataUpload);

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

        // Validate the InputDataUpload in the database
        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertPersistedInputDataUploadToMatchAllProperties(updatedInputDataUpload);
    }

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

    @Test
    @Transactional
    void partialUpdateInputDataUploadWithPatch() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the inputDataUpload using partial update
        InputDataUpload partialUpdatedInputDataUpload = new InputDataUpload();
        partialUpdatedInputDataUpload.setId(inputDataUpload.getId());

        partialUpdatedInputDataUpload
            .title(UPDATED_TITLE)
            .uploadFileName(UPDATED_UPLOAD_FILE_NAME)
            .comments(UPDATED_COMMENTS)
            .creationDate(UPDATED_CREATION_DATE)
            .creationUsername(UPDATED_CREATION_USERNAME);

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

        // Validate the InputDataUpload in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertInputDataUploadUpdatableFieldsEquals(
            createUpdateProxyForBean(partialUpdatedInputDataUpload, inputDataUpload),
            getPersistedInputDataUpload(inputDataUpload)
        );
    }

    @Test
    @Transactional
    void fullUpdateInputDataUploadWithPatch() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        long databaseSizeBeforeUpdate = getRepositoryCount();

        // Update the inputDataUpload using partial update
        InputDataUpload partialUpdatedInputDataUpload = new InputDataUpload();
        partialUpdatedInputDataUpload.setId(inputDataUpload.getId());

        partialUpdatedInputDataUpload
            .title(UPDATED_TITLE)
            .dataFile(UPDATED_DATA_FILE)
            .dataFileContentType(UPDATED_DATA_FILE_CONTENT_TYPE)
            .uploadFileName(UPDATED_UPLOAD_FILE_NAME)
            .diskFileName(UPDATED_DISK_FILE_NAME)
            .type(UPDATED_TYPE)
            .state(UPDATED_STATE)
            .comments(UPDATED_COMMENTS)
            .creationDate(UPDATED_CREATION_DATE)
            .creationUsername(UPDATED_CREATION_USERNAME)
            .version(UPDATED_VERSION);

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

        // Validate the InputDataUpload in the database

        assertSameRepositoryCount(databaseSizeBeforeUpdate);
        assertInputDataUploadUpdatableFieldsEquals(
            partialUpdatedInputDataUpload,
            getPersistedInputDataUpload(partialUpdatedInputDataUpload)
        );
    }

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

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

        // Create the InputDataUpload
        InputDataUploadDTO inputDataUploadDTO = inputDataUploadMapper.toDto(inputDataUpload);

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

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

    @Test
    @Transactional
    void deleteInputDataUpload() throws Exception {
        // Initialize the database
        insertedInputDataUpload = inputDataUploadRepository.saveAndFlush(inputDataUpload);

        long databaseSizeBeforeDelete = getRepositoryCount();

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

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

    protected long getRepositoryCount() {
        return inputDataUploadRepository.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 InputDataUpload getPersistedInputDataUpload(InputDataUpload inputDataUpload) {
        return inputDataUploadRepository.findById(inputDataUpload.getId()).orElseThrow();
    }

    protected void assertPersistedInputDataUploadToMatchAllProperties(InputDataUpload expectedInputDataUpload) {
        assertInputDataUploadAllPropertiesEquals(expectedInputDataUpload, getPersistedInputDataUpload(expectedInputDataUpload));
    }

    protected void assertPersistedInputDataUploadToMatchUpdatableProperties(InputDataUpload expectedInputDataUpload) {
        assertInputDataUploadAllUpdatablePropertiesEquals(expectedInputDataUpload, getPersistedInputDataUpload(expectedInputDataUpload));
    }
}
