Created
November 14, 2024 09:02
-
-
Save EmmanuelDemey/80291ea2e73d6c042e9d5eddbf8a88a0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.demo; | |
import eu.rekawek.toxiproxy.Proxy; | |
import eu.rekawek.toxiproxy.ToxiproxyClient; | |
import eu.rekawek.toxiproxy.model.ToxicDirection; | |
import org.junit.jupiter.api.*; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | |
import org.springframework.boot.test.context.SpringBootTest; | |
import org.springframework.http.MediaType; | |
import org.springframework.test.context.DynamicPropertyRegistry; | |
import org.springframework.test.context.DynamicPropertySource; | |
import org.springframework.test.web.servlet.MockMvc; | |
import org.testcontainers.containers.*; | |
import org.testcontainers.containers.wait.strategy.Wait; | |
import org.testcontainers.elasticsearch.ElasticsearchContainer; | |
import org.testcontainers.junit.jupiter.Container; | |
import org.testcontainers.junit.jupiter.Testcontainers; | |
import java.io.File; | |
import java.io.IOException; | |
import static org.hamcrest.Matchers.is; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | |
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | |
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | |
@SpringBootTest | |
@AutoConfigureMockMvc | |
@Testcontainers | |
class PublicResourcesAuthorizationsTest { | |
private static Proxy proxy; | |
@Autowired | |
private MockMvc mockMvc; | |
static Network network = Network.newNetwork(); | |
@Container | |
static PostgreSQLContainer postgre = new PostgreSQLContainer("postgres:latest"); | |
@Container | |
static GenericContainer pulsarContainer = new GenericContainer<>("apachepulsar/pulsar:2.10.0") | |
.withExposedPorts(6650) | |
.withCommand("bin/pulsar", "standalone"); | |
@Container | |
static ElasticsearchContainer container = new ElasticsearchContainer( | |
"docker.elastic.co/elasticsearch/elasticsearch:8.15.3" | |
).withEnv("xpack.security.transport.ssl.enabled", "false") | |
.withEnv("xpack.security.http.ssl.enabled", "false") | |
.withNetwork(network) | |
.withNetworkAliases("elastic"); | |
@Container | |
static ToxiproxyContainer toxiproxy = new ToxiproxyContainer("ghcr.io/shopify/toxiproxy:2.11.0").withNetwork(network); | |
@BeforeAll | |
static void setUp() { | |
//postgre.withReuse(true); | |
/* | |
toxiproxy.start(); | |
//pulsarContainer.withReuse(true); | |
pulsarContainer.start(); | |
postgre.start(); | |
container.start();*/ | |
} | |
@AfterAll | |
static void tearDown() { | |
/*toxiproxy.stop(); | |
pulsarContainer.stop(); | |
postgre.start(); | |
container.start();*/ | |
} | |
/* @Container | |
public static DockerComposeContainer<?> compose = new DockerComposeContainer<>(new File("compose.yml")) | |
.withExposedService("pulsar", 6650, Wait.forListeningPort()) | |
.withExposedService("postgres", 5432, Wait.forListeningPort()) | |
.withExposedService("elasticsearch", 9200, Wait.forListeningPort()) | |
.withLogConsumer("elasticsearch", outputFrame -> System.out.println(outputFrame.getUtf8String())); | |
*/ | |
@DynamicPropertySource | |
static void configureProperties(DynamicPropertyRegistry registry) throws IOException { | |
/* | |
String postgresHost = compose.getServiceHost("postgres", 5432); | |
Integer postgresPort = compose.getServicePort("postgres", 5432); | |
String postgreJDBCUrl = "jdbc:postgresql://" + postgresHost + ":" + postgresPort + "/testdb"; | |
registry.add("spring.datasource.url", () -> postgreJDBCUrl); | |
registry.add("spring.datasource.username", () -> "postgres"); | |
registry.add("spring.datasource.password", () -> "postgres"); | |
registry.add("spring.pulsar.client.service-url", () -> "pulsar://localhost:" + compose.getServicePort("pulsar", 6650)); | |
String elasticsearchHost = compose.getServiceHost("elasticsearch", 9200); | |
Integer elastisearchPort = compose.getServicePort("elasticsearch", 9200); | |
registry.add("elasticsearch.url", () -> "http://" + elasticsearchHost + ":" + elastisearchPort); | |
*/ | |
var toxiproxyClient = new ToxiproxyClient(toxiproxy.getHost(), toxiproxy.getControlPort()); | |
proxy = toxiproxyClient.createProxy("elastic", "0.0.0.0:8666", "elastic:9200"); | |
registry.add("spring.datasource.url", postgre::getJdbcUrl); | |
registry.add("spring.datasource.username", postgre::getUsername); | |
registry.add("spring.datasource.password", postgre::getPassword); | |
registry.add("elasticsearch.url", () -> toxiproxy.getHost()+":"+toxiproxy.getMappedPort(8666)); | |
registry.add("spring.pulsar.client.service-url", () -> "pulsar://localhost:" + pulsarContainer.getFirstMappedPort()); | |
} | |
@Test | |
public void testSearchUserByKeyword() throws Exception { | |
proxy.toxics().latency("latency", ToxicDirection.DOWNSTREAM, 1_000); | |
mockMvc.perform(post("/user/add") | |
.param("name", "Emmanuel Demey") | |
.param("email", "[email protected]") | |
.contentType(MediaType.APPLICATION_FORM_URLENCODED)); | |
Thread.sleep(5000); | |
mockMvc.perform(get("/user")) | |
.andExpect(status().isOk()) | |
.andExpect(jsonPath("$[0].name", is("Emmanuel Demey"))); | |
mockMvc.perform(post("/user/search") | |
.param("keyword", "Emmanuel") | |
.contentType(MediaType.APPLICATION_FORM_URLENCODED)) | |
.andExpect(status().isOk()) | |
.andExpect(jsonPath("$[0].name", is("Emmanuel Demey"))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment