Skip to content

Commit cb139c2

Browse files
committed
Added reasonable parameters to PaymentService.
1 parent 36627b5 commit cb139c2

File tree

4 files changed

+59
-14
lines changed
  • com.github.groovyosgi.testing.paymentservice/src/main/java/com/github/groovyosgi/testing/paymentservice
  • com.github.groovyosgi.testing.pizzaservice/src/main/java/com/github/groovyosgi/testing/pizzaservice/model
  • com.github.groovyosgi.testing.pizzaservice.impl/src/main/java/com/github/groovyosgi/testing/pizzaservice/impl
  • com.github.groovyosgi.testing.pizzaservice.test/src/main/groovy/com/github/groovyosgi/testing/pizzaservice/test

4 files changed

+59
-14
lines changed

com.github.groovyosgi.testing.paymentservice/src/main/java/com/github/groovyosgi/testing/paymentservice/CreditCardPaymentService.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
public interface CreditCardPaymentService {
44

5-
void handleTransaction();
5+
void handleTransaction(String companyId, long creditCardNumber, String cardHolderName, float price);
66

77
}

com.github.groovyosgi.testing.pizzaservice.impl/src/main/java/com/github/groovyosgi/testing/pizzaservice/impl/PizzaServiceImpl.java

+48-6
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,70 @@
99
import com.github.groovyosgi.testing.paymentservice.CreditCardPaymentService;
1010
import com.github.groovyosgi.testing.pizzaservice.PizzaBaker;
1111
import com.github.groovyosgi.testing.pizzaservice.PizzaService;
12+
import com.github.groovyosgi.testing.pizzaservice.model.CustomerInfo;
1213
import com.github.groovyosgi.testing.pizzaservice.model.Order;
1314
import com.github.groovyosgi.testing.pizzaservice.model.Pizza;
1415

1516
public class PizzaServiceImpl implements PizzaService {
1617

18+
private static final String PAYMENT_SERVICE_COMPANY_ID = "LUIGIS_PIZZA_SERVICE";
1719
private static final String TAG = PizzaServiceImpl.class.getName();
1820
private CreditCardPaymentService creditCardPaymentService;
1921
private EventAdmin eventAdmin;
22+
private int orderNumber = 0;
2023
private PizzaBaker pizzaBaker;
2124

2225
@Override
2326
public void placeOrder(final Order order) {
24-
creditCardPaymentService.handleTransaction();
25-
Pizza pizza = pizzaBaker.bake(order.getPizza());
27+
28+
CustomerInfo customerInfo = order.getCustomerInfo();
29+
Pizza orderedPizza = order.getPizza();
30+
31+
float price = calculatePrice(orderedPizza);
32+
33+
creditCardPaymentService.handleTransaction(PAYMENT_SERVICE_COMPANY_ID, customerInfo.getCreditCardNumber(),
34+
customerInfo.getName(), price);
35+
36+
Pizza pizza = pizzaBaker.bake(orderedPizza);
37+
2638
sendDeliveryEvent(order, pizza);
2739
}
2840

41+
private float calculatePrice(Pizza orderedPizza) {
42+
43+
float price = 3;
44+
45+
if (orderedPizza.isBacon()) {
46+
price += 1;
47+
}
48+
if (orderedPizza.isCheese()) {
49+
price += 0.5;
50+
}
51+
52+
switch (orderedPizza.getSauce()) {
53+
case BBQ:
54+
price += 2;
55+
break;
56+
case HOLLANDAISE:
57+
price += 1.5;
58+
break;
59+
case TOMATO:
60+
price += 1;
61+
break;
62+
default:
63+
break;
64+
}
65+
66+
return price;
67+
}
68+
2969
private void sendDeliveryEvent(final Order order, Pizza pizza) {
70+
3071
HashMap<String, Object> properties = new HashMap<>();
72+
properties.put(EVENT_PROPERTY_ID, orderNumber++);
3173
properties.put(EVENT_PROPERTY_PIZZA, pizza);
3274
properties.put(EVENT_PROPERTY_CUSTOMER_INFO, order.getCustomerInfo());
75+
3376
eventAdmin.postEvent(new Event(EVENT_TOPIC_PIZZA_DELIVERY, properties));
3477
}
3578

@@ -41,10 +84,10 @@ void deactivate(final ComponentContext componentContext) {
4184
System.out.println(TAG + " stoped!");
4285
}
4386

44-
void setCreditCardPaymentService(
45-
final CreditCardPaymentService creditCardPaymentService) {
87+
void setCreditCardPaymentService(final CreditCardPaymentService creditCardPaymentService) {
4688
this.creditCardPaymentService = creditCardPaymentService;
4789
}
90+
4891
void setEventAdmin(EventAdmin eventAdmin) {
4992
this.eventAdmin = eventAdmin;
5093
}
@@ -53,8 +96,7 @@ void setPizzaBaker(PizzaBaker pizzaBaker) {
5396
this.pizzaBaker = pizzaBaker;
5497
}
5598

56-
void unsetCreditCardPaymentService(
57-
final CreditCardPaymentService creditCardPaymentService) {
99+
void unsetCreditCardPaymentService(final CreditCardPaymentService creditCardPaymentService) {
58100
this.creditCardPaymentService = null;
59101
}
60102

com.github.groovyosgi.testing.pizzaservice.test/src/main/groovy/com/github/groovyosgi/testing/pizzaservice/test/PizzaServiceTest.groovy

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,16 @@ import com.github.groovyosgi.testing.pizzaservice.model.Pizza.Sauce
2020
class PizzaServiceTest extends OSGiTest{
2121

2222
@Test
23-
public void test() {
23+
public void assertHandleTransactionIsCalled() {
2424

2525
def transactionCalled = false
2626

2727
def paymentService = [
28-
handleTransaction: {
29-
println "handleTransaction called"
28+
handleTransaction: { String companyId, long creditCardNumber, String cardHolderName, float price ->
29+
assertThat companyId, is(equalTo('LUIGIS_PIZZA_SERVICE'))
30+
assertThat creditCardNumber, is(524030324560)
31+
assertThat cardHolderName, is(equalTo("Max Mustermann"))
32+
assertThat price, is(5f)
3033
transactionCalled = true
3134
}
3235
] as CreditCardPaymentService
@@ -35,7 +38,7 @@ class PizzaServiceTest extends OSGiTest{
3538

3639
PizzaService pizzaService = getService(PizzaService)
3740
def pizza = PizzaBuilder.newPizza().withSauce(Sauce.BBQ).build()
38-
def customerInfo = new CustomerInfo("Max Mustermann", new Address(), 524030324560 as short)
41+
def customerInfo = new CustomerInfo("Max Mustermann", new Address(), 524030324560)
3942
pizzaService.placeOrder(new Order(pizza, customerInfo))
4043

4144
assertThat transactionCalled, is(true)

com.github.groovyosgi.testing.pizzaservice/src/main/java/com/github/groovyosgi/testing/pizzaservice/model/CustomerInfo.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
public class CustomerInfo {
44

55
private final Address address;
6-
private final short creditCardNumber;
6+
private final long creditCardNumber;
77
private final String name;
88

9-
public CustomerInfo(String name, Address address, short creditCardNumber) {
9+
public CustomerInfo(String name, Address address, long creditCardNumber) {
1010
this.name = name;
1111
this.address = address;
1212
this.creditCardNumber = creditCardNumber;
@@ -16,7 +16,7 @@ public Address getAddress() {
1616
return address;
1717
}
1818

19-
public short getCreditCardNumber() {
19+
public long getCreditCardNumber() {
2020
return creditCardNumber;
2121
}
2222

0 commit comments

Comments
 (0)