Skip to content

Commit

Permalink
13/11: testing
Browse files Browse the repository at this point in the history
  • Loading branch information
thuan committed Nov 13, 2023
1 parent bc67405 commit 10de3d7
Show file tree
Hide file tree
Showing 14 changed files with 545 additions and 263 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ public ResponseEntity<Boolean> checkCurrentUserFollowedTargetUser(
@PathVariable("currentUserId") Long currentUserId,
@PathVariable("targetUserId") Long targetUserId
) {
return ResponseEntity.
ok().
body(
return ResponseEntity.ok().body(
followerService.checkCurrentUserFollowedTargetUser(currentUserId,targetUserId)
);
}
Expand Down
17 changes: 0 additions & 17 deletions src/test/java/com/spotify/app/album/AlbumRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,11 @@
//package com.spotify.app.album;
//
//
//import com.spotify.app.SpotifyApplication;
//import com.spotify.app.TestConfig;
//import com.spotify.app.enums.Genre;
//import com.spotify.app.model.Album;
//import com.spotify.app.model.Song;
//import com.spotify.app.model.User;
//import com.spotify.app.repository.AlbumRepository;
//import com.spotify.app.repository.SongRepository;
//import com.spotify.app.repository.UserRepository;
//import jakarta.transaction.Transactional;
//import lombok.AllArgsConstructor;
//import lombok.RequiredArgsConstructor;
//import org.junit.jupiter.api.Test;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.autoconfigure.data.jdbc.DataJdbcTest;
//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.context.annotation.Import;
//import org.springframework.test.annotation.Rollback;
//
//import java.time.LocalDateTime;
//
//@DataJpaTest
//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
Expand Down
4 changes: 4 additions & 0 deletions src/test/java/com/spotify/app/auth/AuthServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.spotify.app.auth;

public class AuthServiceTest {
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@
//public class CategoryRepositoryTest extends AbstractTestcontainers {
//
//
//
//}
13 changes: 13 additions & 0 deletions src/test/java/com/spotify/app/follower/FollowerRepositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//package com.spotify.app.follower;
//
//import com.spotify.app.AbstractTestcontainers;
//import com.spotify.app.TestConfig;
//import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
//import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
//import org.springframework.context.annotation.Import;
//
//@DataJpaTest
//@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
//@Import({TestConfig.class})
//public class FollowerRepositoryTest extends AbstractTestcontainers {
//}
62 changes: 61 additions & 1 deletion src/test/java/com/spotify/app/journey/AlbumIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,64 @@
package com.spotify.app.journey;

public class AlbumIT {
import com.github.javafaker.Faker;
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.AlbumDTO;
import com.spotify.app.dto.request.AlbumRequest;
import com.spotify.app.dto.response.UserNoAssociationResponse;
import com.spotify.app.security.auth.AuthenticationRequest;
import com.spotify.app.security.auth.AuthenticationResponse;
import com.spotify.app.security.jwt.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class AlbumIT extends AbstractTestcontainers {

private static final String AUTH_PATH = "/api/v1/auth";
private static final String ALBUM_PATH = "/api/v1/album";

@Autowired
private TestRestTemplate restTemplate;
@Autowired
private JwtService jwtService;

@Test
public void canArtistAddAlbum() {
// given
Faker faker = new Faker();
String email = "[email protected]";
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<AlbumDTO> response = restTemplate.exchange(url, HttpMethod.POST ,request, AlbumDTO.class);

// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

}
}
38 changes: 37 additions & 1 deletion src/test/java/com/spotify/app/journey/CategoryIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
package com.spotify.app.journey;

public class CategoryIT {
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.CategoryDTO;
import com.spotify.app.dto.response.UserNoAssociationResponse;
import com.spotify.app.security.jwt.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.assertj.core.api.AssertionsForClassTypes;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.net.http.HttpResponse;
import java.util.Set;

import static org.assertj.core.api.FactoryBasedNavigableListAssert.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class CategoryIT extends AbstractTestcontainers {

private static final String CATEGORY_PATH = "/api/v1/category";
@Autowired
private TestRestTemplate restTemplate;

@Test
public void canGetAllCategoryParent() {

// given
String url = CATEGORY_PATH.concat("/getAllParent");
// when
ResponseEntity<Set> response = restTemplate.getForEntity(url, Set.class);
// then
AssertionsForClassTypes.assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
}
}
61 changes: 60 additions & 1 deletion src/test/java/com/spotify/app/journey/FollowerIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,63 @@
package com.spotify.app.journey;

public class FollowerIT {
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.response.UserNoAssociationResponse;
import com.spotify.app.security.auth.AuthenticationRequest;
import com.spotify.app.security.auth.AuthenticationResponse;
import com.spotify.app.security.jwt.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class FollowerIT extends AbstractTestcontainers {

private static final String AUTH_PATH = "/api/v1/auth";
private static final String FOLLOWER_PATH = "/api/v1/follower";

@Autowired
private TestRestTemplate restTemplate;
@Autowired
private JwtService jwtService;


@Test
public void canFollowUser() {
// given
Long currentUserId ;
Long targetUserId = 1L;


String email = "[email protected]";
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<UserNoAssociationResponse[]> response = restTemplate.exchange(url, HttpMethod.GET ,request, UserNoAssociationResponse[].class);

// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

}

}
59 changes: 58 additions & 1 deletion src/test/java/com/spotify/app/journey/PlaylistIT.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,61 @@
package com.spotify.app.journey;

public class PlaylistIT {
import com.github.javafaker.Faker;
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.response.SongResponse;
import com.spotify.app.security.auth.AuthenticationRequest;
import com.spotify.app.security.auth.AuthenticationResponse;
import com.spotify.app.security.jwt.JwtService;
import com.spotify.app.service.PlaylistUserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class PlaylistIT extends AbstractTestcontainers {

private static final String AUTH_PATH = "/api/v1/auth";
private static final String REVIEW_PATH = "/api/v1/playlist";
@Autowired
private TestRestTemplate restTemplate;

@Autowired
private PlaylistUserService playlistUserService;

@Autowired
private JwtService jwtService;

@Test
public void canUserCreatePlaylist() {
String email = "[email protected]";
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<String> response = restTemplate.exchange(url, HttpMethod.GET ,request, String.class);

// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);


}
}
66 changes: 66 additions & 0 deletions src/test/java/com/spotify/app/journey/ReviewIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package com.spotify.app.journey;

import com.github.javafaker.Faker;
import com.spotify.app.AbstractTestcontainers;
import com.spotify.app.dto.ReviewDTO;
import com.spotify.app.dto.response.ReviewResponse;
import com.spotify.app.dto.response.SongResponse;
import com.spotify.app.security.auth.AuthenticationRequest;
import com.spotify.app.security.auth.AuthenticationResponse;
import com.spotify.app.security.jwt.JwtService;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.*;

import java.util.List;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
public class ReviewIT extends AbstractTestcontainers {

private static final String AUTH_PATH = "/api/v1/auth";
private static final String REVIEW_PATH = "/api/v1/review";
@Autowired
private TestRestTemplate restTemplate;

@Autowired
private JwtService jwtService;

@Test
public void canReviewInSong() {

// given
Faker faker = new Faker();
String email = "[email protected]";
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<ReviewResponse[]> response = restTemplate.exchange(urlRequest, HttpMethod.POST ,request, ReviewResponse[].class);
// then
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);

}
}
1 change: 0 additions & 1 deletion src/test/java/com/spotify/app/journey/SongIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j
@Transactional
public class SongIT extends AbstractTestcontainers {
private static final String SONG_PATH = "/api/v1/song";
private static final String AUTH_PATH = "/api/v1/auth";
Expand Down
Loading

0 comments on commit 10de3d7

Please sign in to comment.