-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add brotli4j compression test coverage
add some tweaks for decompression add some tests add some tweaks for compression tests refactor in common methods and fix XML logic
- Loading branch information
1 parent
a1ec8a3
commit 5c90b81
Showing
7 changed files
with
349 additions
and
0 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
30 changes: 30 additions & 0 deletions
30
...reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/Brotli4JHttpServerConfig.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,30 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import io.netty.handler.codec.compression.BrotliOptions; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
|
||
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(); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...dvanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/Brotli4JResource.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,55 @@ | ||
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; | ||
import org.jboss.logging.Logger; | ||
|
||
@Path("/compression") | ||
public class Brotli4JResource { | ||
|
||
private static final Logger LOG = Logger.getLogger(Brotli4JResource.class); | ||
|
||
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; | ||
|
||
@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("/text") | ||
@Produces(MediaType.TEXT_PLAIN) | ||
public String textPlainHttpResponse() { | ||
return DEFAULT_TEXT_PLAIN; | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
...dvanced-reactive/src/main/java/io/quarkus/ts/http/advanced/reactive/Brotli4JRestMock.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,74 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.HashMap; | ||
import java.util.Scanner; | ||
|
||
import jakarta.annotation.PostConstruct; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
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; | ||
|
||
@Inject | ||
private ObjectMapper objectMapper; | ||
|
||
@PostConstruct | ||
public void init() { | ||
loadJsonFile(); | ||
loadXmlResponse(); | ||
} | ||
|
||
private void loadJsonFile() { | ||
try (InputStream inputStream = getClass().getResourceAsStream("/json_sample.json")) { | ||
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() { | ||
try (InputStream inputStream = getClass().getResourceAsStream("/xml_sample.xml")) { | ||
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 enum ResponseType { | ||
JSON, XML | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
http/http-advanced-reactive/src/main/resources/json_sample.json
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,22 @@ | ||
{ | ||
"glossary": { | ||
"title": "example glossary", | ||
"GlossDiv": { | ||
"title": "Hello from a JSON sample", | ||
"GlossList": { | ||
"GlossEntry": { | ||
"ID": "SGML", | ||
"SortAs": "SGML", | ||
"GlossTerm": "Standard Generalized Markup Language", | ||
"Acronym": "SGML", | ||
"Abbrev": "ISO 8879:1986", | ||
"GlossDef": { | ||
"para": "A meta-markup language, used to create markup languages such as DocBook.", | ||
"GlossSeeAlso": ["GML", "XML"] | ||
}, | ||
"GlossSee": "markup" | ||
} | ||
} | ||
} | ||
} | ||
} |
160 changes: 160 additions & 0 deletions
160
...-advanced-reactive/src/test/java/io/quarkus/ts/http/advanced/reactive/Brotli4JHttpIT.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,160 @@ | ||
package io.quarkus.ts.http.advanced.reactive; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.Matchers.containsString; | ||
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.nullValue; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.restassured.response.Response; | ||
import jakarta.ws.rs.core.HttpHeaders; | ||
import jakarta.ws.rs.core.MediaType; | ||
import org.junit.jupiter.api.Tag; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.test.bootstrap.RestService; | ||
import io.quarkus.test.scenarios.QuarkusScenario; | ||
import io.quarkus.test.services.QuarkusApplication; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Scanner; | ||
|
||
@Tag("QQE-378") | ||
@QuarkusScenario | ||
public class Brotli4JHttpIT { | ||
@QuarkusApplication(classes = { Brotli4JHttpServerConfig.class, Brotli4JResource.class, Brotli4JRestMock.class | ||
}, properties = "compression.properties") | ||
static RestService app = new RestService(); | ||
|
||
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"; | ||
|
||
private final static int CONTENT_LENGTH_DEFAULT_TEXT_PLAIN = DEFAULT_TEXT_PLAIN.length(); | ||
|
||
private final static String JSON_CONTENT= "Hello from a JSON sample"; | ||
|
||
private final static String BROTLI_ENCODING = "br"; | ||
|
||
@Test | ||
public void checkTextPlainWithoutBrotli4JEncoding() { | ||
app.given() | ||
.get("/compression/text") | ||
.then() | ||
.statusCode(200) | ||
.and() | ||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(CONTENT_LENGTH_DEFAULT_TEXT_PLAIN)) | ||
.header(HttpHeaders.CONTENT_ENCODING, nullValue()) | ||
.body(is(DEFAULT_TEXT_PLAIN)).log().all(); | ||
} | ||
|
||
@Test | ||
public void checkTextPlainWithtBrotli4J() throws IOException { | ||
assertBrotli4JCompression("/compression/text", MediaType.TEXT_PLAIN, BROTLI_ENCODING, BROTLI_ENCODING, CONTENT_LENGTH_DEFAULT_TEXT_PLAIN); | ||
|
||
} | ||
|
||
public void assertBrotli4JCompression(String path, String contentHeaderType, String acceptHeaderEncoding, String expectedHeaderContentEncoding, int originalContentLength) { | ||
Response response = app.given() | ||
.when() | ||
.contentType(contentHeaderType) | ||
.header(HttpHeaders.ACCEPT_ENCODING, acceptHeaderEncoding) | ||
.get(path) | ||
.then() | ||
.statusCode(200) | ||
.header(HttpHeaders.CONTENT_ENCODING, expectedHeaderContentEncoding) | ||
.extract().response(); | ||
byte[] responseBody = response.getBody().asByteArray(); | ||
int compressedContentLength = responseBody.length; | ||
assertTrue(compressedContentLength < originalContentLength); | ||
|
||
} | ||
|
||
@Test | ||
public void disableCompression(){ | ||
System.setProperty("quarkus.http.enable-compression", "false"); | ||
app.given() | ||
.when() | ||
.contentType(MediaType.TEXT_PLAIN) | ||
.get("/compression/text") | ||
.then() | ||
.statusCode(200) | ||
.header(HttpHeaders.CONTENT_LENGTH, String.valueOf(CONTENT_LENGTH_DEFAULT_TEXT_PLAIN)) | ||
.header(HttpHeaders.CONTENT_ENCODING, nullValue()) | ||
.body(is(DEFAULT_TEXT_PLAIN)) | ||
.log().all(); | ||
} | ||
|
||
@Test | ||
public void checkJsonBrotli4JCompression() throws IOException { | ||
int originalJsonLength = calculateOriginalJsonLength("/json_sample.json"); | ||
assertBrotli4JCompression("/compression/brotli/json", MediaType.APPLICATION_JSON, BROTLI_ENCODING, BROTLI_ENCODING, originalJsonLength ); | ||
} | ||
|
||
@Test | ||
public void checkCompressedAndDecompressedWithQuarkus() { | ||
testCompressedAndDecompressed("/compression/text", DEFAULT_TEXT_PLAIN); | ||
} | ||
@Test | ||
public void checkCompressedAndDecompressedJSONWithQuarkus() { | ||
testCompressedAndDecompressed("/compression/brotli/json", JSON_CONTENT); | ||
} | ||
|
||
@Test | ||
public void checkCompressedAndDecompressedXMLWithQuarkus() { | ||
testCompressedAndDecompressed("/compression/brotli/xml", "Bob Dylan"); | ||
} | ||
|
||
@Test | ||
public void checkXmlBrotli4JCompression() throws IOException { | ||
int originalXMLLength = calculateXmlLength(); | ||
assertBrotli4JCompression("/compression/brotli/xml", MediaType.APPLICATION_XML, BROTLI_ENCODING, BROTLI_ENCODING, originalXMLLength); | ||
} | ||
|
||
private void testCompressedAndDecompressed(String compressionPath, String contentExpected) { | ||
Response response = app.given() | ||
.header(HttpHeaders.ACCEPT_ENCODING, BROTLI_ENCODING) | ||
.get(compressionPath) | ||
.then() | ||
.statusCode(200) | ||
.header(HttpHeaders.CONTENT_ENCODING, BROTLI_ENCODING) | ||
.log().all() | ||
.extract().response(); | ||
byte[] compressedBytes = response.asByteArray(); | ||
|
||
Response decompressionResponse = app.given() | ||
.header(HttpHeaders.CONTENT_ENCODING, BROTLI_ENCODING) | ||
.body(compressedBytes) | ||
.post("/compression/decompression") | ||
.then() | ||
.statusCode(200) | ||
.log().all() | ||
.extract().response(); | ||
assertThat(decompressionResponse.asString(), containsString(contentExpected)); | ||
} | ||
|
||
private int calculateOriginalJsonLength(String jsonFilePath) throws IOException { | ||
try (InputStream inputStream = getClass().getResourceAsStream(jsonFilePath)) { | ||
ObjectMapper objectMapper = new ObjectMapper(); | ||
JsonNode jsonNode = objectMapper.readTree(inputStream); | ||
String jsonString = objectMapper.writeValueAsString(jsonNode); | ||
return jsonString.getBytes().length; | ||
} | ||
} | ||
|
||
private int calculateXmlLength() throws IOException { | ||
try (InputStream inputStream = getClass().getResourceAsStream("/xml_sample.xml")) { | ||
Scanner scanner = new Scanner(inputStream, "UTF-8"); | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
while (scanner.hasNextLine()) { | ||
stringBuilder.append(scanner.nextLine()).append("\n"); | ||
} | ||
return stringBuilder.toString().length(); | ||
} | ||
} | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
http/http-advanced-reactive/src/test/resources/compression.properties
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,4 @@ | ||
quarkus.oidc.enabled=false | ||
quarkus.http.enable-compression=true | ||
quarkus.http.enable-decompression=true | ||
quarkus.http.compress-media-types=application/json,text/plain,application/xml |