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.ProductOrder;
  import com.adyen.demo.store.domain.ShoppingCart;
  import com.adyen.demo.store.repository.ProductOrderRepository;
  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;
  
  /**
   * Integration tests for the {@link ProductOrderResource} REST controller.
   */
  @IntegrationTest
  @AutoConfigureMockMvc
  @WithMockUser(username="admin", authorities={"ROLE_ADMIN"}, password = "admin")
  public class ProductOrderResourceIT {
  
      private static final Integer DEFAULT_QUANTITY = 0;
      private static final Integer UPDATED_QUANTITY = 1;
  
      private static final BigDecimal DEFAULT_TOTAL_PRICE = new BigDecimal(0);
      private static final BigDecimal UPDATED_TOTAL_PRICE = new BigDecimal(1);
  
      private static final String ENTITY_API_URL = "/api/product-orders";
      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 ProductOrderRepository productOrderRepository;
  
      @Autowired
      private EntityManager em;
  
      @Autowired
      private MockMvc restProductOrderMockMvc;
  
      private ProductOrder productOrder;
  
      /**
       * 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 ProductOrder createEntity(EntityManager em) {
          ProductOrder productOrder = new ProductOrder().quantity(DEFAULT_QUANTITY).totalPrice(DEFAULT_TOTAL_PRICE);
          // Add required entity
          Product product;
          if (TestUtil.findAll(em, Product.class).isEmpty()) {
              product = ProductResourceIT.createEntity(em);
              em.persist(product);
              em.flush();
          } else {
              product = TestUtil.findAll(em, Product.class).get(0);
          }
          productOrder.setProduct(product);
          // Add required entity
          ShoppingCart shoppingCart;
          if (TestUtil.findAll(em, ShoppingCart.class).isEmpty()) {
              shoppingCart = ShoppingCartResourceIT.createEntity(em);
              em.persist(shoppingCart);
              em.flush();
          } else {
              shoppingCart = TestUtil.findAll(em, ShoppingCart.class).get(0);
          }
          productOrder.setCart(shoppingCart);
          return productOrder;
      }
  
      /**
       * 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 ProductOrder createUpdatedEntity(EntityManager em) {
          ProductOrder productOrder = new ProductOrder().quantity(UPDATED_QUANTITY).totalPrice(UPDATED_TOTAL_PRICE);
          // Add required entity
          Product product;
          if (TestUtil.findAll(em, Product.class).isEmpty()) {
              product = ProductResourceIT.createUpdatedEntity(em);
              em.persist(product);
              em.flush();
          } else {
              product = TestUtil.findAll(em, Product.class).get(0);
          }
          productOrder.setProduct(product);
          // Add required entity
          ShoppingCart shoppingCart;
          if (TestUtil.findAll(em, ShoppingCart.class).isEmpty()) {
              shoppingCart = ShoppingCartResourceIT.createUpdatedEntity(em);
              em.persist(shoppingCart);
              em.flush();
          } else {
              shoppingCart = TestUtil.findAll(em, ShoppingCart.class).get(0);
          }
          productOrder.setCart(shoppingCart);
          return productOrder;
      }
  
      @BeforeEach
      public void initTest() {
          productOrder = createEntity(em);
      }
  
      @Test
      @Transactional
      void createProductOrder() throws Exception {
          int databaseSizeBeforeCreate = productOrderRepository.findAll().size();
          // Create the ProductOrder
          restProductOrderMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productOrder)))
              .andExpect(status().isCreated());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeCreate + 1);
          ProductOrder testProductOrder = productOrderList.get(productOrderList.size() - 1);
          assertThat(testProductOrder.getQuantity()).isEqualTo(DEFAULT_QUANTITY);
          assertThat(testProductOrder.getTotalPrice()).isEqualByComparingTo(DEFAULT_TOTAL_PRICE);
      }
  
      @Test
      @Transactional
      void createProductOrderWithExistingId() throws Exception {
          // Create the ProductOrder with an existing ID
          productOrder.setId(1L);
  
          int databaseSizeBeforeCreate = productOrderRepository.findAll().size();
  
          // An entity with an existing ID cannot be created, so this API call must fail
          restProductOrderMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productOrder)))
              .andExpect(status().isBadRequest());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeCreate);
      }
  
      @Test
      @Transactional
      void checkQuantityIsRequired() throws Exception {
          int databaseSizeBeforeTest = productOrderRepository.findAll().size();
          // set the field null
          productOrder.setQuantity(null);
  
          // Create the ProductOrder, which fails.
  
          restProductOrderMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productOrder)))
              .andExpect(status().isBadRequest());
  
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void checkTotalPriceIsRequired() throws Exception {
          int databaseSizeBeforeTest = productOrderRepository.findAll().size();
          // set the field null
          productOrder.setTotalPrice(null);
  
          // Create the ProductOrder, which fails.
  
          restProductOrderMockMvc
              .perform(post(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productOrder)))
              .andExpect(status().isBadRequest());
  
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeTest);
      }
  
      @Test
      @Transactional
      void getAllProductOrders() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          // Get all the productOrderList
          restProductOrderMockMvc
              .perform(get(ENTITY_API_URL + "?sort=id,desc"))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.[*].id").value(hasItem(productOrder.getId().intValue())))
              .andExpect(jsonPath("$.[*].quantity").value(hasItem(DEFAULT_QUANTITY)))
              .andExpect(jsonPath("$.[*].totalPrice").value(hasItem(sameNumber(DEFAULT_TOTAL_PRICE))));
      }
  
      @Test
      @Transactional
      void getProductOrder() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          // Get the productOrder
          restProductOrderMockMvc
              .perform(get(ENTITY_API_URL_ID, productOrder.getId()))
              .andExpect(status().isOk())
              .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
              .andExpect(jsonPath("$.id").value(productOrder.getId().intValue()))
              .andExpect(jsonPath("$.quantity").value(DEFAULT_QUANTITY))
              .andExpect(jsonPath("$.totalPrice").value(sameNumber(DEFAULT_TOTAL_PRICE)));
      }
  
      @Test
      @Transactional
      void getNonExistingProductOrder() throws Exception {
          // Get the productOrder
          restProductOrderMockMvc.perform(get(ENTITY_API_URL_ID, Long.MAX_VALUE)).andExpect(status().isNotFound());
      }
  
      @Test
      @Transactional
      void putNewProductOrder() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
  
          // Update the productOrder
          ProductOrder updatedProductOrder = productOrderRepository.findById(productOrder.getId()).get();
          // Disconnect from session so that the updates on updatedProductOrder are not directly saved in db
          em.detach(updatedProductOrder);
          updatedProductOrder.quantity(UPDATED_QUANTITY).totalPrice(UPDATED_TOTAL_PRICE);
  
          restProductOrderMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, updatedProductOrder.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(updatedProductOrder))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
          ProductOrder testProductOrder = productOrderList.get(productOrderList.size() - 1);
          assertThat(testProductOrder.getQuantity()).isEqualTo(UPDATED_QUANTITY);
          assertThat(testProductOrder.getTotalPrice()).isEqualTo(UPDATED_TOTAL_PRICE);
      }
  
      @Test
      @Transactional
      void putNonExistingProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, productOrder.getId())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(productOrder))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithIdMismatchProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(
                  put(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType(MediaType.APPLICATION_JSON)
                      .content(TestUtil.convertObjectToJsonBytes(productOrder))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void putWithMissingIdPathParamProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(put(ENTITY_API_URL).contentType(MediaType.APPLICATION_JSON).content(TestUtil.convertObjectToJsonBytes(productOrder)))
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void partialUpdateProductOrderWithPatch() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
  
          // Update the productOrder using partial update
          ProductOrder partialUpdatedProductOrder = new ProductOrder();
          partialUpdatedProductOrder.setId(productOrder.getId());
  
          restProductOrderMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProductOrder.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProductOrder))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
          ProductOrder testProductOrder = productOrderList.get(productOrderList.size() - 1);
          assertThat(testProductOrder.getQuantity()).isEqualTo(DEFAULT_QUANTITY);
          assertThat(testProductOrder.getTotalPrice()).isEqualByComparingTo(DEFAULT_TOTAL_PRICE);
      }
  
      @Test
      @Transactional
      void fullUpdateProductOrderWithPatch() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
  
          // Update the productOrder using partial update
          ProductOrder partialUpdatedProductOrder = new ProductOrder();
          partialUpdatedProductOrder.setId(productOrder.getId());
  
          partialUpdatedProductOrder.quantity(UPDATED_QUANTITY).totalPrice(UPDATED_TOTAL_PRICE);
  
          restProductOrderMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, partialUpdatedProductOrder.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(partialUpdatedProductOrder))
              )
              .andExpect(status().isOk());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
          ProductOrder testProductOrder = productOrderList.get(productOrderList.size() - 1);
          assertThat(testProductOrder.getQuantity()).isEqualTo(UPDATED_QUANTITY);
          assertThat(testProductOrder.getTotalPrice()).isEqualByComparingTo(UPDATED_TOTAL_PRICE);
      }
  
      @Test
      @Transactional
      void patchNonExistingProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If the entity doesn't have an ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, productOrder.getId())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(productOrder))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithIdMismatchProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(
                  patch(ENTITY_API_URL_ID, count.incrementAndGet())
                      .contentType("application/merge-patch+json")
                      .content(TestUtil.convertObjectToJsonBytes(productOrder))
              )
              .andExpect(status().isBadRequest());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void patchWithMissingIdPathParamProductOrder() throws Exception {
          int databaseSizeBeforeUpdate = productOrderRepository.findAll().size();
          productOrder.setId(count.incrementAndGet());
  
          // If url ID doesn't match entity ID, it will throw BadRequestAlertException
          restProductOrderMockMvc
              .perform(
                  patch(ENTITY_API_URL).contentType("application/merge-patch+json").content(TestUtil.convertObjectToJsonBytes(productOrder))
              )
              .andExpect(status().isMethodNotAllowed());
  
          // Validate the ProductOrder in the database
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeUpdate);
      }
  
      @Test
      @Transactional
      void deleteProductOrder() throws Exception {
          // Initialize the database
          productOrderRepository.saveAndFlush(productOrder);
  
          int databaseSizeBeforeDelete = productOrderRepository.findAll().size();
  
          // Delete the productOrder
          restProductOrderMockMvc
              .perform(delete(ENTITY_API_URL_ID, productOrder.getId()).accept(MediaType.APPLICATION_JSON))
              .andExpect(status().isNoContent());
  
          // Validate the database contains one less item
          List<ProductOrder> productOrderList = productOrderRepository.findAll();
          assertThat(productOrderList).hasSize(databaseSizeBeforeDelete - 1);
      }
  }
  