Skip to content

Commit

Permalink
Add Brotli4J compression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed May 8, 2024
1 parent a1ec8a3 commit 183bf65
Show file tree
Hide file tree
Showing 72 changed files with 1,384 additions and 853 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import org.awaitility.core.ThrowingRunnable;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -90,7 +89,6 @@ public void extensionSetA() {
verifyValidPayloadPresent(app);
}

@Disabled // TODO: enable when 3.9.x platform is released
@Test
public void extensionSetB() {
QuarkusCliRestService app = createAppWithExtensions(EXTENSION_SET_B);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import static io.quarkus.ts.buildtimeanalytics.AnalyticsUtils.QUARKUS_ANALYTICS_URI_BASE_PROPERTY;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -51,7 +50,6 @@ public void extensionSetA() {
verifyValidPayloadPresent(app);
}

@Disabled // TODO: enable when 3.9.x platform is released
@Test
public void extensionSetB() {
QuarkusCliRestService app = createAppWithExtensions(EXTENSION_SET_B);
Expand Down
4 changes: 4 additions & 0 deletions http/http-advanced-reactive/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,9 @@
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.quarkus.ts.http.advanced.reactive;

import jakarta.enterprise.context.ApplicationScoped;

import io.netty.handler.codec.compression.BrotliOptions;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.quarkus.vertx.http.HttpServerOptionsCustomizer;
import io.vertx.core.http.HttpServerOptions;

@ApplicationScoped
public class Brotli4JHttpServerConfig implements HttpServerOptionsCustomizer {
// It depends on compression level that we want to apply, in this case we use level 4, but we won't test the level compression features.
private static final int compressionLevel = 4;

@Override
public void customizeHttpServer(HttpServerOptions options) {
options.addCompressor(getBrotliOptions(compressionLevel));
}

@Override
public void customizeHttpsServer(HttpServerOptions options) {
options.addCompressor(getBrotliOptions(compressionLevel));
}

private static BrotliOptions getBrotliOptions(int compressionLevel) {
return StandardCompressionOptions.brotli();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package io.quarkus.ts.http.advanced.reactive;

import java.util.HashMap;

import jakarta.inject.Inject;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.core.MediaType;

@Path("/compression")
public class Brotli4JResource {

public final static String DEFAULT_TEXT_PLAIN = "In life, you have to trust that every little bit helps. As you know," +
" every small step forward counts." +
" It's the accumulation of these efforts that ultimately leads to success." +
" So, don't underestimate the power of persistence and determination in achieving your dreams";

@Inject
Brotli4JRestMock brotli4JRestMock;

@GET
@Path("/brotli/json")
@Produces(MediaType.APPLICATION_JSON)
public HashMap<String, Object> jsonHttpCompressionResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.JSON);
}

@GET
@Path("/brotli/xml")
@Produces(MediaType.APPLICATION_XML)
public String xmlHttpCompressionResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.XML).get("xml").toString();
}

@POST
@Path("/decompression")
@Produces(MediaType.TEXT_PLAIN)
public String decompressionHttpResponse(byte[] compressedData) {
String decompressedData = new String(compressedData);
return decompressedData;
}

@GET
@Path("/default/text")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainDefaultHttpCompressionResponse() {
return DEFAULT_TEXT_PLAIN;
}

@GET
@Path("/text")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainHttpCompressionResponse() {
return brotli4JRestMock.returnTextPlainResponse(Brotli4JRestMock.ResponseType.TEXT);
}

@GET
@Path("/text/big")
@Produces(MediaType.TEXT_PLAIN)
public String textPlainBigHttpCompressionResponse() {
return brotli4JRestMock.returnTextPlainResponse(Brotli4JRestMock.ResponseType.BIG_TEXT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package io.quarkus.ts.http.advanced.reactive;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Scanner;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;

import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.exc.StreamReadException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.ObjectMapper;

@ApplicationScoped
public class Brotli4JRestMock {

private static final Logger LOGGER = Logger.getLogger(Brotli4JRestMock.class);

private static HashMap<String, Object> jsonResponse = null;
private static HashMap<String, Object> xmlResponse = null;

private String textResponse = "";
private String bigTextResponse = "";

private final ObjectMapper objectMapper;
private final String textPlainFilePath;
private final String jsonFilePath;
private final String xmlFilePath;
private final String textBigPlainFilePath;

public Brotli4JRestMock(ObjectMapper objectMapper) {
this.objectMapper = objectMapper;
this.textPlainFilePath = "/sample.txt";
this.jsonFilePath = "/sample.json";
this.xmlFilePath = "/sample.xml";
this.textBigPlainFilePath = "/big_sample.txt";
}

@PostConstruct
public void init() {
loadJsonFile(jsonFilePath);
loadXmlResponse(xmlFilePath);
textResponse = loadTextData(textPlainFilePath);
bigTextResponse = loadTextData(textBigPlainFilePath);
}

private void loadJsonFile(String jsonFilePath) {
try (InputStream inputStream = getClass().getResourceAsStream(jsonFilePath)) {
assert inputStream != null;
byte[] bytes = inputStream.readAllBytes();
jsonResponse = objectMapper.readValue(bytes, new TypeReference<>() {
});
} catch (StreamReadException | DatabindException e) {
LOGGER.error("Error occurred while deserializing JSON file: " + e.getMessage());
} catch (IOException e) {
LOGGER.error("Error occurred while reading the JSON file: " + e.getMessage());
}
}

private void loadXmlResponse(String xmlFilePath) {
try (InputStream inputStream = getClass().getResourceAsStream(xmlFilePath)) {
assert inputStream != null;
Scanner scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
StringBuilder stringBuilder = new StringBuilder();
while (scanner.hasNextLine()) {
stringBuilder.append(scanner.nextLine()).append("\n");
}
xmlResponse = new HashMap<>();
xmlResponse.put("xml", stringBuilder.toString());
} catch (IOException e) {
throw new RuntimeException("Error loading XML response", e);
}
}

public HashMap<String, Object> returnResponse(ResponseType type) {
if (type == ResponseType.JSON) {
return jsonResponse;
} else if (type == ResponseType.XML) {
return xmlResponse;
} else {
throw new IllegalArgumentException("Invalid response type: " + type);
}
}

public String loadTextData(String filePath) {
try (InputStream inputStream = getClass().getResourceAsStream(filePath)) {
if (inputStream != null) {
byte[] textData = inputStream.readAllBytes();
return new String(textData, StandardCharsets.UTF_8);
} else {
throw new IOException("File not found: " + filePath);
}
} catch (IOException e) {
throw new RuntimeException("Error loading text data from file: " + filePath, e);
}
}

public String returnTextPlainResponse(ResponseType type) {
if (type == ResponseType.TEXT) {
return textResponse;
} else if (type == ResponseType.BIG_TEXT) {
return bigTextResponse;
} else {
throw new IllegalArgumentException("Invalid response type: " + type);
}
}

public enum ResponseType {
JSON,
XML,
TEXT,
BIG_TEXT
}

}
Loading

0 comments on commit 183bf65

Please sign in to comment.