8
8
import org .springframework .boot .test .autoconfigure .jdbc .JdbcTest ;
9
9
import org .springframework .jdbc .core .JdbcTemplate ;
10
10
import org .springframework .test .annotation .DirtiesContext ;
11
- import ru .yandex .practicum .filmorate .dao .FriendshipStorage ;
12
- import ru .yandex .practicum .filmorate .dao .UserStorage ;
13
- import ru .yandex .practicum .filmorate .dao .impl .FriendshipDbStorage ;
14
- import ru .yandex .practicum .filmorate .dao .impl .UserDbStorage ;
11
+ import ru .yandex .practicum .filmorate .dao .*;
12
+ import ru .yandex .practicum .filmorate .dao .impl .*;
15
13
import ru .yandex .practicum .filmorate .exception .NotFoundException ;
14
+ import ru .yandex .practicum .filmorate .model .Film ;
16
15
import ru .yandex .practicum .filmorate .model .Friendship ;
16
+ import ru .yandex .practicum .filmorate .model .Mpa ;
17
17
import ru .yandex .practicum .filmorate .model .User ;
18
+ import ru .yandex .practicum .filmorate .service .impl .UserServiceImpl ;
18
19
19
20
import java .time .LocalDate ;
20
- import java .util .Collection ;
21
- import java .util .List ;
21
+ import java .util .*;
22
22
23
23
import static org .assertj .core .api .Assertions .assertThat ;
24
24
import static org .junit .jupiter .api .Assertions .assertEquals ;
@@ -34,16 +34,26 @@ class UserDbStorageTest {
34
34
35
35
private final JdbcTemplate jdbcTemplate ;
36
36
private UserStorage userStorage ;
37
+ private UserServiceImpl userService ;
37
38
private FriendshipStorage friendshipStorage ;
39
+ private FilmGenreStorage filmGenreStorage ;
40
+ private FilmLikeStorage filmLikeStorage ;
41
+ private FilmStorage filmDbStorage ;
38
42
private User user ;
39
43
private User updatedUser ;
40
44
private User anotherUser ;
45
+ private Film filmOne ;
46
+ private Film filmTwo ;
41
47
42
48
43
49
@ BeforeEach
44
50
void setUp () {
51
+ filmLikeStorage = new FilmLikeDbStorage (jdbcTemplate );
52
+ filmGenreStorage = new FilmGenreDbStorage (jdbcTemplate );
53
+ filmDbStorage = new FilmDbStorage (jdbcTemplate , filmGenreStorage );
45
54
userStorage = new UserDbStorage (jdbcTemplate );
46
55
friendshipStorage = new FriendshipDbStorage (jdbcTemplate );
56
+ userService = new UserServiceImpl (userStorage , filmDbStorage , friendshipStorage , filmLikeStorage );
47
57
user = User .builder ()
48
58
.id (1 )
49
59
.email ("email" )
@@ -65,6 +75,26 @@ void setUp() {
65
75
.name ("another_name" )
66
76
.birthday (LocalDate .now ())
67
77
.build ();
78
+
79
+ Mpa mpa = new Mpa (1 , "G" );
80
+
81
+ filmOne = Film .builder ()
82
+ .id (1 )
83
+ .name ("film" )
84
+ .description ("film description" )
85
+ .releaseDate (LocalDate .of (2020 , 12 , 12 ))
86
+ .duration (123 )
87
+ .mpa (mpa )
88
+ .build ();
89
+
90
+ filmTwo = Film .builder ()
91
+ .id (2 )
92
+ .name ("film two" )
93
+ .description ("film two description" )
94
+ .releaseDate (LocalDate .of (2020 , 12 , 12 ))
95
+ .duration (123 )
96
+ .mpa (mpa )
97
+ .build ();
68
98
}
69
99
70
100
@ Test
@@ -334,4 +364,44 @@ void testDeleteNotExistingUser() {
334
364
NotFoundException e = assertThrows (NotFoundException .class , () -> userStorage .remove (userId ));
335
365
assertEquals (formattedResponse , e .getMessage ());
336
366
}
367
+
368
+ @ Test
369
+ @ DisplayName ("Тест получения мапы с ключами userId и сетом с списком айдишников залайканных фильмов." )
370
+ void testGetRecommendationsList () {
371
+ userStorage .add (user );
372
+ userStorage .add (anotherUser );
373
+
374
+ filmDbStorage .add (filmOne );
375
+ filmDbStorage .add (filmTwo );
376
+
377
+ filmLikeStorage .add (filmOne .getId (), user .getId ());
378
+ filmLikeStorage .add (filmOne .getId (), anotherUser .getId ());
379
+ filmLikeStorage .add (filmTwo .getId (), anotherUser .getId ());
380
+
381
+ Map <Long , Set <Long >> filmRecommendations = filmLikeStorage .getUsersAndFilmLikes ();
382
+
383
+ assertThat (filmRecommendations .get (1L ))
384
+ .isNotNull ()
385
+ .isNotEmpty ()
386
+ .containsExactly (filmOne .getId ());
387
+
388
+ assertThat (filmRecommendations .get (2L ))
389
+ .isNotNull ()
390
+ .isNotEmpty ()
391
+ .containsExactly (filmOne .getId (), filmTwo .getId ());
392
+ }
393
+
394
+ @ Test
395
+ @ DisplayName ("Тест получения мапы с ключами userId и сетом с списком айдишников залайканных фильмов, когда лайков нет." )
396
+ void testGetRecommendationsListNoLikes () {
397
+ userStorage .add (user );
398
+ userStorage .add (anotherUser );
399
+ filmDbStorage .add (filmOne );
400
+
401
+ Map <Long , Set <Long >> filmRecommendations = filmLikeStorage .getUsersAndFilmLikes ();
402
+
403
+ assertThat (filmRecommendations )
404
+ .isNotNull ()
405
+ .isEmpty ();
406
+ }
337
407
}
0 commit comments