-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tests to abstract, add integration tests, add Redis o11y
- Loading branch information
Showing
8 changed files
with
297 additions
and
170 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,3 +31,5 @@ build/ | |
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
.envrc |
This file contains 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
This file contains 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
This file contains 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
152 changes: 152 additions & 0 deletions
152
src/test/java/com/javagrunt/listener/youtube/AbstractAppTests.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.javagrunt.listener.youtube; | ||
|
||
import com.redis.testcontainers.RedisContainer; | ||
import io.restassured.builder.RequestSpecBuilder; | ||
import io.restassured.specification.RequestSpecification; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
import org.springframework.restdocs.RestDocumentationContextProvider; | ||
import org.springframework.restdocs.RestDocumentationExtension; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
|
||
import static io.restassured.RestAssured.given; | ||
import static org.hamcrest.core.Is.is; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.modifyUris; | ||
import static org.springframework.restdocs.operation.preprocess.Preprocessors.preprocessRequest; | ||
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.document; | ||
import static org.springframework.restdocs.restassured.RestAssuredRestDocumentation.documentationConfiguration; | ||
|
||
@ExtendWith(RestDocumentationExtension.class) | ||
@Testcontainers | ||
public abstract class AbstractAppTests { | ||
|
||
static Logger logger = LoggerFactory.getLogger(AbstractAppTests.class); | ||
|
||
abstract int getPort(); | ||
|
||
private RequestSpecification spec; | ||
private static final Network network = Network.newNetwork(); | ||
|
||
static Network getNetwork(){ | ||
return network; | ||
} | ||
|
||
@Container | ||
@ServiceConnection(name = "redis") | ||
static final RedisContainer redis = new RedisContainer( | ||
RedisContainer.DEFAULT_IMAGE_NAME.withTag(RedisContainer.DEFAULT_TAG)) | ||
.withExposedPorts(6379) | ||
.withNetworkAliases("redis") | ||
.withNetwork(network); | ||
|
||
@BeforeEach | ||
void setUp(RestDocumentationContextProvider restDocumentation) { | ||
this.spec = new RequestSpecBuilder() | ||
.addFilter(documentationConfiguration(restDocumentation)) | ||
.build(); | ||
} | ||
@Test | ||
public void getShouldEchoHubChallenge() { | ||
given(this.spec) | ||
.filter(document("hello", | ||
preprocessRequest(modifyUris() | ||
.scheme("https") | ||
.host("youtube-listener.javagrunt.com") | ||
.removePort()))) | ||
.when() | ||
.port(getPort()) | ||
.get("/api/?hub.mode=subscribe&hub.challenge=CHALLENGE_STRING&hub.topic=somthingcool") | ||
.then() | ||
.assertThat().statusCode(is(200)) | ||
.assertThat().body(is("CHALLENGE_STRING")); | ||
} | ||
|
||
@Test | ||
void postShouldReturnSuccess() throws Exception { | ||
String exampleEvent = """ | ||
<feed xmlns:yt="http://www.youtube.com/xml/schemas/2015" | ||
xmlns="http://www.w3.org/2005/Atom"> | ||
<link rel="hub" href="https://pubsubhubbub.appspot.com"/> | ||
<link rel="self" href="https://www.youtube.com/xml/feeds/videos.xml?channel_id=CHANNEL_ID"/> | ||
<title>YouTube video feed</title> | ||
<updated>2015-04-01T19:05:24.552394234+00:00</updated> | ||
<entry> | ||
<id>yt:video:VIDEO_ID</id> | ||
<yt:videoId>VIDEO_ID</yt:videoId> | ||
<yt:channelId>CHANNEL_ID</yt:channelId> | ||
<title>Video title</title> | ||
<link rel="alternate" href="http://www.youtube.com/watch?v=VIDEO_ID"/> | ||
<author> | ||
<name>Channel title</name> | ||
<uri>http://www.youtube.com/channel/CHANNEL_ID</uri> | ||
</author> | ||
<published>2015-03-06T21:40:57+00:00</published> | ||
<updated>2015-03-09T19:05:24.552394234+00:00</updated> | ||
</entry> | ||
</feed> | ||
"""; | ||
given(this.spec) | ||
.filter(document("listen", | ||
preprocessRequest(modifyUris() | ||
.scheme("https") | ||
.host("youtube-listener.javagrunt.com") | ||
.removePort()))) | ||
.contentType("application/atom+xml") | ||
.body(exampleEvent) | ||
.when() | ||
.port(getPort()) | ||
.post("/api/") | ||
.then() | ||
.assertThat() | ||
.statusCode(is(200)); | ||
} | ||
|
||
@Test | ||
public void actuatorInfo() { | ||
given(this.spec) | ||
.filter(document("info", | ||
preprocessRequest(modifyUris() | ||
.scheme("https") | ||
.host("youtube-listener.javagrunt.com") | ||
.removePort()))) | ||
.when() | ||
.port(getPort()) | ||
.get("/actuator/info") | ||
.then() | ||
.assertThat().statusCode(is(200)); | ||
} | ||
|
||
@Test | ||
public void actuatorHealth() { | ||
given(this.spec) | ||
.filter(document("health", | ||
preprocessRequest(modifyUris() | ||
.scheme("https") | ||
.host("youtube-listener.javagrunt.com") | ||
.removePort()))) | ||
.when() | ||
.port(getPort()) | ||
.get("/actuator/health") | ||
.then() | ||
.assertThat().statusCode(is(200)); | ||
} | ||
|
||
@Test | ||
void redisShouldBeRunning() { | ||
Assertions.assertTrue(redis.isRunning()); | ||
} | ||
|
||
@AfterAll | ||
static void tearDown() { | ||
redis.stop(); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
src/test/java/com/javagrunt/listener/youtube/JavaVirtualMachineTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.javagrunt.listener.youtube; | ||
|
||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
|
||
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
class JavaVirtualMachineTest extends AbstractAppTests { | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
@Override | ||
int getPort() { | ||
return this.port; | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
src/test/java/com/javagrunt/listener/youtube/NativeImageIT.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.javagrunt.listener.youtube; | ||
|
||
import fr.brouillard.oss.jgitver.GitVersionCalculator; | ||
import org.apache.maven.shared.invoker.DefaultInvocationRequest; | ||
import org.apache.maven.shared.invoker.DefaultInvoker; | ||
import org.apache.maven.shared.invoker.InvocationResult; | ||
import org.apache.maven.shared.invoker.MavenInvocationException; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.testcontainers.DockerClientFactory; | ||
import org.testcontainers.containers.GenericContainer; | ||
import org.testcontainers.containers.wait.strategy.Wait; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.utility.LazyFuture; | ||
|
||
import java.io.File; | ||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.concurrent.Future; | ||
|
||
public class NativeImageIT extends AbstractAppTests { | ||
|
||
private static final Future<String> IMAGE_FUTURE = new LazyFuture<>() { | ||
@Override | ||
protected String resolve() { | ||
// Find project's root dir | ||
File cwd; | ||
cwd = new File("."); | ||
while (!new File(cwd, "mvnw").isFile()) { | ||
cwd = cwd.getParentFile(); | ||
} | ||
|
||
var request = new DefaultInvocationRequest() | ||
.addShellEnvironment("DOCKER_HOST", DockerClientFactory.instance().getTransportConfig().getDockerHost().toString()) | ||
.setPomFile(new File(cwd, "pom.xml")) | ||
.setGoals(List.of("spring-boot:build-image")) | ||
.setMavenExecutable(new File(cwd, "mvnw")) | ||
.setProfiles(List.of("native")); | ||
|
||
InvocationResult invocationResult; | ||
try { | ||
invocationResult = new DefaultInvoker().execute(request); | ||
} catch (MavenInvocationException e) { | ||
throw new RuntimeException(e); | ||
} | ||
|
||
if (invocationResult.getExitCode() != 0) { | ||
throw new RuntimeException(invocationResult.getExecutionException()); | ||
} | ||
|
||
String semanticVersion = null; | ||
File workDir = new File(System.getProperty("user.dir")); | ||
|
||
try (GitVersionCalculator jgitver = GitVersionCalculator.location(workDir)) { | ||
semanticVersion = jgitver.getVersion().split("-")[0]; | ||
} catch (Exception e) { | ||
logger.error("Error getting semantic version", e); | ||
} | ||
|
||
if (System.getProperty("os.arch").contains("aarch")) { | ||
return String.format("dashaun/com.javagrunt.listener.youtube:v%s-aarch_64", semanticVersion); | ||
} else { | ||
return String.format("dashaun/com.javagrunt.listener.youtube:v%s-amd_64", semanticVersion); | ||
} | ||
} | ||
}; | ||
|
||
private static int port; | ||
|
||
@Override | ||
int getPort() { | ||
return port; | ||
} | ||
|
||
|
||
@Container | ||
static final GenericContainer<?> APP = new GenericContainer<>(IMAGE_FUTURE) | ||
.withExposedPorts(8080) | ||
.withNetworkAliases("app") | ||
.withNetwork(getNetwork()) | ||
.withEnv("REDIS_PORT", "6379") | ||
.withEnv("REDIS_HOST", "redis") | ||
.waitingFor(Wait.forHttp("/actuator/health")) | ||
.withStartupTimeout(Duration.of(600, ChronoUnit.SECONDS)) | ||
.dependsOn(redis); | ||
|
||
@BeforeAll | ||
static void setUp() { | ||
APP.start(); | ||
port = APP.getFirstMappedPort(); | ||
} | ||
|
||
} |
Oops, something went wrong.