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.Department;
  import com.example.demo.repository.DepartmentRepository;
  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 DepartmentResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class DepartmentResourceIT {
  
      private static final String DEFAULT_DEPARTMENT_NAME = "AAAAAAAAAA";
      private static final String UPDATED_DEPARTMENT_NAME = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/departments";
      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 DepartmentRepository departmentRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restDepartmentMockMvc;
  
      private Department department;
  
      /**
       * 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 Department createEntity(EntityManager em) {
          Department department = new Department().departmentName(DEFAULT_DEPARTMENT_NAME);
          return department;
      }
  
      /**
       * 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 Department createUpdatedEntity(EntityManager em) {
          Department department = new Department().departmentName(UPDATED_DEPARTMENT_NAME);
          return department;
      }
  
      @BeforeEach
      public void initTest() {
          department = createEntity(em);
      }
  
      @Test
      @Transactional
      void createDepartment() throws Exception {
          int databaseSizeBeforeCreate = departmentRepository.findAll().size();
          // Create the Department
          restDepartmentMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(department)))
              .andExpect(status().isCreated());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeCreate + 1);
          Department testDepartment = departmentList.get(departmentList.size() - 1);
          assertThat(testDepartment.getDepartmentName()).isEqualTo(DEFAULT_DEPARTMENT_NAME);
      }
  
      @Test
      @Transactional
      void createDepartmentWithExistingId() throws Exception {
          // Create the Department with an existing ID
          department.setId(1L);
  
          int databaseSizeBeforeCreate = departmentRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restDepartmentMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(department)))
              .andExpect(status().isBadRequest());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void checkDepartmentNameIsRequired() throws Exception {
          int databaseSizeBeforeTest = departmentRepository.findAll().size();
          // set the field null
          department.setDepartmentName(null);
  
          // Create the Department, which fails.
  
          restDepartmentMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(department)))
              .andExpect(status().isBadRequest());
  
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void getAllDepartments() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          // Get all the departmentList
          restDepartmentMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(department.getId().intValue())))
              .andExpect(jsonPath("$.[*].departmentName").value(hasItem(DEFAULT_DEPARTMENT_NAME)));
      }
  
      @Test
      @Transactional
      void getDepartment() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          // Get the department
          restDepartmentMockMvc
              .perform(get(ENTITY_API_URL_ID, department.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(department.getId().intValue()))
              .andExpect(jsonPath("$.departmentName").value(DEFAULT_DEPARTMENT_NAME));
      }
  
      @Test
      @Transactional
      void getNonExistingDepartment() throws Exception {
          // Get the department
          restDepartmentMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewDepartment() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
  
          // Update the department
          Department updatedDepartment = departmentRepository.findById(department.getId()).get();
          // Disconnect from session so that the updates on updatedDepartment are not directly saved in db
          em.detach(updatedDepartment);
          updatedDepartment.departmentName(UPDATED_DEPARTMENT_NAME);
  
          restDepartmentMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedDepartment.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedDepartment))
              )
              .andExpect(status().isOk());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
          Department testDepartment = departmentList.get(departmentList.size() - 1);
          assertThat(testDepartment.getDepartmentName()).isEqualTo(UPDATED_DEPARTMENT_NAME);
      }
  
      @Test
      @Transactional
      void putNonExistingDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, department.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(department))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(department))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(department)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateDepartmentWithPatch() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
  
          // Update the department using partial update
          Department partialUpdatedDepartment = new Department();
          partialUpdatedDepartment.setId(department.getId());
  
          restDepartmentMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedDepartment.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedDepartment))
              )
              .andExpect(status().isOk());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
          Department testDepartment = departmentList.get(departmentList.size() - 1);
          assertThat(testDepartment.getDepartmentName()).isEqualTo(DEFAULT_DEPARTMENT_NAME);
      }
  
      @Test
      @Transactional
      void fullUpdateDepartmentWithPatch() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
  
          // Update the department using partial update
          Department partialUpdatedDepartment = new Department();
          partialUpdatedDepartment.setId(department.getId());
  
          partialUpdatedDepartment.departmentName(UPDATED_DEPARTMENT_NAME);
  
          restDepartmentMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedDepartment.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedDepartment))
              )
              .andExpect(status().isOk());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
          Department testDepartment = departmentList.get(departmentList.size() - 1);
          assertThat(testDepartment.getDepartmentName()).isEqualTo(UPDATED_DEPARTMENT_NAME);
      }
  
      @Test
      @Transactional
      void patchNonExistingDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, department.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(department))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(department))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamDepartment() throws Exception {
          int databaseSizeBeforeUpdate = departmentRepository.findAll().size();
          department.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restDepartmentMockMvc
              .perform(
                  patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(department))
              )
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Department in the database
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteDepartment() throws Exception {
          // Initialize the database
          departmentRepository.saveAndFlush(department);
  
          int databaseSizeBeforeDelete = departmentRepository.findAll().size();
  
          // Delete the department
          restDepartmentMockMvc
              .perform(delete(ENTITY_API_URL_ID, department.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Department> departmentList = departmentRepository.findAll();
          assertThat(departmentList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  