Merge branch 'master' of https://github.com/abh1navv/tutorials into BAEL-3201
This commit is contained in:
@@ -0,0 +1,85 @@
|
||||
package com.baeldung.keycloaktestcontainers;
|
||||
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.Collections;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
import org.apache.http.client.utils.URIBuilder;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.boot.json.JacksonJsonParser;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.boot.web.server.LocalServerPort;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.util.LinkedMultiValueMap;
|
||||
import org.springframework.util.MultiValueMap;
|
||||
import org.springframework.web.reactive.function.BodyInserters;
|
||||
import org.springframework.web.reactive.function.client.WebClient;
|
||||
|
||||
import dasniko.testcontainers.keycloak.KeycloakContainer;
|
||||
import io.restassured.RestAssured;
|
||||
|
||||
@ContextConfiguration(initializers = { IntegrationTest.Initializer.class })
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
|
||||
public abstract class IntegrationTest {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationTest.class.getName());
|
||||
|
||||
@LocalServerPort
|
||||
private int port;
|
||||
|
||||
static final KeycloakContainer keycloak = new KeycloakContainer().withRealmImportFile("keycloak/realm-export.json");
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
RestAssured.baseURI = "http://localhost:" + port;
|
||||
}
|
||||
|
||||
static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
|
||||
|
||||
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
|
||||
keycloak.start();
|
||||
TestPropertyValues.of("keycloak.auth-server-url=" + keycloak.getAuthServerUrl())
|
||||
.applyTo(configurableApplicationContext.getEnvironment());
|
||||
}
|
||||
}
|
||||
|
||||
protected String getJaneDoeBearer() {
|
||||
|
||||
try {
|
||||
URI authorizationURI = new URIBuilder(keycloak.getAuthServerUrl() + "/realms/baeldung/protocol/openid-connect/token").build();
|
||||
WebClient webclient = WebClient.builder()
|
||||
.build();
|
||||
MultiValueMap<String, String> formData = new LinkedMultiValueMap<>();
|
||||
formData.put("grant_type", Collections.singletonList("password"));
|
||||
formData.put("client_id", Collections.singletonList("baeldung-api"));
|
||||
formData.put("username", Collections.singletonList("jane.doe@baeldung.com"));
|
||||
formData.put("password", Collections.singletonList("s3cr3t"));
|
||||
|
||||
String result = webclient.post()
|
||||
.uri(authorizationURI)
|
||||
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
|
||||
.body(BodyInserters.fromFormData(formData))
|
||||
.retrieve()
|
||||
.bodyToMono(String.class)
|
||||
.block();
|
||||
|
||||
JacksonJsonParser jsonParser = new JacksonJsonParser();
|
||||
|
||||
return "Bearer " + jsonParser.parseMap(result)
|
||||
.get("access_token")
|
||||
.toString();
|
||||
} catch (URISyntaxException e) {
|
||||
LOGGER.error("Can't obtain an access token from Keycloak!", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.baeldung.keycloaktestcontainers;
|
||||
|
||||
import static io.restassured.RestAssured.given;
|
||||
import static org.hamcrest.Matchers.equalTo;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class UserControllerIntegrationTest extends IntegrationTest {
|
||||
|
||||
@Test
|
||||
void givenAuthenticatedUser_whenGetMe_shouldReturnMyInfo() {
|
||||
|
||||
given().header("Authorization", getJaneDoeBearer())
|
||||
.when()
|
||||
.get("/users/me")
|
||||
.then()
|
||||
.body("username", equalTo("janedoe"))
|
||||
.body("lastname", equalTo("Doe"))
|
||||
.body("firstname", equalTo("Jane"))
|
||||
.body("email", equalTo("jane.doe@baeldung.com"));
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
package com.baeldung.redistestcontainers.service;
|
||||
|
||||
import com.baeldung.redistestcontainers.hash.Product;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.testcontainers.containers.GenericContainer;
|
||||
import org.testcontainers.utility.DockerImageName;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
|
||||
@SpringBootTest
|
||||
public class ProductServiceIntegrationTest {
|
||||
|
||||
static {
|
||||
GenericContainer<?> redis = new GenericContainer<>(DockerImageName.parse("redis:5.0.3-alpine"))
|
||||
.withExposedPorts(6379);
|
||||
redis.start();
|
||||
System.setProperty("spring.redis.host", redis.getHost());
|
||||
System.setProperty("spring.redis.port", redis.getMappedPort(6379).toString());
|
||||
}
|
||||
|
||||
@Autowired
|
||||
private ProductService productService;
|
||||
|
||||
@Test
|
||||
void givenProductCreated_whenGettingProductById_thenProductExistsAndHasSameProperties() {
|
||||
Product product = new Product("1", "Test Product", 10.0);
|
||||
productService.createProduct(product);
|
||||
Product productFromDb = productService.getProduct("1");
|
||||
assertEquals("1", productFromDb.getId());
|
||||
assertEquals("Test Product", productFromDb.getName());
|
||||
assertEquals(10.0, productFromDb.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenProductCreatedAndUpdated_whenGettingTheProduct_thenUpdatedProductReturned() {
|
||||
Product product = new Product("1", "Test Product", 10.0);
|
||||
productService.createProduct(product);
|
||||
Product productFromDb = productService.getProduct("1");
|
||||
assertEquals("1", productFromDb.getId());
|
||||
assertEquals("Test Product", productFromDb.getName());
|
||||
assertEquals(10.0, productFromDb.getPrice());
|
||||
productFromDb.setName("Updated Product");
|
||||
productFromDb.setPrice(20.0);
|
||||
productService.updateProduct(productFromDb);
|
||||
Product updatedProductFromDb = productService.getProduct("1");
|
||||
assertEquals("Updated Product", updatedProductFromDb.getName());
|
||||
assertEquals(20.0, updatedProductFromDb.getPrice());
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenProductCreatedAndDeleted_whenGettingTheProduct_thenNoProductReturned() {
|
||||
Product product = new Product("1", "Test Product", 10.0);
|
||||
productService.createProduct(product);
|
||||
Product productFromDb = productService.getProduct("1");
|
||||
assertEquals("1", productFromDb.getId());
|
||||
assertEquals("Test Product", productFromDb.getName());
|
||||
assertEquals(10.0, productFromDb.getPrice());
|
||||
productService.deleteProduct("1");
|
||||
Product deletedProductFromDb = productService.getProduct("1");
|
||||
assertNull(deletedProductFromDb);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenProductCreated_whenGettingProductById_thenSameProductReturned() {
|
||||
Product product = new Product("1", "Test Product", 10.0);
|
||||
productService.createProduct(product);
|
||||
Product productFromDb = productService.getProduct("1");
|
||||
assertEquals("1", productFromDb.getId());
|
||||
assertEquals("Test Product", productFromDb.getName());
|
||||
assertEquals(10.0, productFromDb.getPrice());
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,4 @@
|
||||
# logging.level.com.baeldung.testloglevel=DEBUG
|
||||
|
||||
# logging.level.root=INFO
|
||||
keycloak.enabled=true
|
||||
keycloak.realm=baeldung
|
||||
keycloak.resource=baeldung-api
|
||||
keycloak.auth-server-url=http://localhost:8081
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user