forked from BNYMellon/CodeKatas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDonutShopTest.java
145 lines (128 loc) · 5.55 KB
/
DonutShopTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
* Copyright 2024 The Bank of New York Mellon.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package bny.codekatas.donutkata;
import java.time.Clock;
import java.time.Instant;
import java.time.LocalDate;
import java.time.ZoneOffset;
import org.eclipse.collections.impl.factory.Lists;
import org.eclipse.collections.impl.test.Verify;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.eclipse.collections.impl.tuple.primitive.PrimitiveTuples.pair;
public class DonutShopTest
{
private final Clock clock = Clock.fixed(Instant.now(), ZoneOffset.UTC);
private final LocalDate today = LocalDate.now(this.clock);
private final LocalDate tomorrow = this.today.plusDays(1);
private final LocalDate yesterday = this.today.minusDays(1);
private DonutShop donutShop;
@BeforeEach
public void setup()
{
this.donutShop = new DonutShop();
this.donutShop.makeDonuts(DonutType.BOSTON_CREAM, 10);
this.donutShop.makeDonuts(DonutType.BAVARIAN_CREAM, 10);
this.donutShop.makeDonuts(DonutType.BLUEBERRY, 10);
this.donutShop.makeDonuts(DonutType.GLAZED, 10);
this.donutShop.makeDonuts(DonutType.OLD_FASHIONED, 10);
this.donutShop.makeDonuts(DonutType.PUMPKIN, 10);
this.donutShop.makeDonuts(DonutType.JELLY, 10);
this.donutShop.makeDonuts(DonutType.VANILLA_FROSTED, 10);
var delivery1 =
this.donutShop.deliverOrder("Ted Smith", this.today, "BC:2,BA:1,B:2");
Assertions.assertEquals(6.75d, delivery1.getTotalPrice(), 0.001);
var delivery2 =
this.donutShop.deliverOrder("Mary Williams", this.today, "BC:1,G:1");
Assertions.assertEquals(2.70d, delivery2.getTotalPrice(), 0.001);
var delivery3 =
this.donutShop.deliverOrder("Sally Prince", this.tomorrow, "BC:6,P:2,B:2,OF:2");
Assertions.assertEquals(12.0d, delivery3.getTotalPrice(), 0.001);
var delivery4 =
this.donutShop.deliverOrder("Donnie Dapper", this.yesterday, "BC:6,P:2,B:2,OF:2,G:10");
Assertions.assertEquals(20.9d, delivery4.getTotalPrice(), 0.001);
System.out.println(this.donutShop);
}
@AfterEach
public void tearDown()
{
this.donutShop = null;
}
@Test
public void getTop2Donuts()
{
var expected = Lists.mutable.empty()
.with(pair(DonutType.BOSTON_CREAM, 15))
.with(pair(DonutType.GLAZED, 11));
Assertions.assertEquals(expected, this.donutShop.getTopDonuts(2));
}
@Test
public void totalDeliveryValueByDate()
{
Assertions.assertEquals(
9.45d,
this.donutShop.getTotalDeliveryValueFor(this.today),
0.001);
Assertions.assertEquals(
12.0d,
this.donutShop.getTotalDeliveryValueFor(this.tomorrow),
0.001);
Assertions.assertEquals(
20.9d,
this.donutShop.getTotalDeliveryValueFor(this.yesterday),
0.001);
}
@Test
public void getTopCustomer()
{
Assertions.assertEquals("Donnie Dapper", this.donutShop.getTopCustomer().name());
}
@Test
public void getCustomersByDonutTypesOrdered()
{
// Do you know the type of multimap?
var multimap = this.donutShop.getCustomersByDonutTypesOrdered();
Assertions.assertEquals(6, multimap.keySet().size());
Verify.assertIterableSize(1, multimap.get(DonutType.BAVARIAN_CREAM));
Verify.assertAllSatisfy(
multimap.get(DonutType.BAVARIAN_CREAM),
customer -> customer.named("Ted Smith"));
}
@Test
public void getDonutPriceStatistics()
{
var stats1 = this.donutShop.getDonutPriceStatistics(this.today, this.today);
Assertions.assertEquals(9.45d, stats1.getSum(), 0.01);
Assertions.assertEquals(1.35d, stats1.getAverage(), 0.01);
Assertions.assertEquals(7, stats1.getCount(), 0.01);
var stats2 = this.donutShop.getDonutPriceStatistics(this.tomorrow, this.tomorrow);
Assertions.assertEquals(12.0d, stats2.getSum(), 0.01);
Assertions.assertEquals(1.0d, stats2.getAverage(), 0.01);
Assertions.assertEquals(12, stats2.getCount(), 0.01);
var stats3 = this.donutShop.getDonutPriceStatistics(this.yesterday, this.yesterday);
Assertions.assertEquals(20.9d, stats3.getSum(), 0.001);
Assertions.assertEquals(0.95d, stats3.getAverage(), 0.01);
Assertions.assertEquals(22, stats3.getCount(), 0.01);
var statsTotal = this.donutShop.getDonutPriceStatistics(this.yesterday, this.tomorrow);
Assertions.assertEquals(42.35d, statsTotal.getSum(), 0.01);
Assertions.assertEquals(1.03d, statsTotal.getAverage(), 0.01);
Assertions.assertEquals(41, statsTotal.getCount(), 0.01);
Assertions.assertEquals(0.95, statsTotal.getMin(), 0.01);
Assertions.assertEquals(1.35, statsTotal.getMax(), 0.01);
}
}