Skip to content

Commit

Permalink
test(VertxMainITCase): migrate tests to testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
baudoliver7 committed Jan 29, 2022
1 parent c3279f8 commit 4009fb2
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 83 deletions.
140 changes: 57 additions & 83 deletions src/test/java/com/artipie/VertxMainITCase.java
Expand Up @@ -4,115 +4,89 @@
*/
package com.artipie;

import com.artipie.asto.Content;
import com.artipie.asto.Key;
import com.artipie.asto.Storage;
import com.artipie.asto.fs.FileStorage;
import com.artipie.http.rs.RsStatus;
import com.artipie.test.ContainerResultMatcher;
import com.artipie.test.TestDeployment;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;
import org.hamcrest.MatcherAssert;
import org.hamcrest.core.IsEqual;
import org.junit.jupiter.api.AfterEach;
import org.cactoos.map.MapEntry;
import org.cactoos.map.MapOf;
import org.hamcrest.core.StringContains;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
* Test for {@link VertxMain}.
* @since 0.1
*/
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
class VertxMainITCase {
final class VertxMainITCase {

/**
* Repository name.
* Test deployments.
* @checkstyle VisibilityModifierCheck (15 lines)
*/
private static final String REPO_NAME = "my-file";

/**
* Temporary directory for all tests.
*
* @checkstyle VisibilityModifierCheck (3 lines)
*/
@TempDir
Path tmp;

/**
* Artipie server.
*/
private ArtipieServer server;

/**
* Test storage.
*/
private Storage storage;

/**
* Repo path.
*/
private Path path;
@RegisterExtension
final TestDeployment deployment = new TestDeployment(
new MapOf<>(
new MapEntry<>(
"artipie-config-key-present",
() -> TestDeployment.ArtipieContainer.defaultDefinition()
.withConfig("artipie-repo-config-key.yaml")
.withRepoConfig("binary/bin.yml", "my_configs/my-file")
),
new MapEntry<>(
"artipie-invalid-repo-config",
() -> TestDeployment.ArtipieContainer.defaultDefinition()
.withRepoConfig("invalid_repo.yaml", "my-file")
)
),
() -> new TestDeployment.ClientContainer("alpine:3.11")
.withWorkingDirectory("/w")
);

@BeforeEach
void init() {
this.path = this.tmp.resolve("repos");
this.storage = new FileStorage(this.path);
this.storage.save(
new Key.From(VertxMainITCase.REPO_NAME, "item.txt"), Content.EMPTY
).join();
void setUp() throws IOException {
this.deployment.assertExec(
"Failed to install deps",
new ContainerResultMatcher(),
"apk", "add", "--no-cache", "curl"
);
}

@Test
void startsWhenNotValidRepoConfigsArePresent() throws IOException {
this.storage.save(
new Key.From("invalid_repo.yaml"), new Content.From("any text.yaml".getBytes())
).join();
this.server = new ArtipieServer(
this.tmp, VertxMainITCase.REPO_NAME,
new RepoConfigYaml("file").withFileStorage(this.path),
Optional.empty()
this.deployment.putBinaryToArtipie(
"artipie-invalid-repo-config",
"Hello world".getBytes(),
"/var/artipie/data/my-file/item.txt"
);
final int port = this.server.start();
final HttpURLConnection con = (HttpURLConnection) new URL(this.formatUrl(port))
.openConnection();
con.setRequestMethod("GET");
MatcherAssert.assertThat(
"Artipie is started and responding 200 ",
con.getResponseCode(),
new IsEqual<>(Integer.parseInt(RsStatus.OK.code()))
this.deployment.assertExec(
"Artipie isn't started or not responding 200",
new ContainerResultMatcher(
ContainerResultMatcher.SUCCESS,
new StringContains("HTTP/1.1 200 OK")
),
"curl", "-i", "-X", "GET",
"http://artipie-invalid-repo-config:8080/my-file/item.txt"
);
con.disconnect();
}

@Test
void worksWhenRepoConfigsKeyIsPresent() throws IOException {
this.server = new ArtipieServer(
this.tmp, VertxMainITCase.REPO_NAME,
new RepoConfigYaml("file").withFileStorage(this.path),
Optional.of(new Key.From("my_configs"))
this.deployment.putBinaryToArtipie(
"artipie-config-key-present",
"Hello world".getBytes(),
"/var/artipie/data/my-file/item.txt"
);
final int port = this.server.start();
final HttpURLConnection con = (HttpURLConnection) new URL(this.formatUrl(port))
.openConnection();
con.setRequestMethod("GET");
MatcherAssert.assertThat(
"Artipie is started and responding 200 ",
con.getResponseCode(),
new IsEqual<>(Integer.parseInt(RsStatus.OK.code()))
this.deployment.assertExec(
"Artipie isn't started or not responding 200",
new ContainerResultMatcher(
ContainerResultMatcher.SUCCESS,
new StringContains("HTTP/1.1 200 OK")
),
"curl", "-i", "-X", "GET",
"http://artipie-config-key-present:8080/my-file/item.txt"
);
con.disconnect();
}

@AfterEach
void stop() {
this.server.stop();
}

private String formatUrl(final int port) {
return String.format("http://localhost:%s/%s/item.txt", port, VertxMainITCase.REPO_NAME);
}

}
8 changes: 8 additions & 0 deletions src/test/resources/artipie-repo-config-key.yaml
@@ -0,0 +1,8 @@
meta:
storage:
type: fs
path: /var/artipie/repo
layout: flat
base_url: http://artipie:8080/
meta:
repo_configs: my_configs

0 comments on commit 4009fb2

Please sign in to comment.