diff --git a/patterns/dip/pom.xml b/patterns/dip/pom.xml new file mode 100644 index 0000000000..dac3f824f2 --- /dev/null +++ b/patterns/dip/pom.xml @@ -0,0 +1,39 @@ + + + 4.0.0 + com.baeldung.dip + dip + dip + 1.0-SNAPSHOT + + + com.baeldung + patterns + 1.0.0-SNAPSHOT + .. + + + + + junit + junit + 4.12 + test + + + org.assertj + assertj-core + 3.12.1 + test + + + + + UTF-8 + 11 + 11 + + + diff --git a/patterns/dip/src/main/java/com/baeldung/dip/application/Application.java b/patterns/dip/src/main/java/com/baeldung/dip/application/Application.java new file mode 100644 index 0000000000..8cc8901f70 --- /dev/null +++ b/patterns/dip/src/main/java/com/baeldung/dip/application/Application.java @@ -0,0 +1,18 @@ +package com.baeldung.dip.application; + +import com.baeldung.dip.daoimplementations.SimpleCustomerDao; +import com.baeldung.dip.entities.Customer; +import com.baeldung.dip.services.CustomerService; +import java.util.Map; +import java.util.HashMap; + +public class Application { + + public static void main(String[] args) { + Map customers = new HashMap<>(); + customers.put(1, new Customer("John")); + customers.put(2, new Customer("Susan")); + CustomerService customerService = new CustomerService(new SimpleCustomerDao(customers)); + customerService.findAll().forEach(System.out::println); + } +} diff --git a/patterns/dip/src/main/java/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java b/patterns/dip/src/main/java/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java new file mode 100644 index 0000000000..ea2bf3f00a --- /dev/null +++ b/patterns/dip/src/main/java/com/baeldung/dip/daoimplementations/SimpleCustomerDao.java @@ -0,0 +1,28 @@ +package com.baeldung.dip.daoimplementations; + +import com.baeldung.dip.entities.Customer; +import com.baeldung.dip.daointerfaces.CustomerDao; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; + +public class SimpleCustomerDao implements CustomerDao { + + private Map customers = new HashMap<>(); + + public SimpleCustomerDao(Map customers) { + this.customers = customers; + } + + @Override + public Optional findById(int id) { + return Optional.ofNullable(customers.get(id)); + } + + @Override + public List findAll() { + return new ArrayList<>(customers.values()); + } +} diff --git a/patterns/dip/src/main/java/com/baeldung/dip/daointerfaces/CustomerDao.java b/patterns/dip/src/main/java/com/baeldung/dip/daointerfaces/CustomerDao.java new file mode 100644 index 0000000000..8ea83673b0 --- /dev/null +++ b/patterns/dip/src/main/java/com/baeldung/dip/daointerfaces/CustomerDao.java @@ -0,0 +1,13 @@ +package com.baeldung.dip.daointerfaces; + +import com.baeldung.dip.entities.Customer; +import java.util.List; +import java.util.Optional; + +public interface CustomerDao { + + Optional findById(int id); + + List findAll(); + +} diff --git a/patterns/dip/src/main/java/com/baeldung/dip/entities/Customer.java b/patterns/dip/src/main/java/com/baeldung/dip/entities/Customer.java new file mode 100644 index 0000000000..f8d5d82102 --- /dev/null +++ b/patterns/dip/src/main/java/com/baeldung/dip/entities/Customer.java @@ -0,0 +1,19 @@ +package com.baeldung.dip.entities; + +public class Customer { + + private final String name; + + public Customer(String name) { + this.name = name; + } + + public String getName() { + return name; + } + + @Override + public String toString() { + return "Customer{" + "name=" + name + '}'; + } +} diff --git a/patterns/dip/src/main/java/com/baeldung/dip/services/CustomerService.java b/patterns/dip/src/main/java/com/baeldung/dip/services/CustomerService.java new file mode 100644 index 0000000000..4566186ced --- /dev/null +++ b/patterns/dip/src/main/java/com/baeldung/dip/services/CustomerService.java @@ -0,0 +1,23 @@ +package com.baeldung.dip.services; + +import com.baeldung.dip.daointerfaces.CustomerDao; +import com.baeldung.dip.entities.Customer; +import java.util.List; +import java.util.Optional; + +public class CustomerService { + + private final CustomerDao customerDao; + + public CustomerService(CustomerDao customerDao) { + this.customerDao = customerDao; + } + + public Optional findById(int id) { + return customerDao.findById(id); + } + + public List findAll() { + return customerDao.findAll(); + } +} diff --git a/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerDaoUnitTest.java b/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerDaoUnitTest.java new file mode 100644 index 0000000000..2a03822ce2 --- /dev/null +++ b/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerDaoUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.dip.tests; + +import com.baeldung.dip.daoimplementations.SimpleCustomerDao; +import com.baeldung.dip.daointerfaces.CustomerDao; +import com.baeldung.dip.entities.Customer; +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Before; +import org.junit.Test; + +public class CustomerDaoUnitTest { + + private CustomerDao customerDao; + + @Before + public void setUpCustomerDaoInstance() { + Map customers = new HashMap<>(); + customers.put(1, new Customer("John")); + customers.put(2, new Customer("Susan")); + customerDao = new SimpleCustomerDao(customers); + } + + @Test + public void givenCustomerDaoInstance_whenCalledFindById_thenCorrect() { + assertThat(customerDao.findById(1)).isInstanceOf(Optional.class); + } + + @Test + public void givenCustomerDaoInstance_whenCalledFindAll_thenCorrect() { + assertThat(customerDao.findAll()).isInstanceOf(List.class); + } + + @Test + public void givenCustomerDaoInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() { + Map customers = new HashMap(); + customers.put(1, null); + CustomerDao customerDaoObject = new SimpleCustomerDao(customers); + + Customer customer = customerDaoObject.findById(1).orElseGet(() -> new Customer("Non-existing customer")); + + assertThat(customer.getName()).isEqualTo("Non-existing customer"); + } +} diff --git a/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerServiceUnitTest.java b/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerServiceUnitTest.java new file mode 100644 index 0000000000..5ffd6fad51 --- /dev/null +++ b/patterns/dip/src/test/java/com/baeldung/dip/tests/CustomerServiceUnitTest.java @@ -0,0 +1,46 @@ +package com.baeldung.dip.tests; + +import com.baeldung.dip.daoimplementations.SimpleCustomerDao; +import com.baeldung.dip.entities.Customer; +import com.baeldung.dip.services.CustomerService; +import java.util.Map; +import java.util.HashMap; +import java.util.List; +import java.util.Optional; +import static org.assertj.core.api.Assertions.assertThat; +import org.junit.Before; +import org.junit.Test; + +public class CustomerServiceUnitTest { + + private CustomerService customerService; + + @Before + public void setUpCustomerServiceInstance() { + Map customers = new HashMap<>(); + customers.put(1, new Customer("John")); + customers.put(2, new Customer("Susan")); + customerService = new CustomerService(new SimpleCustomerDao(customers)); + } + + @Test + public void givenCustomerServiceInstance_whenCalledFindById_thenCorrect() { + assertThat(customerService.findById(1)).isInstanceOf(Optional.class); + } + + @Test + public void givenCustomerServiceInstance_whenCalledFindAll_thenCorrect() { + assertThat(customerService.findAll()).isInstanceOf(List.class); + } + + @Test + public void givenCustomerServiceInstance_whenCalledFindByIdWithNullCustomer_thenCorrect() { + Map customers = new HashMap<>(); + customers.put(1, null); + customerService = new CustomerService(new SimpleCustomerDao(customers)); + + Customer customer = customerService.findById(1).orElseGet(() -> new Customer("Non-existing customer")); + + assertThat(customer.getName()).isEqualTo("Non-existing customer"); + } +} diff --git a/patterns/pom.xml b/patterns/pom.xml index 111e72b9ca..bf302a7a74 100644 --- a/patterns/pom.xml +++ b/patterns/pom.xml @@ -1,10 +1,10 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 patterns pom - patterns + patterns com.baeldung @@ -19,6 +19,7 @@ design-patterns design-patterns-2 solid + dip @@ -55,4 +56,4 @@ 9.4.0.v20161208 - \ No newline at end of file +