From 04a21a42f8e80ae83f847d96a7d254df913fdf7f Mon Sep 17 00:00:00 2001 From: thuan Date: Mon, 13 Nov 2023 22:19:19 +0700 Subject: [PATCH] 13/11: testing --- .../spotify/app/AbstractTestcontainers.java | 114 +++--- .../java/com/spotify/app/journey/AlbumIT.java | 128 +++---- .../spotify/app/journey/AuthenticationIT.java | 222 ++++++------ .../com/spotify/app/journey/CategoryIT.java | 80 ++--- .../com/spotify/app/journey/FollowerIT.java | 126 +++---- .../com/spotify/app/journey/PlaylistIT.java | 122 +++---- .../com/spotify/app/journey/ReviewIT.java | 132 +++---- .../java/com/spotify/app/journey/SongIT.java | 330 +++++++++--------- .../java/com/spotify/app/journey/UserIT.java | 96 ++--- 9 files changed, 675 insertions(+), 675 deletions(-) diff --git a/src/test/java/com/spotify/app/AbstractTestcontainers.java b/src/test/java/com/spotify/app/AbstractTestcontainers.java index 1d3fab2..10a07ab 100644 --- a/src/test/java/com/spotify/app/AbstractTestcontainers.java +++ b/src/test/java/com/spotify/app/AbstractTestcontainers.java @@ -1,59 +1,59 @@ -package com.spotify.app; - -import lombok.extern.slf4j.Slf4j; -import org.flywaydb.core.Flyway; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import org.springframework.test.context.DynamicPropertyRegistry; -import org.springframework.test.context.DynamicPropertySource; -import org.testcontainers.containers.MySQLContainer; -import org.testcontainers.junit.jupiter.Container; -import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.utility.MountableFile; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - - -@Testcontainers -@Slf4j -public abstract class AbstractTestcontainers { - - - @BeforeAll - static void beforeAll() { - Flyway flyway = Flyway - .configure() - .dataSource( - mySQLContainer.getJdbcUrl(), - mySQLContainer.getUsername(), - mySQLContainer.getPassword() - ).load(); - flyway.migrate(); - } - @Container - protected static final MySQLContainer mySQLContainer = - new MySQLContainer<>("mysql:latest") - .withDatabaseName("spotify-dao-unit-test") - .withUsername("kai") - .withPassword("password") - ; - - @DynamicPropertySource - static void dynamicProperties(DynamicPropertyRegistry registry) { - registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl); - registry.add("spring.datasource.username", mySQLContainer::getUsername); - registry.add("spring.datasource.password", mySQLContainer::getPassword); - } - +//package com.spotify.app; +// +//import lombok.extern.slf4j.Slf4j; +//import org.flywaydb.core.Flyway; +//import org.junit.jupiter.api.BeforeAll; +//import org.junit.jupiter.api.Test; +//import org.springframework.test.context.DynamicPropertyRegistry; +//import org.springframework.test.context.DynamicPropertySource; +//import org.testcontainers.containers.MySQLContainer; +//import org.testcontainers.junit.jupiter.Container; +//import org.testcontainers.junit.jupiter.Testcontainers; +//import org.testcontainers.utility.MountableFile; +// +//import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +// +// +//@Testcontainers +//@Slf4j +//public abstract class AbstractTestcontainers { +// +// // @BeforeAll -// static void setUp() { -// mySQLContainer.withInitScript("schema.sql"); -// mySQLContainer.start(); +// static void beforeAll() { +// Flyway flyway = Flyway +// .configure() +// .dataSource( +// mySQLContainer.getJdbcUrl(), +// mySQLContainer.getUsername(), +// mySQLContainer.getPassword() +// ).load(); +// flyway.migrate(); // } - - @Test - void connectionEstablished() { - assertThat(mySQLContainer.isCreated()).isTrue(); - assertThat(mySQLContainer.isRunning()).isTrue(); - } -} +// @Container +// protected static final MySQLContainer mySQLContainer = +// new MySQLContainer<>("mysql:latest") +// .withDatabaseName("spotify-dao-unit-test") +// .withUsername("kai") +// .withPassword("password") +// ; +// +// @DynamicPropertySource +// static void dynamicProperties(DynamicPropertyRegistry registry) { +// registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl); +// registry.add("spring.datasource.username", mySQLContainer::getUsername); +// registry.add("spring.datasource.password", mySQLContainer::getPassword); +// } +// +//// @BeforeAll +//// static void setUp() { +//// mySQLContainer.withInitScript("schema.sql"); +//// mySQLContainer.start(); +//// } +// +// @Test +// void connectionEstablished() { +// assertThat(mySQLContainer.isCreated()).isTrue(); +// assertThat(mySQLContainer.isRunning()).isTrue(); +// } +//} diff --git a/src/test/java/com/spotify/app/journey/AlbumIT.java b/src/test/java/com/spotify/app/journey/AlbumIT.java index 60dad07..8f46bca 100644 --- a/src/test/java/com/spotify/app/journey/AlbumIT.java +++ b/src/test/java/com/spotify/app/journey/AlbumIT.java @@ -1,64 +1,64 @@ -package com.spotify.app.journey; - -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); - - } -} +//package com.spotify.app.journey; +// +//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/AuthenticationIT.java b/src/test/java/com/spotify/app/journey/AuthenticationIT.java index 02e9ed9..e2f301a 100644 --- a/src/test/java/com/spotify/app/journey/AuthenticationIT.java +++ b/src/test/java/com/spotify/app/journey/AuthenticationIT.java @@ -1,111 +1,111 @@ -package com.spotify.app.journey; - - -import com.github.javafaker.Faker; -import com.spotify.app.AbstractTestcontainers; -import com.spotify.app.model.User; -import com.spotify.app.security.auth.AuthenticationRequest; -import com.spotify.app.security.auth.AuthenticationResponse; -import com.spotify.app.security.auth.RegisterRequest; -import com.spotify.app.security.jwt.JwtService; -import com.spotify.app.service.UserService; -import jakarta.transaction.Transactional; -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.Random; -import java.util.UUID; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Slf4j -public class AuthenticationIT { - - @Autowired - private TestRestTemplate restTemplate; - @Autowired - private JwtService jwtService; - @Autowired - private UserService userService; - private static final String AUTH_PATH = "/api/v1/auth"; - private static final Random random = new Random(); - - - // register - @Test - public void canRegister() { - // given - Faker faker = new Faker(); - - String firstName = faker.name().firstName(); - String lastName = faker.name().lastName(); - String email = faker.name().firstName() + UUID.randomUUID() + "@gmail.com"; - String password = "passwordd"; - int day = 1 ; - int month = 11; - int year = 2002; - String gender = random.nextInt(2,3) % 2 == 0 ? "MALE" : "FEMALE"; - RegisterRequest request = new RegisterRequest(firstName,lastName ,email,password,day,month,year,gender); - AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/register", request, AuthenticationResponse.class); - - assertThat(response.getUser().email()).isEqualTo(email) ; - assertThat(response.getUser().firstName()).isEqualTo(firstName) ; - assertThat(response.getUser().lastName()).isEqualTo(lastName) ; -// assertThat(jwtService.isTokenValid(response.getAccessToken(), response.getUser())); - } - - - // login - @Test - public void canLogin() { - String email = "taylor@gmail.com"; - String password = "thuan2023"; - AuthenticationRequest request = new AuthenticationRequest(email,password); - AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/authenticate", request, AuthenticationResponse.class); - log.info(String.valueOf(response)); - assertThat(response.getUser().email()).isEqualTo(email) ; - assertThat(response.getAccessToken()).isNotNull() ; - } - - // refresh Token - @Test - public void canRefreshToken() { - // given - String email = "taylor@gmail.com"; - String password = "thuan2023"; - AuthenticationRequest request = new AuthenticationRequest(email,password); - AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", request, AuthenticationResponse.class); - String refreshToken = authResponse.getRefreshToken(); - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - log.info(refreshToken); - httpHeaders.set("Authorization", "Bearer " + refreshToken); - - HttpEntity requestEntity = new HttpEntity<>( httpHeaders); - // when - AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/refresh-token", requestEntity, AuthenticationResponse.class); - - // then - User user = userService.get(authResponse.getUser().id()); - log.info(String.valueOf(response)); - - assertThat(jwtService.isTokenValid(response.getRefreshToken(),user)).isTrue(); - assertThat(jwtService.isTokenValid(response.getAccessToken(),user)).isTrue(); - } - // login fail return exception - - @Test - public void shouldNotLogin() { - String email = "taylor@gmail.com"; - String password = "thuan202333"; - AuthenticationRequest request = new AuthenticationRequest(email,password); - ResponseEntity response = restTemplate.postForEntity(AUTH_PATH + "/authenticate", request, AuthenticationResponse.class); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); - } - -} +//package com.spotify.app.journey; +// +// +//import com.github.javafaker.Faker; +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.model.User; +//import com.spotify.app.security.auth.AuthenticationRequest; +//import com.spotify.app.security.auth.AuthenticationResponse; +//import com.spotify.app.security.auth.RegisterRequest; +//import com.spotify.app.security.jwt.JwtService; +//import com.spotify.app.service.UserService; +//import jakarta.transaction.Transactional; +//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.Random; +//import java.util.UUID; +// +//import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +// +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@Slf4j +//public class AuthenticationIT { +// +// @Autowired +// private TestRestTemplate restTemplate; +// @Autowired +// private JwtService jwtService; +// @Autowired +// private UserService userService; +// private static final String AUTH_PATH = "/api/v1/auth"; +// private static final Random random = new Random(); +// +// +// // register +// @Test +// public void canRegister() { +// // given +// Faker faker = new Faker(); +// +// String firstName = faker.name().firstName(); +// String lastName = faker.name().lastName(); +// String email = faker.name().firstName() + UUID.randomUUID() + "@gmail.com"; +// String password = "passwordd"; +// int day = 1 ; +// int month = 11; +// int year = 2002; +// String gender = random.nextInt(2,3) % 2 == 0 ? "MALE" : "FEMALE"; +// RegisterRequest request = new RegisterRequest(firstName,lastName ,email,password,day,month,year,gender); +// AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/register", request, AuthenticationResponse.class); +// +// assertThat(response.getUser().email()).isEqualTo(email) ; +// assertThat(response.getUser().firstName()).isEqualTo(firstName) ; +// assertThat(response.getUser().lastName()).isEqualTo(lastName) ; +//// assertThat(jwtService.isTokenValid(response.getAccessToken(), response.getUser())); +// } +// +// +// // login +// @Test +// public void canLogin() { +// String email = "taylor@gmail.com"; +// String password = "thuan2023"; +// AuthenticationRequest request = new AuthenticationRequest(email,password); +// AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/authenticate", request, AuthenticationResponse.class); +// log.info(String.valueOf(response)); +// assertThat(response.getUser().email()).isEqualTo(email) ; +// assertThat(response.getAccessToken()).isNotNull() ; +// } +// +// // refresh Token +// @Test +// public void canRefreshToken() { +// // given +// String email = "taylor@gmail.com"; +// String password = "thuan2023"; +// AuthenticationRequest request = new AuthenticationRequest(email,password); +// AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", request, AuthenticationResponse.class); +// String refreshToken = authResponse.getRefreshToken(); +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.setContentType(MediaType.APPLICATION_JSON); +// log.info(refreshToken); +// httpHeaders.set("Authorization", "Bearer " + refreshToken); +// +// HttpEntity requestEntity = new HttpEntity<>( httpHeaders); +// // when +// AuthenticationResponse response = restTemplate.postForObject(AUTH_PATH+"/refresh-token", requestEntity, AuthenticationResponse.class); +// +// // then +// User user = userService.get(authResponse.getUser().id()); +// log.info(String.valueOf(response)); +// +// assertThat(jwtService.isTokenValid(response.getRefreshToken(),user)).isTrue(); +// assertThat(jwtService.isTokenValid(response.getAccessToken(),user)).isTrue(); +// } +// // login fail return exception +// +// @Test +// public void shouldNotLogin() { +// String email = "taylor@gmail.com"; +// String password = "thuan202333"; +// AuthenticationRequest request = new AuthenticationRequest(email,password); +// ResponseEntity response = restTemplate.postForEntity(AUTH_PATH + "/authenticate", request, AuthenticationResponse.class); +// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); +// } +// +//} diff --git a/src/test/java/com/spotify/app/journey/CategoryIT.java b/src/test/java/com/spotify/app/journey/CategoryIT.java index 390f8a7..48dc36e 100644 --- a/src/test/java/com/spotify/app/journey/CategoryIT.java +++ b/src/test/java/com/spotify/app/journey/CategoryIT.java @@ -1,40 +1,40 @@ -package com.spotify.app.journey; - -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); - } -} +//package com.spotify.app.journey; +// +//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 331c55e..36dcf12 100644 --- a/src/test/java/com/spotify/app/journey/FollowerIT.java +++ b/src/test/java/com/spotify/app/journey/FollowerIT.java @@ -1,63 +1,63 @@ -package com.spotify.app.journey; - -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); - - } - -} +//package com.spotify.app.journey; +// +//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 5c3169d..12506b3 100644 --- a/src/test/java/com/spotify/app/journey/PlaylistIT.java +++ b/src/test/java/com/spotify/app/journey/PlaylistIT.java @@ -1,61 +1,61 @@ -package com.spotify.app.journey; - -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); - - - } -} +//package com.spotify.app.journey; +// +//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 index 033d693..d541ade 100644 --- a/src/test/java/com/spotify/app/journey/ReviewIT.java +++ b/src/test/java/com/spotify/app/journey/ReviewIT.java @@ -1,66 +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); - - } -} +//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 d6f97c3..c692117 100644 --- a/src/test/java/com/spotify/app/journey/SongIT.java +++ b/src/test/java/com/spotify/app/journey/SongIT.java @@ -1,165 +1,165 @@ -package com.spotify.app.journey; - -import com.github.javafaker.Faker; -import com.spotify.app.AbstractTestcontainers; -import com.spotify.app.dto.request.SongRequest; -import com.spotify.app.dto.response.SongResponse; -import com.spotify.app.enums.Genre; -import com.spotify.app.model.Song; -import com.spotify.app.repository.SongRepository; -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.SongService; -import com.spotify.app.service.UserService; -import jakarta.transaction.Transactional; -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 SongIT extends AbstractTestcontainers { - private static final String SONG_PATH = "/api/v1/song"; - private static final String AUTH_PATH = "/api/v1/auth"; - - @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private JwtService jwtService; - - @Autowired - private SongService songService; - - @Autowired - private SongRepository songRepository; - - @Autowired - private UserService userService; - @Test - public void canSaveSong__WhenArtistAct() { - // 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(); - - String name = faker.funnyName().name(); - String genre = Genre.Rock.toString(); - int duration = 200 ; - String lyric = "
lyric
"; - int month = 12 ; - int day = 12 ; - int year = 2022; - String label = "happy"; - Long userId = authResponse.getUser().id(); - - SongRequest songRequest = new SongRequest(name, genre, duration,lyric,day,month,year,label,userId); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); - httpHeaders.set("Authorization", "Bearer " + token); - - HttpEntity request = new HttpEntity<>(songRequest,httpHeaders); - // when - ResponseEntity response = restTemplate.exchange(SONG_PATH + "/save", HttpMethod.POST ,request, SongResponse.class); - // then - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - } - - @Test - public void canUpdateSong() { - // 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); - String token = authResponse.getAccessToken(); - Song underUpdate = songService.get(1L); - - String updateName = faker.funnyName().name(); - int year = 2023; - int month = 3 ; - int day = 30 ; - - SongRequest songRequest = new SongRequest( - updateName, - underUpdate.getGenre().toString(), - underUpdate.getDuration(), - underUpdate.getLyric(), - day,month,year, - underUpdate.getLabel(), - authResponse.getUser().id()); - // when - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); - httpHeaders.set("Authorization", "Bearer " + token); - HttpEntity request = new HttpEntity<>(songRequest,httpHeaders); - ResponseEntity response = restTemplate.exchange(SONG_PATH + "/update/" + underUpdate.getId(), HttpMethod.PUT ,request, SongResponse.class); - // then - SongResponse actual = response.getBody(); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(actual.name()).isEqualTo(updateName); - assertThat(actual.label()).isEqualTo(underUpdate.getLabel()); - } - - @Test - public void canUpdateStatusSong() { - - // given - String email = "taylor@gmail.com"; - String password = "thuan2023"; - AuthenticationRequest authRequest = new AuthenticationRequest(email,password); - AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); - String token = authResponse.getAccessToken(); - Song underUpdate = songService.get(1L); - boolean expected = !underUpdate.isStatus(); - // when - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); - httpHeaders.set("Authorization", "Bearer " + token); - HttpEntity request = new HttpEntity<>(httpHeaders); - ResponseEntity response = restTemplate.exchange(SONG_PATH + "/update/status/" + underUpdate.getId(), HttpMethod.PUT ,request, SongResponse.class); - - // then - boolean actual = response.getBody().status(); - assertThat(expected).isEqualTo(actual); - } - - @Test - public void canAddCollapse() { - // given - String email = "taylor@gmail.com"; - String password = "thuan2023"; - AuthenticationRequest authRequest = new AuthenticationRequest(email,password); - AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); - String token = authResponse.getAccessToken(); - Song underTest = songRepository.findByIdReturnUsersAlbumsReviews(1L).get(); - - Long addUserId = 2L ; - int expectSize = underTest.getUsers().size() + 1; - - boolean checkExist = underTest.getUsers().stream().anyMatch(user -> user.getId().equals(addUserId)); - // when - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); - httpHeaders.set("Authorization", "Bearer " + token); - HttpEntity request = new HttpEntity<>(httpHeaders); - ResponseEntity response = restTemplate.exchange(SONG_PATH + "/1/add/"+addUserId, HttpMethod.GET ,request, SongResponse.class); - // then - assertThat(checkExist).isFalse(); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.getBody().users().size()).isEqualTo(expectSize); - } -} +//package com.spotify.app.journey; +// +//import com.github.javafaker.Faker; +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.dto.request.SongRequest; +//import com.spotify.app.dto.response.SongResponse; +//import com.spotify.app.enums.Genre; +//import com.spotify.app.model.Song; +//import com.spotify.app.repository.SongRepository; +//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.SongService; +//import com.spotify.app.service.UserService; +//import jakarta.transaction.Transactional; +//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 SongIT extends AbstractTestcontainers { +// private static final String SONG_PATH = "/api/v1/song"; +// private static final String AUTH_PATH = "/api/v1/auth"; +// +// @Autowired +// private TestRestTemplate restTemplate; +// +// @Autowired +// private JwtService jwtService; +// +// @Autowired +// private SongService songService; +// +// @Autowired +// private SongRepository songRepository; +// +// @Autowired +// private UserService userService; +// @Test +// public void canSaveSong__WhenArtistAct() { +// // 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(); +// +// String name = faker.funnyName().name(); +// String genre = Genre.Rock.toString(); +// int duration = 200 ; +// String lyric = "
lyric
"; +// int month = 12 ; +// int day = 12 ; +// int year = 2022; +// String label = "happy"; +// Long userId = authResponse.getUser().id(); +// +// SongRequest songRequest = new SongRequest(name, genre, duration,lyric,day,month,year,label,userId); +// +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); +// httpHeaders.set("Authorization", "Bearer " + token); +// +// HttpEntity request = new HttpEntity<>(songRequest,httpHeaders); +// // when +// ResponseEntity response = restTemplate.exchange(SONG_PATH + "/save", HttpMethod.POST ,request, SongResponse.class); +// // then +// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); +// } +// +// @Test +// public void canUpdateSong() { +// // 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); +// String token = authResponse.getAccessToken(); +// Song underUpdate = songService.get(1L); +// +// String updateName = faker.funnyName().name(); +// int year = 2023; +// int month = 3 ; +// int day = 30 ; +// +// SongRequest songRequest = new SongRequest( +// updateName, +// underUpdate.getGenre().toString(), +// underUpdate.getDuration(), +// underUpdate.getLyric(), +// day,month,year, +// underUpdate.getLabel(), +// authResponse.getUser().id()); +// // when +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); +// httpHeaders.set("Authorization", "Bearer " + token); +// HttpEntity request = new HttpEntity<>(songRequest,httpHeaders); +// ResponseEntity response = restTemplate.exchange(SONG_PATH + "/update/" + underUpdate.getId(), HttpMethod.PUT ,request, SongResponse.class); +// // then +// SongResponse actual = response.getBody(); +// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); +// assertThat(actual.name()).isEqualTo(updateName); +// assertThat(actual.label()).isEqualTo(underUpdate.getLabel()); +// } +// +// @Test +// public void canUpdateStatusSong() { +// +// // given +// String email = "taylor@gmail.com"; +// String password = "thuan2023"; +// AuthenticationRequest authRequest = new AuthenticationRequest(email,password); +// AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); +// String token = authResponse.getAccessToken(); +// Song underUpdate = songService.get(1L); +// boolean expected = !underUpdate.isStatus(); +// // when +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); +// httpHeaders.set("Authorization", "Bearer " + token); +// HttpEntity request = new HttpEntity<>(httpHeaders); +// ResponseEntity response = restTemplate.exchange(SONG_PATH + "/update/status/" + underUpdate.getId(), HttpMethod.PUT ,request, SongResponse.class); +// +// // then +// boolean actual = response.getBody().status(); +// assertThat(expected).isEqualTo(actual); +// } +// +// @Test +// public void canAddCollapse() { +// // given +// String email = "taylor@gmail.com"; +// String password = "thuan2023"; +// AuthenticationRequest authRequest = new AuthenticationRequest(email,password); +// AuthenticationResponse authResponse = restTemplate.postForObject(AUTH_PATH+"/authenticate", authRequest, AuthenticationResponse.class); +// String token = authResponse.getAccessToken(); +// Song underTest = songRepository.findByIdReturnUsersAlbumsReviews(1L).get(); +// +// Long addUserId = 2L ; +// int expectSize = underTest.getUsers().size() + 1; +// +// boolean checkExist = underTest.getUsers().stream().anyMatch(user -> user.getId().equals(addUserId)); +// // when +// HttpHeaders httpHeaders = new HttpHeaders(); +// httpHeaders.set(HttpHeaders.ACCEPT , MediaType.APPLICATION_JSON_VALUE); +// httpHeaders.set("Authorization", "Bearer " + token); +// HttpEntity request = new HttpEntity<>(httpHeaders); +// ResponseEntity response = restTemplate.exchange(SONG_PATH + "/1/add/"+addUserId, HttpMethod.GET ,request, SongResponse.class); +// // then +// assertThat(checkExist).isFalse(); +// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); +// assertThat(response.getBody().users().size()).isEqualTo(expectSize); +// } +//} diff --git a/src/test/java/com/spotify/app/journey/UserIT.java b/src/test/java/com/spotify/app/journey/UserIT.java index 99ab774..a9411f1 100644 --- a/src/test/java/com/spotify/app/journey/UserIT.java +++ b/src/test/java/com/spotify/app/journey/UserIT.java @@ -1,53 +1,53 @@ -package com.spotify.app.journey; - -import com.spotify.app.AbstractTestcontainers; -import com.spotify.app.dto.UserDTO; -import com.spotify.app.dto.response.UserResponse; -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.HttpMethod; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; - -import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - -@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -@Slf4j -public class UserIT { - - @Autowired - private TestRestTemplate restTemplate; - - @Autowired - private JwtService jwtService; - - private static final String USER_PATH = "/api/v1/user"; - - @Test - public void shouldReturnUser__WhenGivenValidId() { - // given - Long userId = 1L; - String url = USER_PATH.concat(String.format("/%d",userId)); -// log.info(url); - ResponseEntity response = restTemplate. - exchange(url, HttpMethod.GET,null,UserDTO.class); - assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); - assertThat(response.getBody()).isNotNull(); - } - +//package com.spotify.app.journey; +// +//import com.spotify.app.AbstractTestcontainers; +//import com.spotify.app.dto.UserDTO; +//import com.spotify.app.dto.response.UserResponse; +//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.HttpMethod; +//import org.springframework.http.HttpStatus; +//import org.springframework.http.ResponseEntity; +// +//import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +// +//@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +//@Slf4j +//public class UserIT { +// +// @Autowired +// private TestRestTemplate restTemplate; +// +// @Autowired +// private JwtService jwtService; +// +// private static final String USER_PATH = "/api/v1/user"; +// // @Test -// public void shouldReturnFail__WhenGivenUnValidId(){ -// Long userId = 0L ; +// public void shouldReturnUser__WhenGivenValidId() { +// // given +// Long userId = 1L; // String url = USER_PATH.concat(String.format("/%d",userId)); //// log.info(url); // ResponseEntity response = restTemplate. -// getForEntity(url,UserDTO.class); -//// log.info(String.valueOf(response.getBody())); -// log.info(String.valueOf(response.getStatusCode())); +// exchange(url, HttpMethod.GET,null,UserDTO.class); +// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); +// assertThat(response.getBody()).isNotNull(); // } - -} +// +//// @Test +//// public void shouldReturnFail__WhenGivenUnValidId(){ +//// Long userId = 0L ; +//// String url = USER_PATH.concat(String.format("/%d",userId)); +////// log.info(url); +//// ResponseEntity response = restTemplate. +//// getForEntity(url,UserDTO.class); +////// log.info(String.valueOf(response.getBody())); +//// log.info(String.valueOf(response.getStatusCode())); +//// } +// +//}