Created
April 20, 2016 07:32
-
-
Save BenoitAverty/817ab117caa767ffdffb6d88184426c8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.domain; | |
import ...; | |
@Service | |
public interface CreditCardService { | |
boolean isMaxedOut(String creditCardNumber, Double amount); | |
LocalDate expirationDate(String creditCardNumber); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.infrastructure; | |
import ...; | |
/** | |
* Implementation of the creditCardService that calls the webservice of the bank (for example). | |
* In the real world, this would contain HTTP calls, Soap, rest... | |
*/ | |
@Service | |
public class CreditCardWebservice implements CreditCardService{ | |
@Override | |
public boolean isMaxedOut(String creditCardNumber, Double amount) { | |
return false; | |
} | |
@Override | |
public LocalDate expirationDate(String creditCardNumber) { | |
return LocalDate.of(2018, 01, 01); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.domain; | |
import ...; | |
@Entity | |
public class Customer { | |
@Id | |
private Long tid; | |
private List<Order> orders = new ArrayList<>(); | |
public Long getTid() { | |
return tid; | |
} | |
public Order placeOrder(Long itemId) { | |
Order o = new Order(this, itemId); | |
this.orders.add(o); | |
return o; | |
} | |
/** Return orders that are not failed or closed */ | |
public List<Order> getProcessingOrders() { | |
return orders.stream() | |
.filter(o -> o.getStatus().equals("IN PROGRESS") || o.getStatus().equals("NEW")) | |
.collect(Collectors.toList()); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.domain; | |
import...; | |
@Entity | |
public class Order { | |
@Id | |
private Long tid ; | |
private String status; | |
private Customer customer; | |
private Long item; | |
public Order(Customer customer, Long itemId) { | |
this.customer = customer; | |
this.item = itemId; | |
this.status = "NEW"; | |
} | |
public Double calculateAmount() { | |
return (item.hashCode() % 1000) / 100.0; | |
} | |
public void processPayment(boolean paymentSuccess) { | |
this.status = (paymentSuccess) ? "IN PROGRESS" : "PAYMENT FAILED"; | |
} | |
public Long getTid() { | |
return tid; | |
} | |
public String getStatus() { | |
return status; | |
} | |
public Customer getCustomer() { | |
return customer; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.application; | |
import ...; | |
@Service | |
public class OrderApplication { | |
@Autowired | |
CustomerRepository customerRepository; | |
@Autowired | |
OrderRepository orderRepository; | |
@Autowired | |
PaymentValidationService paymentValidationService; | |
@Transactional | |
public Long placeOrder(Long itemId, Long customerId, String creditCardNumber) { | |
Customer customer = customerRepository.findOne(customerId); | |
Order newOrder = customer.placeOrder(itemId); | |
boolean paymentSuccess = paymentValidationService.verifyCreditCardPayment(creditCardNumber, newOrder.calculateAmount()); | |
newOrder.processPayment(paymentSuccess); | |
orderRepository.save(newOrder); | |
customerRepository.save(customer); | |
return newOrder.getTid(); | |
} | |
public String getOrderStatus(Long orderId) { | |
Order order = orderRepository.findOne(orderId); | |
return order.getStatus(); | |
} | |
public List<Order> findOrdersForCustomer(Long tid) { | |
return customerRepository.findOne(tid).getProcessingOrders(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.unit.testing.domain; | |
import ...; | |
@Service | |
public class PaymentValidationService { | |
@Autowired | |
CreditCardService creditCardService; | |
public boolean verifyCreditCardPayment(String creditCardNumber, Double amount) { | |
return !creditCardService.isMaxedOut(creditCardNumber, amount) | |
&& creditCardService.expirationDate(creditCardNumber).compareTo(LocalDate.now().plusMonths(3)) > 0 ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment