content: package com.adyen.demo.store.web.rest;
  
  import static com.adyen.demo.store.web.rest.TestUtil.sameNumber;
  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.Product;
  import com.adyen.demo.store.domain.ProductCategory;
  import com.adyen.demo.store.domain.enumeration.Size;
  import com.adyen.demo.store.repository.ProductRepository;
  import java.math.BigDecimal;
  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;
  import org.springframework.util.Base64Utils;
  /**
   * Integration tests for the {@link ProductResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser(username="admin", authorities={"ROLE_ADMIN"}, password = "admin")
  public class ProductResourceIT {
  
      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 BigDecimal DEFAULT_PRICE = new BigDecimal(0);
      private static final BigDecimal UPDATED_PRICE = new BigDecimal(1);
  
      private static final Size DEFAULT_ITEM_SIZE = Size.S;
      private static final Size UPDATED_ITEM_SIZE = Size.M;
  
      private static final byte[] DEFAULT_IMAGE = TestUtil.createByteArray(1, "0");
      private static final byte[] UPDATED_IMAGE = TestUtil.createByteArray(1, "1");
      private static final String DEFAULT_IMAGE_CONTENT_TYPE = "image/jpg";
      private static final String UPDATED_IMAGE_CONTENT_TYPE = "image/png";
  
      private static final String ENTITY_API_URL = "/api/products";
      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 ProductRepository productRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restProductMockMvc;
  
      private Product product;
  
      /**
       * 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 Product createEntity(EntityManager em) {
          Product product = new Product()
              .name(DEFAULT_NAME)
              .description(DEFAULT_DESCRIPTION)
              .price(DEFAULT_PRICE)
              .itemSize(DEFAULT_ITEM_SIZE)
              .image(DEFAULT_IMAGE)
              .imageContentType(DEFAULT_IMAGE_CONTENT_TYPE);
          // Add required entity
          ProductCategory productCategory;
          if (TestUtil.findAll(em, ProductCategory.class).isEmpty()) {
              productCategory = ProductCategoryResourceIT.createEntity(em);
              em.persist(productCategory);
              em.flush();
          } else {
              productCategory = TestUtil.findAll(em, ProductCategory.class).get(0);
          }
          product.setProductCategory(productCategory);
          return product;
      }
  
      /**
       * 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 Product createUpdatedEntity(EntityManager em) {
          Product product = new Product()
              .name(UPDATED_NAME)
              .description(UPDATED_DESCRIPTION)
              .price(UPDATED_PRICE)
              .itemSize(UPDATED_ITEM_SIZE)
              .image(UPDATED_IMAGE)
              .imageContentType(UPDATED_IMAGE_CONTENT_TYPE);
          // Add required entity
          ProductCategory productCategory;
          if (TestUtil.findAll(em, ProductCategory.class).isEmpty()) {
              productCategory = ProductCategoryResourceIT.createUpdatedEntity(em);
              em.persist(productCategory);
              em.flush();
          } else {
              productCategory = TestUtil.findAll(em, ProductCategory.class).get(0);
          }
          product.setProductCategory(productCategory);
          return product;
      }
  
      @BeforeEach
      public void initTest() {
          product = createEntity(em);
      }
  
      @Test
      @Transactional
      void createProduct() throws Exception {
          int databaseSizeBeforeCreate = productRepository.findAll().size();
          // Create the Product
          restProductMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isCreated());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeCreate + 1);
          Product testProduct = productList.get(productList.size() - 1);
          assertThat(testProduct.getName()).isEqualTo(DEFAULT_NAME);
          assertThat(testProduct.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
          assertThat(testProduct.getPrice()).isEqualByComparingTo(DEFAULT_PRICE);
          assertThat(testProduct.getItemSize()).isEqualTo(DEFAULT_ITEM_SIZE);
          assertThat(testProduct.getImage()).isEqualTo(DEFAULT_IMAGE);
          assertThat(testProduct.getImageContentType()).isEqualTo(DEFAULT_IMAGE_CONTENT_TYPE);
      }
  
      @Test
      @Transactional
      void createProductWithExistingId() throws Exception {
          // Create the Product with an existing ID
          product.setId(1L);
  
          int databaseSizeBeforeCreate = productRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restProductMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isBadRequest());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void checkNameIsRequired() throws Exception {
          int databaseSizeBeforeTest = productRepository.findAll().size();
          // set the field null
          product.setName(null);
  
          // Create the Product, which fails.
  
          restProductMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isBadRequest());
  
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void checkPriceIsRequired() throws Exception {
          int databaseSizeBeforeTest = productRepository.findAll().size();
          // set the field null
          product.setPrice(null);
  
          // Create the Product, which fails.
  
          restProductMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isBadRequest());
  
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void checkItemSizeIsRequired() throws Exception {
          int databaseSizeBeforeTest = productRepository.findAll().size();
          // set the field null
          product.setItemSize(null);
  
          // Create the Product, which fails.
  
          restProductMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isBadRequest());
  
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void getAllProducts() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          // Get all the productList
          restProductMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(product.getId().intValue())))
              .andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME)))
              .andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION)))
              .andExpect(jsonPath("$.[*].price").value(hasItem(sameNumber(DEFAULT_PRICE))))
              .andExpect(jsonPath("$.[*].itemSize").value(hasItem(DEFAULT_ITEM_SIZE.toString())))
              .andExpect(jsonPath("$.[*].imageContentType").value(hasItem(DEFAULT_IMAGE_CONTENT_TYPE)))
              .andExpect(jsonPath("$.[*].image").value(hasItem(Base64Utils.encodeToString(DEFAULT_IMAGE))));
      }
  
      @Test
      @Transactional
      void getProduct() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          // Get the product
          restProductMockMvc
              .perform(get(ENTITY_API_URL_ID, product.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(product.getId().intValue()))
              .andExpect(jsonPath("$.name").value(DEFAULT_NAME))
              .andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION))
              .andExpect(jsonPath("$.price").value(sameNumber(DEFAULT_PRICE)))
              .andExpect(jsonPath("$.itemSize").value(DEFAULT_ITEM_SIZE.toString()))
              .andExpect(jsonPath("$.imageContentType").value(DEFAULT_IMAGE_CONTENT_TYPE))
              .andExpect(jsonPath("$.image").value(Base64Utils.encodeToString(DEFAULT_IMAGE)));
      }
  
      @Test
      @Transactional
      void getNonExistingProduct() throws Exception {
          // Get the product
          restProductMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewProduct() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
  
          // Update the product
          Product updatedProduct = productRepository.findById(product.getId()).get();
          // Disconnect from session so that the updates on updatedProduct are not directly saved in db
          em.detach(updatedProduct);
          updatedProduct
              .name(UPDATED_NAME)
              .description(UPDATED_DESCRIPTION)
              .price(UPDATED_PRICE)
              .itemSize(UPDATED_ITEM_SIZE)
              .image(UPDATED_IMAGE)
              .imageContentType(UPDATED_IMAGE_CONTENT_TYPE);
  
          restProductMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedProduct.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedProduct))
              )
              .andExpect(status().isOk());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
          Product testProduct = productList.get(productList.size() - 1);
          assertThat(testProduct.getName()).isEqualTo(UPDATED_NAME);
          assertThat(testProduct.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
          assertThat(testProduct.getPrice()).isEqualTo(UPDATED_PRICE);
          assertThat(testProduct.getItemSize()).isEqualTo(UPDATED_ITEM_SIZE);
          assertThat(testProduct.getImage()).isEqualTo(UPDATED_IMAGE);
          assertThat(testProduct.getImageContentType()).isEqualTo(UPDATED_IMAGE_CONTENT_TYPE);
      }
  
      @Test
      @Transactional
      void putNonExistingProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, product.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(product))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(product))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateProductWithPatch() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
  
          // Update the product using partial update
          Product partialUpdatedProduct = new Product();
          partialUpdatedProduct.setId(product.getId());
  
          partialUpdatedProduct.price(UPDATED_PRICE).image(UPDATED_IMAGE).imageContentType(UPDATED_IMAGE_CONTENT_TYPE);
  
          restProductMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProduct.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProduct))
              )
              .andExpect(status().isOk());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
          Product testProduct = productList.get(productList.size() - 1);
          assertThat(testProduct.getName()).isEqualTo(DEFAULT_NAME);
          assertThat(testProduct.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
          assertThat(testProduct.getPrice()).isEqualByComparingTo(UPDATED_PRICE);
          assertThat(testProduct.getItemSize()).isEqualTo(DEFAULT_ITEM_SIZE);
          assertThat(testProduct.getImage()).isEqualTo(UPDATED_IMAGE);
          assertThat(testProduct.getImageContentType()).isEqualTo(UPDATED_IMAGE_CONTENT_TYPE);
      }
  
      @Test
      @Transactional
      void fullUpdateProductWithPatch() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
  
          // Update the product using partial update
          Product partialUpdatedProduct = new Product();
          partialUpdatedProduct.setId(product.getId());
  
          partialUpdatedProduct
              .name(UPDATED_NAME)
              .description(UPDATED_DESCRIPTION)
              .price(UPDATED_PRICE)
              .itemSize(UPDATED_ITEM_SIZE)
              .image(UPDATED_IMAGE)
              .imageContentType(UPDATED_IMAGE_CONTENT_TYPE);
  
          restProductMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProduct.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProduct))
              )
              .andExpect(status().isOk());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
          Product testProduct = productList.get(productList.size() - 1);
          assertThat(testProduct.getName()).isEqualTo(UPDATED_NAME);
          assertThat(testProduct.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
          assertThat(testProduct.getPrice()).isEqualByComparingTo(UPDATED_PRICE);
          assertThat(testProduct.getItemSize()).isEqualTo(UPDATED_ITEM_SIZE);
          assertThat(testProduct.getImage()).isEqualTo(UPDATED_IMAGE);
          assertThat(testProduct.getImageContentType()).isEqualTo(UPDATED_IMAGE_CONTENT_TYPE);
      }
  
      @Test
      @Transactional
      void patchNonExistingProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, product.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(product))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(product))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamProduct() throws Exception {
          int databaseSizeBeforeUpdate = productRepository.findAll().size();
          product.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductMockMvc
              .perform(patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(product)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the Product in the database
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteProduct() throws Exception {
          // Initialize the database
          productRepository.saveAndFlush(product);
  
          int databaseSizeBeforeDelete = productRepository.findAll().size();
  
          // Delete the product
          restProductMockMvc
              .perform(delete(ENTITY_API_URL_ID, product.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<Product> productList = productRepository.findAll();
          assertThat(productList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  