1+ package com .kt .service .wishlist ;
2+
3+ import static org .assertj .core .api .Assertions .*;
4+
5+ import java .time .LocalDate ;
6+ import java .util .List ;
7+
8+ import org .junit .jupiter .api .BeforeEach ;
9+ import org .junit .jupiter .api .DisplayName ;
10+ import org .junit .jupiter .api .Test ;
11+ import org .springframework .beans .factory .annotation .Autowired ;
12+ import org .springframework .boot .test .context .SpringBootTest ;
13+ import org .springframework .context .ApplicationEventPublisher ;
14+ import org .springframework .security .crypto .password .PasswordEncoder ;
15+ import org .springframework .test .context .ActiveProfiles ;
16+ import org .springframework .transaction .annotation .Transactional ;
17+
18+ import com .kt .common .exception .CustomException ;
19+ import com .kt .common .exception .ErrorCode ;
20+ import com .kt .domain .category .Category ;
21+ import com .kt .domain .membership .Membership ;
22+ import com .kt .domain .product .Product ;
23+ import com .kt .domain .user .Gender ;
24+ import com .kt .domain .user .User ;
25+ import com .kt .dto .wishlist .WishlistResponse ;
26+ import com .kt .repository .category .CategoryRepository ;
27+ import com .kt .repository .membership .MembershipRepository ;
28+ import com .kt .repository .product .ProductRepository ;
29+ import com .kt .repository .user .UserRepository ;
30+ import com .kt .repository .wishlist .WishlistRepository ;
31+
32+ @ Transactional
33+ @ ActiveProfiles ("test" )
34+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
35+ public class WishlistServiceTest {
36+
37+ @ Autowired
38+ private WishlistService wishlistService ;
39+
40+ @ Autowired
41+ private WishlistRepository wishlistRepository ;
42+
43+ @ Autowired
44+ private UserRepository userRepository ;
45+
46+ @ Autowired
47+ private ProductRepository productRepository ;
48+
49+ @ Autowired
50+ private MembershipRepository membershipRepository ;
51+
52+ @ Autowired
53+ private CategoryRepository categoryRepository ;
54+
55+ @ Autowired
56+ private PasswordEncoder passwordEncoder ;
57+
58+ private User savedUser ;
59+ private Product savedProduct ;
60+
61+ @ BeforeEach
62+ void setUp () {
63+ initWishlistTestData ();
64+ }
65+
66+ void initWishlistTestData () {
67+ Membership membership = new Membership ("BRONZE" );
68+ membershipRepository .save (membership );
69+
70+ savedUser = userRepository .saveAndFlush (User .normalUser (
71+ "wishlist_user" ,
72+ passwordEncoder .encode ("password1234" ),
73+ "μ°μ μ " ,
74+ "wishlist@kttech.com" ,
75+ "010-1234-5678" ,
76+ Gender .FEMALE ,
77+ LocalDate .of (1999 , 1 , 10 ),
78+ membership
79+ ));
80+
81+ Category category = categoryRepository .saveAndFlush (new Category ("μλ₯" ));
82+
83+ savedProduct = productRepository .saveAndFlush (
84+ new Product ("μ€ννμ΄νΈ ν°μ
μΈ " , "μ€ννμ΄νΈ λ‘κ³ ν°μ
μΈ " , 120000L , 100L , category )
85+ );
86+ }
87+
88+ @ Test
89+ @ DisplayName ("μ° μΆκ° μ±κ³΅" )
90+ public void μ°_μΆκ°_μ±κ³΅ () {
91+ // when
92+ Long wishlistId = wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
93+
94+ // then
95+ assertThat (wishlistId ).isNotNull ();
96+
97+ var wishlists = wishlistRepository .findAll ();
98+ assertThat (wishlists ).hasSize (1 );
99+
100+ var savedWishlist = wishlists .getFirst ();
101+ assertThat (savedWishlist .getUser ().getId ()).isEqualTo (savedUser .getId ());
102+ assertThat (savedWishlist .getProduct ().getId ()).isEqualTo (savedProduct .getId ());
103+ }
104+
105+ @ Test
106+ @ DisplayName ("μ° μΆκ° μ€ν¨ - μ‘΄μ¬νμ§ μλ μ¬μ©μ" )
107+ public void μ°_μΆκ°_μ€ν¨_μ‘΄μ¬νμ§μλμ¬μ©μ () {
108+ // given
109+ Long nonExistUserId = 9999L ;
110+
111+ // when & then
112+ assertThatThrownBy (() -> wishlistService .addWishlist (nonExistUserId , savedProduct .getId ()))
113+ .isInstanceOf (CustomException .class )
114+ .hasMessageContaining (ErrorCode .NOT_FOUND_USER .getMessage ());
115+ }
116+
117+ @ Test
118+ @ DisplayName ("μ° μΆκ° μ€ν¨ - μ‘΄μ¬νμ§ μλ μν" )
119+ public void μ°_μΆκ°_μ€ν¨_μ‘΄μ¬νμ§μλμν () {
120+ // given
121+ Long nonExistProductId = 9999L ;
122+
123+ // when & then
124+ assertThatThrownBy (() -> wishlistService .addWishlist (savedUser .getId (), nonExistProductId ))
125+ .isInstanceOf (CustomException .class )
126+ .hasMessageContaining (ErrorCode .NOT_FOUND_PRODUCT .getMessage ());
127+ }
128+
129+ @ Test
130+ @ DisplayName ("μ° μΆκ° μ€ν¨ - μ΄λ―Έ μ°ν μν" )
131+ public void μ°_μΆκ°_μ€ν¨_μ΄λ―Έμ°νμν () {
132+ // given
133+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
134+
135+ // when & then
136+ assertThatThrownBy (() -> wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ()))
137+ .isInstanceOf (CustomException .class )
138+ .hasMessageContaining (ErrorCode .ALREADY_WISHLISTED .getMessage ());
139+ }
140+
141+ @ Test
142+ @ DisplayName ("μ° μμ μ±κ³΅" )
143+ public void μ°_μμ _μ±κ³΅ () {
144+ // given
145+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
146+
147+ // when
148+ wishlistService .removeWishlist (savedUser .getId (), savedProduct .getId ());
149+
150+ // then
151+ boolean exists = wishlistRepository .existsByUserIdAndProductId (savedUser .getId (), savedProduct .getId ());
152+ assertThat (exists ).isFalse ();
153+ }
154+
155+ @ Test
156+ @ DisplayName ("μ° μμ μ€ν¨ - μ‘΄μ¬νμ§ μλ μ°" )
157+ public void μ°_μμ _μ€ν¨_μ‘΄μ¬νμ§μλμ° () {
158+ // when & then
159+ assertThatThrownBy (() -> wishlistService .removeWishlist (savedUser .getId (), savedProduct .getId ()))
160+ .isInstanceOf (CustomException .class )
161+ .hasMessageContaining (ErrorCode .NOT_FOUND_WISHLIST .getMessage ());
162+ }
163+
164+ @ Test
165+ @ DisplayName ("μ 체 μ° μμ μ±κ³΅" )
166+ public void μ 체_μ°_μμ _μ±κ³΅ () {
167+ // given
168+ Category category2 = categoryRepository .saveAndFlush (new Category ("μλ₯" ));
169+ Product product2 = productRepository .saveAndFlush (
170+ new Product ("λμ΄ν€ μ λ°" , "μ΄λν" , 150000L , 30L , category2 )
171+ );
172+
173+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
174+ wishlistService .addWishlist (savedUser .getId (), product2 .getId ());
175+
176+ // when
177+ wishlistService .removeAllWishlist (savedUser .getId ());
178+
179+ // then
180+ List <WishlistResponse > userWishlists = wishlistService .getWishlists (savedUser .getId ());
181+ assertThat (userWishlists ).isEmpty ();
182+ }
183+
184+ @ Test
185+ @ DisplayName ("μ° λͺ©λ‘ μ‘°ν μ±κ³΅" )
186+ public void μ°_λͺ©λ‘_μ‘°ν_μ±κ³΅ () {
187+ // given
188+ Category category2 = categoryRepository .saveAndFlush (new Category ("μλ₯" ));
189+ Product product2 = productRepository .saveAndFlush (
190+ new Product ("μ€ννμ΄νΈ νλν°" , "μ€ννμ΄νΈ νλ μ€μ»μ
μΈ " , 350000L , 40L , category2 )
191+ );
192+ Product product3 = productRepository .saveAndFlush (
193+ new Product ("μ€ννμ΄νΈ μμΌ" , "μ€ννμ΄νΈ μμ°ν° μμΌ" , 890000L , 15L , category2 )
194+ );
195+
196+
197+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
198+ wishlistService .addWishlist (savedUser .getId (), product2 .getId ());
199+ wishlistService .addWishlist (savedUser .getId (), product3 .getId ());
200+
201+ // when
202+ List <WishlistResponse > wishlists = wishlistService .getWishlists (savedUser .getId ());
203+
204+ // then
205+ assertThat (wishlists ).hasSize (3 );
206+
207+ var productIds = wishlists .stream ()
208+ .map (wishlistResponse -> wishlistResponse .productId ())
209+ .toList ();
210+
211+ assertThat (productIds ).containsExactlyInAnyOrder (
212+ savedProduct .getId (),
213+ product2 .getId (),
214+ product3 .getId ()
215+ );
216+
217+ var productNames = wishlists .stream ()
218+ .map (wishlistResponse -> wishlistResponse .productName ())
219+ .toList ();
220+
221+ assertThat (productNames ).containsExactlyInAnyOrder (
222+ "μ€ννμ΄νΈ ν°μ
μΈ " ,
223+ "μ€ννμ΄νΈ νλν°" ,
224+ "μ€ννμ΄νΈ μμΌ"
225+ );
226+
227+ }
228+
229+ @ Test
230+ @ DisplayName ("μ° λͺ©λ‘ μ‘°ν μ±κ³΅ - λΉ λͺ©λ‘" )
231+ public void μ°_λͺ©λ‘_μ‘°ν_μ±κ³΅_λΉλͺ©λ‘ () {
232+ // when
233+ List <WishlistResponse > wishlists = wishlistService .getWishlists (savedUser .getId ());
234+
235+ // then
236+ assertThat (wishlists ).isEmpty ();
237+ }
238+
239+ @ Test
240+ @ DisplayName ("λ€λ₯Έ μ¬μ©μμ μ°μ μ‘°νλμ§ μμ" )
241+ public void λ€λ₯Έμ¬μ©μ_μ°_μ‘°νμλ¨ () {
242+ // given
243+ User anotherUser = userRepository .saveAndFlush (User .normalUser (
244+ "another_user" ,
245+ passwordEncoder .encode ("password1234" ),
246+ "λ€λ₯Έμ μ " ,
247+ "another@kttech.com" ,
248+ "010-9999-9999" ,
249+ Gender .MALE ,
250+ LocalDate .of (1995 , 5 , 15 ),
251+ savedUser .getMembership ()
252+ ));
253+
254+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
255+ wishlistService .addWishlist (anotherUser .getId (), savedProduct .getId ());
256+
257+ // when
258+ List <WishlistResponse > user1Wishlists = wishlistService .getWishlists (savedUser .getId ());
259+ List <WishlistResponse > user2Wishlists = wishlistService .getWishlists (anotherUser .getId ());
260+
261+ // then
262+ assertThat (user1Wishlists ).hasSize (1 );
263+ assertThat (user2Wishlists ).hasSize (1 );
264+
265+ // κ°μ μμ μ μ°λ§ μ‘°νλ¨
266+ assertThat (user1Wishlists .get (0 ).productId ()).isEqualTo (savedProduct .getId ());
267+ assertThat (user2Wishlists .get (0 ).productId ()).isEqualTo (savedProduct .getId ());
268+ }
269+
270+ @ Test
271+ @ DisplayName ("μν μμ ν μ° λͺ©λ‘μμ μ‘°ν μλ¨" )
272+ public void μνμμ ν_μ°λͺ©λ‘_μ‘°νμλ¨ () {
273+ // given
274+ wishlistService .addWishlist (savedUser .getId (), savedProduct .getId ());
275+
276+ // μνμ λΉνμ±ν (μ€μ λ‘λ μννΈ μμ λ μ μμ)
277+ savedProduct .updateInActive ();
278+ productRepository .saveAndFlush (savedProduct );
279+
280+ // when
281+ List <WishlistResponse > wishlists = wishlistService .getWishlists (savedUser .getId ());
282+
283+ // then
284+ // μνμ΄ λΉνμ±νλμ΄λ μ° λͺ©λ‘μλ μ¬μ ν λ¨μμμ΄μΌ ν¨ (λΉμ¦λμ€ λ‘μ§μ λ°λΌ)
285+ // λ§μ½ λΉνμ±νλ μνμ μ° λͺ©λ‘μμ μ μΈν΄μΌ νλ€λ©΄ μλΉμ€ λ‘μ§ μμ νμ
286+ assertThat (wishlists ).hasSize (1 );
287+ }
288+ }
0 commit comments