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.Location;
  import com.example.demo.repository.LocationRepository;
  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 LocationResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser
  class LocationResourceIT {
  
      private static final String DEFAULT_STREET_ADDRESS = "AAAAAAAAAA";
      private static final String UPDATED_STREET_ADDRESS = "BBBBBBBBBB";
  
      private static final String DEFAULT_POSTAL_CODE = "AAAAAAAAAA";
      private static final String UPDATED_POSTAL_CODE = "BBBBBBBBBB";
  
      private static final String DEFAULT_CITY = "AAAAAAAAAA";
      private static final String UPDATED_CITY = "BBBBBBBBBB";
  
      private static final String DEFAULT_STATE_PROVINCE = "AAAAAAAAAA";
      private static final String UPDATED_STATE_PROVINCE = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/locations";
      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 LocationRepository locationRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restLocationMockMvc;
  
      private Location location;
  
      /**
       * 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 Location createEntity(EntityManager em) {
          Location location = new Location()
              .streetAddress(DEFAULT_STREET_ADDRESS)
              .postalCode(DEFAULT_POSTAL_CODE)
              .city(DEFAULT_CITY)
              .stateProvince(DEFAULT_STATE_PROVINCE);
          return location;
      }
  
      /**
       * 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 Location createUpdatedEntity(EntityManager em) {
          Location location = new Location()
              .streetAddress(UPDATED_STREET_ADDRESS)
              .postalCode(UPDATED_POSTAL_CODE)
              .city(UPDATED_CITY)
              .stateProvince(UPDATED_STATE_PROVINCE);
          return location;
      }
  
      @BeforeEach
      public void initTest() {
          location = createEntity(em);
      }
  
      @Test
      @Transactional
      void createLocation() throws Exception {
          int databaseSizeBeforeCreate = locationRepository.findAll().size();
          // Create the Location
          restLocationMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(location)))
              .andExpect(status().isCreated());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeCreate + 1);
          Location testLocation = locationList.get(locationList.size() - 1);
          assertThat(testLocation.getStreetAddress()).isEqualTo(DEFAULT_STREET_ADDRESS);
          assertThat(testLocation.getPostalCode()).isEqualTo(DEFAULT_POSTAL_CODE);
          assertThat(testLocation.getCity()).isEqualTo(DEFAULT_CITY);
          assertThat(testLocation.getStateProvince()).isEqualTo(DEFAULT_STATE_PROVINCE);
      }
  
      @Test
      @Transactional
      void createLocationWithExistingId() throws Exception {
          // Create the Location with an existing ID
          location.setId(1L);
  
          int databaseSizeBeforeCreate = locationRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restLocationMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(location)))
              .andExpect(status().isBadRequest());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void getAllLocations() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          // Get all the locationList
          restLocationMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(location.getId().intValue())))
              .andExpect(jsonPath("$.[*].streetAddress").value(hasItem(DEFAULT_STREET_ADDRESS)))
              .andExpect(jsonPath("$.[*].postalCode").value(hasItem(DEFAULT_POSTAL_CODE)))
              .andExpect(jsonPath("$.[*].city").value(hasItem(DEFAULT_CITY)))
              .andExpect(jsonPath("$.[*].stateProvince").value(hasItem(DEFAULT_STATE_PROVINCE)));
      }
  
      @Test
      @Transactional
      void getLocation() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          // Get the location
          restLocationMockMvc
              .perform(get(ENTITY_API_URL_ID, location.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(location.getId().intValue()))
              .andExpect(jsonPath("$.streetAddress").value(DEFAULT_STREET_ADDRESS))
              .andExpect(jsonPath("$.postalCode").value(DEFAULT_POSTAL_CODE))
              .andExpect(jsonPath("$.city").value(DEFAULT_CITY))
              .andExpect(jsonPath("$.stateProvince").value(DEFAULT_STATE_PROVINCE));
      }
  
      @Test
      @Transactional
      void getNonExistingLocation() throws Exception {
          // Get the location
          restLocationMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewLocation() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
  
          // Update the location
          Location updatedLocation = locationRepository.findById(location.getId()).get();
          // Disconnect from session so that the updates on updatedLocation are not directly saved in db
          em.detach(updatedLocation);
          updatedLocation
              .streetAddress(UPDATED_STREET_ADDRESS)
              .postalCode(UPDATED_POSTAL_CODE)
              .city(UPDATED_CITY)
              .stateProvince(UPDATED_STATE_PROVINCE);
  
          restLocationMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedLocation.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedLocation))
              )
              .andExpect(status().isOk());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
          Location testLocation = locationList.get(locationList.size() - 1);
          assertThat(testLocation.getStreetAddress()).isEqualTo(UPDATED_STREET_ADDRESS);
          assertThat(testLocation.getPostalCode()).isEqualTo(UPDATED_POSTAL_CODE);
          assertThat(testLocation.getCity()).isEqualTo(UPDATED_CITY);
          assertThat(testLocation.getStateProvince()).isEqualTo(UPDATED_STATE_PROVINCE);
      }
  
      @Test
      @Transactional
      void putNonExistingLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, location.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(location))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(location))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(location)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateLocationWithPatch() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
  
          // Update the location using partial update
          Location partialUpdatedLocation = new Location();
          partialUpdatedLocation.setId(location.getId());
  
          partialUpdatedLocation.streetAddress(UPDATED_STREET_ADDRESS).city(UPDATED_CITY).stateProvince(UPDATED_STATE_PROVINCE);
  
          restLocationMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedLocation.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedLocation))
              )
              .andExpect(status().isOk());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
          Location testLocation = locationList.get(locationList.size() - 1);
          assertThat(testLocation.getStreetAddress()).isEqualTo(UPDATED_STREET_ADDRESS);
          assertThat(testLocation.getPostalCode()).isEqualTo(DEFAULT_POSTAL_CODE);
          assertThat(testLocation.getCity()).isEqualTo(UPDATED_CITY);
          assertThat(testLocation.getStateProvince()).isEqualTo(UPDATED_STATE_PROVINCE);
      }
  
      @Test
      @Transactional
      void fullUpdateLocationWithPatch() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
  
          // Update the location using partial update
          Location partialUpdatedLocation = new Location();
          partialUpdatedLocation.setId(location.getId());
  
          partialUpdatedLocation
              .streetAddress(UPDATED_STREET_ADDRESS)
              .postalCode(UPDATED_POSTAL_CODE)
              .city(UPDATED_CITY)
              .stateProvince(UPDATED_STATE_PROVINCE);
  
          restLocationMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedLocation.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedLocation))
              )
              .andExpect(status().isOk());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
          Location testLocation = locationList.get(locationList.size() - 1);
          assertThat(testLocation.getStreetAddress()).isEqualTo(UPDATED_STREET_ADDRESS);
          assertThat(testLocation.getPostalCode()).isEqualTo(UPDATED_POSTAL_CODE);
          assertThat(testLocation.getCity()).isEqualTo(UPDATED_CITY);
          assertThat(testLocation.getStateProvince()).isEqualTo(UPDATED_STATE_PROVINCE);
      }
  
      @Test
      @Transactional
      void patchNonExistingLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, location.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(location))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(location))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamLocation() throws Exception {
          int databaseSizeBeforeUpdate = locationRepository.findAll().size();
          location.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restLocationMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(location)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Location in the database
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteLocation() throws Exception {
          // Initialize the database
          locationRepository.saveAndFlush(location);
  
          int databaseSizeBeforeDelete = locationRepository.findAll().size();
  
          // Delete the location
          restLocationMockMvc
              .perform(delete(ENTITY_API_URL_ID, location.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Location> locationList = locationRepository.findAll();
          assertThat(locationList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  