cz.PA165.vozovyPark.dao.ServiceIntervalDAOTest.java Source code

Java tutorial

Introduction

Here is the source code for cz.PA165.vozovyPark.dao.ServiceIntervalDAOTest.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 cz.PA165.vozovyPark.dao;

import cz.PA165.vozovyPark.dao.impl.ServiceIntervalDAOImpl;
import cz.PA165.vozovyPark.dao.impl.VehicleDAOImpl;
import cz.PA165.vozovyPark.entities.ServiceInterval;
import cz.PA165.vozovyPark.entities.Vehicle;
import cz.PA165.vozovyPark.enums.UserClassEnum;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.Assert;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author Juraj Pleko, 359530
 */
public class ServiceIntervalDAOTest {

    @Autowired
    private ServiceIntervalDAO serviceIntervalDAO;
    @Autowired
    private VehicleDAO vehicleDao;
    private static final String PERSISTENCE_UNIT_NAME = "VozovnaPU";

    private EntityManagerFactory emf;
    private EntityManager em;

    /**
     *
     * @throws ParseException
     */
    @Before
    public void setUp() throws ParseException {

        emf = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
        em = emf.createEntityManager();

        VehicleDAOImpl vehicleDaoImpl = new VehicleDAOImpl(em);
        ServiceIntervalDAOImpl serviceIntervalDaoImpl = new ServiceIntervalDAOImpl(em);
        this.vehicleDao = vehicleDaoImpl;
        this.serviceIntervalDAO = serviceIntervalDaoImpl;

        //cleaning
        List<ServiceInterval> allServiceIntervals = serviceIntervalDAO.findAll();
        for (ServiceInterval si : allServiceIntervals) {
            em.getTransaction().begin();
            serviceIntervalDAO.removeSI(si);
            em.getTransaction().commit();
        }
        Collection<Vehicle> allVehicles = vehicleDao.findAll();
        for (Vehicle v : allVehicles) {
            em.getTransaction().begin();
            vehicleDao.removeVehicle(v);
            em.getTransaction().commit();
        }

    }

    /**
     *
     */
    @After
    public void tearDown() {
        List<ServiceInterval> allServiceIntervals = serviceIntervalDAO.findAll();
        for (ServiceInterval si : allServiceIntervals) {
            em.getTransaction().begin();
            serviceIntervalDAO.removeSI(si);
            em.getTransaction().commit();
        }
        Collection<Vehicle> allVehicles = vehicleDao.findAll();
        for (Vehicle v : allVehicles) {
            em.getTransaction().begin();
            vehicleDao.removeVehicle(v);
            em.getTransaction().commit();
        }
        if (emf.isOpen()) {
            emf.close();
        }
    }

    /**
     *
     */
    @Test
    public void testCreateWithNullArgument() {
        try {
            serviceIntervalDAO.createSI(null);
            Assert.fail("Exception for null argument was not throwed!");
        } catch (IllegalArgumentException ex) {
        } catch (Exception ex) {
            Assert.fail("Unknown exception type was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testUpdateWithNullArgument() {
        try {
            serviceIntervalDAO.updateSI(null);
            Assert.fail("Exception for null argument was not throwed!");
        } catch (IllegalArgumentException ex) {
        } catch (Exception ex) {
            Assert.fail("Unknown exception type was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testRemoveWithNullArgument() {
        try {
            serviceIntervalDAO.removeSI(null);
            Assert.fail("Exception for null argument was not throwed!");
        } catch (IllegalArgumentException ex) {
        } catch (Exception ex) {
            Assert.fail("Unknown exception type was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testGetByIdWithNullArgument() {
        try {
            serviceIntervalDAO.getById(null);
            Assert.fail("Exception for null argument was not throwed!");
        } catch (IllegalArgumentException ex) {
        } catch (Exception ex) {
            Assert.fail("Unknown exception type was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testFindAllByVehicleWithNullArgument() {
        try {
            serviceIntervalDAO.findAllByVehicle(null);
            Assert.fail("Exception for null argument was not throwed!");
        } catch (IllegalArgumentException ex) {
        } catch (Exception ex) {
            Assert.fail("Unknown exception type was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidCreateAndGetById() {
        try {
            Vehicle toyota = ServiceIntervalDAOTest.getVehicle("Toyota", 20000, "V-8", "Supra",
                    UserClassEnum.MANAGER, "78-88as-ouu899", 2004);
            vehicleDao.createVehicle(toyota);
            ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Prehodeni koles", 7, toyota);
            serviceIntervalDAO.createSI(interval1);

            System.out.println("Id to load: " + interval1.getId());
            ServiceInterval loaded = serviceIntervalDAO.getById(interval1.getId());
            Assert.assertEquals("Inserted entity is not same as loaded", interval1, loaded);
        } catch (Exception ex) {
            Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidUpdate() {
        try {
            Vehicle mercedes = ServiceIntervalDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E",
                    UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009);
            vehicleDao.createVehicle(mercedes);
            ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Prehodeni koles", 7, mercedes);
            serviceIntervalDAO.createSI(interval1);

            interval1.setDescription("Vymeneni oleje");
            serviceIntervalDAO.updateSI(interval1);

            ServiceInterval loaded = serviceIntervalDAO.getById(interval1.getId());
            Assert.assertNotSame("Description has not been updated.", "Prehodeni koles", loaded.getDescription());
        } catch (Exception ex) {
            Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidFindAll() {
        try {
            Vehicle mercedes = ServiceIntervalDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "C",
                    UserClassEnum.PRESIDENT, "2f-4a7i-121245", 2009);
            this.vehicleDao.createVehicle(mercedes);

            ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Prehodeni koles", 7, mercedes);
            ServiceInterval interval2 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Vymena oleje", 7, mercedes);
            serviceIntervalDAO.createSI(interval1);
            serviceIntervalDAO.createSI(interval2);

            em.getTransaction().begin();
            List<ServiceInterval> intervals = serviceIntervalDAO.findAll();
            em.getTransaction().commit();
            if (intervals.isEmpty()) {
                Assert.fail("Any entities were loaded from databse!");
            }
        } catch (Exception ex) {
            Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidRemove() {
        try {
            Vehicle mercedes = ServiceIntervalDAOTest.getVehicle("Mercedes", 14000, "R4 Diesel", "A",
                    UserClassEnum.PRESIDENT, "2f-4xxi-121245", 2009);
            this.vehicleDao.createVehicle(mercedes);
            ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Prehodeni koles", 7, mercedes);
            this.serviceIntervalDAO.createSI(interval1);
            ServiceInterval loaded = serviceIntervalDAO.getById(interval1.getId());
            Assert.assertNotNull("Service interval was not created", loaded);

            em.getTransaction().begin();
            serviceIntervalDAO.removeSI(interval1);
            em.getTransaction().commit();

            loaded = serviceIntervalDAO.getById(interval1.getId());
            if (loaded != null) {
                Assert.fail("Interval was not deleted from database.");
            }
            Assert.assertNull("Service interval was not deleted from database", loaded);
        } catch (Exception ex) {
            Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage());
        }
    }

    /**
     *
     */
    @Test
    public void testValidFindByVehicle() {
        try {
            Vehicle mercedes = ServiceIntervalDAOTest.getVehicle("Mercedes", 14000, "R4 Diesel", "A",
                    UserClassEnum.PRESIDENT, "2f-4xxi-121245", 2009);
            vehicleDao.createVehicle(mercedes);
            ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(new ArrayList<Date>(),
                    "Prehodeni koles", 7, mercedes);
            serviceIntervalDAO.createSI(interval1);

            em.getTransaction().begin();
            List<ServiceInterval> intervalsOfVehicle = serviceIntervalDAO.findAllByVehicle(mercedes);
            em.getTransaction().commit();
            if (intervalsOfVehicle.isEmpty()) {
                Assert.fail("Any intervals of given vehicle were loaded from databse!");
            }
        } catch (Exception ex) {
            Assert.fail("Unexpected exception was throwed: " + ex + " " + ex.getMessage());
        }
    }

    private static Vehicle getVehicle(String brand, int distanceCount, String engineType, String type,
            UserClassEnum userClass, String vin, int year) {

        Vehicle vehicle = new Vehicle();
        vehicle.setBrand(brand);
        vehicle.setMaxDistance(distanceCount);
        vehicle.setEngineType(engineType);
        vehicle.setType(type);
        vehicle.setUserClass(userClass);
        vehicle.setVin(vin);
        vehicle.setYearMade(year);
        vehicle.setRegistrationPlate("BAC9562D");

        return vehicle;
    }

    private static ServiceInterval getServiceInterval(List<Date> dates, String description, int inspectionInterval,
            Vehicle vehicle) {
        ServiceInterval interval = new ServiceInterval();
        interval.setDated(dates);
        interval.setDescription(description);
        interval.setInspectionInterval(inspectionInterval);
        interval.setVehicle(vehicle);

        return interval;
    }

    private void createData() {
        SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy");
        Vehicle toyota = ServiceIntervalDAOTest.getVehicle("Toyota", 20000, "V-8", "Supra", UserClassEnum.MANAGER,
                "78-88as-ouu899", 2004);
        Vehicle mercedes = ServiceIntervalDAOTest.getVehicle("Mercedes", 20000, "R4 Diesel", "E",
                UserClassEnum.PRESIDENT, "2a-447i-882a45", 2009);
        vehicleDao.createVehicle(mercedes);
        vehicleDao.createVehicle(toyota);

        List<Date> dates = new ArrayList<>();
        ServiceInterval interval1 = ServiceIntervalDAOTest.getServiceInterval(dates, "Prehodeni koles", 7, toyota);
        ServiceInterval interval2 = ServiceIntervalDAOTest.getServiceInterval(dates, "Kontrola stavu oleja", 10,
                mercedes);
        serviceIntervalDAO.createSI(interval1);
        serviceIntervalDAO.createSI(interval2);
    }
}