content: package com.adyen.demo.store.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.adyen.demo.store.IntegrationTest;
  import com.adyen.demo.store.domain.ProductCategory;
  import com.adyen.demo.store.repository.ProductCategoryRepository;
  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 ProductCategoryResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser(username="admin", authorities={"ROLE_ADMIN"}, password = "admin")
  public class ProductCategoryResourceIT {
  
      private static final String DEFAULT_NAME = "AAAAAAAAAA";
      private static final String UPDATED_NAME = "BBBBBBBBBB";
  
      private static final String DEFAULT_DESCRIPTION = "AAAAAAAAAA";
      private static final String UPDATED_DESCRIPTION = "BBBBBBBBBB";
  
      private static final String ENTITY_API_URL = "/api/product-categories";
      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 ProductCategoryRepository productCategoryRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restProductCategoryMockMvc;
  
      private ProductCategory productCategory;
  
      /**
       * 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 ProductCategory createEntity(EntityManager em) {
          ProductCategory productCategory = new ProductCategory().name(DEFAULT_NAME).description(DEFAULT_DESCRIPTION);
          return productCategory;
      }
  
      /**
       * 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 ProductCategory createUpdatedEntity(EntityManager em) {
          ProductCategory productCategory = new ProductCategory().name(UPDATED_NAME).description(UPDATED_DESCRIPTION);
          return productCategory;
      }
  
      @BeforeEach
      public void initTest() {
          productCategory = createEntity(em);
      }
  
      @Test
      @Transactional
      void createProductCategory() throws Exception {
          int databaseSizeBeforeCreate = productCategoryRepository.findAll().size();
          // Create the ProductCategory
          restProductCategoryMockMvc
              .perform(
                  post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isCreated());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeCreate + 1);
          ProductCategory testProductCategory = productCategoryList.get(productCategoryList.size() - 1);
          assertThat(testProductCategory.getName()).isEqualTo(DEFAULT_NAME);
          assertThat(testProductCategory.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void createProductCategoryWithExistingId() throws Exception {
          // Create the ProductCategory with an existing ID
          productCategory.setId(1L);
  
          int databaseSizeBeforeCreate = productCategoryRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restProductCategoryMockMvc
              .perform(
                  post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void checkNameIsRequired() throws Exception {
          int databaseSizeBeforeTest = productCategoryRepository.findAll().size();
          // set the field null
          productCategory.setName(null);
  
          // Create the ProductCategory, which fails.
  
          restProductCategoryMockMvc
              .perform(
                  post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void getAllProductCategories() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          // Get all the productCategoryList
          restProductCategoryMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(productCategory.getId().intValue())))
              .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
              .andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION)));
      }
  
      @Test
      @Transactional
      void getProductCategory() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          // Get the productCategory
          restProductCategoryMockMvc
              .perform(get(ENTITY_API_URL_ID, productCategory.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(productCategory.getId().intValue()))
              .andExpect(jsonPath("$.name").value(DEFAULT_NAME))
              .andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION));
      }
  
      @Test
      @Transactional
      void getNonExistingProductCategory() throws Exception {
          // Get the productCategory
          restProductCategoryMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewProductCategory() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
  
          // Update the productCategory
          ProductCategory updatedProductCategory = productCategoryRepository.findById(productCategory.getId()).get();
          // Disconnect from session so that the updates on updatedProductCategory are not directly saved in db
          em.detach(updatedProductCategory);
          updatedProductCategory.name(UPDATED_NAME).description(UPDATED_DESCRIPTION);
  
          restProductCategoryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedProductCategory.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedProductCategory))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
          ProductCategory testProductCategory = productCategoryList.get(productCategoryList.size() - 1);
          assertThat(testProductCategory.getName()).isEqualTo(UPDATED_NAME);
          assertThat(testProductCategory.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void putNonExistingProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, productCategory.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateProductCategoryWithPatch() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
  
          // Update the productCategory using partial update
          ProductCategory partialUpdatedProductCategory = new ProductCategory();
          partialUpdatedProductCategory.setId(productCategory.getId());
  
          restProductCategoryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProductCategory.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProductCategory))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
          ProductCategory testProductCategory = productCategoryList.get(productCategoryList.size() - 1);
          assertThat(testProductCategory.getName()).isEqualTo(DEFAULT_NAME);
          assertThat(testProductCategory.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void fullUpdateProductCategoryWithPatch() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
  
          // Update the productCategory using partial update
          ProductCategory partialUpdatedProductCategory = new ProductCategory();
          partialUpdatedProductCategory.setId(productCategory.getId());
  
          partialUpdatedProductCategory.name(UPDATED_NAME).description(UPDATED_DESCRIPTION);
  
          restProductCategoryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProductCategory.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProductCategory))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
          ProductCategory testProductCategory = productCategoryList.get(productCategoryList.size() - 1);
          assertThat(testProductCategory.getName()).isEqualTo(UPDATED_NAME);
          assertThat(testProductCategory.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
      }
  
      @Test
      @Transactional
      void patchNonExistingProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, productCategory.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamProductCategory() throws Exception {
          int databaseSizeBeforeUpdate = productCategoryRepository.findAll().size();
          productCategory.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductCategoryMockMvc
              .perform(
                  patch(ENTITY_API_URL)
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(productCategory))
              )
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the ProductCategory in the database
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteProductCategory() throws Exception {
          // Initialize the database
          productCategoryRepository.saveAndFlush(productCategory);
  
          int databaseSizeBeforeDelete = productCategoryRepository.findAll().size();
  
          // Delete the productCategory
          restProductCategoryMockMvc
              .perform(delete(ENTITY_API_URL_ID, productCategory.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<ProductCategory> productCategoryList = productCategoryRepository.findAll();
          assertThat(productCategoryList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  