diff --git a/src/main/java/com/spotify/app/controller/FollowerController.java b/src/main/java/com/spotify/app/controller/FollowerController.java index b36985c..ce6db9d 100644 --- a/src/main/java/com/spotify/app/controller/FollowerController.java +++ b/src/main/java/com/spotify/app/controller/FollowerController.java @@ -55,9 +55,7 @@ public ResponseEntity checkCurrentUserFollowedTargetUser( @PathVariable("currentUserId") Long currentUserId, @PathVariable("targetUserId") Long targetUserId ) { - return ResponseEntity. - ok(). - body( + return ResponseEntity.ok().body( followerService.checkCurrentUserFollowedTargetUser(currentUserId,targetUserId) ); } diff --git a/src/test/java/com/spotify/app/album/AlbumRepositoryTest.java b/src/test/java/com/spotify/app/album/AlbumRepositoryTest.java index 3b4cd6d..f453c53 100644 --- a/src/test/java/com/spotify/app/album/AlbumRepositoryTest.java +++ b/src/test/java/com/spotify/app/album/AlbumRepositoryTest.java @@ -1,28 +1,11 @@ //package com.spotify.app.album; // // -//import com.spotify.app.SpotifyApplication; //import com.spotify.app.TestConfig; -//import com.spotify.app.enums.Genre; -//import com.spotify.app.model.Album; -//import com.spotify.app.model.Song; -//import com.spotify.app.model.User; -//import com.spotify.app.repository.AlbumRepository; -//import com.spotify.app.repository.SongRepository; -//import com.spotify.app.repository.UserRepository; -//import jakarta.transaction.Transactional; -//import lombok.AllArgsConstructor; -//import lombok.RequiredArgsConstructor; -//import org.junit.jupiter.api.Test; -//import org.springframework.beans.factory.annotation.Autowired; -//import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest; //import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; //import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -//import org.springframework.boot.test.context.SpringBootTest; //import org.springframework.context.annotation.Import; -//import org.springframework.test.annotation.Rollback; // -//import java.time.LocalDateTime; // //@DataJpaTest //@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) diff --git a/src/test/java/com/spotify/app/auth/AuthServiceTest.java b/src/test/java/com/spotify/app/auth/AuthServiceTest.java new file mode 100644 index 0000000..5a23bf9 --- /dev/null +++ b/src/test/java/com/spotify/app/auth/AuthServiceTest.java @@ -0,0 +1,4 @@ +package com.spotify.app.auth; + +public class AuthServiceTest { +} diff --git a/src/test/java/com/spotify/app/category/CategoryRepositoryTest.java b/src/test/java/com/spotify/app/category/CategoryRepositoryTest.java index e6e6866..a0c237a 100644 --- a/src/test/java/com/spotify/app/category/CategoryRepositoryTest.java +++ b/src/test/java/com/spotify/app/category/CategoryRepositoryTest.java @@ -12,4 +12,5 @@ //public class CategoryRepositoryTest extends AbstractTestcontainers { // // +// //} diff --git a/src/test/java/com/spotify/app/follower/FollowerRepositoryTest.java b/src/test/java/com/spotify/app/follower/FollowerRepositoryTest.java new file mode 100644 index 0000000..532e9c7 --- /dev/null +++ b/src/test/java/com/spotify/app/follower/FollowerRepositoryTest.java @@ -0,0 +1,13 @@ +//package com.spotify.app.follower; +// +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.TestConfig; +//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +//import org.springframework.context.annotation.Import; +// +//@DataJpaTest +//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +//@Import({TestConfig.class}) +//public class FollowerRepositoryTest extends AbstractTestcontainers { +//} diff --git a/src/test/java/com/spotify/app/journey/AlbumIT.java b/src/test/java/com/spotify/app/journey/AlbumIT.java index 1a542e4..60dad07 100644 --- a/src/test/java/com/spotify/app/journey/AlbumIT.java +++ b/src/test/java/com/spotify/app/journey/AlbumIT.java @@ -1,4 +1,64 @@ package com.spotify.app.journey; -public class AlbumIT { +import com.github.javafaker.Faker; +import com.spotify.app.AbstractTestcontainers; +import com.spotify.app.dto.AlbumDTO; +import com.spotify.app.dto.request.AlbumRequest; +import com.spotify.app.dto.response.UserNoAssociationResponse; +import com.spotify.app.security.auth.AuthenticationRequest; +import com.spotify.app.security.auth.AuthenticationResponse; +import com.spotify.app.security.jwt.JwtService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +public class AlbumIT extends AbstractTestcontainers { + + private static final String AUTH_PATH = "/api/v1/auth"; + private static final String ALBUM_PATH = "/api/v1/album"; + + @Autowired + private TestRestTemplate restTemplate; + @Autowired + private JwtService jwtService; + + @Test + public void canArtistAddAlbum() { + // given + Faker faker = new Faker(); + String email = "taylor@gmail.com"; + String password = "thuan2023"; + AuthenticationRequest authRequest = new AuthenticationRequest(email,password); + AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); + log.info(String.valueOf(authResponse)); + String token = authResponse.getAccessToken(); + + + Long userId = authResponse.getUser().id(); + + AlbumRequest albumRequest = new AlbumRequest(faker.funnyName().name()); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); + httpHeaders.set("Authorization", "Bearer " + token); + + HttpEntity request = new HttpEntity<>(albumRequest, httpHeaders); + + String url = String.format(ALBUM_PATH.concat("/%d/add"),userId); + log.info(url); + + // when + ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST ,request, AlbumDTO.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + } } diff --git a/src/test/java/com/spotify/app/journey/CategoryIT.java b/src/test/java/com/spotify/app/journey/CategoryIT.java index 858be4e..390f8a7 100644 --- a/src/test/java/com/spotify/app/journey/CategoryIT.java +++ b/src/test/java/com/spotify/app/journey/CategoryIT.java @@ -1,4 +1,40 @@ package com.spotify.app.journey; -public class CategoryIT { +import com.spotify.app.AbstractTestcontainers; +import com.spotify.app.dto.CategoryDTO; +import com.spotify.app.dto.response.UserNoAssociationResponse; +import com.spotify.app.security.jwt.JwtService; +import lombok.extern.slf4j.Slf4j; +import org.assertj.core.api.AssertionsForClassTypes; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.net.http.HttpResponse; +import java.util.Set; + +import static org.assertj.core.api.FactoryBasedNavigableListAssert.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +public class CategoryIT extends AbstractTestcontainers { + + private static final String CATEGORY_PATH = "/api/v1/category"; + @Autowired + private TestRestTemplate restTemplate; + + @Test + public void canGetAllCategoryParent() { + + // given + String url = CATEGORY_PATH.concat("/getAllParent"); + // when + ResponseEntity response = restTemplate.getForEntity(url, Set.class); + // then + AssertionsForClassTypes.assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + } } diff --git a/src/test/java/com/spotify/app/journey/FollowerIT.java b/src/test/java/com/spotify/app/journey/FollowerIT.java index 68119c6..331c55e 100644 --- a/src/test/java/com/spotify/app/journey/FollowerIT.java +++ b/src/test/java/com/spotify/app/journey/FollowerIT.java @@ -1,4 +1,63 @@ package com.spotify.app.journey; -public class FollowerIT { +import com.spotify.app.AbstractTestcontainers; +import com.spotify.app.dto.response.UserNoAssociationResponse; +import com.spotify.app.security.auth.AuthenticationRequest; +import com.spotify.app.security.auth.AuthenticationResponse; +import com.spotify.app.security.jwt.JwtService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +public class FollowerIT extends AbstractTestcontainers { + + private static final String AUTH_PATH = "/api/v1/auth"; + private static final String FOLLOWER_PATH = "/api/v1/follower"; + + @Autowired + private TestRestTemplate restTemplate; + @Autowired + private JwtService jwtService; + + + @Test + public void canFollowUser() { + // given + Long currentUserId ; + Long targetUserId = 1L; + + + String email = "taylor@gmail.com"; + String password = "thuan2023"; + AuthenticationRequest authRequest = new AuthenticationRequest(email,password); + AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); + log.info(String.valueOf(authResponse)); + String token = authResponse.getAccessToken(); + + currentUserId = authResponse.getUser().id(); + + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); + httpHeaders.set("Authorization", "Bearer " + token); + + HttpEntity request = new HttpEntity<>(httpHeaders); + + String url = String.format(FOLLOWER_PATH.concat("/%d/follow/%d"),currentUserId,targetUserId); + + log.info(url); + // when + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET ,request, UserNoAssociationResponse[].class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + } + } diff --git a/src/test/java/com/spotify/app/journey/PlaylistIT.java b/src/test/java/com/spotify/app/journey/PlaylistIT.java index 272600c..5c3169d 100644 --- a/src/test/java/com/spotify/app/journey/PlaylistIT.java +++ b/src/test/java/com/spotify/app/journey/PlaylistIT.java @@ -1,4 +1,61 @@ package com.spotify.app.journey; -public class PlaylistIT { +import com.github.javafaker.Faker; +import com.spotify.app.AbstractTestcontainers; +import com.spotify.app.dto.response.SongResponse; +import com.spotify.app.security.auth.AuthenticationRequest; +import com.spotify.app.security.auth.AuthenticationResponse; +import com.spotify.app.security.jwt.JwtService; +import com.spotify.app.service.PlaylistUserService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +public class PlaylistIT extends AbstractTestcontainers { + + private static final String AUTH_PATH = "/api/v1/auth"; + private static final String REVIEW_PATH = "/api/v1/playlist"; + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private PlaylistUserService playlistUserService; + + @Autowired + private JwtService jwtService; + + @Test + public void canUserCreatePlaylist() { + String email = "taylor@gmail.com"; + String password = "thuan2023"; + AuthenticationRequest authRequest = new AuthenticationRequest(email,password); + AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); + log.info(String.valueOf(authResponse)); + String token = authResponse.getAccessToken(); + + Long userId = authResponse.getUser().id(); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); + httpHeaders.set("Authorization", "Bearer " + token); + + HttpEntity request = new HttpEntity<>(httpHeaders); + + String url = String.format(REVIEW_PATH.concat("/%d/create/playlist"),userId); + + log.info(url); + // when + ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET ,request, String.class); + + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + + } } diff --git a/src/test/java/com/spotify/app/journey/ReviewIT.java b/src/test/java/com/spotify/app/journey/ReviewIT.java new file mode 100644 index 0000000..033d693 --- /dev/null +++ b/src/test/java/com/spotify/app/journey/ReviewIT.java @@ -0,0 +1,66 @@ +package com.spotify.app.journey; + +import com.github.javafaker.Faker; +import com.spotify.app.AbstractTestcontainers; +import com.spotify.app.dto.ReviewDTO; +import com.spotify.app.dto.response.ReviewResponse; +import com.spotify.app.dto.response.SongResponse; +import com.spotify.app.security.auth.AuthenticationRequest; +import com.spotify.app.security.auth.AuthenticationResponse; +import com.spotify.app.security.jwt.JwtService; +import lombok.extern.slf4j.Slf4j; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.web.client.TestRestTemplate; +import org.springframework.http.*; + +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@Slf4j +public class ReviewIT extends AbstractTestcontainers { + + private static final String AUTH_PATH = "/api/v1/auth"; + private static final String REVIEW_PATH = "/api/v1/review"; + @Autowired + private TestRestTemplate restTemplate; + + @Autowired + private JwtService jwtService; + + @Test + public void canReviewInSong() { + + // given + Faker faker = new Faker(); + String email = "taylor@gmail.com"; + String password = "thuan2023"; + AuthenticationRequest authRequest = new AuthenticationRequest(email,password); + AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); + log.info(String.valueOf(authResponse)); + String token = authResponse.getAccessToken(); + + Long userId = authResponse.getUser().id(); + Long songId = 1L; + String reviewContent = faker.animal().name(); + + ReviewDTO reviewRequest = new ReviewDTO(0L,reviewContent); + HttpHeaders httpHeaders = new HttpHeaders(); + httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); + httpHeaders.set("Authorization", "Bearer " + token); + + HttpEntity request = new HttpEntity<>(reviewRequest,httpHeaders); + + String urlRequest = String.format(REVIEW_PATH.concat("/%d/review/in/%d"),userId,songId); + + log.info(urlRequest); + // when + ResponseEntity response = restTemplate.exchange(urlRequest, HttpMethod.POST ,request, ReviewResponse[].class); + // then + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + + } +} diff --git a/src/test/java/com/spotify/app/journey/SongIT.java b/src/test/java/com/spotify/app/journey/SongIT.java index ef9cca7..d6f97c3 100644 --- a/src/test/java/com/spotify/app/journey/SongIT.java +++ b/src/test/java/com/spotify/app/journey/SongIT.java @@ -25,7 +25,6 @@ @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @Slf4j -@Transactional public class SongIT extends AbstractTestcontainers { private static final String SONG_PATH = "/api/v1/song"; private static final String AUTH_PATH = "/api/v1/auth"; diff --git a/src/test/java/com/spotify/app/playlist/PlaylistRepositoryTest.java b/src/test/java/com/spotify/app/playlist/PlaylistRepositoryTest.java index 7d85eb3..1d6916f 100644 --- a/src/test/java/com/spotify/app/playlist/PlaylistRepositoryTest.java +++ b/src/test/java/com/spotify/app/playlist/PlaylistRepositoryTest.java @@ -1,33 +1,39 @@ //package com.spotify.app.playlist; // // +//import com.spotify.app.AbstractTestcontainers; //import com.spotify.app.TestConfig; -//import com.spotify.app.enums.Genre; -//import com.spotify.app.model.Album; -//import com.spotify.app.model.Category; -//import com.spotify.app.model.Playlist; -//import com.spotify.app.model.Song; -//import com.spotify.app.repository.AlbumRepository; -//import com.spotify.app.repository.CategoryRepository; //import com.spotify.app.repository.PlaylistRepository; -//import com.spotify.app.repository.SongRepository; -//import jakarta.persistence.EntityManager; -//import jakarta.transaction.Transactional; -//import org.junit.jupiter.api.DisplayName; +// //import org.junit.jupiter.api.Test; //import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; //import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; //import org.springframework.context.annotation.Import; -//import org.springframework.test.annotation.Rollback; // -//import java.time.LocalDateTime; -//import java.util.List; +// +//import static org.assertj.core.api.AssertionsForClassTypes.assertThat; // //@DataJpaTest //@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) //@Import({TestConfig.class}) -//public class PlaylistRepositoryTest { +//public class PlaylistRepositoryTest extends AbstractTestcontainers { +// +// +// @Autowired +// private PlaylistRepository underTest ; +// +// @Test +// public void shouldReturnCreatedUser_WhenFindByIdReturnPlaylistUsers() { +// // given +// Long playlistId = 1L; +// +// // when +// var playlist = underTest.findByIdReturnPlaylistUsers(playlistId); // +// // then +// assertThat(playlist).isPresent(); +// assertThat(playlist.get().getPlaylistUserList().size()).isGreaterThan(0); +// } // //} diff --git a/src/test/java/com/spotify/app/song/SongRepositoryTest.java b/src/test/java/com/spotify/app/song/SongRepositoryTest.java index 894e17f..953e2d8 100644 --- a/src/test/java/com/spotify/app/song/SongRepositoryTest.java +++ b/src/test/java/com/spotify/app/song/SongRepositoryTest.java @@ -1,76 +1,76 @@ -package com.spotify.app.song; - -import com.spotify.app.AbstractTestcontainers; -import com.spotify.app.TestConfig; -import com.spotify.app.enums.Gender; -import com.spotify.app.enums.Genre; -import com.spotify.app.model.Album; -import com.spotify.app.model.Song; -import com.spotify.app.model.User; -import com.spotify.app.repository.SongRepository; -import com.spotify.app.repository.UserRepository; -import lombok.extern.slf4j.Slf4j; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.context.annotation.Import; - -import java.util.Optional; - -import static org.hamcrest.MatcherAssert.assertThat; - -@DataJpaTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) -@Import({TestConfig.class}) -@Slf4j -public class SongRepositoryTest extends AbstractTestcontainers { - - - @Autowired - private SongRepository underTest ; - - -// @BeforeEach -// public void setUp() { -// underTest.deleteAll(); -// } - - - @Test - public void canFindByIdReturnUserAlbumReview () { - // given - Long songId = 1L; - - // when - var actual = underTest.findByIdReturnUsersAlbumsReviews(songId); - log.info(String.valueOf(actual)); - // then - assert (actual.isPresent()); - } - - - @Test - public void canUserAddSong() { +//package com.spotify.app.song; +// +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.TestConfig; +//import com.spotify.app.enums.Gender; +//import com.spotify.app.enums.Genre; +//import com.spotify.app.model.Album; +//import com.spotify.app.model.Song; +//import com.spotify.app.model.User; +//import com.spotify.app.repository.SongRepository; +//import com.spotify.app.repository.UserRepository; +//import lombok.extern.slf4j.Slf4j; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +//import org.springframework.context.annotation.Import; +// +//import java.util.Optional; +// +//import static org.hamcrest.MatcherAssert.assertThat; +// +//@DataJpaTest +//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +//@Import({TestConfig.class}) +//@Slf4j +//public class SongRepositoryTest extends AbstractTestcontainers { +// +// +// @Autowired +// private SongRepository underTest ; +// +// +//// @BeforeEach +//// public void setUp() { +//// underTest.deleteAll(); +//// } +// +// +// @Test +// public void canFindByIdReturnUserAlbumReview () { // // given -// User artist1 = User.builder().firstName("f").lastName("s").email("aa").gender(Gender.MALE).build(); +// Long songId = 1L; // -// Song expected = Song.builder() -// .id(100L) -// .name("song1") -// .genre(Genre.POP) -// .lyric("lyric") -// .build(); -// Album album = Album.builder().user(artist1).name("album1").id(100L).build(); -// album.addSong(expected); -// artist1.addSong(expected); -// userRepository.saveAndFlush(artist1); // // when -// Optional actual = underTest.findByIdReturnUsersAlbums(100L); -// +// var actual = underTest.findByIdReturnUsersAlbumsReviews(songId); +// log.info(String.valueOf(actual)); // // then // assert (actual.isPresent()); - - } -} +// } +// +// +// @Test +// public void canUserAddSong() { +//// // given +//// User artist1 = User.builder().firstName("f").lastName("s").email("aa").gender(Gender.MALE).build(); +//// +//// Song expected = Song.builder() +//// .id(100L) +//// .name("song1") +//// .genre(Genre.POP) +//// .lyric("lyric") +//// .build(); +//// Album album = Album.builder().user(artist1).name("album1").id(100L).build(); +//// album.addSong(expected); +//// artist1.addSong(expected); +//// userRepository.saveAndFlush(artist1); +//// // when +//// Optional actual = underTest.findByIdReturnUsersAlbums(100L); +//// +//// // then +//// assert (actual.isPresent()); +// +// } +//} diff --git a/src/test/java/com/spotify/app/user/UserRepositoryTest.java b/src/test/java/com/spotify/app/user/UserRepositoryTest.java index ec19513..8abaf4e 100644 --- a/src/test/java/com/spotify/app/user/UserRepositoryTest.java +++ b/src/test/java/com/spotify/app/user/UserRepositoryTest.java @@ -1,152 +1,152 @@ -package com.spotify.app.user; - -import com.spotify.app.AbstractTestcontainers; -import com.spotify.app.TestConfig; -import com.spotify.app.enums.Genre; -import com.spotify.app.model.Role; -import com.spotify.app.model.Song; -import com.spotify.app.model.User; -import com.spotify.app.repository.RoleRepository; -import com.spotify.app.repository.UserRepository; -import jakarta.transaction.Transactional; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; -import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; -import org.springframework.context.annotation.Import; -import java.time.LocalDateTime; -import static org.assertj.core.api.Assertions.*; -import static org.junit.jupiter.api.Assertions.assertAll; -import static org.mockito.ArgumentMatchers.any; -import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; - -@DataJpaTest -@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) -@Import({TestConfig.class}) -public class UserRepositoryTest { - - @Autowired - private UserRepository underTest ; - @Autowired - private RoleRepository roleRepository; - - @Test - @DisplayName("Test create user") - public void canCreateUser() { - // given - String email = "thuan@gmail.com"; - User user = User - .builder() - .firstName("firstName") - .lastName("lastName") - .email("thuan@gmail.com") - .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) - .build(); - underTest.saveAndFlush(user); - - // when - var actual = underTest.findByEmail(email); - - // then - assertThat(actual).isNotNull(); - } - - - - @Test - @DisplayName("Test find user by id") - public void existUserById() { - // given - String email = "thuan@gmail.com"; - User user = User - .builder() - .firstName("firstName") - .lastName("lastName") - .email(email) - .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) - .build(); - underTest.saveAndFlush(user); - - long id = underTest. - findAll(). - stream(). - filter(u-> u.getEmail().equals(email)). - map(User::getId). - findFirst(). - orElseThrow(); - - // when - var actual = underTest.findById(id); - - // then - assertThat(actual.isPresent()); - } - - - @Test - @DisplayName("Test find user by keyword") - public void existUser__whenFindByKeyword() { - // given - String keyword = "a"; - String email = "thuan@gmail.com"; - User user = User - .builder() - .firstName("abc") - .lastName("def") - .email(email) - .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) - .build(); - underTest.saveAndFlush(user); - - - // when - var actual = underTest.findAllWithKeyword(keyword,any()); - - // then - assertThat(actual.getSize() > 0); - } - - @Test - @DisplayName("Test find user by id") - public void whenFindUserById_thenReturnSongAndRoleNotNull() { - // given - Role role = roleRepository.save(Role.builder().name("role_user").build()); - String songName = "name_song"; - Song song = Song.builder().name(songName).genre(Genre.CLASSICAL).build(); - String email = "thuan@gmail.com"; - User user = User - .builder() - .firstName("firstName") - .lastName("lastName") - .email(email) - .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) - .role(role) - .build(); - user.addSong(song); - - underTest.saveAndFlush(user); - - // when - var actual = underTest.findByEmail(email); - - // then - assertAll( - () -> assertThat(actual.isPresent()), - () -> assertEquals(role.getName(), actual.get().getRole().getName(),true), - () -> assertThat(actual.get().getSongs().size() > 0) - ); - } - - @Test - public void exitUserByIdFailWhenIdNotExit() { - // given - long userid = 0L; - - //when - var actual = underTest.findById(userid); - - // then - assertThat(actual).isNotPresent(); - } -} +//package com.spotify.app.user; +// +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.TestConfig; +//import com.spotify.app.enums.Genre; +//import com.spotify.app.model.Role; +//import com.spotify.app.model.Song; +//import com.spotify.app.model.User; +//import com.spotify.app.repository.RoleRepository; +//import com.spotify.app.repository.UserRepository; +//import jakarta.transaction.Transactional; +//import org.junit.jupiter.api.DisplayName; +//import org.junit.jupiter.api.Test; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; +//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +//import org.springframework.context.annotation.Import; +//import java.time.LocalDateTime; +//import static org.assertj.core.api.Assertions.*; +//import static org.junit.jupiter.api.Assertions.assertAll; +//import static org.mockito.ArgumentMatchers.any; +//import static org.skyscreamer.jsonassert.JSONAssert.assertEquals; +// +//@DataJpaTest +//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +//@Import({TestConfig.class}) +//public class UserRepositoryTest extends AbstractTestcontainers { +// +// @Autowired +// private UserRepository underTest ; +// @Autowired +// private RoleRepository roleRepository; +// +// @Test +// @DisplayName("Test create user") +// public void canCreateUser() { +// // given +// String email = "thuan@gmail.com"; +// User user = User +// .builder() +// .firstName("firstName") +// .lastName("lastName") +// .email("thuan@gmail.com") +// .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) +// .build(); +// underTest.saveAndFlush(user); +// +// // when +// var actual = underTest.findByEmail(email); +// +// // then +// assertThat(actual).isNotNull(); +// } +// +// +// +// @Test +// @DisplayName("Test find user by id") +// public void existUserById() { +// // given +// String email = "thuan@gmail.com"; +// User user = User +// .builder() +// .firstName("firstName") +// .lastName("lastName") +// .email(email) +// .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) +// .build(); +// underTest.saveAndFlush(user); +// +// long id = underTest. +// findAll(). +// stream(). +// filter(u-> u.getEmail().equals(email)). +// map(User::getId). +// findFirst(). +// orElseThrow(); +// +// // when +// var actual = underTest.findById(id); +// +// // then +// assertThat(actual.isPresent()); +// } +// +// +// @Test +// @DisplayName("Test find user by keyword") +// public void existUser__whenFindByKeyword() { +// // given +// String keyword = "a"; +// String email = "thuan@gmail.com"; +// User user = User +// .builder() +// .firstName("abc") +// .lastName("def") +// .email(email) +// .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) +// .build(); +// underTest.saveAndFlush(user); +// +// +// // when +// var actual = underTest.findAllWithKeyword(keyword,any()); +// +// // then +// assertThat(actual.getSize() > 0); +// } +// +// @Test +// @DisplayName("Test find user by id") +// public void whenFindUserById_thenReturnSongAndRoleNotNull() { +// // given +// Role role = roleRepository.save(Role.builder().name("role_user").build()); +// String songName = "name_song"; +// Song song = Song.builder().name(songName).genre(Genre.CLASSICAL).build(); +// String email = "thuan@gmail.com"; +// User user = User +// .builder() +// .firstName("firstName") +// .lastName("lastName") +// .email(email) +// .dateOfBrith(LocalDateTime.of(2002,7,30,0,0)) +// .role(role) +// .build(); +// user.addSong(song); +// +// underTest.saveAndFlush(user); +// +// // when +// var actual = underTest.findByEmail(email); +// +// // then +// assertAll( +// () -> assertThat(actual.isPresent()), +// () -> assertEquals(role.getName(), actual.get().getRole().getName(),true), +// () -> assertThat(actual.get().getSongs().size() > 0) +// ); +// } +// +// @Test +// public void exitUserByIdFailWhenIdNotExit() { +// // given +// long userid = 0L; +// +// //when +// var actual = underTest.findById(userid); +// +// // then +// assertThat(actual).isNotPresent(); +// } +//}