content: package com.example.demo.web.rest;
  
  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.example.demo.IntegrationTest;
  import com.example.demo.domain.Job;
  import com.example.demo.repository.JobRepository;
  import java.util.ArrayList;
  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.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.PageRequest;
  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 JobResource} REST controller.
   */
  @IntegrationTest
  @ExtendWith(MockitoExtension.class)
  @AutoConfigureMockMvc
  @WithMockUser
  class JobResourceIT {
  
      private static final String DEFAULT_JOB_TITLE = "AAAAAAAAAA";
      private static final String UPDATED_JOB_TITLE = "BBBBBBBBBB";
  
      private static final Long DEFAULT_MIN_SALARY = 1L;
      private static final Long UPDATED_MIN_SALARY = 2L;
  
      private static final Long DEFAULT_MAX_SALARY = 1L;
      private static final Long UPDATED_MAX_SALARY = 2L;
  
      private static final String ENTITY_API_URL = "/api/jobs";
      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 JobRepository jobRepository;
  
      @Mock
      private JobRepository jobRepositoryMock;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restJobMockMvc;
  
      private Job job;
  
      /**
       * 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 Job createEntity(EntityManager em) {
          Job job = new Job().jobTitle(DEFAULT_JOB_TITLE).minSalary(DEFAULT_MIN_SALARY).maxSalary(DEFAULT_MAX_SALARY);
          return job;
      }
  
      /**
       * 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 Job createUpdatedEntity(EntityManager em) {
          Job job = new Job().jobTitle(UPDATED_JOB_TITLE).minSalary(UPDATED_MIN_SALARY).maxSalary(UPDATED_MAX_SALARY);
          return job;
      }
  
      @BeforeEach
      public void initTest() {
          job = createEntity(em);
      }
  
      @Test
      @Transactional
      void createJob() throws Exception {
          int databaseSizeBeforeCreate = jobRepository.findAll().size();
          // Create the Job
          restJobMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(job)))
              .andExpect(status().isCreated());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeCreate + 1);
          Job testJob = jobList.get(jobList.size() - 1);
          assertThat(testJob.getJobTitle()).isEqualTo(DEFAULT_JOB_TITLE);
          assertThat(testJob.getMinSalary()).isEqualTo(DEFAULT_MIN_SALARY);
          assertThat(testJob.getMaxSalary()).isEqualTo(DEFAULT_MAX_SALARY);
      }
  
      @Test
      @Transactional
      void createJobWithExistingId() throws Exception {
          // Create the Job with an existing ID
          job.setId(1L);
  
          int databaseSizeBeforeCreate = jobRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restJobMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(job)))
              .andExpect(status().isBadRequest());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllJobs() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          // Get all the jobList
          restJobMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(job.getId().intValue())))
              .andExpect(jsonPath("$.[*].jobTitle").value(hasItem(DEFAULT_JOB_TITLE)))
              .andExpect(jsonPath("$.[*].minSalary").value(hasItem(DEFAULT_MIN_SALARY.intValue())))
              .andExpect(jsonPath("$.[*].maxSalary").value(hasItem(DEFAULT_MAX_SALARY.intValue())));
      }
  
      @SuppressWarnings({ "unchecked" })
      void getAllJobsWithEagerRelationshipsIsEnabled() throws Exception {
          when(jobRepositoryMock.findAllWithEagerRelationships(any())).thenReturn(new PageImpl(new ArrayList<>()));
  
          restJobMockMvc.perform(get(ENTITY_API_URL + "?eagerload=true")).andExpect(status().isOk());
  
          verify(jobRepositoryMock, times(1)).findAllWithEagerRelationships(any());
      }
  
      @SuppressWarnings({ "unchecked" })
      void getAllJobsWithEagerRelationshipsIsNotEnabled() throws Exception {
          when(jobRepositoryMock.findAllWithEagerRelationships(any())).thenReturn(new PageImpl(new ArrayList<>()));
  
          restJobMockMvc.perform(get(ENTITY_API_URL + "?eagerload=true")).andExpect(status().isOk());
  
          verify(jobRepositoryMock, times(1)).findAllWithEagerRelationships(any());
      }
  
      @Test
      @Transactional
      void getJob() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          // Get the job
          restJobMockMvc
              .perform(get(ENTITY_API_URL_ID, job.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(job.getId().intValue()))
              .andExpect(jsonPath("$.jobTitle").value(DEFAULT_JOB_TITLE))
              .andExpect(jsonPath("$.minSalary").value(DEFAULT_MIN_SALARY.intValue()))
              .andExpect(jsonPath("$.maxSalary").value(DEFAULT_MAX_SALARY.intValue()));
      }
  
      @Test
      @Transactional
      void getNonExistingJob() throws Exception {
          // Get the job
          restJobMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewJob() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
  
          // Update the job
          Job updatedJob = jobRepository.findById(job.getId()).get();
          // Disconnect from session so that the updates on updatedJob are not directly saved in db
          em.detach(updatedJob);
          updatedJob.jobTitle(UPDATED_JOB_TITLE).minSalary(UPDATED_MIN_SALARY).maxSalary(UPDATED_MAX_SALARY);
  
          restJobMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedJob.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedJob))
              )
              .andExpect(status().isOk());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
          Job testJob = jobList.get(jobList.size() - 1);
          assertThat(testJob.getJobTitle()).isEqualTo(UPDATED_JOB_TITLE);
          assertThat(testJob.getMinSalary()).isEqualTo(UPDATED_MIN_SALARY);
          assertThat(testJob.getMaxSalary()).isEqualTo(UPDATED_MAX_SALARY);
      }
  
      @Test
      @Transactional
      void putNonExistingJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, job.getId()).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(job))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(job))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(job)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateJobWithPatch() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
  
          // Update the job using partial update
          Job partialUpdatedJob = new Job();
          partialUpdatedJob.setId(job.getId());
  
          partialUpdatedJob.minSalary(UPDATED_MIN_SALARY);
  
          restJobMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedJob.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedJob))
              )
              .andExpect(status().isOk());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
          Job testJob = jobList.get(jobList.size() - 1);
          assertThat(testJob.getJobTitle()).isEqualTo(DEFAULT_JOB_TITLE);
          assertThat(testJob.getMinSalary()).isEqualTo(UPDATED_MIN_SALARY);
          assertThat(testJob.getMaxSalary()).isEqualTo(DEFAULT_MAX_SALARY);
      }
  
      @Test
      @Transactional
      void fullUpdateJobWithPatch() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
  
          // Update the job using partial update
          Job partialUpdatedJob = new Job();
          partialUpdatedJob.setId(job.getId());
  
          partialUpdatedJob.jobTitle(UPDATED_JOB_TITLE).minSalary(UPDATED_MIN_SALARY).maxSalary(UPDATED_MAX_SALARY);
  
          restJobMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedJob.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedJob))
              )
              .andExpect(status().isOk());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
          Job testJob = jobList.get(jobList.size() - 1);
          assertThat(testJob.getJobTitle()).isEqualTo(UPDATED_JOB_TITLE);
          assertThat(testJob.getMinSalary()).isEqualTo(UPDATED_MIN_SALARY);
          assertThat(testJob.getMaxSalary()).isEqualTo(UPDATED_MAX_SALARY);
      }
  
      @Test
      @Transactional
      void patchNonExistingJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, job.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(job))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(job))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamJob() throws Exception {
          int databaseSizeBeforeUpdate = jobRepository.findAll().size();
          job.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restJobMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(job)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Job in the database
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteJob() throws Exception {
          // Initialize the database
          jobRepository.saveAndFlush(job);
  
          int databaseSizeBeforeDelete = jobRepository.findAll().size();
  
          // Delete the job
          restJobMockMvc.perform(delete(ENTITY_API_URL_ID, job.getId()).accept(MediaType.APPLICATION_JSON)).andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Job> jobList = jobRepository.findAll();
          assertThat(jobList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  