Skip to content

Commit

Permalink
Add brotli4J compression test coverage
Browse files Browse the repository at this point in the history
 LOG from resource, and add new lines at the ends of files
rt imports
  • Loading branch information
jcarranzan committed May 2, 2024
1 parent a1ec8a3 commit 05b23d3
Show file tree
Hide file tree
Showing 11 changed files with 1,051 additions and 0 deletions.
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,30 @@
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.runtime.Startup;
import io.quarkus.vertx.http.HttpServerOptionsCustomizer;
import io.vertx.core.http.HttpServerOptions;

@Startup
@ApplicationScoped
public class Brotli4JHttpServerConfig implements HttpServerOptionsCustomizer {
// It depends on compression level that we want apply
private 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,71 @@
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 {

private 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;

/*
* @Inject
* Brotli4JServiceMock brotliMockService;
*/

@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.returnBigTextPlainResponse(Brotli4JRestMock.ResponseType.TEXT);
}

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

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

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

import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.exc.StreamReadException;
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 = "";

@Inject
private ObjectMapper objectMapper;

@Inject
@ConfigProperty(name = "textPlainFilePath")
private String textPlainFilePath;

@Inject
@ConfigProperty(name = "jsonFilePath")
private String jsonFilePath;

@Inject
@ConfigProperty(name = "xmlFilePath")
private String xmlFilePath;

@Inject
@ConfigProperty(name = "textBigPlainFilePath")
private String textBigPlainFilePath;

@PostConstruct
public void init() throws IOException {
loadJsonFile(jsonFilePath);
loadXmlResponse(xmlFilePath);
loadTextData(textPlainFilePath);
loadBigTextData(textBigPlainFilePath);
}

private void loadJsonFile(String jsonFilePath) {
try (InputStream inputStream = getClass().getResourceAsStream(jsonFilePath)) {
byte[] bytes = inputStream.readAllBytes();
jsonResponse = objectMapper.readValue(bytes, HashMap.class);
} 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)) {
Scanner scanner = new Scanner(inputStream, "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 returnTextPlainResponse(ResponseType type) {
if (type == ResponseType.TEXT) {
return textResponse;
} else {
throw new IllegalArgumentException("Invalid response type: " + type);
}
}

public String loadTextData(String textPlainFilePath) throws IOException {
byte[] textData = Files.readAllBytes(Paths.get(textPlainFilePath));
this.textResponse = new String(textData, StandardCharsets.UTF_8);
return this.textResponse;
}

public String loadBigTextData(String textBigPlainFilePath) throws IOException {
byte[] textData = Files.readAllBytes(Paths.get(textBigPlainFilePath));
this.bigTextResponse = new String(textData, StandardCharsets.UTF_8);
return this.bigTextResponse;
}

public String returnBigTextPlainResponse(ResponseType type) {
if (textBigPlainFilePath != null && type == ResponseType.TEXT) {
return bigTextResponse;
} else {
throw new IllegalStateException("textBigPlainFilePath must be defined");
}
}

public enum ResponseType {
JSON,
XML,
TEXT
}

}
Loading

0 comments on commit 05b23d3

Please sign in to comment.