Skip to content
Snippets Groups Projects
Commit c1edf0a6 authored by Christopher Bohn's avatar Christopher Bohn :thinking:
Browse files

added unit tests

parent 0f11b1f7
No related branches found
No related tags found
No related merge requests found
......@@ -26,6 +26,7 @@ abstract class CustomerEntity <<entity>> {
+ {static} MAXIMUM_CITY_LENGTH
+ {static} STATE_LENGTH
+ {static} ZIPCODE_LENGTH
~ {static} getCustomerByName(String name)
~ boolean canRent()
}
class CorporateCustomerEntity <<entity>> {
......@@ -67,6 +68,7 @@ interface Model {
}
class ModelEntity <<entity>> {
~ {static} Set<Model> getModelsByClass(VehicleClass vehicleClass)
+ void removeCar(Car car)
}
......
package edu.unl.cse.csce361.car_rental.backend;
import java.util.Collection;
import java.util.Set;
/**
* A façade for the Backend subsystem.
*/
public class Backend {
private Inventory inventory;
private static Backend instance;
public static Backend getInstance() {
if (instance == null) {
instance = new Backend();
}
return instance;
}
private Backend() {
super();
inventory = new Inventory();
}
public Collection<Car> getCar(String modelName) {
return null;
public Customer getCustomer(String name) {
return CustomerEntity.getCustomerByName(name);
}
public Collection<Model> getModels() {
return inventory.getModels();
public Set<Model> getModels(String vehicleClass) {
Set<Model> models;
try {
Model.VehicleClass vehicleClassEnum = Model.VehicleClass.valueOf(vehicleClass.toUpperCase());
models = ModelEntity.getModelsByClass(vehicleClassEnum);
} catch (IllegalArgumentException | NullPointerException exception) {
System.err.println("No such vehicle class: " + vehicleClass.toUpperCase() + ". " + exception.getMessage());
models = Set.of();
}
return models;
}
}
package edu.unl.cse.csce361.car_rental.backend;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import javax.persistence.CascadeType;
......@@ -23,6 +25,29 @@ import java.util.Set;
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class CustomerEntity implements Customer {
/* Database code */
/**
* Retrieves the customer that has the specified name, if such a customer exists.
* @param name The name of the customer
* @return The specified customer if it is present in the database; <code>null</code> otherwise
*/
static Customer getCustomerByName(String name) {
Session session = HibernateUtil.getSession();
session.beginTransaction();
Customer customer = null;
try {
customer = session.bySimpleNaturalId(CustomerEntity.class).load(name);
session.getTransaction().commit();
} catch (HibernateException exception) {
System.err.println("Could not load Customer " + name + ". " + exception.getMessage());
}
return customer;
}
/* POJO code */
/** The maximum length of each of the customer's name and two street address lines */
public static final int MAXIMUM_LINE_LENGTH = 48;
/** The maximum length of the city's name */
......
......@@ -14,7 +14,7 @@ import static edu.unl.cse.csce361.car_rental.backend.Model.VehicleClass.*;
public class DatabasePopulator {
private static Set<Model> createModels() {
static Set<Model> createModels() {
System.out.println("Creating car models...");
return Set.of(
new ModelEntity("Triumph", "Stag", COMPACT, 2, MANUAL, GASOLINE, 19),
......@@ -33,7 +33,7 @@ public class DatabasePopulator {
}
@SuppressWarnings("SpellCheckingInspection")
private static Set<Car> createCars() {
static Set<Car> createCars() {
System.out.println("Creating cars...");
return Set.of(
new CarEntity("Ranger", "Blue", "123 ABC", "1234567890ABCDEFG"),
......@@ -60,7 +60,7 @@ public class DatabasePopulator {
);
}
private static Set<Customer> createCorporateCustomers() {
static Set<Customer> createCorporateCustomers() {
System.out.println("Creating corporate customers...");
return Set.of(
new CorporateCustomerEntity("Cornhusker Airlines", "123 Airport Avenue", "",
......@@ -81,7 +81,7 @@ public class DatabasePopulator {
}
@SuppressWarnings("SpellCheckingInspection")
private static Set<Customer> createIndividualCustomers() {
static Set<Customer> createIndividualCustomers() {
System.out.println("Creating individual customers...");
return Set.of(
new IndividualCustomerEntity("Stu Dent", "Fawlty Tower", "Room SB32",
......@@ -108,7 +108,7 @@ public class DatabasePopulator {
}
@SuppressWarnings("SpellCheckingInspection")
private static Set<RentalEntity> createRentals(Session session) {
static Set<RentalEntity> createRentals(Session session) {
System.out.println("Creating rentals...");
Car car;
Customer customer;
......
package edu.unl.cse.csce361.car_rental.backend;
import java.util.Collection;
import java.util.Collections;
public class Inventory {
private Collection<Car> cars; // this representation will probably change
private Collection<Model> models;
public Collection<Model> getModels() {
return Collections.unmodifiableCollection(models);
}
}
package edu.unl.cse.csce361.car_rental.backend;
import org.hibernate.Session;
import org.hibernate.annotations.NaturalId;
import javax.persistence.CascadeType;
......@@ -10,6 +11,7 @@ import javax.persistence.Id;
import javax.persistence.OneToMany;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
......@@ -20,6 +22,25 @@ import java.util.Set;
@Entity
public class ModelEntity implements Model {
/* Database code */
/**
* Retrieves a collection of car models that are of the specified vehicle class.
*
* @param vehicleClass The vehicle class of interest
* @return A set of car models
*/
static Set<Model> getModelsByClass(VehicleClass vehicleClass) {
Session session = HibernateUtil.getSession();
List<ModelEntity> models = session
.createQuery("from ModelEntity where classType=" + vehicleClass.ordinal(), ModelEntity.class)
.list();
return Set.copyOf(models);
}
/* POJO code */
/**
* The upper bound on the length of a model name
*/
......
package edu.unl.cse.csce361.car_rental.backend;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
public class BackendTest {
Backend backend;
@Before
public void setUp() {
backend = Backend.getInstance();
Session session = HibernateUtil.getSession();
session.beginTransaction();
DatabasePopulator.createModels().forEach(session::saveOrUpdate);
DatabasePopulator.createCars().forEach(session::saveOrUpdate);
DatabasePopulator.createCorporateCustomers().forEach(session::saveOrUpdate);
DatabasePopulator.createIndividualCustomers().forEach(session::saveOrUpdate);
DatabasePopulator.createRentals(session).forEach(session::saveOrUpdate);
session.getTransaction().commit();
}
@After
public void tearDown() {
Session session = HibernateUtil.getSession();
session.beginTransaction();
session.createQuery("delete from RentalEntity").executeUpdate();
session.createQuery("delete from CarEntity").executeUpdate();
session.createQuery("delete from ModelEntity").executeUpdate();
session.createQuery("delete from CustomerEntity").executeUpdate();
session.createQuery("delete from CorporateCustomerEntity").executeUpdate();
session.createQuery("delete from IndividualCustomerEntity").executeUpdate();
session.getTransaction().commit();
}
@Test
public void testGetCustomer() {
// arrange
String customerName = "Mary O'Kart";
String expectedCustomerAddressStart = "1 Racing Road";
// act
Customer customer = backend.getCustomer(customerName);
// assert
assertTrue(customer.getAddress().startsWith(expectedCustomerAddressStart));
}
@Test
public void testGetModels() {
// arrange
String vehicleClass = "Midsized";
Set<String> expectedModelNames = Set.of("Malibu", "Model 3");
// act
Set<Model> models = backend.getModels(vehicleClass);
Set<String> actualModelNames = models.stream().map(Model::getModel).collect(Collectors.toSet());
// assert
assertEquals(expectedModelNames, actualModelNames);
}
}
\ No newline at end of file
package edu.unl.cse.csce361.car_rental.backend;
import org.hibernate.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.Assert.*;
public class DatabaseTest {
Session session;
@Before
public void setUp() {
session = HibernateUtil.getSession();
session.beginTransaction();
DatabasePopulator.createModels().forEach(session::saveOrUpdate);
DatabasePopulator.createCars().forEach(session::saveOrUpdate);
DatabasePopulator.createCorporateCustomers().forEach(session::saveOrUpdate);
DatabasePopulator.createIndividualCustomers().forEach(session::saveOrUpdate);
DatabasePopulator.createRentals(session).forEach(session::saveOrUpdate);
session.getTransaction().commit();
}
@After
public void tearDown() {
session.beginTransaction();
session.createQuery("delete from RentalEntity").executeUpdate();
session.createQuery("delete from CarEntity").executeUpdate();
session.createQuery("delete from ModelEntity").executeUpdate();
session.createQuery("delete from CustomerEntity").executeUpdate();
session.createQuery("delete from CorporateCustomerEntity").executeUpdate();
session.createQuery("delete from IndividualCustomerEntity").executeUpdate();
session.getTransaction().commit();
}
@Test
public void testGetIndividualCustomerByName() {
// arrange
String customerName = "Mary O'Kart";
String expectedCustomerAddressStart = "1 Racing Road";
// act
Customer customer = CustomerEntity.getCustomerByName(customerName);
// assert
assertTrue(customer.getAddress().startsWith(expectedCustomerAddressStart));
}
@Test
public void testGetModelsByClass() {
// arrange
Model.VehicleClass vehicleClass = Model.VehicleClass.MIDSIZED;
Set<String> expectedModelNames = Set.of("Malibu", "Model 3");
// act
Set<Model> models = ModelEntity.getModelsByClass(vehicleClass);
Set<String> actualModelNames = models.stream().map(Model::getModel).collect(Collectors.toSet());
// assert
assertEquals(expectedModelNames, actualModelNames);
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment