content: package com.example.demo.web.rest;
  
  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.example.demo.IntegrationTest;
  import com.example.demo.domain.Task;
  import com.example.demo.repository.TaskRepository;
  import java.util.List;
  import java.util.Random;
  import java.util.concurrent.atomic.AtomicLong;
  import javax.persistence.EntityManager;
  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 TaskResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class TaskResourceIT {
  
      private static final String DEFAULT_TITLE = "AAAAAAAAAA";
      private static final String UPDATED_TITLE = "BBBBBBBBBB";
  
      private static final String DEFAULT_DESCRIPTION = "AAAAAAAAAA";
      private static final String UPDATED_DESCRIPTION = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/tasks";
      private static final String ENTITY_API_URL_ID = ENTITY_API_URL + "/{id}";
  
      private static Random random = new Random();
      private static AtomicLong count = new AtomicLong(random.nextInt() + (2 * Integer.MAX_VALUE));
  
      @Autowired
      private TaskRepository taskRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restTaskMockMvc;
  
      private Task task;
  
      /**
       * 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 Task createEntity(EntityManager em) {
          Task task = new Task().title(DEFAULT_TITLE).description(DEFAULT_DESCRIPTION);
          return task;
      }
  
      /**
       * 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 Task createUpdatedEntity(EntityManager em) {
          Task task = new Task().title(UPDATED_TITLE).description(UPDATED_DESCRIPTION);
          return task;
      }
  
      @BeforeEach
      public void initTest() {
          task = createEntity(em);
      }
  
      @Test
      @Transactional
      void createTask() throws Exception {
          int databaseSizeBeforeCreate = taskRepository.findAll().size();
          // Create the Task
          restTaskMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(task)))
              .andExpect(status().isCreated());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeCreate + 1);
          Task testTask = taskList.get(taskList.size() - 1);
          assertThat(testTask.getTitle()).isEqualTo(DEFAULT_TITLE);
          assertThat(testTask.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void createTaskWithExistingId() throws Exception {
          // Create the Task with an existing ID
          task.setId(1L);
  
          int databaseSizeBeforeCreate = taskRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restTaskMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(task)))
              .andExpect(status().isBadRequest());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllTasks() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          // Get all the taskList
          restTaskMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(task.getId().intValue())))
              .andExpect(jsonPath("$.[*].title").value(hasItem(DEFAULT_TITLE)))
              .andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION)));
      }
  
      @Test
      @Transactional
      void getTask() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          // Get the task
          restTaskMockMvc
              .perform(get(ENTITY_API_URL_ID, task.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(task.getId().intValue()))
              .andExpect(jsonPath("$.title").value(DEFAULT_TITLE))
              .andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION));
      }
  
      @Test
      @Transactional
      void getNonExistingTask() throws Exception {
          // Get the task
          restTaskMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewTask() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
  
          // Update the task
          Task updatedTask = taskRepository.findById(task.getId()).get();
          // Disconnect from session so that the updates on updatedTask are not directly saved in db
          em.detach(updatedTask);
          updatedTask.title(UPDATED_TITLE).description(UPDATED_DESCRIPTION);
  
          restTaskMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedTask.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedTask))
              )
              .andExpect(status().isOk());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
          Task testTask = taskList.get(taskList.size() - 1);
          assertThat(testTask.getTitle()).isEqualTo(UPDATED_TITLE);
          assertThat(testTask.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void putNonExistingTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, task.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(task))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(task))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(task)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateTaskWithPatch() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
  
          // Update the task using partial update
          Task partialUpdatedTask = new Task();
          partialUpdatedTask.setId(task.getId());
  
          partialUpdatedTask.description(UPDATED_DESCRIPTION);
  
          restTaskMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedTask.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedTask))
              )
              .andExpect(status().isOk());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
          Task testTask = taskList.get(taskList.size() - 1);
          assertThat(testTask.getTitle()).isEqualTo(DEFAULT_TITLE);
          assertThat(testTask.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void fullUpdateTaskWithPatch() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
  
          // Update the task using partial update
          Task partialUpdatedTask = new Task();
          partialUpdatedTask.setId(task.getId());
  
          partialUpdatedTask.title(UPDATED_TITLE).description(UPDATED_DESCRIPTION);
  
          restTaskMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedTask.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedTask))
              )
              .andExpect(status().isOk());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
          Task testTask = taskList.get(taskList.size() - 1);
          assertThat(testTask.getTitle()).isEqualTo(UPDATED_TITLE);
          assertThat(testTask.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void patchNonExistingTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, task.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(task))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(task))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamTask() throws Exception {
          int databaseSizeBeforeUpdate = taskRepository.findAll().size();
          task.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restTaskMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(task)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Task in the database
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteTask() throws Exception {
          // Initialize the database
          taskRepository.saveAndFlush(task);
  
          int databaseSizeBeforeDelete = taskRepository.findAll().size();
  
          // Delete the task
          restTaskMockMvc
              .perform(delete(ENTITY_API_URL_ID, task.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Task> taskList = taskRepository.findAll();
          assertThat(taskList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  