Skip to content

Commit 075f731

Browse files
Fixing some issues with the OrderService test class.
1 parent ac1dfb6 commit 075f731

File tree

2 files changed

+93
-107
lines changed

2 files changed

+93
-107
lines changed

src/main/java/coreapi/Cafeteria.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public Cafeteria()
1515
{
1616
productStock = new LinkedHashMap<Product, Integer>();
1717
ordFact = new OrderFactory();
18+
orderHistory = new ArrayList<Order>();
1819
}
1920

2021
public void ProductRegister(Product prod, int q)

src/test/java/coreapiTest/OrderServiceTest.java

Lines changed: 92 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,40 @@
1010
import org.junit.Before;
1111
import org.junit.Test;
1212

13-
import org.junit.Assert.*;
1413

1514
public class OrderServiceTest
1615
{
1716

1817
private Cafeteria coffe;
19-
private OrderImpl ord1;
20-
private OrderImpl ord2;
21-
private OrderImpl ord3;
18+
private OrderImpl myOrder;
2219
private OrderService ordSer;
2320
private Date date;
24-
private Product Product1;
25-
private Product Product2;
26-
private Product Product3;
21+
private Product product1 = ProductCatalog.Instance().getProduct(0);
22+
private Product product2 = ProductCatalog.Instance().getProduct(1);
23+
private Product product3 = ProductCatalog.Instance().getProduct(2);
2724

2825

2926
@Before
3027
public void setUp()
3128
{
3229
coffe = new Cafeteria();
3330
date = new Date(System.currentTimeMillis());
34-
ord1 = (OrderImpl) OrderFactory.createOrder(date);
35-
ord2 = (OrderImpl) OrderFactory.createOrder(date);
36-
ord3 = (OrderImpl) OrderFactory.createOrder(date);
31+
myOrder = (OrderImpl) OrderFactory.createOrder(date);
3732
ordSer = new OrderService();
33+
coffe.orderHistory.add(myOrder);
3834

39-
// We introduce products from the catalog to the cafeteria with a certain stock
40-
Product1 = ProductCatalog.Instance().getProduct(0);
41-
Product2 = ProductCatalog.Instance().getProduct(1);
42-
Product3 = ProductCatalog.Instance().getProduct(2);
43-
coffe.ProductRegister(Product1, 10);
44-
coffe.ProductRegister(Product2, 8);
45-
coffe.ProductRegister(Product3, 30);
35+
// We introduce the products to the cafeteria with a certain stock
36+
coffe.ProductRegister(product1, 8);
37+
coffe.ProductRegister(product2, 35);
38+
coffe.ProductRegister(product3, 30);
4639
}
4740

4841
@After
4942
public void tearDown()
5043
{
44+
coffe.orderHistory.remove(myOrder);
5145
coffe = null;
52-
ord1 = null;
46+
myOrder = null;
5347
}
5448

5549
/*---------------------------ORDER_PRODUCTS_CHECK------------------------------------------*/
@@ -65,12 +59,13 @@ public void addProductToOrder_Correctly_OrderService()
6559
{
6660
try
6761
{
68-
ordSer.addProductToOrder(coffe,ord1,2,30);
62+
ordSer.addProductToOrder(coffe, myOrder, 1, 30);
6963
}
7064
catch(Exception e)
7165
{
72-
Assert.assertEquals(e.getMessage(),null);
66+
System.err.println(e.getMessage());
7367
}
68+
Assert.assertEquals(30, myOrder.checkProductQuantity(1));
7469
}
7570

7671
@Test
@@ -79,15 +74,10 @@ public void addProductToOrder_Correctly_OrderService()
7974
*/
8075
public void addProductToOrder_NoStock_OrderService()
8176
{
82-
try
83-
{
84-
85-
ordSer.addProductToOrder(coffe,ord1,1,10);
86-
}
87-
catch(Exception e)
88-
{
89-
Assert.assertEquals(e.getMessage(),"There is not enough stock of the product.");
90-
}
77+
Assert.assertThrows(InsufficientStockException.class, () -> {
78+
ordSer.addProductToOrder(coffe, myOrder, 0, 10);
79+
});
80+
Assert.assertFalse(myOrder.containsProduct(0));
9181
}
9282

9383
@Test
@@ -96,14 +86,17 @@ public void addProductToOrder_NoStock_OrderService()
9686
*/
9787
public void addProductToOrder_ProductInBasket_OrderService()
9888
{
99-
try
100-
{
101-
ordSer.addProductToOrder(coffe,ord1,2,1);
89+
try {
90+
ordSer.addProductToOrder(coffe, myOrder, 1, 1);
10291
}
103-
catch(Exception e)
92+
catch(Exception e)
10493
{
105-
Assert.assertEquals(e.getMessage(),"This product is already in your basket, you can modify the quantity of it if you wish.");
106-
}
94+
System.err.println(e.getMessage());
95+
}
96+
97+
Assert.assertThrows(ProductAlreadyInOrderException.class, () -> {
98+
ordSer.addProductToOrder(coffe, myOrder, 1, 1);
99+
});
107100
}
108101

109102
/*_____________________________MODIFY_PRODUCT_QUANTITY_CHECK__________________________________*/
@@ -117,12 +110,14 @@ public void modifyProductQuantity_Correctly_OrderService()
117110
{
118111
try
119112
{
120-
ordSer.modifyProductQuantity(coffe, ord1, 2, 10);
113+
ordSer.addProductToOrder(coffe, myOrder, 2, 1);
114+
ordSer.modifyProductQuantity(coffe, myOrder, 2, 10);
121115
}
122116
catch(Exception e)
123117
{
124-
Assert.assertEquals(e.getMessage(),null);
118+
System.err.println(e.getMessage());
125119
}
120+
Assert.assertEquals(11, myOrder.checkProductQuantity(2));
126121
}
127122

128123
@Test
@@ -131,14 +126,17 @@ public void modifyProductQuantity_Correctly_OrderService()
131126
*/
132127
public void modifyProductQuantity_NoStock_OrderService()
133128
{
134-
try
135-
{
136-
ordSer.modifyProductQuantity(coffe, ord1, 2, 50);
129+
try {
130+
ordSer.addProductToOrder(coffe, myOrder, 2, 1);
137131
}
138-
catch(Exception e)
132+
catch(Exception e)
139133
{
140-
Assert.assertEquals(e.getMessage(),"There is not enough stock of the product.");
134+
System.err.println(e.getMessage());
141135
}
136+
Assert.assertThrows(InsufficientStockException.class, () -> {
137+
ordSer.modifyProductQuantity(coffe, myOrder, 2, 50);
138+
});
139+
Assert.assertEquals(1, myOrder.checkProductQuantity(2));;
142140
}
143141

144142
@Test
@@ -147,14 +145,9 @@ public void modifyProductQuantity_NoStock_OrderService()
147145
*/
148146
public void modifyProductQuantity_NotInBasket_OrderService()
149147
{
150-
try
151-
{
152-
ordSer.modifyProductQuantity(coffe, ord1, 0, 4);
153-
}
154-
catch(Exception e)
155-
{
156-
Assert.assertEquals(e.getMessage(),"The product is not in your basket.");
157-
}
148+
Assert.assertThrows(ProductNotContainedInOrderException.class, () -> {
149+
ordSer.modifyProductQuantity(coffe, myOrder, 0, 4);
150+
});
158151
}
159152

160153
/*_____________________________REMOVE_PRODUCT_FROM_ORDER_CHECK__________________________________*/
@@ -168,13 +161,15 @@ public void removeProductFromOrder_Correctly_OrderService()
168161
{
169162
try
170163
{
171-
ordSer.removeProductFromOrder(ord1, 2, 10);
164+
ordSer.addProductToOrder(coffe, myOrder, 2, 15);
165+
ordSer.removeProductFromOrder(myOrder, 2, 10);
172166
}
173167
catch(Exception e)
174168
{
175-
Assert.assertEquals(e.getMessage(),null);
169+
System.err.println(e.getMessage());
176170
}
177-
171+
Assert.assertEquals("The incorrect amount of product was removed from the order.", 5,
172+
myOrder.checkProductQuantity(2));
178173
}
179174

180175
@Test
@@ -184,15 +179,17 @@ public void removeProductFromOrder_Correctly_OrderService()
184179
*/
185180
public void removeProductFromOrder_NotEnoughQuantity_OrderService()
186181
{
187-
try
188-
{
189-
ordSer.removeProductFromOrder(ord1, 2, 100);
182+
try {
183+
ordSer.addProductToOrder(coffe, myOrder, 2, 1);
190184
}
191185
catch(Exception e)
192186
{
193-
Assert.assertEquals(e.getMessage(),"Can't remove that amount of product.");
187+
System.err.println(e.getMessage());
194188
}
195-
189+
Assert.assertThrows(InsufficientStockException.class, () -> {
190+
ordSer.removeProductFromOrder(myOrder, 2, 100);
191+
});
192+
Assert.assertEquals(1, myOrder.checkProductQuantity(2));
196193
}
197194

198195
@Test
@@ -202,81 +199,56 @@ public void removeProductFromOrder_NotEnoughQuantity_OrderService()
202199
*/
203200
public void removeProductFromOrder_NotInBasket_OrderService()
204201
{
205-
try
206-
{
207-
ordSer.removeProductFromOrder(ord1, 0, 4);
208-
}
209-
catch(Exception e)
210-
{
211-
Assert.assertEquals(e.getMessage(), "This object is not in your basket.");
212-
}
202+
Assert.assertThrows(ProductNotContainedInOrderException.class, () -> {
203+
ordSer.removeProductFromOrder(myOrder, 0, 4);
204+
});
213205

214206
}
215207

216208
/* ---------------------------------ORDER_STATUS_CHECK----------------------------------*/
217209

218210
@Test
219211
/*
220-
* Check that the Order Status has been changed to the one indicated.
212+
* Check that the Order Status can't be changed to the one indicated.
221213
*/
222-
public void OrderStatus_InKitchenCheck_OrderService()
214+
public void checkInvalidSetOrderStatus_InKitchen()
223215
{
224-
try
225-
{
226-
ordSer.OrderStatus_InKitchen(ord1);
227-
}
228-
catch(Exception e)
229-
{
230-
Assert.assertEquals("Incorrect Order Status",ord1.getStatus(),"IN_KITCHEN");
231-
}
216+
Assert.assertThrows(UnreachableStatusException.class, () -> {
217+
ordSer.OrderStatus_InKitchen(myOrder);
218+
});
232219
}
233220

234221
@Test
235222
/*
236-
* Check that the Order Status has been changed to the one indicated.
223+
* Check that the Order Status can't be changed to the one indicated.
237224
*/
238-
public void OrderStatus_DeliveredCheck_OrderService()
225+
public void checkInvalidSetOrderStatus_Delivered()
239226
{
240-
try
241-
{
242-
ordSer.OrderStatus_Delivered(ord1);
243-
}
244-
catch(Exception e)
245-
{
246-
Assert.assertEquals("Incorrect Order Status",ord1.getStatus(),"DELIVERED");
247-
}
227+
Assert.assertThrows(UnreachableStatusException.class, () -> {
228+
ordSer.OrderStatus_Delivered(myOrder);
229+
});
248230
}
249231

250232
@Test
251233
/*
252-
* Check that the Order Status has been changed to the one indicated.
234+
* Check that the Order Status can't be changed to the one indicated.
253235
*/
254-
public void OrderStatus_PayedCheck_OrderService()
236+
public void checkInvalidSetOrderStatus_Payed()
255237
{
256-
try
257-
{
258-
ordSer.OrderStatus_Payed(ord1);
259-
}
260-
catch(Exception e)
261-
{
262-
Assert.assertEquals("Incorrect Order Status",ord1.getStatus(),"PAYED");
263-
}
238+
Assert.assertThrows(UnreachableStatusException.class, () -> {
239+
ordSer.OrderStatus_Payed(myOrder);
240+
});
264241
}
265242

266243
@Test
267244
/*
268-
*Check that the Order Status has been changed to the one indicated.
245+
* Check that the Order Status can't be changed to the one indicated.
269246
*/
270-
public void OrderStatus_FinishedCheck_OrderService()
247+
public void checkInvalidSetOrderStatus_Finished()
271248
{
272-
try
273-
{
274-
ordSer.OrderStatus_Finished(ord1);
275-
}
276-
catch(Exception e)
277-
{
278-
Assert.assertEquals("Incorrect Order Status",ord1.getStatus(),"FINISHED");
279-
}
249+
Assert.assertThrows(UnreachableStatusException.class, () -> {
250+
ordSer.OrderStatus_Finished(myOrder);
251+
});
280252
}
281253

282254

@@ -288,8 +260,21 @@ public void OrderStatus_FinishedCheck_OrderService()
288260
*/
289261
public void DailyRegisterCheck_OrderService()
290262
{
263+
OrderImpl auxOrder1 = (OrderImpl) OrderFactory.createOrder(date);
264+
OrderImpl auxOrder2 = (OrderImpl) OrderFactory.createOrder(date);
265+
coffe.orderHistory.add(auxOrder1);
266+
coffe.orderHistory.add(auxOrder2);
267+
268+
try {
269+
ordSer.addProductToOrder(coffe, myOrder, 0, 3);
270+
ordSer.addProductToOrder(coffe, myOrder, 1, 1);
271+
ordSer.addProductToOrder(coffe, auxOrder1, 1, 4);
272+
ordSer.addProductToOrder(coffe, auxOrder2, 2, 8);
273+
}
274+
catch (Exception e) { }
275+
291276
BigDecimal expectedRegister = BigDecimal.ZERO;
292-
expectedRegister = expectedRegister.add(ord1.totalCost()).add(ord2.totalCost()).add(ord3.totalCost());
277+
expectedRegister = expectedRegister.add(myOrder.totalCost()).add(auxOrder1.totalCost()).add(auxOrder2.totalCost());
293278

294279
Assert.assertEquals(0, expectedRegister.compareTo(ordSer.getDailyRegister(coffe, date)));
295280

0 commit comments

Comments
 (0)