Skip to content

Commit

Permalink
50 Adding integration tests (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
cmatlak authored Feb 6, 2024
2 parents bf4a184 + 73dc9b4 commit 5333c5f
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 34 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
cache: maven

- name: Compile and test with Maven
run: mvn -B test --file pom.xml
run: mvn -B verify --file pom.xml

- name: Set up QEMU
uses: docker/setup-qemu-action@v2
Expand Down
14 changes: 11 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
FROM eclipse-temurin:21-jre-jammy
COPY target/dependency /lib
COPY target/classes /app
WORKDIR /app
ARG USERNAME=hazeuser
ARG USER_UID=1000
ARG USER_GID=$USER_UID
RUN groupadd --gid $USER_GID $USERNAME && \
useradd --uid $USER_UID --gid $USER_GID -m $USERNAME
ADD https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-core/2.20.0/log4j-core-2.20.0.jar ./lib/
ADD https://repo1.maven.org/maven2/org/apache/logging/log4j/log4j-api/2.20.0/log4j-api-2.20.0.jar ./lib/
COPY target/classes .
RUN chown --recursive $USERNAME:$USERNAME .
USER $USERNAME
EXPOSE 6379
ENTRYPOINT ["java","-cp" , "/app:/lib/*", "--enable-preview", "org.fungover.haze.Main"]
ENTRYPOINT ["java","-cp","/app:/app/lib/*","org.fungover.haze.Main"]
64 changes: 36 additions & 28 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,37 @@
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>5.1.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>testcontainers</artifactId>
<version>1.19.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.17.6</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-nop</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<compilerArgs>
--enable-preview
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand Down Expand Up @@ -95,6 +114,16 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<executions>
<execution>
<id>default-jar</id>
<phase>none</phase>
<configuration>
<finalName>unwanted</finalName>
<classifier>unwanted</classifier>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
Expand All @@ -114,7 +143,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.0.0-M9</version>
<version>3.2.1</version>
<executions>
<execution>
<goals>
Expand All @@ -123,9 +152,6 @@
</goals>
</execution>
</executions>
<configuration>
<argLine>@{argLine} --enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down Expand Up @@ -154,32 +180,14 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M9</version>
<version>3.2.5</version>
<configuration>
<environmentVariables>
<HAZE_PORT>6380</HAZE_PORT>
<HAZE_PASSWORD>12345</HAZE_PASSWORD>
</environmentVariables>
<argLine>@{argLine} --enable-preview</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>test</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<includeScope>runtime</includeScope>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3 changes: 1 addition & 2 deletions src/test/java/org/fungover/haze/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.fungover.haze.Main.printThreadDebug;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertFalse;


class MainTest {
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/org/fungover/haze/integration/HazeExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.fungover.haze.integration;

import org.junit.jupiter.api.extension.AfterAllCallback;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.images.builder.ImageFromDockerfile;
import redis.clients.jedis.JedisPooled;

import java.lang.reflect.Field;
import java.nio.file.Path;
import java.util.List;

public class HazeExtension implements BeforeAllCallback, AfterAllCallback, BeforeEachCallback {

private GenericContainer<?> haze;
private JedisPooled pool;

@Override
public void beforeAll(ExtensionContext extensionContext) throws Exception {
haze = new GenericContainer<>(new ImageFromDockerfile()
.withDockerfile(Path.of("./Dockerfile")))
.withExposedPorts(6379);
haze.start();
pool = new JedisPooled(haze.getHost(), haze.getFirstMappedPort());
}

@Override
public void afterAll(ExtensionContext context) {
// do nothing, Testcontainers handles container shutdown
if (pool != null)
pool.close();
}

@Override
public void beforeEach(ExtensionContext extensionContext) throws Exception {

// Get the list of test instances (instances of test classes)
final List<Object> testInstances =
extensionContext.getRequiredTestInstances().getAllInstances();

testInstances.forEach((ti) -> {
for (Field field : ti.getClass().getDeclaredFields()) {
if (field.isAnnotationPresent(Pool.class)) {
try {
field.set(ti, pool);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
});
}
}
90 changes: 90 additions & 0 deletions src/test/java/org/fungover/haze/integration/HazeIT.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package org.fungover.haze.integration;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import redis.clients.jedis.JedisPooled;
import redis.clients.jedis.Protocol;
import redis.clients.jedis.util.SafeEncoder;

import static org.assertj.core.api.Assertions.assertThat;

@ExtendWith(HazeExtension.class)
class HazeIT {

@Pool
JedisPooled pool;

@Test
void pingPong() {
//Simple PING command with no message should return PONG as simple string
var result = pool.sendCommand(Protocol.Command.PING);
assertThat(SafeEncoder.encode((byte[]) result)).isEqualTo("PONG");
//PING with message argument should return bulk string with the argument
result = pool.sendCommand(Protocol.Command.PING, "HELLO");
assertThat(SafeEncoder.encode((byte[]) result)).isEqualTo("HELLO");
//PING with message argument containing space should return bulk string with the argument
// result = pool.sendCommand(Protocol.Command.PING, "HELLO\r\n There");
// assertThat(SafeEncoder.encode((byte[]) result)).isEqualTo("HELLO\r\n There");
}

@Test
void setNx() {
pool.del("test");
pool.del("test1");
assertThat(pool.setnx("test", "test")).isEqualTo(1);
assertThat(pool.setnx("test1", "test")).isEqualTo(1);
//Key test already exists so should not be set
assertThat(pool.setnx("test", "test1")).isZero();
pool.del("test");
pool.del("test1");
}

@Test
void setGet() {
assertThat(pool.set("test", "test")).isEqualTo("OK");
assertThat(pool.get("test")).isEqualTo("test");
pool.del("test");
}

@Test
void exists() {
assertThat(pool.set("test", "test")).isEqualTo("OK");
assertThat(pool.exists("test")).isTrue();
pool.del("notused");
assertThat(pool.exists("notused")).isFalse();
}

@Test
void listLPushLPop() {
assertThat(pool.lpush("left", "first")).isEqualTo(1);
assertThat(pool.llen("left")).isEqualTo(1);
assertThat(pool.lpop("left")).isEqualTo("first");
assertThat(pool.llen("left")).isZero();
pool.del("left");
assertThat(pool.exists("left")).isFalse();
}

@Test
void listRPushRPop() {
assertThat(pool.rpush("right", "first")).isEqualTo(1);
assertThat(pool.llen("right")).isEqualTo(1);
assertThat(pool.rpop("right")).isEqualTo("first");
assertThat(pool.llen("right")).isZero();
pool.del("right");
assertThat(pool.exists("right")).isFalse();
}

@Test
void listKeyWithMultipleValues() {
assertThat(pool.lpush("test", "first")).isEqualTo(1);
assertThat(pool.lpush("test", "second")).isEqualTo(2);
assertThat(pool.llen("test")).isEqualTo(2);
assertThat(pool.lpush("test", "third", "fourth")).isEqualTo(4);
assertThat(pool.llen("test")).isEqualTo(4);
assertThat(pool.rpush("test", "fifth", "sixth")).isEqualTo(6);
assertThat(pool.llen("test")).isEqualTo(6);

pool.del("test");
assertThat(pool.exists("right")).isFalse();
}
}
9 changes: 9 additions & 0 deletions src/test/java/org/fungover/haze/integration/Pool.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.fungover.haze.integration;

import java.lang.annotation.*;

@Documented
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Pool {
}

0 comments on commit 5333c5f

Please sign in to comment.