Skip to content

Commit

Permalink
refactor in common methods and fix XML logic
Browse files Browse the repository at this point in the history
  • Loading branch information
jcarranzan committed Apr 30, 2024
1 parent 9525520 commit 9fd6151
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 103 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,22 @@ public class Brotli4JResource {
@GET
@Path("/brotli/json")
@Produces(MediaType.APPLICATION_JSON)
public HashMap<String, Object> jsonHttpResponse() {
public HashMap<String, Object> jsonHttpCompressionResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.JSON);
}

@GET
@Path("/brotli/xml")
@Produces(MediaType.APPLICATION_XML)
public HashMap<String, Object> xmlHttpResponse() {
return brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.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);
LOG.info("DECOMPRESSED DATA " + decompressedData);
return decompressedData;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Scanner;

import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.ApplicationScoped;
Expand All @@ -27,33 +26,13 @@ public class Brotli4JRestMock {
@Inject
private ObjectMapper objectMapper;

@Inject
private CityListWrapperSerializer cityListWrapperSerializer;

@PostConstruct
public void init() {
parseJsonFile();
createXmlFile();
}

public void createXmlFile() {
List<City> cityList = new ArrayList<>();

cityList.add(new City("San Bernardino", "EEUU"));
cityList.add(new City("Brno", "Czech Republic"));
cityList.add(new City("Zaragoza", "Spain"));

CityListDTO cityListDTO = new CityListDTO(cityList);

String xmlData = cityListWrapperSerializer.toXML(cityListDTO);

// Store the XML data in the xmlResponse HashMap
xmlResponse = new HashMap<>();
xmlResponse.put("xmlData", xmlData);

loadJsonFile();
loadXmlResponse();
}

private void parseJsonFile() {
private void loadJsonFile() {
try (InputStream inputStream = getClass().getResourceAsStream("/json_sample.json")) {
byte[] bytes = inputStream.readAllBytes();
jsonResponse = objectMapper.readValue(bytes, HashMap.class);
Expand All @@ -64,6 +43,21 @@ private void parseJsonFile() {
}
}


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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import io.restassured.response.Response;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

Expand All @@ -22,13 +20,12 @@

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Scanner;

@Tag("QQE-378")
@QuarkusScenario
public class Brotli4JHttpIT {
@QuarkusApplication(classes = { Brotli4JHttpServerConfig.class, Brotli4JResource.class, Brotli4JRestMock.class,
City.class,CityListDTO.class,CityListWrapper.class, CityListWrapperSerializer.class
@QuarkusApplication(classes = { Brotli4JHttpServerConfig.class, Brotli4JResource.class, Brotli4JRestMock.class
}, properties = "compression.properties")
static RestService app = new RestService();

Expand Down Expand Up @@ -100,64 +97,28 @@ public void checkJsonBrotli4JCompression() throws IOException {

@Test
public void checkCompressedAndDecompressedWithQuarkus() {
// Send the compressed text to the endpoint
Response response = app.given()
.header(HttpHeaders.ACCEPT_ENCODING, BROTLI_ENCODING)
.get("/compression/text")
.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();
Assertions.assertEquals(DEFAULT_TEXT_PLAIN, decompressionResponse.asString());

testCompressedAndDecompressed("/compression/text", DEFAULT_TEXT_PLAIN);
}

@Test
public void checkCompressedAndDecompressedJSONWithQuarkus() {
// Send the compressed text to the endpoint
Response response = app.given()
.when()
.contentType(MediaType.APPLICATION_JSON)
.header(HttpHeaders.ACCEPT_ENCODING, BROTLI_ENCODING)
.get("/compression/brotli/json")
.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.body().asString(), containsString(JSON_CONTENT));
testCompressedAndDecompressed("/compression/brotli/json", JSON_CONTENT);
}

@Test
@Disabled
public void checkCompressedAndDecompressedXMLWithQuarkus() {
// Send the compressed text to the endpoint
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()
.when()
.contentType(MediaType.APPLICATION_XML)
.header(HttpHeaders.ACCEPT_ENCODING, BROTLI_ENCODING)
.get("/compression/brotli/xml")
.get(compressionPath)
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_ENCODING, BROTLI_ENCODING)
Expand All @@ -173,23 +134,9 @@ public void checkCompressedAndDecompressedXMLWithQuarkus() {
.statusCode(200)
.log().all()
.extract().response();
System.out.println("XML IS " + decompressionResponse.asString());
assertThat(decompressionResponse.body().asString(), containsString("Spain"));
assertThat(decompressionResponse.asString(), containsString(contentExpected));
}

@Test
@Disabled("There is an issue with XML compression: [Fatal Error] :1:1: Content is not allowed in prolog." )
public void checkXmlBrotli4JCompression() {
app.given()
.when()
.contentType(MediaType.APPLICATION_XML)
.header(HttpHeaders.ACCEPT_ENCODING, BROTLI_ENCODING)
.get("/compression/brotli/xml")
.then()
.statusCode(200)
.header(HttpHeaders.CONTENT_ENCODING, BROTLI_ENCODING)
.log().all();
}
private int calculateOriginalJsonLength(String jsonFilePath) throws IOException {
try (InputStream inputStream = getClass().getResourceAsStream(jsonFilePath)) {
ObjectMapper objectMapper = new ObjectMapper();
Expand All @@ -199,11 +146,15 @@ private int calculateOriginalJsonLength(String jsonFilePath) throws IOException
}
}

private int calculateXMLLength() {
Brotli4JRestMock brotli4JRestMock = new Brotli4JRestMock();
HashMap<String, Object> xmlResponse = brotli4JRestMock.returnResponse(Brotli4JRestMock.ResponseType.XML);
String xmlData = (String) xmlResponse.get("xmlData");
return xmlData.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();
}
}

}

0 comments on commit 9fd6151

Please sign in to comment.