BAEL-2789 - The Dependency Inversion Principle in Java (#6613)
* Initial Commit * Added dip dipmodular modules to pom.xml * Delete pom.xml * Add pom.xml * Update pom.xml * Update pom.xml * Update pom.xml * Update Application.java * Update CustomerDaoUnitTest.java * Update CustomerServiceUnitTest.java * Update Application.java * Update CustomerDaoUnitTest.java * Update CustomerServiceUnitTest.java * Update CustomerDaoUnitTest.java * Update CustomerServiceUnitTest.java
This commit is contained in:
committed by
KevinGilmore
parent
86bded12be
commit
b67a006020
@@ -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<Integer, Customer> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<Integer, Customer> customers = new HashMap<>();
|
||||
|
||||
public SimpleCustomerDao(Map<Integer, Customer> customers) {
|
||||
this.customers = customers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Optional<Customer> findById(int id) {
|
||||
return Optional.ofNullable(customers.get(id));
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Customer> findAll() {
|
||||
return new ArrayList<>(customers.values());
|
||||
}
|
||||
}
|
||||
@@ -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<Customer> findById(int id);
|
||||
|
||||
List<Customer> findAll();
|
||||
|
||||
}
|
||||
@@ -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 + '}';
|
||||
}
|
||||
}
|
||||
@@ -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<Customer> findById(int id) {
|
||||
return customerDao.findById(id);
|
||||
}
|
||||
|
||||
public List<Customer> findAll() {
|
||||
return customerDao.findAll();
|
||||
}
|
||||
}
|
||||
@@ -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<Integer, Customer> 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<Integer, Customer> customers = new HashMap<Integer, Customer>();
|
||||
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");
|
||||
}
|
||||
}
|
||||
@@ -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<Integer, Customer> 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<Integer, Customer> 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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user