Java 11 migrate c-d (remaining) (#1111)
* Moves converter pattern to Java 11 * Moves cqrs pattern to Java 11 * Moves dao pattern to Java 11 * Moves data-bus pattern to Java 11 * Moves data-locality pattern to Java 11 * Moves data-mapper pattern to Java 11 * Moves data-transfer-object pattern to Java 11 * Moves decorator pattern to Java 11 * Moves delegation pattern to Java 11 * Moves dependency-injection to Java 11 * Moves dirty-flag to Java 11 * Moves double-buffer to Java 11 * Moves double-checked-locking to Java 11 * Moves double-dispatch to Java 11 * Corrects with changes thats breaking test cases
This commit is contained in:
committed by
Ilkka Seppälä
parent
5681684157
commit
ea57934db6
@ -23,11 +23,8 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.List;
|
||||
import java.util.stream.Stream;
|
||||
import javax.sql.DataSource;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.slf4j.Logger;
|
||||
@ -57,32 +54,32 @@ public class App {
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
public static void main(final String[] args) throws Exception {
|
||||
final CustomerDao inMemoryDao = new InMemoryCustomerDao();
|
||||
final var inMemoryDao = new InMemoryCustomerDao();
|
||||
performOperationsUsing(inMemoryDao);
|
||||
|
||||
final DataSource dataSource = createDataSource();
|
||||
final var dataSource = createDataSource();
|
||||
createSchema(dataSource);
|
||||
final CustomerDao dbDao = new DbCustomerDao(dataSource);
|
||||
final var dbDao = new DbCustomerDao(dataSource);
|
||||
performOperationsUsing(dbDao);
|
||||
deleteSchema(dataSource);
|
||||
}
|
||||
|
||||
private static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static void createSchema(DataSource dataSource) throws SQLException {
|
||||
try (Connection connection = dataSource.getConnection();
|
||||
Statement statement = connection.createStatement()) {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private static DataSource createDataSource() {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
var dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(DB_URL);
|
||||
return dataSource;
|
||||
}
|
||||
@ -90,18 +87,18 @@ public class App {
|
||||
private static void performOperationsUsing(final CustomerDao customerDao) throws Exception {
|
||||
addCustomers(customerDao);
|
||||
log.info(ALL_CUSTOMERS);
|
||||
try (Stream<Customer> customerStream = customerDao.getAll()) {
|
||||
try (var customerStream = customerDao.getAll()) {
|
||||
customerStream.forEach((customer) -> log.info(customer.toString()));
|
||||
}
|
||||
log.info("customerDao.getCustomerById(2): " + customerDao.getById(2));
|
||||
final Customer customer = new Customer(4, "Dan", "Danson");
|
||||
final var customer = new Customer(4, "Dan", "Danson");
|
||||
customerDao.add(customer);
|
||||
log.info(ALL_CUSTOMERS + customerDao.getAll());
|
||||
customer.setFirstName("Daniel");
|
||||
customer.setLastName("Danielson");
|
||||
customerDao.update(customer);
|
||||
log.info(ALL_CUSTOMERS);
|
||||
try (Stream<Customer> customerStream = customerDao.getAll()) {
|
||||
try (var customerStream = customerDao.getAll()) {
|
||||
customerStream.forEach((cust) -> log.info(cust.toString()));
|
||||
}
|
||||
customerDao.delete(customer);
|
||||
@ -109,7 +106,7 @@ public class App {
|
||||
}
|
||||
|
||||
private static void addCustomers(CustomerDao customerDao) throws Exception {
|
||||
for (Customer customer : generateSampleCustomers()) {
|
||||
for (var customer : generateSampleCustomers()) {
|
||||
customerDao.add(customer);
|
||||
}
|
||||
}
|
||||
@ -120,9 +117,9 @@ public class App {
|
||||
* @return list of customers.
|
||||
*/
|
||||
public static List<Customer> generateSampleCustomers() {
|
||||
final Customer customer1 = new Customer(1, "Adam", "Adamson");
|
||||
final Customer customer2 = new Customer(2, "Bob", "Bobson");
|
||||
final Customer customer3 = new Customer(3, "Carl", "Carlson");
|
||||
final var customer1 = new Customer(1, "Adam", "Adamson");
|
||||
final var customer2 = new Customer(2, "Bob", "Bobson");
|
||||
final var customer3 = new Customer(3, "Carl", "Carlson");
|
||||
return List.of(customer1, customer2, customer3);
|
||||
}
|
||||
}
|
||||
|
@ -73,11 +73,11 @@ public class Customer {
|
||||
|
||||
@Override
|
||||
public boolean equals(final Object that) {
|
||||
boolean isEqual = false;
|
||||
var isEqual = false;
|
||||
if (this == that) {
|
||||
isEqual = true;
|
||||
} else if (that != null && getClass() == that.getClass()) {
|
||||
final Customer customer = (Customer) that;
|
||||
final var customer = (Customer) that;
|
||||
if (getId() == customer.getId()) {
|
||||
isEqual = true;
|
||||
}
|
||||
|
@ -65,13 +65,10 @@ public class DbCustomerDao implements CustomerDao {
|
||||
*/
|
||||
@Override
|
||||
public Stream<Customer> getAll() throws Exception {
|
||||
|
||||
Connection connection;
|
||||
try {
|
||||
connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
connection.prepareStatement("SELECT * FROM CUSTOMERS"); // NOSONAR
|
||||
ResultSet resultSet = statement.executeQuery(); // NOSONAR
|
||||
var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS");
|
||||
var resultSet = statement.executeQuery(); // NOSONAR
|
||||
return StreamSupport.stream(new Spliterators.AbstractSpliterator<Customer>(Long.MAX_VALUE,
|
||||
Spliterator.ORDERED) {
|
||||
|
||||
@ -121,9 +118,8 @@ public class DbCustomerDao implements CustomerDao {
|
||||
|
||||
ResultSet resultSet = null;
|
||||
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("SELECT * FROM CUSTOMERS WHERE ID = ?")) {
|
||||
|
||||
statement.setInt(1, id);
|
||||
resultSet = statement.executeQuery();
|
||||
@ -150,9 +146,8 @@ public class DbCustomerDao implements CustomerDao {
|
||||
return false;
|
||||
}
|
||||
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("INSERT INTO CUSTOMERS VALUES (?,?,?)")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
statement.setString(2, customer.getFirstName());
|
||||
statement.setString(3, customer.getLastName());
|
||||
@ -168,8 +163,8 @@ public class DbCustomerDao implements CustomerDao {
|
||||
*/
|
||||
@Override
|
||||
public boolean update(Customer customer) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
try (var connection = getConnection();
|
||||
var statement =
|
||||
connection
|
||||
.prepareStatement("UPDATE CUSTOMERS SET FNAME = ?, LNAME = ? WHERE ID = ?")) {
|
||||
statement.setString(1, customer.getFirstName());
|
||||
@ -186,9 +181,8 @@ public class DbCustomerDao implements CustomerDao {
|
||||
*/
|
||||
@Override
|
||||
public boolean delete(Customer customer) throws Exception {
|
||||
try (Connection connection = getConnection();
|
||||
PreparedStatement statement =
|
||||
connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
try (var connection = getConnection();
|
||||
var statement = connection.prepareStatement("DELETE FROM CUSTOMERS WHERE ID = ?")) {
|
||||
statement.setInt(1, customer.getId());
|
||||
return statement.executeUpdate() > 0;
|
||||
} catch (SQLException ex) {
|
||||
|
@ -31,7 +31,6 @@ import org.junit.jupiter.api.Test;
|
||||
public class AppTest {
|
||||
@Test
|
||||
public void test() throws Exception {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
App.main(new String[]{});
|
||||
}
|
||||
}
|
||||
|
@ -46,36 +46,36 @@ public class CustomerTest {
|
||||
|
||||
@Test
|
||||
public void getAndSetId() {
|
||||
final int newId = 2;
|
||||
final var newId = 2;
|
||||
customer.setId(newId);
|
||||
assertEquals(newId, customer.getId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndSetFirstname() {
|
||||
final String newFirstname = "Bill";
|
||||
final var newFirstname = "Bill";
|
||||
customer.setFirstName(newFirstname);
|
||||
assertEquals(newFirstname, customer.getFirstName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getAndSetLastname() {
|
||||
final String newLastname = "Clinton";
|
||||
final var newLastname = "Clinton";
|
||||
customer.setLastName(newLastname);
|
||||
assertEquals(newLastname, customer.getLastName());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notEqualWithDifferentId() {
|
||||
final int newId = 2;
|
||||
final Customer otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME);
|
||||
final var newId = 2;
|
||||
final var otherCustomer = new Customer(newId, FIRSTNAME, LASTNAME);
|
||||
assertNotEquals(customer, otherCustomer);
|
||||
assertNotEquals(customer.hashCode(), otherCustomer.hashCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWithSameObjectValues() {
|
||||
final Customer otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME);
|
||||
final var otherCustomer = new Customer(ID, FIRSTNAME, LASTNAME);
|
||||
assertEquals(customer, otherCustomer);
|
||||
assertEquals(customer.hashCode(), otherCustomer.hashCode());
|
||||
}
|
||||
|
@ -23,20 +23,6 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
@ -46,6 +32,17 @@ import static org.mockito.Mockito.doReturn;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import javax.sql.DataSource;
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.mockito.Mockito;
|
||||
|
||||
/**
|
||||
* Tests {@link DbCustomerDao}.
|
||||
*/
|
||||
@ -57,12 +54,13 @@ public class DbCustomerDaoTest {
|
||||
|
||||
/**
|
||||
* Creates customers schema.
|
||||
*
|
||||
* @throws SQLException if there is any error while creating schema.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void createSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.CREATE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
@ -75,14 +73,15 @@ public class DbCustomerDaoTest {
|
||||
|
||||
/**
|
||||
* Setup for connection success scenario.
|
||||
*
|
||||
* @throws Exception if any error occurs.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() throws Exception {
|
||||
JdbcDataSource dataSource = new JdbcDataSource();
|
||||
var dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(DB_URL);
|
||||
dao = new DbCustomerDao(dataSource);
|
||||
boolean result = dao.add(existingCustomer);
|
||||
var result = dao.add(existingCustomer);
|
||||
assertTrue(result);
|
||||
}
|
||||
|
||||
@ -94,12 +93,12 @@ public class DbCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInSuccess() throws Exception {
|
||||
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||
try (var allCustomers = dao.getAll()) {
|
||||
assumeTrue(allCustomers.count() == 1);
|
||||
}
|
||||
|
||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
boolean result = dao.add(nonExistingCustomer);
|
||||
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
var result = dao.add(nonExistingCustomer);
|
||||
assertTrue(result);
|
||||
|
||||
assertCustomerCountIs(2);
|
||||
@ -108,8 +107,8 @@ public class DbCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
boolean result = dao.delete(nonExistingCustomer);
|
||||
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
var result = dao.delete(nonExistingCustomer);
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
@ -117,11 +116,11 @@ public class DbCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
final int nonExistingId = getNonExistingCustomerId();
|
||||
final String newFirstname = "Douglas";
|
||||
final String newLastname = "MacArthur";
|
||||
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
|
||||
boolean result = dao.update(customer);
|
||||
final var nonExistingId = getNonExistingCustomerId();
|
||||
final var newFirstname = "Douglas";
|
||||
final var newLastname = "MacArthur";
|
||||
final var customer = new Customer(nonExistingId, newFirstname, newLastname);
|
||||
var result = dao.update(customer);
|
||||
|
||||
assertFalse(result);
|
||||
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||
@ -136,16 +135,14 @@ public class DbCustomerDaoTest {
|
||||
/**
|
||||
* Represents a scenario where DAO operations are being performed on an already existing
|
||||
* customer.
|
||||
*
|
||||
*/
|
||||
@Nested
|
||||
public class ExistingCustomer {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
Customer existingCustomer = new Customer(1, "Freddy", "Krueger");
|
||||
|
||||
boolean result = dao.add(existingCustomer);
|
||||
var existingCustomer = new Customer(1, "Freddy", "Krueger");
|
||||
var result = dao.add(existingCustomer);
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
@ -154,7 +151,7 @@ public class DbCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
|
||||
boolean result = dao.delete(existingCustomer);
|
||||
var result = dao.delete(existingCustomer);
|
||||
|
||||
assertTrue(result);
|
||||
assertCustomerCountIs(0);
|
||||
@ -162,15 +159,16 @@ public class DbCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws Exception {
|
||||
final String newFirstname = "Bernard";
|
||||
final String newLastname = "Montgomery";
|
||||
final Customer customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
|
||||
boolean result = dao.update(customer);
|
||||
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
|
||||
Exception {
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
final var customer = new Customer(existingCustomer.getId(), newFirstname, newLastname);
|
||||
var result = dao.update(customer);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
final Customer cust = dao.getById(existingCustomer.getId()).get();
|
||||
final var cust = dao.getById(existingCustomer.getId()).get();
|
||||
assertEquals(newFirstname, cust.getFirstName());
|
||||
assertEquals(newLastname, cust.getLastName());
|
||||
}
|
||||
@ -178,28 +176,28 @@ public class DbCustomerDaoTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents a scenario where DB connectivity is not present due to network issue, or
|
||||
* DB service unavailable.
|
||||
*
|
||||
* Represents a scenario where DB connectivity is not present due to network issue, or DB service
|
||||
* unavailable.
|
||||
*/
|
||||
@Nested
|
||||
public class ConnectivityIssue {
|
||||
|
||||
|
||||
private static final String EXCEPTION_CAUSE = "Connection not available";
|
||||
|
||||
/**
|
||||
* setup a connection failure scenario.
|
||||
*
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@BeforeEach
|
||||
public void setUp() throws SQLException {
|
||||
dao = new DbCustomerDao(mockedDatasource());
|
||||
}
|
||||
|
||||
|
||||
private DataSource mockedDatasource() throws SQLException {
|
||||
DataSource mockedDataSource = mock(DataSource.class);
|
||||
Connection mockedConnection = mock(Connection.class);
|
||||
SQLException exception = new SQLException(EXCEPTION_CAUSE);
|
||||
var mockedDataSource = mock(DataSource.class);
|
||||
var mockedConnection = mock(Connection.class);
|
||||
var exception = new SQLException(EXCEPTION_CAUSE);
|
||||
doThrow(exception).when(mockedConnection).prepareStatement(Mockito.anyString());
|
||||
doReturn(mockedConnection).when(mockedDataSource).getConnection();
|
||||
return mockedDataSource;
|
||||
@ -211,30 +209,30 @@ public class DbCustomerDaoTest {
|
||||
dao.add(new Customer(2, "Bernard", "Montgomery"));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void deletingACustomerFailsWithExceptionAsFeedbackToTheClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.delete(existingCustomer);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void updatingACustomerFailsWithFeedbackToTheClient() {
|
||||
final String newFirstname = "Bernard";
|
||||
final String newLastname = "Montgomery";
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.update(new Customer(existingCustomer.getId(), newFirstname, newLastname));
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void retrievingACustomerByIdFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
dao.getById(existingCustomer.getId());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void retrievingAllCustomersFailsWithExceptionAsFeedbackToClient() {
|
||||
assertThrows(Exception.class, () -> {
|
||||
@ -246,18 +244,19 @@ public class DbCustomerDaoTest {
|
||||
|
||||
/**
|
||||
* Delete customer schema for fresh setup per test.
|
||||
*
|
||||
* @throws SQLException if any error occurs.
|
||||
*/
|
||||
@AfterEach
|
||||
public void deleteSchema() throws SQLException {
|
||||
try (Connection connection = DriverManager.getConnection(DB_URL);
|
||||
Statement statement = connection.createStatement()) {
|
||||
try (var connection = DriverManager.getConnection(DB_URL);
|
||||
var statement = connection.createStatement()) {
|
||||
statement.execute(CustomerSchemaSql.DELETE_SCHEMA_SQL);
|
||||
}
|
||||
}
|
||||
|
||||
private void assertCustomerCountIs(int count) throws Exception {
|
||||
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||
try (var allCustomers = dao.getAll()) {
|
||||
assertEquals(count, allCustomers.count());
|
||||
}
|
||||
}
|
||||
@ -265,7 +264,7 @@ public class DbCustomerDaoTest {
|
||||
|
||||
/**
|
||||
* An arbitrary number which does not correspond to an active Customer id.
|
||||
*
|
||||
*
|
||||
* @return an int of a customer id which doesn't exist
|
||||
*/
|
||||
private int getNonExistingCustomerId() {
|
||||
|
@ -23,18 +23,15 @@
|
||||
|
||||
package com.iluwatar.dao;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* Tests {@link InMemoryCustomerDao}.
|
||||
*/
|
||||
@ -50,20 +47,20 @@ public class InMemoryCustomerDaoTest {
|
||||
}
|
||||
|
||||
/**
|
||||
* Represents the scenario when the DAO operations are being performed on a non existent
|
||||
* customer.
|
||||
* Represents the scenario when the DAO operations are being performed on a non existent
|
||||
* customer.
|
||||
*/
|
||||
@Nested
|
||||
public class NonExistingCustomer {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInSuccess() throws Exception {
|
||||
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||
try (var allCustomers = dao.getAll()) {
|
||||
assumeTrue(allCustomers.count() == 1);
|
||||
}
|
||||
|
||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
boolean result = dao.add(nonExistingCustomer);
|
||||
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
var result = dao.add(nonExistingCustomer);
|
||||
assertTrue(result);
|
||||
|
||||
assertCustomerCountIs(2);
|
||||
@ -72,8 +69,8 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
final Customer nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
boolean result = dao.delete(nonExistingCustomer);
|
||||
final var nonExistingCustomer = new Customer(2, "Robert", "Englund");
|
||||
var result = dao.delete(nonExistingCustomer);
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
@ -81,11 +78,11 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void updationShouldBeFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
final int nonExistingId = getNonExistingCustomerId();
|
||||
final String newFirstname = "Douglas";
|
||||
final String newLastname = "MacArthur";
|
||||
final Customer customer = new Customer(nonExistingId, newFirstname, newLastname);
|
||||
boolean result = dao.update(customer);
|
||||
final var nonExistingId = getNonExistingCustomerId();
|
||||
final var newFirstname = "Douglas";
|
||||
final var newLastname = "MacArthur";
|
||||
final var customer = new Customer(nonExistingId, newFirstname, newLastname);
|
||||
var result = dao.update(customer);
|
||||
|
||||
assertFalse(result);
|
||||
assertFalse(dao.getById(nonExistingId).isPresent());
|
||||
@ -106,7 +103,7 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void addingShouldResultInFailureAndNotAffectExistingCustomers() throws Exception {
|
||||
boolean result = dao.add(CUSTOMER);
|
||||
var result = dao.add(CUSTOMER);
|
||||
|
||||
assertFalse(result);
|
||||
assertCustomerCountIs(1);
|
||||
@ -115,7 +112,7 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
@Test
|
||||
public void deletionShouldBeSuccessAndCustomerShouldBeNonAccessible() throws Exception {
|
||||
boolean result = dao.delete(CUSTOMER);
|
||||
var result = dao.delete(CUSTOMER);
|
||||
|
||||
assertTrue(result);
|
||||
assertCustomerCountIs(0);
|
||||
@ -123,23 +120,24 @@ public class InMemoryCustomerDaoTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws Exception {
|
||||
final String newFirstname = "Bernard";
|
||||
final String newLastname = "Montgomery";
|
||||
final Customer customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
|
||||
boolean result = dao.update(customer);
|
||||
public void updationShouldBeSuccessAndAccessingTheSameCustomerShouldReturnUpdatedInformation() throws
|
||||
Exception {
|
||||
final var newFirstname = "Bernard";
|
||||
final var newLastname = "Montgomery";
|
||||
final var customer = new Customer(CUSTOMER.getId(), newFirstname, newLastname);
|
||||
var result = dao.update(customer);
|
||||
|
||||
assertTrue(result);
|
||||
|
||||
final Customer cust = dao.getById(CUSTOMER.getId()).get();
|
||||
final var cust = dao.getById(CUSTOMER.getId()).get();
|
||||
assertEquals(newFirstname, cust.getFirstName());
|
||||
assertEquals(newLastname, cust.getLastName());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void retriveShouldReturnTheCustomer() {
|
||||
Optional<Customer> optionalCustomer = dao.getById(CUSTOMER.getId());
|
||||
|
||||
var optionalCustomer = dao.getById(CUSTOMER.getId());
|
||||
|
||||
assertTrue(optionalCustomer.isPresent());
|
||||
assertEquals(CUSTOMER, optionalCustomer.get());
|
||||
}
|
||||
@ -147,15 +145,15 @@ public class InMemoryCustomerDaoTest {
|
||||
|
||||
/**
|
||||
* An arbitrary number which does not correspond to an active Customer id.
|
||||
*
|
||||
*
|
||||
* @return an int of a customer id which doesn't exist
|
||||
*/
|
||||
private int getNonExistingCustomerId() {
|
||||
return 999;
|
||||
}
|
||||
|
||||
|
||||
private void assertCustomerCountIs(int count) throws Exception {
|
||||
try (Stream<Customer> allCustomers = dao.getAll()) {
|
||||
try (var allCustomers = dao.getAll()) {
|
||||
assertEquals(count, allCustomers.count());
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user