Skip to content

Commit

Permalink
feat: finish validation and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jsteenke committed Feb 8, 2024
1 parent e55cbea commit 6a8b01f
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 87 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@
<modelPackage>gen.io.github.onecx.image.rs.internal.model</modelPackage>
<generateModelTests>true</generateModelTests>
<generateModelDocumentation>true</generateModelDocumentation>
<typeMappings>File=InputStream</typeMappings>
</configuration>
</execution>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Response getImage(String refId, String refType) {

Image image = imageDAO.findByRefIdAndRefType(refId, refType);

if (image == null) {
if (image == null || image.getImageData() == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
ByteArrayInputStream imageByteInputStream = new ByteArrayInputStream(image.getImageData());
Expand Down
32 changes: 8 additions & 24 deletions src/main/openapi/onecx-image-internal-openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
---
openapi: 3.0.3
info:
title: onecx-theme image import from file during the start of the application
Expand Down Expand Up @@ -27,19 +28,10 @@ paths:
requestBody:
required: true
content:
multipart/form-data:
application/octet-stream:
schema:
type: object
required:
- image
properties:
image:
format: binary
description: file data
type: string
encoding:
image:
contentType: application/octet-stream
type: string
format: binary
responses:
"201":
description: CREATED
Expand Down Expand Up @@ -102,19 +94,11 @@ paths:
requestBody:
required: true
content:
multipart/form-data:
application/octet-stream:
schema:
type: object
required:
- image
properties:
image:
format: binary
description: file data
type: string
encoding:
image:
contentType: application/octet-stream
minLength: 1
type: string
format: binary
responses:
"200":
description: OK
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,18 @@

import java.io.File;

import jakarta.ws.rs.core.MediaType;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.tkit.quarkus.test.WithDBData;

import gen.io.github.onecx.image.rs.internal.model.ImageInfoDTO;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;

@QuarkusTest
//@TestHTTPEndpoint(ImageRestController.class)
@TestHTTPEndpoint(ImageRestController.class)
@WithDBData(value = "data/testdata-internal.xml", deleteBeforeInsert = true, deleteAfterTest = true, rinseAndRepeat = true)
class ImageRestControllerTest {

Expand All @@ -23,14 +26,14 @@ void uploadImage() {

var refId = "themeName";
var refType = "LOGO";
File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var imgPost = given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.post("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode())
.extract()
Expand All @@ -42,70 +45,96 @@ void uploadImage_shouldReturnBadRequest_whenImageIs() {

var refId = "themeNameUpload";
var refType = "LOGO";
File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var imgPost = given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.post("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode());
}

@Test
void getImageTest() {

File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var refId = "themeNameGetTest";
var refType = "FAVICON";

given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.post("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode());

given()
.contentType(APPLICATION_JSON)
.pathParam("refId", refId)
.pathParam("refType", refType)
.get("/internal/images/{refId}/{refType}")
.get()
.then()
.statusCode(OK.getStatusCode());
}

@Test
void getImageTest_shouldReturnNotFound_whenImagesDoesNotExist() {

File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var refId = "themeNameGetTest";
var refType = "FAVICON";

given()
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode());

given()
.contentType(APPLICATION_JSON)
.pathParam("refId", refId)
.pathParam("refType", "wrongRefType")
.get()
.then()
.statusCode(NOT_FOUND.getStatusCode());
}

@Test
void updateImage() {

File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var refId = "themeNameUpload";
var refType = "LOGO";

given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.post("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode())
.extract()
.body().as(ImageInfoDTO.class);

var res = given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.put("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.put()
.then()
.statusCode(OK.getStatusCode())
.extract()
Expand All @@ -117,68 +146,34 @@ void updateImage() {
@Test
void updateImage_returnNotFound_whenEntryNotExists() {

File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
File file = new File(ImageRestControllerTest.class.getResource("/images/Testimage.png").getFile());
var refId = "themeNameUpdateFailed";
var refType = "LOGO";

given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", refId)
.pathParam("refType", refType)
.when()
.post("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.post()
.then()
.statusCode(CREATED.getStatusCode())
.extract()
.body().as(ImageInfoDTO.class);

var exception = given()
.multiPart("image", file)
.contentType("multipart/form-data")
.pathParam("refId", "wrongRefId")
.pathParam("refType", "wrongRefType")
.when()
.put("/internal/images/{refId}/{refType}")
.body(file)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.put()
.then()
.statusCode(NOT_FOUND.getStatusCode());

Assertions.assertNotNull(exception);
}

// @Test
// void updateImage_returnBadRequest_whenRefTypeNull() {
//
// File file = new File(ImageRestControllerTest.class.getResource("/META-INF/resources/Testimage.png").getFile());
// var refId = "themeNameUpdateFailed";
// var refType = "LOGO";
//
// given()
// .multiPart("image", file)
// .contentType("multipart/form-data")
// .pathParam("refId", refId)
// .pathParam("refType", refType)
// .when()
// .post("/internal/images/{refId}/{refType}")
// .then()
// .statusCode(CREATED.getStatusCode())
// .extract()
// .body().as(ImageInfoDTO.class);
//
// var exception = given()
// .multiPart("image", file)
// .contentType("multipart/form-data")
// .pathParam("refId", refId)
// .when()
// .put("/internal/images/{refId}")
// .then()
// .statusCode(BAD_REQUEST.getStatusCode())
// .extract().as(ProblemDetailResponseDTO.class);
//
// Assertions.assertNotNull(exception);
// Assertions.assertEquals("CONSTRAINT_VIOLATIONS", exception.getErrorCode());
// Assertions.assertNotNull(exception.getInvalidParams());
// Assertions.assertEquals(1, exception.getInvalidParams().size());
// }

}
File renamed without changes

0 comments on commit 6a8b01f

Please sign in to comment.