crud.ProductCrud.java Source code

Java tutorial

Introduction

Here is the source code for crud.ProductCrud.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package crud;

import com.company.transactionapp.appconfig.ConnectionConfig;
import com.company.transactionapp.model.Product;
import com.company.transactionapp.model.ProductType;
import com.company.transactionapp.repository.ProductRepository;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import static org.testng.Assert.*;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

/**
 *
 * @author Luke
 */
public class ProductCrud {
    public static ApplicationContext ctx;
    private static ProductRepository productRepo;
    private static ProductType productType;
    private Long id;

    public ProductCrud() {
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}

    @BeforeClass
    public static void setUpClass() throws Exception {
        ctx = new AnnotationConfigApplicationContext(ConnectionConfig.class);

        productType = new ProductType.Builder().productType("Luxury").productBrand("Nestle").build();
    }

    @AfterClass
    public static void tearDownClass() throws Exception {
    }

    @BeforeMethod
    public void setUpMethod() throws Exception {
    }

    @AfterMethod
    public void tearDownMethod() throws Exception {
    }

    @Test
    public void createProductTest() {
        productRepo = ctx.getBean(ProductRepository.class);

        Product product = new Product.Builder().productName("Topdeck").price(9.50).productType(productType)
                .quantity(10).build();

        productRepo.save(product);
        id = product.getId();
        assertNotNull(product);
    }

    @Test(dependsOnMethods = "createProductTest")
    public void readProductTest() {
        productRepo = ctx.getBean(ProductRepository.class);
        Product product = productRepo.findOne(id);
        assertEquals(product.getProductName(), "Topdeck");

    }

    @Test(dependsOnMethods = "readProductTest")
    private void updateProductTest() {
        productRepo = ctx.getBean(ProductRepository.class);
        Product product = productRepo.findOne(id);
        Product updatedProduct = new Product.Builder().clone(product).quantity(23).build();
        productRepo.save(updatedProduct);

        Product newProduct = productRepo.findOne(id);
        assertEquals(newProduct.getQuantity(), 23);

    }

    @Test(dependsOnMethods = "updateProductTest")
    private void deleteProduct() {
        productRepo = ctx.getBean(ProductRepository.class);
        Product product = productRepo.findOne(id);
        productRepo.delete(product);

        Product deletedProduct = productRepo.findOne(id);
        assertNull(deletedProduct);

    }
}