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 10de3d7 commit 04a21a4
Show file tree
Hide file tree
Showing 9 changed files with 675 additions and 675 deletions.
114 changes: 57 additions & 57 deletions src/test/java/com/spotify/app/AbstractTestcontainers.java
Original file line number Diff line number Diff line change
@@ -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();
// }
//}
128 changes: 64 additions & 64 deletions src/test/java/com/spotify/app/journey/AlbumIT.java
Original file line number Diff line number Diff line change
@@ -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 = "[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);

}
}
//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 = "[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);
//
// }
//}
Loading

0 comments on commit 04a21a4

Please sign in to comment.