From fd30c646854205ffd8f4d5a2117a0b473912749c Mon Sep 17 00:00:00 2001 From: thuan Date: Sat, 13 Jan 2024 10:45:28 +0700 Subject: [PATCH] 013/01: exclude integrationTest when build image --- pom.xml | 41 +++ .../config/SecurityFilterChainConfig.java | 106 +++--- .../spotify/app/AbstractTestcontainers.java | 6 +- .../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 ++--- 11 files changed, 715 insertions(+), 674 deletions(-) diff --git a/pom.xml b/pom.xml index 39893bf..4ef59c5 100644 --- a/pom.xml +++ b/pom.xml @@ -21,6 +21,46 @@ 0.11.5 thuanvn2002/spotify:latest + + + unit-tests + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + + **/*Test.java + + + + + + + + + integration-tests + + + + org.apache.maven.plugins + maven-failsafe-plugin + 3.0.0-M7 + + + + **/*IT.java + + + + + + + + com.cloudinary @@ -196,6 +236,7 @@ + diff --git a/src/main/java/com/spotify/app/security/config/SecurityFilterChainConfig.java b/src/main/java/com/spotify/app/security/config/SecurityFilterChainConfig.java index fff6cd5..319a9b1 100644 --- a/src/main/java/com/spotify/app/security/config/SecurityFilterChainConfig.java +++ b/src/main/java/com/spotify/app/security/config/SecurityFilterChainConfig.java @@ -41,67 +41,67 @@ public JwtAuthenticationFilter jwtAuthenticationFilter () { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http -// .csrf((AbstractHttpConfigurer::disable)) -// .cors(Customizer.withDefaults()) -// .authorizeHttpRequests( -// authz -> -// authz -// .requestMatchers( -// "/api/v1/song/save", -// "/api/v1/song/update/**", -// "/api/v1/song/upload/**", -// "/api/v1/album/upload/**", -// "/api/v1/album/*/add/**", -// "/api/v1/album/*/remove/**", -// "/api/v1/album/*/add", -// "/api/v1/album/update/**" -// ) -// .hasRole( "ARTIST") -// .requestMatchers( -// "/api/v1/role/**", -// "/api/v1/playlist/admin/**", -// "/api/v1/category/admin/**", -// "/api/v1/review/admin/**" -// ) -// .hasRole("ADMIN") -// .requestMatchers( -// "/api/v1/song/find/by/sentiment/**", -// "/api/v1/song/increase/view/**", -// "/api/v1/user/increase/view/**", -// "/api/v1/user/*/playlists/followings", -// "/api/v1/user/*/add/**", -// "/api/v1/user/*/remove/**", -// "/api/v1/playlist/user/*/add/**", -// "/api/v1/playlist/user/*/remove/**", -// "/api/v1/playlist/*/create/playlist", -// "/api/v1/playlist/*/add/song/**", -// "/api/v1/playlist/*/remove/song/**", -// "/api/v1/playlist/upload/**", -// "/api/v1/follower/*/follow/**", -// "/api/v1/follower/*/cancel/**", -// "/api/v1/follower/*/followings", -// "/api/v1/follower/is/*/followed/**", -// "/api/v1/review/*/review/in/**" -// ) -// .authenticated() -// .anyRequest() -// .permitAll()) -// .sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer -// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) -// .authenticationProvider(authenticationProvider) -// .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) -// .logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout") -// .logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())) .csrf((AbstractHttpConfigurer::disable)) .cors(Customizer.withDefaults()) - .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> authorizationManagerRequestMatcherRegistry - .requestMatchers("/api/v1/allowAllByPhi/**").authenticated().anyRequest().permitAll()) + .authorizeHttpRequests( + authz -> + authz + .requestMatchers( + "/api/v1/song/save", + "/api/v1/song/update/**", + "/api/v1/song/upload/**", + "/api/v1/album/upload/**", + "/api/v1/album/*/add/**", + "/api/v1/album/*/remove/**", + "/api/v1/album/*/add", + "/api/v1/album/update/**" + ) + .hasRole( "ARTIST") + .requestMatchers( + "/api/v1/role/**", + "/api/v1/playlist/admin/**", + "/api/v1/category/admin/**", + "/api/v1/review/admin/**" + ) + .hasRole("ADMIN") + .requestMatchers( + "/api/v1/song/find/by/sentiment/**", + "/api/v1/song/increase/view/**", + "/api/v1/user/increase/view/**", + "/api/v1/user/*/playlists/followings", + "/api/v1/user/*/add/**", + "/api/v1/user/*/remove/**", + "/api/v1/playlist/user/*/add/**", + "/api/v1/playlist/user/*/remove/**", + "/api/v1/playlist/*/create/playlist", + "/api/v1/playlist/*/add/song/**", + "/api/v1/playlist/*/remove/song/**", + "/api/v1/playlist/upload/**", + "/api/v1/follower/*/follow/**", + "/api/v1/follower/*/cancel/**", + "/api/v1/follower/*/followings", + "/api/v1/follower/is/*/followed/**", + "/api/v1/review/*/review/in/**" + ) + .authenticated() + .anyRequest() + .permitAll()) .sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) .authenticationProvider(authenticationProvider) .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) .logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout") .logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())) +// .csrf((AbstractHttpConfigurer::disable)) +// .cors(Customizer.withDefaults()) +// .authorizeHttpRequests(authorizationManagerRequestMatcherRegistry -> authorizationManagerRequestMatcherRegistry +// .requestMatchers("/api/v1/allowAllByPhi/**").authenticated().anyRequest().permitAll()) +// .sessionManagement(httpSecuritySessionManagementConfigurer -> httpSecuritySessionManagementConfigurer +// .sessionCreationPolicy(SessionCreationPolicy.STATELESS)) +// .authenticationProvider(authenticationProvider) +// .addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class) +// .logout(httpSecurityLogoutConfigurer -> httpSecurityLogoutConfigurer.logoutUrl("/api/v1/auth/logout") +// .logoutSuccessHandler((request, response, authentication) -> SecurityContextHolder.clearContext())) ; return http.build(); } diff --git a/src/test/java/com/spotify/app/AbstractTestcontainers.java b/src/test/java/com/spotify/app/AbstractTestcontainers.java index 1d3fab2..4048055 100644 --- a/src/test/java/com/spotify/app/AbstractTestcontainers.java +++ b/src/test/java/com/spotify/app/AbstractTestcontainers.java @@ -24,7 +24,7 @@ static void beforeAll() { Flyway flyway = Flyway .configure() .dataSource( - mySQLContainer.getJdbcUrl(), + mySQLContainer.getJdbcUrl().concat("?autoReconnect=true&useSSL=false"), mySQLContainer.getUsername(), mySQLContainer.getPassword() ).load(); @@ -40,9 +40,9 @@ static void beforeAll() { @DynamicPropertySource static void dynamicProperties(DynamicPropertyRegistry registry) { - registry.add("spring.datasource.url", mySQLContainer::getJdbcUrl); + String jdbcUrl = mySQLContainer.getJdbcUrl().concat("?autoReconnect=true&useSSL=false"); + registry.add("spring.datasource.url", () -> jdbcUrl); registry.add("spring.datasource.username", mySQLContainer::getUsername); - registry.add("spring.datasource.password", mySQLContainer::getPassword); } // @BeforeAll diff --git a/src/test/java/com/spotify/app/journey/AlbumIT.java b/src/test/java/com/spotify/app/journey/AlbumIT.java index 8f46bca..60dad07 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 e2f301a..02e9ed9 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 48dc36e..390f8a7 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 36dcf12..331c55e 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 12506b3..5c3169d 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 d541ade..033d693 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 c692117..d6f97c3 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 a9411f1..99ab774 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"; -// +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(); + } + // @Test -// public void shouldReturnUser__WhenGivenValidId() { -// // given -// Long userId = 1L; +// public void shouldReturnFail__WhenGivenUnValidId(){ +// Long userId = 0L ; // 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(); +// getForEntity(url,UserDTO.class); +//// log.info(String.valueOf(response.getBody())); +// log.info(String.valueOf(response.getStatusCode())); // } -// -//// @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())); -//// } -// -//} + +}