Search in sources :

Example 1 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2022-feb-spring-data by bankin.

the class _02Main method main.

public static void main(String[] args) {
    EntityManagerFactory factory = Persistence.createEntityManagerFactory("CodeFirstEx");
    EntityManager entityManager = factory.createEntityManager();
    entityManager.getTransaction().begin();
    Product product = new Product("product", 123, BigDecimal.TEN);
    Customer customer = new Customer("customer", "customer", "asd");
    StoreLocation location = new StoreLocation("location");
    Sale sale = new Sale(product, customer, location);
    entityManager.persist(product);
    entityManager.persist(customer);
    entityManager.persist(location);
    entityManager.persist(sale);
    entityManager.getTransaction().commit();
    entityManager.close();
}
Also used : Sale(_02.entities.Sale) EntityManager(javax.persistence.EntityManager) Customer(_02.entities.Customer) EntityManagerFactory(javax.persistence.EntityManagerFactory) StoreLocation(_02.entities.StoreLocation) Product(_02.entities.Product)

Example 2 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project C-DAC-Notes by shreeshailaya.

the class SaveProduct method main.

public static void main(String[] args) {
    StandardServiceRegistry registry = new StandardServiceRegistryBuilder().configure().build();
    Metadata metadata = new MetadataSources(registry).getMetadataBuilder().build();
    SessionFactory sf = metadata.getSessionFactoryBuilder().build();
    System.out.println("session factory created");
    Session session = sf.openSession();
    Category category = new Category("Computer");
    Product pc = new Product("DELL PC", "Quad-core PC", 1200, category);
    Product laptop = new Product("MacBook", "Apple High-end laptop", 2100, category);
    Product phone = new Product("iPhone 5", "Apple Best-selling smartphone", 499, category);
    Product tablet = new Product("iPad 3", "Apple Best-selling tablet", 1099, category);
    Set<Product> products = new HashSet<Product>();
    products.add(pc);
    products.add(laptop);
    products.add(phone);
    products.add(tablet);
    category.setProducts(products);
    Transaction tr = session.beginTransaction();
    session.save(category);
    tr.commit();
    System.out.println(" saved");
    session.close();
    sf.close();
}
Also used : SessionFactory(org.hibernate.SessionFactory) Category(entities.Category) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Transaction(org.hibernate.Transaction) Metadata(org.hibernate.boot.Metadata) MetadataSources(org.hibernate.boot.MetadataSources) Product(entities.Product) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Session(org.hibernate.Session) HashSet(java.util.HashSet)

Example 3 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method getFakeProductPositions.

private List<ProductPosition> getFakeProductPositions() {
    List<Product> fakeProducts = getFakeProducts();
    Warehouse fakeWarehouse = getFakeWarehouse1();
    Date d1 = new Date();
    cal.setTime(d1);
    cal.add(Calendar.DATE, -10);
    d1.setTime(cal.getTime().getTime());
    Date d2 = new Date();
    cal.setTime(d2);
    cal.add(Calendar.DATE, -1);
    d2.setTime(cal.getTime().getTime());
    Date d3 = new Date();
    cal.setTime(d3);
    cal.add(Calendar.DATE, -40);
    d3.setTime(cal.getTime().getTime());
    ProductPosition ps1 = new ProductPosition(1L, fakeProducts.get(0), fakeWarehouse, "AAA", 5, 3, d1, new BigDecimal(10000.0), "ИП ИВАНОВ", true, d1);
    ps1.setId(1L);
    ProductPosition ps2 = new ProductPosition(1L, fakeProducts.get(0), fakeWarehouse, "AAA", 5, 3, d2, new BigDecimal(10000.0), "ИП ИВАНОВ", true, d2);
    ps2.setId(2L);
    ProductPosition ps3 = new ProductPosition(2L, fakeProducts.get(1), fakeWarehouse, "AAA", 5, 3, d1, new BigDecimal(10000.0), "ИП ПЕТРОВ", true, d1);
    ps3.setId(3L);
    ProductPosition ps4 = new ProductPosition(2L, fakeProducts.get(1), fakeWarehouse, "AAA", 10, 8, d2, new BigDecimal(10000.0), "ИП ПЕТРОВ", true, d2);
    ps4.setId(4L);
    ProductPosition ps5 = new ProductPosition(3L, fakeProducts.get(2), fakeWarehouse, "AAA", 20, 20, d3, new BigDecimal(10000.0), "ИП ФЕДОРОВ", true, d3);
    ps5.setId(5L);
    ProductPosition ps6 = new ProductPosition(3L, fakeProducts.get(2), fakeWarehouse, "AAA", 15, 15, d1, new BigDecimal(10000.0), "ИП ФЕДОРОВ", true, d1);
    ps6.setId(6L);
    return new ArrayList<>(Arrays.asList(ps1, ps2, ps3, ps4, ps5, ps6));
}
Also used : OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) BigDecimal(java.math.BigDecimal)

Example 4 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2021-msk-food-delivery by netcracker-edu.

the class OrderServiceTest method countOrderCostSuccessfulTest.

@Test
public void countOrderCostSuccessfulTest() {
    WarehouseInfoDTO fakeWarehouseInfoDTO = getFakeWarehouseInfoDTO();
    List<Product> fakeProducts = getFakeProducts();
    List<ProductPosition> fakePositions = getFakeProductPositions();
    Mockito.when(courierRepo.countWorkingCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(courierRepo.countDeliveringCouriersByWarehouse(Mockito.any(Long.class))).thenReturn((short) 100);
    Mockito.when(warehouseService.getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class))).thenReturn(fakeWarehouseInfoDTO);
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(1L, 1L)).thenReturn(Arrays.asList(fakePositions.get(0), fakePositions.get(1)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(2L, 1L)).thenReturn(Arrays.asList(fakePositions.get(2), fakePositions.get(3)));
    Mockito.when(productPositionRepo.findByProductIdAndWarehouseIdWithLock(3L, 1L)).thenReturn(Arrays.asList(fakePositions.get(4), fakePositions.get(5)));
    Mockito.when(productRepo.findById(1l)).thenReturn(Optional.of(fakeProducts.get(0)));
    Mockito.when(productRepo.findById(2l)).thenReturn(Optional.of(fakeProducts.get(1)));
    Mockito.when(productRepo.findById(3l)).thenReturn(Optional.of(fakeProducts.get(2)));
    HashMap<Long, Integer> fakeHashMap = new HashMap<>();
    fakeHashMap.put(1L, 2);
    fakeHashMap.put(2L, 3);
    fakeHashMap.put(3L, 5);
    Double[] res = orderService.countOrderCost(getFakeCoordsDTO(), fakeHashMap, 1L);
    Assertions.assertEquals(res[0], 760.0);
    Assertions.assertEquals(res[1], 20.0);
    Assertions.assertEquals(res[2], 2.0);
    Mockito.verify(courierRepo, Mockito.times(1)).countWorkingCouriersByWarehouse(1L);
    Mockito.verify(courierRepo, Mockito.times(1)).countDeliveringCouriersByWarehouse(1L);
    Mockito.verify(productPositionRepo, Mockito.times(3)).findByProductIdAndWarehouseIdWithLock(Mockito.any(Long.class), Mockito.eq(1L));
    Mockito.verify(warehouseService, Mockito.times(1)).getNearestWarehouse(Mockito.any(BigDecimal.class), Mockito.any(BigDecimal.class));
}
Also used : OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) BigDecimal(java.math.BigDecimal) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2021-msk-food-delivery by netcracker-edu.

the class ProductServiceTest method searchProductsSuccess.

@Test
public void searchProductsSuccess() {
    Pageable pageable = PageRequest.of(0, 2);
    Page<Product> productPage = ProductUtils.createPageWithMilkProducts(pageable);
    String requestSearchPhrase = "Milk taste";
    String perfectPhrase = "Milk:* & taste:*";
    Long warehouseId = 4L;
    Point geo = makeGeo();
    WarehouseInfoDTO warehouseInfoDTO = new WarehouseInfoDTO(warehouseId, null, null, null, null, false);
    when(warehouseServiceMock.getNearestWarehouse(geo)).thenReturn(warehouseInfoDTO);
    when(productRepoMock.searchProducts(perfectPhrase, warehouseId, pageable)).thenReturn(productPage);
    SearchProductDTO searchDTO = createSearchProductDTO(requestSearchPhrase);
    List<ProductDTO> resultList = productService.searchProducts(searchDTO, pageable);
    List<ProductDTO> perfectList = ProductUtils.createProductDTOListFromPage(productPage);
    verify(warehouseServiceMock, times(1)).getNearestWarehouse(geo);
    verify(productRepoMock, times(1)).searchProducts(perfectPhrase, warehouseId, pageable);
    assertEquals(resultList, perfectList);
}
Also used : Pageable(org.springframework.data.domain.Pageable) Product(com.ncedu.fooddelivery.api.v1.entities.Product) Point(com.vividsolutions.jts.geom.Point) SearchProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO) ProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO) SearchProductDTO(com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

Product (com.ncedu.fooddelivery.api.v1.entities.Product)17 Test (org.junit.jupiter.api.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)10 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)9 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)9 Product (entities.Product)9 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)8 SearchProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Point (com.vividsolutions.jts.geom.Point)5 Pageable (org.springframework.data.domain.Pageable)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)4 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)3 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)3 PageImpl (org.springframework.data.domain.PageImpl)3 com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)2 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2