1
+ package kitchenpos .product .application ;
2
+
3
+ import static org .assertj .core .api .Assertions .assertThat ;
4
+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
5
+ import static org .mockito .ArgumentMatchers .any ;
6
+ import static org .mockito .Mockito .never ;
7
+ import static org .mockito .Mockito .verify ;
8
+
9
+ import java .util .UUID ;
10
+ import kitchenpos .product .Fixtures ;
11
+ import kitchenpos .product .application .exception .NotExistProductException ;
12
+ import kitchenpos .product .application .port .in .ProductPriceChangeUseCase ;
13
+ import kitchenpos .product .application .port .out .ProductNewRepository ;
14
+ import kitchenpos .product .application .port .out .ProductPriceChangeEventPublisher ;
15
+ import kitchenpos .product .domain .ProductNew ;
16
+ import kitchenpos .product .domain .ProductPrice ;
17
+ import kitchenpos .product .fakerepository .ProductNewFakeRepository ;
18
+ import org .junit .jupiter .api .BeforeEach ;
19
+ import org .junit .jupiter .api .DisplayNameGeneration ;
20
+ import org .junit .jupiter .api .DisplayNameGenerator .ReplaceUnderscores ;
21
+ import org .junit .jupiter .api .Test ;
22
+ import org .junit .jupiter .api .extension .ExtendWith ;
23
+ import org .junit .jupiter .params .ParameterizedTest ;
24
+ import org .junit .jupiter .params .provider .NullSource ;
25
+ import org .mockito .Mock ;
26
+ import org .mockito .junit .jupiter .MockitoExtension ;
27
+
28
+ @ DisplayNameGeneration (ReplaceUnderscores .class )
29
+ @ ExtendWith (MockitoExtension .class )
30
+ class DefaultProductPriceChangeUseCaseTest {
31
+
32
+ @ Mock
33
+ private ProductPriceChangeEventPublisher mockEventPublisher ;
34
+
35
+ private ProductNewRepository repository ;
36
+
37
+ private ProductPriceChangeUseCase useCase ;
38
+
39
+ @ BeforeEach
40
+ void setUp () {
41
+ repository = new ProductNewFakeRepository ();
42
+
43
+ useCase = new DefaultProductPriceChangeUseCase (repository , mockEventPublisher );
44
+ }
45
+
46
+
47
+ @ ParameterizedTest
48
+ @ NullSource
49
+ void change_invalid_parameters_id가_null이면_예외를_발생시킨다 (final UUID value ) {
50
+
51
+ // when & then
52
+ assertThatThrownBy (() -> useCase .change (value , Fixtures .VALID_PRODUCT_PRICE ))
53
+ .isExactlyInstanceOf (IllegalArgumentException .class );
54
+ }
55
+
56
+ @ ParameterizedTest
57
+ @ NullSource
58
+ void change_invalid_parameters_price가_null이면_예외를_발생시킨다 (final ProductPrice value ) {
59
+
60
+ // when & then
61
+ assertThatThrownBy (() -> useCase .change (UUID .randomUUID (), value ))
62
+ .isExactlyInstanceOf (IllegalArgumentException .class );
63
+ }
64
+
65
+ @ Test
66
+ void change_id에_해당하는_음식이_없으면_예외를_발생시킨다 () {
67
+
68
+ // when & then
69
+ assertThatThrownBy (() -> useCase .change (UUID .randomUUID (), Fixtures .VALID_PRODUCT_PRICE ))
70
+ .isExactlyInstanceOf (NotExistProductException .class );
71
+ }
72
+
73
+ @ Test
74
+ void change_id에_해당하는_음식이_없으면_가격변경_이벤트를_발행하지_않는다 () {
75
+ // given
76
+
77
+ // when
78
+ throwExceptionScenario ();
79
+
80
+ // then
81
+ verify (mockEventPublisher , never ())
82
+ .publish (any ());
83
+ }
84
+
85
+ private void throwExceptionScenario () {
86
+ try {
87
+ useCase .change (UUID .randomUUID (), Fixtures .VALID_PRODUCT_PRICE );
88
+ } catch (final Exception ignored ) {
89
+ }
90
+ }
91
+
92
+ @ Test
93
+ void change_입력받은_가격으로_음식_가격을_변경시킨다 () {
94
+ // given
95
+ final ProductNew product = repository .save (Fixtures .create (5_000L ));
96
+
97
+ // when
98
+ useCase .change (product .getId (), ProductPrice .of (10_000L ));
99
+
100
+ // then
101
+ assertThat (product .getPrice ())
102
+ .isEqualTo (ProductPrice .of (10_000L ));
103
+ }
104
+
105
+ @ Test
106
+ void change_가격이_변경되면_가격변경_이벤트를_발행한다 () {
107
+ // given
108
+ final ProductNew product = repository .save (Fixtures .create (5_000L ));
109
+
110
+ // when
111
+ useCase .change (product .getId (), ProductPrice .of (10_000L ));
112
+ // then
113
+ // verify
114
+ verify (mockEventPublisher )
115
+ .publish (product .getId ());
116
+ }
117
+ }
0 commit comments