Commit 6445e2bf authored by Matti Henning's avatar Matti Henning
Browse files

JUnit4 tests check-in

parent 300ee0ad
......@@ -3,3 +3,5 @@
.project
.settings
bin/**
target/**
lib/**
package model;
import java.util.NoSuchElementException;
import java.util.Optional;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import model.Customer;
import testsuites.TestBuilder;
import data_access.DataRepository;
/**
*
* JUnit4 tests for Customer class.
*
* Use of assertions, see:
* https://junit.org/junit4/javadoc/latest/org/junit/Assert.html
*
* @author sgra64
*/
public class CustomerTest {
/*
* Constants to compare expected results.
*/
private final static String idLarry = "C44698";
private final static String nameLarry = "Larry Hagman";
private final static String firstNameLarry = "Larry";
private final static String lastNameLarry = "Hagman";
private final static String contactLarry = "lh812@gmx.de";
public final static String idEric = "C86516";
public final static String firstNameEric = "Eric";
public final static String lastNameEric = "Schulz-Mueller";
public final static String contactEric = "eric2346@gmail.com";
private final static String idAnne = "C64327";
private final static String firstNameAnne = "Anne";
private final static String lastNameAnne = "Meyer";
private final static String contactAnne = "+4917223524";
private final static String idNadine = "C12396";
private final static String firstNameNadine = "Nadine Ulla";
private final static String lastNameNadine = "Blumenfeld";
private final static String contactNadine = "+4915292454";
private DataRepository<Customer,String> customerData = null;
/**
* JUnit4 Test Setup Code
* ------------------------------------------------------------------------
* Setup method invoked once before any @Test method is executed.
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//System.out.println( CustomerTest.class.getSimpleName() + ".setUpBeforeClass() called." );
}
/**
* Setup method executed each time before a @Test method executes. Each @Test
* method executes with a new instance of this class.
* @throws Exception
*/
@Before
public void setUp() throws Exception {
//System.out.println( this.getClass().getSimpleName() + ".setUp() called." );
this.customerData = TestBuilder.getInstance().dataAccess().getCustomerData();
}
/**
* JUnit4 Test Code
* ------------------------------------------------------------------------
*/
@Test
public void testConstructor() {
Customer cLarry = new Customer( idLarry, nameLarry, contactLarry );
assertNotNull( cLarry.getId() ); // Customer id must not be null
assertEquals( cLarry.getId(), idLarry ); // assert that correct ID is returned
assertEquals( cLarry.getFirstName(), firstNameLarry );
assertEquals( cLarry.getLastName(), lastNameLarry );
assertEquals( cLarry.getContact(), contactLarry );
}
@Test
public void testEmptyStringConstructor() {
Customer c1 = new Customer( "", "", "" );
assertEquals( c1.getId(), "" );
assertEquals( c1.getFirstName(), "" ); // "" is returned, not null
assertEquals( c1.getLastName(), "" ); // "" is returned, not null
assertEquals( c1.getContact(), "" );
}
@Test
public void testNullConstructor() {
Customer c1 = new Customer( null, null, null );
assertNull( c1.getId() );
assertEquals( c1.getFirstName(), "" ); // "" is returned, not null
assertEquals( c1.getLastName(), "" ); // "" is returned, not null
assertNull( c1.getContact() );
}
@Test
public void testCustomerData() {
assertNotNull( customerData );
Optional<Customer> optEric = customerData.findById( idEric );
assertTrue( optEric.isPresent() );
Customer cEric = optEric.get();
assertNotNull( cEric );
assertEquals( cEric.getFirstName(), firstNameEric );
assertEquals( cEric.getLastName(), lastNameEric );
assertEquals( cEric.getContact(), contactEric );
Optional<Customer> optAnne = customerData.findById( idAnne );
assertTrue( optAnne.isPresent() );
Customer cAnne = optAnne.get();
assertNotNull( cAnne );
assertEquals( cAnne.getFirstName(), firstNameAnne );
assertEquals( cAnne.getLastName(), lastNameAnne );
assertEquals( cAnne.getContact(), contactAnne );
Optional<Customer> optNadine = customerData.findById( idNadine );
assertTrue( optNadine.isPresent() );
Customer cNadine = optNadine.get();
assertNotNull( cNadine );
assertEquals( cNadine.getFirstName(), firstNameNadine );
assertEquals( cNadine.getLastName(), lastNameNadine );
assertEquals( cNadine.getContact(), contactNadine );
}
@Test
public void testCustomerDataUpdate() {
assertNotNull( customerData );
long count1 = customerData.count();
Customer cLarry = new Customer( idLarry, nameLarry, contactLarry );
customerData.save( cLarry );
long count2 = customerData.count();
assertEquals( count1 + 1, count2 );
// MUST throw exception
NoSuchElementException nse = assertThrows( NoSuchElementException.class, () -> {
Customer cLarryNotFound = customerData.findById( "XX--invaild_id--??//" ).get();
assertNull( cLarryNotFound );
});
assertNotNull( nse );
Customer cLarryFound = customerData.findById( idLarry ).get();
assertNotNull( cLarryFound );
assertSame( cLarry, cLarryFound ); // same object found as saved, cLarry == cLarryFound
}
@Test
public void testCustomerNameResolution() {
Customer cEric = new Customer( "C86516", "Eric Meyer", "eric2346@gmail.com" );
assertNotNull( cEric.getId() );
assertEquals( cEric.getFirstName(), "Eric" );
assertEquals( cEric.getLastName(), "Meyer" );
cEric.setName( "Meyer, Eric" );
assertEquals( cEric.getFirstName(), "Eric" );
assertEquals( cEric.getLastName(), "Meyer" );
Customer c1 = new Customer( "", "", "" );
c1.setName( "Nadine Ulla-Blumenfeld" );
assertEquals( c1.getFirstName(), "Nadine" );
assertEquals( c1.getLastName(), "Ulla-Blumenfeld" );
c1.setName( "Blumenfeld, Nadine Ulla" );
assertEquals( c1.getFirstName(), "Nadine Ulla" );
assertEquals( c1.getLastName(), "Blumenfeld" );
c1.setName( "Blumenfeld" );
assertEquals( c1.getFirstName(), "" );
assertEquals( c1.getLastName(), "Blumenfeld" );
/*
* Ambitionierte Tests
* /
c1.setName( "Blumenfeld," );
assertEquals( c1.getFirstName(), "" );
assertEquals( c1.getLastName(), "Blumenfeld" );
c1.setName( ",Blumenfeld" );
assertEquals( c1.getFirstName(), "" );
assertEquals( c1.getLastName(), "Blumenfeld" );
c1.setName( " , , Blumenfeld , ,, " );
assertEquals( c1.getFirstName(), "" );
assertEquals( c1.getLastName(), "Blumenfeld" );
c1.setName( "Nadine Ulla-Mona Blumenfeld-Meyer" );
assertEquals( c1.getFirstName(), "Nadine Ulla-Mona" );
assertEquals( c1.getLastName(), "Blumenfeld-Meyer" );
c1.setName( "Nadine Ulla-Mona-Blumenfeld-Meyer" );
assertEquals( c1.getFirstName(), "Nadine" );
assertEquals( c1.getLastName(), "Ulla-Mona-Blumenfeld-Meyer" );
/* */
}
/**
* JUnit4 Test Tear-down Code
* ------------------------------------------------------------------------
*
* Tear-down method invoked each time after a @Test method has finished.
* @throws Exception
*/
@After
public void tearDown() throws Exception {
//System.out.println( this.getClass().getSimpleName() + ".tearDown() called." );
}
/**
* Tear-down method invoked once after all @Test methods in this class have finished.
* @throws Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
//System.out.println( CustomerTest.class.getSimpleName() + ".tearDownAfterClass() called." );
TestBuilder.getInstance().tearDown();
}
}
package system;
import java.util.List;
import java.util.Optional;
import org.junit.Test;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import data_access.DataAccess;
import data_access.DataRepository;
import model.Article;
import model.Customer;
import model.CustomerTest;
import model.Order;
import model.OrderItem;
import testsuites.TestBuilder;
/**
*
* JUnit4 tests for Logic interface.
*
* Use of assertions, see:
* https://junit.org/junit4/javadoc/latest/org/junit/Assert.html
*
* @author sgra64
*/
public class LogicTest {
/*
* Constants to compare expected results.
*/
private final static long idO5234 = 5234968294L; // id of Eric's 1st order
private final static long idO8592 = 8592356245L; // id of Eric's 2nd order
private final static long idO3563 = 3563561357L; // id of Anne's order
private final static long idO6135 = 6135735635L; // id of Nadine's order
private final static long idO9235 = 9235856245L; // id of Timo's order
private final static long idO7335 = 7335856245L; // id of Sandra's order
private final static long idO7356 = 7356613535L; // id of Nadine's 2nd order
private Logic logic = null;
private DataRepository<Customer,String> customerData = null;
private DataRepository<Article,String> articleData = null;
private DataRepository<Order,Long> orderData = null;
/**
* JUnit4 Test Setup Code
* ------------------------------------------------------------------------
* Setup method invoked once before any @Test method is executed.
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//System.out.println( LogicTest.class.getSimpleName() + ".setUpBeforeClass() called." );
}
/**
* Setup method executed each time before a @Test method executes. Each @Test
* method executes with a new instance of this class.
* @throws Exception
*/
@Before
public void setUp() throws Exception {
//System.out.println( this.getClass().getSimpleName() + ".setUp() called." );
TestBuilder tb = TestBuilder.getInstance();
DataAccess da = tb.dataAccess();
this.logic = tb.logic();
this.customerData = da.getCustomerData();
this.articleData = da.getArticleData();
this.orderData = da.getOrderData();
}
/**
* JUnit4 Test Code
* ------------------------------------------------------------------------
*/
@Test
public void testOrderItems() {
assertNotNull( orderData );
// Eric's 1st order
Optional<Order> opt5234 = orderData.findById( idO5234 );
assertTrue( opt5234.isPresent() );
Order o5234 = opt5234.get();
assertNotNull( o5234 );
Customer cEric = o5234.getCustomer();
Customer cEricx = customerData.findById( CustomerTest.idEric ).get();
assertSame( cEric, cEricx );
List<OrderItem> items5234 = o5234.getItems();
assertEquals( items5234.size(), 1 );
OrderItem oi5234_1 = items5234.get( 0 );
Article a3 = oi5234_1.getArticle();
Article a3x = articleData.findById( "SKU-518957" ).get();
assertSame( a3, a3x );
}
@Test
public void testOrderCalculations() {
Order o5234 = orderData.findById( idO5234 ).get(); // 2000, 319
long total5234 = logic.calculateTotal( o5234 );
assertEquals( total5234, 2000L );
long vat5234 = logic.calculateIncludedVAT( o5234 );
assertEquals( vat5234, 319L );
Order o8592 = orderData.findById( idO8592 ).get(); // 4984, 796
assertEquals( logic.calculateTotal( o8592 ), 4984L );
assertEquals( logic.calculateIncludedVAT( o8592 ), 796 );
Order o3563 = orderData.findById( idO3563 ).get(); // 2000, 319
assertEquals( logic.calculateTotal( o3563 ), 2000L );
assertEquals( logic.calculateIncludedVAT( o3563 ), 319 );
Order o6135 = orderData.findById( idO6135 ).get(); // 7788,1243
assertEquals( logic.calculateTotal( o6135 ), 7788L );
assertEquals( logic.calculateIncludedVAT( o6135 ), 1243 );
Order o9235 = orderData.findById( idO9235 ).get(); // 4793, 765
assertEquals( logic.calculateTotal( o9235 ), 4793L );
assertEquals( logic.calculateIncludedVAT( o9235 ), 765 );
Order o7335 = orderData.findById( idO7335 ).get(); // 5191, 829
assertEquals( logic.calculateTotal( o7335 ), 5191L );
assertEquals( logic.calculateIncludedVAT( o7335 ), 829 );
Order o7356 = orderData.findById( idO7356 ).get(); // 3894, 622
assertEquals( logic.calculateTotal( o7356 ), 3894L );
assertEquals( logic.calculateIncludedVAT( o7356 ), 622 );
}
@Test
public void testTotalOrderValue() {
long total = 0L;
long vat = 0L;
for( Order order : orderData.findAll() ) {
long orderTotal = 0L;
for( OrderItem item : order.getItems() ) {
orderTotal += item.getUnitsOrdered() * item.getArticle().getUnitPrice();
}
long orderTotalCalculated = logic.calculateTotal( order );
assertEquals( orderTotal, orderTotalCalculated );
total += orderTotalCalculated;
long vatCalculated = logic.calculateIncludedVAT( order );
vat += vatCalculated;
}
assertEquals( total, 30650L ); // total order value: 306,50; 48,93 VAT
assertEquals( vat, 4893L );
}
/**
* JUnit4 Test Tear-down Code
* ------------------------------------------------------------------------
*
* Tear-down method invoked each time after a @Test method has finished.
* @throws Exception
*/
@After
public void tearDown() throws Exception {
//System.out.println( this.getClass().getSimpleName() + ".tearDown() called." );
}
/**
* Tear-down method invoked once after all @Test methods in this class have finished.
* @throws Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
//System.out.println( LogicTest.class.getSimpleName() + ".tearDownAfterClass() called." );
TestBuilder.getInstance().tearDown();
}
}
package testsuites;
import data_access.DataAccess;
import system.Logic;
import system.builder.Builder;
/**
* Singleton class to build the Test-Environment. Class uses the
* Builder-class of the main application to set up all component
* instances (instances implementing Logic.java and DataAccess.java
* interfaces) including data repositories with mock data.
*
* @author sgra64
*
*/
public class TestBuilder {
private static TestBuilder instance = null;
private final boolean runFromTestSuite;
private final Builder builder;
private Logic logic = null;
private DataAccess dataAccess = null;
/*
* Private constructor according to the Singleton pattern.
*/
private TestBuilder( boolean runFromTestSuite ) {
this.runFromTestSuite = runFromTestSuite;
System.out.println( "TestBuilder set up Unit Tests Bestellsystem SE1." );
this.builder = Builder.getInstance();
}
/**
* Public access methods to singleton TestBuilder instance.
*
* @param runFromTestSuite indicator that method is called from TestSuite
* @return singleton TestBuilder instance
*/
public static TestBuilder getInstance( boolean runFromTestSuite ) {
if( instance == null ) {
instance = new TestBuilder( runFromTestSuite );
instance.buildTestEnvironment();
}
return instance;
}
public static TestBuilder getInstance() {
return getInstance( false );
}
/**
* Provide access to instance that implements the Logic interface.
*
* @return instance that implements the Logic interface
*/
public Logic logic() {
return logic;
}
/**
* Provide access to instance that implements the DataAccess interface
* containing data repositories for Customer, Article and Order objects.
*
* @return instance that implements the DataAccess interface
*/
public DataAccess dataAccess() {
return dataAccess;
}
/**
* Public method called when test environment is torn down.
*
*/
public void tearDown() {
tearDown( ! runFromTestSuite );
}
/**
* Public method called when test environment is torn down.
*
* @param runFromTestSuite indicator that method is called from TestSuite
*/
public void tearDown( boolean runFromTestSuite ) {
if( runFromTestSuite ) {
System.out.println( "TestBuilder tear down." );
builder.shutdown();
}
}
/*
* Private methods.
*/
private void buildTestEnvironment() {
builder.startup();
logic = builder.logic();
dataAccess = builder.dataAccess();
builder
.build( 1, _builder -> {
//logic.printInventory();
})
.build( 2 );
}
}
package testsuites;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
*
* JUnit4 TestSuite class.
*
* @author sgra64
*
*/
@RunWith( Suite.class )
@Suite.SuiteClasses({ // Annotation to configure suite's test classes
model.CustomerTest.class,
system.LogicTest.class,
// add more test classes ...
})
public class TestSuite {
/**
* Test setup method called by JUnit before tests execute.
* ------------------------------------------------------------------------
*
* Setup method invoked before test classes in test suite are executed.
* @throws Exception
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception {
//System.out.println( TestSuite.class.getSimpleName() + ".setUpBeforeClass() called." );
TestBuilder.getInstance( true ); // trigger TestBuilder initialization.
}
/**
* Test tear down method called by JUnit after tests execution.
* ------------------------------------------------------------------------
*
* Tear-down method invoked after test classes in test suite have finished.
* @throws Exception
*/
@AfterClass
public static void tearDownAfterClass() throws Exception {
//System.out.println( TestSuite.class.getSimpleName() + ".tearDownAfterClass() called." );
TestBuilder.getInstance().tearDown( true );
}
}