mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-16 14:59:21 +00:00
feature: Added Domain Model pattern (#1795)
* domain-model pattern * fixed optional get before check isPresent * readme minor changes * change currency representation with joda money * changed names of test methods * fixed code smells * Update domain-model/README.md Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com> * Update domain-model/README.md Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com> * updated readme and diagrams Co-authored-by: Ilkka Seppälä <iluwatar@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/** Tests that Domain Model example runs without errors. */
|
||||
final class AppTest {
|
||||
|
||||
@Test
|
||||
void shouldExecuteApplicationWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[] {}));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,164 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.joda.money.CurrencyUnit.USD;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class CustomerDaoImplTest {
|
||||
|
||||
public static final String INSERT_CUSTOMER_SQL = "insert into CUSTOMERS values('customer', 100)";
|
||||
public static final String SELECT_CUSTOMERS_SQL = "select name, money from CUSTOMERS";
|
||||
public static final String INSERT_PURCHASES_SQL =
|
||||
"insert into PURCHASES values('product', 'customer')";
|
||||
public static final String SELECT_PURCHASES_SQL =
|
||||
"select product_name, customer_name from PURCHASES";
|
||||
|
||||
private DataSource dataSource;
|
||||
private Product product;
|
||||
private Customer customer;
|
||||
private CustomerDao customerDao;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws SQLException {
|
||||
// create db schema
|
||||
dataSource = TestUtils.createDataSource();
|
||||
|
||||
TestUtils.deleteSchema(dataSource);
|
||||
TestUtils.createSchema(dataSource);
|
||||
|
||||
// setup objects
|
||||
customerDao = new CustomerDaoImpl(dataSource);
|
||||
|
||||
customer = Customer.builder().name("customer").money(Money.of(CurrencyUnit.USD,100.0)).customerDao(customerDao).build();
|
||||
|
||||
product =
|
||||
Product.builder()
|
||||
.name("product")
|
||||
.price(Money.of(USD, 100.0))
|
||||
.expirationDate(LocalDate.parse("2021-06-27"))
|
||||
.productDao(new ProductDaoImpl(dataSource))
|
||||
.build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws SQLException {
|
||||
TestUtils.deleteSchema(dataSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindCustomerByName() throws SQLException {
|
||||
var customer = customerDao.findByName("customer");
|
||||
|
||||
assertTrue(customer.isEmpty());
|
||||
|
||||
TestUtils.executeSQL(INSERT_CUSTOMER_SQL, dataSource);
|
||||
|
||||
customer = customerDao.findByName("customer");
|
||||
|
||||
assertTrue(customer.isPresent());
|
||||
assertEquals("customer", customer.get().getName());
|
||||
assertEquals(Money.of(USD, 100), customer.get().getMoney());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSaveCustomer() throws SQLException {
|
||||
customerDao.save(customer);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_CUSTOMERS_SQL)) {
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(customer.getName(), rs.getString("name"));
|
||||
assertEquals(customer.getMoney(), Money.of(USD, rs.getBigDecimal("money")));
|
||||
}
|
||||
|
||||
assertThrows(SQLException.class, () -> customerDao.save(customer));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUpdateCustomer() throws SQLException {
|
||||
TestUtils.executeSQL(INSERT_CUSTOMER_SQL, dataSource);
|
||||
|
||||
customer.setMoney(Money.of(CurrencyUnit.USD, 99));
|
||||
|
||||
customerDao.update(customer);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_CUSTOMERS_SQL)) {
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(customer.getName(), rs.getString("name"));
|
||||
assertEquals(customer.getMoney(), Money.of(USD, rs.getBigDecimal("money")));
|
||||
assertFalse(rs.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddProductToPurchases() throws SQLException {
|
||||
TestUtils.executeSQL(INSERT_CUSTOMER_SQL, dataSource);
|
||||
TestUtils.executeSQL(ProductDaoImplTest.INSERT_PRODUCT_SQL, dataSource);
|
||||
|
||||
customerDao.addProduct(product, customer);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_PURCHASES_SQL)) {
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(product.getName(), rs.getString("product_name"));
|
||||
assertEquals(customer.getName(), rs.getString("customer_name"));
|
||||
assertFalse(rs.next());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldDeleteProductFromPurchases() throws SQLException {
|
||||
TestUtils.executeSQL(INSERT_CUSTOMER_SQL, dataSource);
|
||||
TestUtils.executeSQL(ProductDaoImplTest.INSERT_PRODUCT_SQL, dataSource);
|
||||
TestUtils.executeSQL(INSERT_PURCHASES_SQL, dataSource);
|
||||
|
||||
customerDao.deleteProduct(product, customer);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_PURCHASES_SQL)) {
|
||||
|
||||
assertFalse(rs.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.joda.money.CurrencyUnit;
|
||||
import org.joda.money.Money;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.joda.money.CurrencyUnit.USD;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class CustomerTest {
|
||||
|
||||
private CustomerDao customerDao;
|
||||
private Customer customer;
|
||||
private Product product;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
customerDao = mock(CustomerDao.class);
|
||||
|
||||
customer = Customer.builder()
|
||||
.name("customer")
|
||||
.money(Money.of(CurrencyUnit.USD, 100.0))
|
||||
.customerDao(customerDao)
|
||||
.build();
|
||||
|
||||
product = Product.builder()
|
||||
.name("product")
|
||||
.price(Money.of(USD, 100.0))
|
||||
.expirationDate(LocalDate.now().plusDays(10))
|
||||
.productDao(mock(ProductDao.class))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSaveCustomer() throws SQLException {
|
||||
when(customerDao.findByName("customer")).thenReturn(Optional.empty());
|
||||
|
||||
customer.save();
|
||||
|
||||
verify(customerDao, times(1)).save(customer);
|
||||
|
||||
when(customerDao.findByName("customer")).thenReturn(Optional.of(customer));
|
||||
|
||||
customer.save();
|
||||
|
||||
verify(customerDao, times(1)).update(customer);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldAddProductToPurchases() {
|
||||
product.setPrice(Money.of(USD, 200.0));
|
||||
|
||||
customer.buyProduct(product);
|
||||
|
||||
assertEquals(customer.getPurchases(), new ArrayList<>());
|
||||
assertEquals(customer.getMoney(), Money.of(USD,100));
|
||||
|
||||
product.setPrice(Money.of(USD, 100.0));
|
||||
|
||||
customer.buyProduct(product);
|
||||
|
||||
assertEquals(new ArrayList<>(Arrays.asList(product)), customer.getPurchases());
|
||||
assertEquals(Money.zero(USD), customer.getMoney());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldRemoveProductFromPurchases() {
|
||||
customer.setPurchases(new ArrayList<>(Arrays.asList(product)));
|
||||
|
||||
customer.returnProduct(product);
|
||||
|
||||
assertEquals(new ArrayList<>(), customer.getPurchases());
|
||||
assertEquals(Money.of(USD, 200), customer.getMoney());
|
||||
|
||||
customer.returnProduct(product);
|
||||
|
||||
assertEquals(new ArrayList<>(), customer.getPurchases());
|
||||
assertEquals(Money.of(USD, 200), customer.getMoney());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.joda.money.Money;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
|
||||
import static org.joda.money.CurrencyUnit.USD;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class ProductDaoImplTest {
|
||||
|
||||
public static final String INSERT_PRODUCT_SQL =
|
||||
"insert into PRODUCTS values('product', 100, DATE '2021-06-27')";
|
||||
public static final String SELECT_PRODUCTS_SQL =
|
||||
"select name, price, expiration_date from PRODUCTS";
|
||||
|
||||
private DataSource dataSource;
|
||||
private ProductDao productDao;
|
||||
private Product product;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws SQLException {
|
||||
// create schema
|
||||
dataSource = TestUtils.createDataSource();
|
||||
|
||||
TestUtils.deleteSchema(dataSource);
|
||||
TestUtils.createSchema(dataSource);
|
||||
|
||||
// setup objects
|
||||
productDao = new ProductDaoImpl(dataSource);
|
||||
|
||||
product =
|
||||
Product.builder()
|
||||
.name("product")
|
||||
.price(Money.of(USD, 100.0))
|
||||
.expirationDate(LocalDate.parse("2021-06-27"))
|
||||
.productDao(productDao)
|
||||
.build();
|
||||
}
|
||||
|
||||
@AfterEach
|
||||
void tearDown() throws SQLException {
|
||||
TestUtils.deleteSchema(dataSource);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldFindProductByName() throws SQLException {
|
||||
var product = productDao.findByName("product");
|
||||
|
||||
assertTrue(product.isEmpty());
|
||||
|
||||
TestUtils.executeSQL(INSERT_PRODUCT_SQL, dataSource);
|
||||
|
||||
product = productDao.findByName("product");
|
||||
|
||||
assertTrue(product.isPresent());
|
||||
assertEquals("product", product.get().getName());
|
||||
assertEquals(Money.of(USD, 100), product.get().getPrice());
|
||||
assertEquals(LocalDate.parse("2021-06-27"), product.get().getExpirationDate());
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSaveProduct() throws SQLException {
|
||||
|
||||
productDao.save(product);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_PRODUCTS_SQL)) {
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(product.getName(), rs.getString("name"));
|
||||
assertEquals(product.getPrice(), Money.of(USD, rs.getBigDecimal("price")));
|
||||
assertEquals(product.getExpirationDate(), rs.getDate("expiration_date").toLocalDate());
|
||||
}
|
||||
|
||||
assertThrows(SQLException.class, () -> productDao.save(product));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldUpdateProduct() throws SQLException {
|
||||
TestUtils.executeSQL(INSERT_PRODUCT_SQL, dataSource);
|
||||
|
||||
product.setPrice(Money.of(USD, 99.0));
|
||||
|
||||
productDao.update(product);
|
||||
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement();
|
||||
ResultSet rs = statement.executeQuery(SELECT_PRODUCTS_SQL)) {
|
||||
|
||||
assertTrue(rs.next());
|
||||
assertEquals(product.getName(), rs.getString("name"));
|
||||
assertEquals(product.getPrice(), Money.of(USD, rs.getBigDecimal("price")));
|
||||
assertEquals(product.getExpirationDate(), rs.getDate("expiration_date").toLocalDate());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.joda.money.Money;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.sql.SQLException;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Optional;
|
||||
|
||||
import static org.joda.money.CurrencyUnit.USD;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
class ProductTest {
|
||||
|
||||
private ProductDao productDao;
|
||||
private Product product;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() {
|
||||
productDao = mock(ProductDaoImpl.class);
|
||||
|
||||
product = Product.builder()
|
||||
.name("product")
|
||||
.price(Money.of(USD, 100.0))
|
||||
.expirationDate(LocalDate.now().plusDays(10))
|
||||
.productDao(productDao)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSaveProduct() throws SQLException {
|
||||
when(productDao.findByName("product")).thenReturn(Optional.empty());
|
||||
|
||||
product.save();
|
||||
|
||||
verify(productDao, times(1)).save(product);
|
||||
|
||||
when(productDao.findByName("product")).thenReturn(Optional.of(product));
|
||||
|
||||
product.save();
|
||||
|
||||
verify(productDao, times(1)).update(product);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldGetSalePriceOfProduct() {
|
||||
assertEquals(Money.of(USD, 100), product.getSalePrice());
|
||||
|
||||
product.setExpirationDate(LocalDate.now().plusDays(2));
|
||||
|
||||
assertEquals(Money.of(USD, 80), product.getSalePrice());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2021 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.domainmodel;
|
||||
|
||||
import org.h2.jdbcx.JdbcDataSource;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.SQLException;
|
||||
|
||||
public class TestUtils {
|
||||
|
||||
public static void executeSQL( String sql, DataSource dataSource) throws SQLException {
|
||||
try (var connection = dataSource.getConnection();
|
||||
var statement = connection.createStatement()) {
|
||||
statement.executeUpdate(sql);
|
||||
}
|
||||
}
|
||||
|
||||
public static void createSchema(DataSource dataSource) throws SQLException {
|
||||
TestUtils.executeSQL(App.CREATE_SCHEMA_SQL, dataSource);
|
||||
}
|
||||
|
||||
public static void deleteSchema(DataSource dataSource) throws SQLException {
|
||||
TestUtils.executeSQL(App.DELETE_SCHEMA_SQL, dataSource);
|
||||
}
|
||||
|
||||
public static DataSource createDataSource() {
|
||||
var dataSource = new JdbcDataSource();
|
||||
dataSource.setURL(App.H2_DB_URL);
|
||||
return dataSource;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user