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.Employee;
  import com.example.demo.repository.EmployeeRepository;
  import java.time.Instant;
  import java.time.temporal.ChronoUnit;
  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 EmployeeResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class EmployeeResourceIT {
  
      private static final String DEFAULT_FIRST_NAME = "AAAAAAAAAA";
      private static final String UPDATED_FIRST_NAME = "BBBBBBBBBB";
  
      private static final String DEFAULT_LAST_NAME = "AAAAAAAAAA";
      private static final String UPDATED_LAST_NAME = "BBBBBBBBBB";
  
      private static final String DEFAULT_EMAIL = "AAAAAAAAAA";
      private static final String UPDATED_EMAIL = "BBBBBBBBBB";
  
      private static final String DEFAULT_PHONE_NUMBER = "AAAAAAAAAA";
      private static final String UPDATED_PHONE_NUMBER = "BBBBBBBBBB";
  
      private static final Instant DEFAULT_HIRE_DATE = Instant.ofEpochMilli(0L);
      private static final Instant UPDATED_HIRE_DATE = Instant.now().truncatedTo(ChronoUnit.MILLIS);
  
      private static final Long DEFAULT_SALARY = 1L;
      private static final Long UPDATED_SALARY = 2L;
  
      private static final Long DEFAULT_COMMISSION_PCT = 1L;
      private static final Long UPDATED_COMMISSION_PCT = 2L;
  
      private static final String ENTITY_API_URL = "/api/employees";
      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 EmployeeRepository employeeRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restEmployeeMockMvc;
  
      private Employee employee;
  
      /**
       * 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 Employee createEntity(EntityManager em) {
          Employee employee = new Employee()
              .firstName(DEFAULT_FIRST_NAME)
              .lastName(DEFAULT_LAST_NAME)
              .email(DEFAULT_EMAIL)
              .phoneNumber(DEFAULT_PHONE_NUMBER)
              .hireDate(DEFAULT_HIRE_DATE)
              .salary(DEFAULT_SALARY)
              .commissionPct(DEFAULT_COMMISSION_PCT);
          return employee;
      }
  
      /**
       * 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 Employee createUpdatedEntity(EntityManager em) {
          Employee employee = new Employee()
              .firstName(UPDATED_FIRST_NAME)
              .lastName(UPDATED_LAST_NAME)
              .email(UPDATED_EMAIL)
              .phoneNumber(UPDATED_PHONE_NUMBER)
              .hireDate(UPDATED_HIRE_DATE)
              .salary(UPDATED_SALARY)
              .commissionPct(UPDATED_COMMISSION_PCT);
          return employee;
      }
  
      @BeforeEach
      public void initTest() {
          employee = createEntity(em);
      }
  
      @Test
      @Transactional
      void createEmployee() throws Exception {
          int databaseSizeBeforeCreate = employeeRepository.findAll().size();
          // Create the Employee
          restEmployeeMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(employee)))
              .andExpect(status().isCreated());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeCreate + 1);
          Employee testEmployee = employeeList.get(employeeList.size() - 1);
          assertThat(testEmployee.getFirstName()).isEqualTo(DEFAULT_FIRST_NAME);
          assertThat(testEmployee.getLastName()).isEqualTo(DEFAULT_LAST_NAME);
          assertThat(testEmployee.getEmail()).isEqualTo(DEFAULT_EMAIL);
          assertThat(testEmployee.getPhoneNumber()).isEqualTo(DEFAULT_PHONE_NUMBER);
          assertThat(testEmployee.getHireDate()).isEqualTo(DEFAULT_HIRE_DATE);
          assertThat(testEmployee.getSalary()).isEqualTo(DEFAULT_SALARY);
          assertThat(testEmployee.getCommissionPct()).isEqualTo(DEFAULT_COMMISSION_PCT);
      }
  
      @Test
      @Transactional
      void createEmployeeWithExistingId() throws Exception {
          // Create the Employee with an existing ID
          employee.setId(1L);
  
          int databaseSizeBeforeCreate = employeeRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restEmployeeMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(employee)))
              .andExpect(status().isBadRequest());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllEmployees() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          // Get all the employeeList
          restEmployeeMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(employee.getId().intValue())))
              .andExpect(jsonPath("$.[*].firstName").value(hasItem(DEFAULT_FIRST_NAME)))
              .andExpect(jsonPath("$.[*].lastName").value(hasItem(DEFAULT_LAST_NAME)))
              .andExpect(jsonPath("$.[*].email").value(hasItem(DEFAULT_EMAIL)))
              .andExpect(jsonPath("$.[*].phoneNumber").value(hasItem(DEFAULT_PHONE_NUMBER)))
              .andExpect(jsonPath("$.[*].hireDate").value(hasItem(DEFAULT_HIRE_DATE.toString())))
              .andExpect(jsonPath("$.[*].salary").value(hasItem(DEFAULT_SALARY.intValue())))
              .andExpect(jsonPath("$.[*].commissionPct").value(hasItem(DEFAULT_COMMISSION_PCT.intValue())));
      }
  
      @Test
      @Transactional
      void getEmployee() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          // Get the employee
          restEmployeeMockMvc
              .perform(get(ENTITY_API_URL_ID, employee.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(employee.getId().intValue()))
              .andExpect(jsonPath("$.firstName").value(DEFAULT_FIRST_NAME))
              .andExpect(jsonPath("$.lastName").value(DEFAULT_LAST_NAME))
              .andExpect(jsonPath("$.email").value(DEFAULT_EMAIL))
              .andExpect(jsonPath("$.phoneNumber").value(DEFAULT_PHONE_NUMBER))
              .andExpect(jsonPath("$.hireDate").value(DEFAULT_HIRE_DATE.toString()))
              .andExpect(jsonPath("$.salary").value(DEFAULT_SALARY.intValue()))
              .andExpect(jsonPath("$.commissionPct").value(DEFAULT_COMMISSION_PCT.intValue()));
      }
  
      @Test
      @Transactional
      void getNonExistingEmployee() throws Exception {
          // Get the employee
          restEmployeeMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewEmployee() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
  
          // Update the employee
          Employee updatedEmployee = employeeRepository.findById(employee.getId()).get();
          // Disconnect from session so that the updates on updatedEmployee are not directly saved in db
          em.detach(updatedEmployee);
          updatedEmployee
              .firstName(UPDATED_FIRST_NAME)
              .lastName(UPDATED_LAST_NAME)
              .email(UPDATED_EMAIL)
              .phoneNumber(UPDATED_PHONE_NUMBER)
              .hireDate(UPDATED_HIRE_DATE)
              .salary(UPDATED_SALARY)
              .commissionPct(UPDATED_COMMISSION_PCT);
  
          restEmployeeMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedEmployee.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedEmployee))
              )
              .andExpect(status().isOk());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
          Employee testEmployee = employeeList.get(employeeList.size() - 1);
          assertThat(testEmployee.getFirstName()).isEqualTo(UPDATED_FIRST_NAME);
          assertThat(testEmployee.getLastName()).isEqualTo(UPDATED_LAST_NAME);
          assertThat(testEmployee.getEmail()).isEqualTo(UPDATED_EMAIL);
          assertThat(testEmployee.getPhoneNumber()).isEqualTo(UPDATED_PHONE_NUMBER);
          assertThat(testEmployee.getHireDate()).isEqualTo(UPDATED_HIRE_DATE);
          assertThat(testEmployee.getSalary()).isEqualTo(UPDATED_SALARY);
          assertThat(testEmployee.getCommissionPct()).isEqualTo(UPDATED_COMMISSION_PCT);
      }
  
      @Test
      @Transactional
      void putNonExistingEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, employee.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(employee))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(employee))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(employee)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateEmployeeWithPatch() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
  
          // Update the employee using partial update
          Employee partialUpdatedEmployee = new Employee();
          partialUpdatedEmployee.setId(employee.getId());
  
          partialUpdatedEmployee.lastName(UPDATED_LAST_NAME).email(UPDATED_EMAIL).commissionPct(UPDATED_COMMISSION_PCT);
  
          restEmployeeMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedEmployee.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedEmployee))
              )
              .andExpect(status().isOk());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
          Employee testEmployee = employeeList.get(employeeList.size() - 1);
          assertThat(testEmployee.getFirstName()).isEqualTo(DEFAULT_FIRST_NAME);
          assertThat(testEmployee.getLastName()).isEqualTo(UPDATED_LAST_NAME);
          assertThat(testEmployee.getEmail()).isEqualTo(UPDATED_EMAIL);
          assertThat(testEmployee.getPhoneNumber()).isEqualTo(DEFAULT_PHONE_NUMBER);
          assertThat(testEmployee.getHireDate()).isEqualTo(DEFAULT_HIRE_DATE);
          assertThat(testEmployee.getSalary()).isEqualTo(DEFAULT_SALARY);
          assertThat(testEmployee.getCommissionPct()).isEqualTo(UPDATED_COMMISSION_PCT);
      }
  
      @Test
      @Transactional
      void fullUpdateEmployeeWithPatch() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
  
          // Update the employee using partial update
          Employee partialUpdatedEmployee = new Employee();
          partialUpdatedEmployee.setId(employee.getId());
  
          partialUpdatedEmployee
              .firstName(UPDATED_FIRST_NAME)
              .lastName(UPDATED_LAST_NAME)
              .email(UPDATED_EMAIL)
              .phoneNumber(UPDATED_PHONE_NUMBER)
              .hireDate(UPDATED_HIRE_DATE)
              .salary(UPDATED_SALARY)
              .commissionPct(UPDATED_COMMISSION_PCT);
  
          restEmployeeMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedEmployee.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedEmployee))
              )
              .andExpect(status().isOk());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
          Employee testEmployee = employeeList.get(employeeList.size() - 1);
          assertThat(testEmployee.getFirstName()).isEqualTo(UPDATED_FIRST_NAME);
          assertThat(testEmployee.getLastName()).isEqualTo(UPDATED_LAST_NAME);
          assertThat(testEmployee.getEmail()).isEqualTo(UPDATED_EMAIL);
          assertThat(testEmployee.getPhoneNumber()).isEqualTo(UPDATED_PHONE_NUMBER);
          assertThat(testEmployee.getHireDate()).isEqualTo(UPDATED_HIRE_DATE);
          assertThat(testEmployee.getSalary()).isEqualTo(UPDATED_SALARY);
          assertThat(testEmployee.getCommissionPct()).isEqualTo(UPDATED_COMMISSION_PCT);
      }
  
      @Test
      @Transactional
      void patchNonExistingEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, employee.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(employee))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(employee))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamEmployee() throws Exception {
          int databaseSizeBeforeUpdate = employeeRepository.findAll().size();
          employee.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restEmployeeMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(employee)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Employee in the database
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteEmployee() throws Exception {
          // Initialize the database
          employeeRepository.saveAndFlush(employee);
  
          int databaseSizeBeforeDelete = employeeRepository.findAll().size();
  
          // Delete the employee
          restEmployeeMockMvc
              .perform(delete(ENTITY_API_URL_ID, employee.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Employee> employeeList = employeeRepository.findAll();
          assertThat(employeeList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  