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_입력받은_가격으로_음식_가격을_변경시킨다 () {
75
+ // given
76
+ final ProductNew product = repository .save (Fixtures .create (5_000L ));
77
+
78
+ // when
79
+ useCase .change (product .getId (), ProductPrice .of (10_000L ));
80
+
81
+ // then
82
+ assertThat (product .getPrice ())
83
+ .isEqualTo (ProductPrice .of (10_000L ));
84
+ }
85
+
86
+ @ Test
87
+ void change_가격이_변경되면_가격변경_이벤트를_발행한다 () {
88
+ // given
89
+ final ProductNew product = repository .save (Fixtures .create (5_000L ));
90
+
91
+ // when
92
+ useCase .change (product .getId (), ProductPrice .of (10_000L ));
93
+
94
+ // then
95
+ verify (mockEventPublisher )
96
+ .publish (product .getId ());
97
+ }
98
+
99
+ @ Test
100
+ void change_예외가_발생하면_가격변경_이벤트는_발행되지_않는다 () {
101
+ // when
102
+ throwExceptionScenario ();
103
+
104
+ // then
105
+ verify (mockEventPublisher , never ())
106
+ .publish (any ());
107
+ }
108
+
109
+ private void throwExceptionScenario () {
110
+ try {
111
+ useCase .change (UUID .randomUUID (), Fixtures .VALID_PRODUCT_PRICE );
112
+ } catch (final Exception ignored ) {
113
+ }
114
+ }
115
+ }
0 commit comments