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.Region;
  import com.example.demo.repository.RegionRepository;
  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 RegionResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class RegionResourceIT {
  
      private static final String DEFAULT_REGION_NAME = "AAAAAAAAAA";
      private static final String UPDATED_REGION_NAME = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/regions";
      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 RegionRepository regionRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restRegionMockMvc;
  
      private Region region;
  
      /**
       * 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 Region createEntity(EntityManager em) {
          Region region = new Region().regionName(DEFAULT_REGION_NAME);
          return region;
      }
  
      /**
       * 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 Region createUpdatedEntity(EntityManager em) {
          Region region = new Region().regionName(UPDATED_REGION_NAME);
          return region;
      }
  
      @BeforeEach
      public void initTest() {
          region = createEntity(em);
      }
  
      @Test
      @Transactional
      void createRegion() throws Exception {
          int databaseSizeBeforeCreate = regionRepository.findAll().size();
          // Create the Region
          restRegionMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(region)))
              .andExpect(status().isCreated());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeCreate + 1);
          Region testRegion = regionList.get(regionList.size() - 1);
          assertThat(testRegion.getRegionName()).isEqualTo(DEFAULT_REGION_NAME);
      }
  
      @Test
      @Transactional
      void createRegionWithExistingId() throws Exception {
          // Create the Region with an existing ID
          region.setId(1L);
  
          int databaseSizeBeforeCreate = regionRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restRegionMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(region)))
              .andExpect(status().isBadRequest());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllRegions() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          // Get all the regionList
          restRegionMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(region.getId().intValue())))
              .andExpect(jsonPath("$.[*].regionName").value(hasItem(DEFAULT_REGION_NAME)));
      }
  
      @Test
      @Transactional
      void getRegion() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          // Get the region
          restRegionMockMvc
              .perform(get(ENTITY_API_URL_ID, region.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(region.getId().intValue()))
              .andExpect(jsonPath("$.regionName").value(DEFAULT_REGION_NAME));
      }
  
      @Test
      @Transactional
      void getNonExistingRegion() throws Exception {
          // Get the region
          restRegionMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewRegion() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
  
          // Update the region
          Region updatedRegion = regionRepository.findById(region.getId()).get();
          // Disconnect from session so that the updates on updatedRegion are not directly saved in db
          em.detach(updatedRegion);
          updatedRegion.regionName(UPDATED_REGION_NAME);
  
          restRegionMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedRegion.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedRegion))
              )
              .andExpect(status().isOk());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
          Region testRegion = regionList.get(regionList.size() - 1);
          assertThat(testRegion.getRegionName()).isEqualTo(UPDATED_REGION_NAME);
      }
  
      @Test
      @Transactional
      void putNonExistingRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, region.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(region))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(region))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(region)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateRegionWithPatch() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
  
          // Update the region using partial update
          Region partialUpdatedRegion = new Region();
          partialUpdatedRegion.setId(region.getId());
  
          partialUpdatedRegion.regionName(UPDATED_REGION_NAME);
  
          restRegionMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedRegion.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRegion))
              )
              .andExpect(status().isOk());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
          Region testRegion = regionList.get(regionList.size() - 1);
          assertThat(testRegion.getRegionName()).isEqualTo(UPDATED_REGION_NAME);
      }
  
      @Test
      @Transactional
      void fullUpdateRegionWithPatch() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
  
          // Update the region using partial update
          Region partialUpdatedRegion = new Region();
          partialUpdatedRegion.setId(region.getId());
  
          partialUpdatedRegion.regionName(UPDATED_REGION_NAME);
  
          restRegionMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedRegion.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedRegion))
              )
              .andExpect(status().isOk());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
          Region testRegion = regionList.get(regionList.size() - 1);
          assertThat(testRegion.getRegionName()).isEqualTo(UPDATED_REGION_NAME);
      }
  
      @Test
      @Transactional
      void patchNonExistingRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, region.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(region))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(region))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamRegion() throws Exception {
          int databaseSizeBeforeUpdate = regionRepository.findAll().size();
          region.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restRegionMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(region)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Region in the database
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteRegion() throws Exception {
          // Initialize the database
          regionRepository.saveAndFlush(region);
  
          int databaseSizeBeforeDelete = regionRepository.findAll().size();
  
          // Delete the region
          restRegionMockMvc
              .perform(delete(ENTITY_API_URL_ID, region.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Region> regionList = regionRepository.findAll();
          assertThat(regionList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  