From aecb45a4941671a9ad7c6c1160a5ce3c2ef4a5a0 Mon Sep 17 00:00:00 2001 From: "milan.horvath" Date: Tue, 9 Jan 2024 09:41:42 +0100 Subject: [PATCH] feat: initial commit --- .../controllers/AvatarV1RestController.java | 31 ++++-------------- .../AvatarV1RestControllerTest.java | 32 ++++++++++++++++++- src/test/resources/data/avatar_empty.jpg | 0 src/test/resources/data/testdata.xml | 8 ++++- 4 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 src/test/resources/data/avatar_empty.jpg diff --git a/src/main/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestController.java b/src/main/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestController.java index 91a1281..d2cda53 100644 --- a/src/main/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestController.java +++ b/src/main/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestController.java @@ -14,10 +14,7 @@ import jakarta.ws.rs.core.UriInfo; import org.eclipse.microprofile.config.inject.ConfigProperty; -import org.jboss.resteasy.reactive.RestResponse; -import org.jboss.resteasy.reactive.server.ServerExceptionMapper; import org.tkit.quarkus.context.ApplicationContext; -import org.tkit.quarkus.jpa.exceptions.DAOException; import org.tkit.quarkus.log.cdi.LogService; import gen.io.github.onecx.user.profile.rs.external.v1.AvatarV1Api; @@ -142,39 +139,25 @@ public Response uploadAvatar(File body) { .concat(userProfile.getSmallAvatar().getId())); return Response.ok(imageInfo).build(); - } catch (IOException ioe) { - var e = exceptionMapper.exception("IO_EXCEPTION", ioe.getMessage()); + } catch (Exception ioe) { + var e = exceptionMapper.exception("UPLOAD_ERROR", ioe.getMessage()); return Response.status(Response.Status.BAD_REQUEST).entity(e).build(); } } - @ServerExceptionMapper - public RestResponse exception(DAOException ex) { - return exceptionMapper.exception(ex); - } - - private Image updateUserAvatar(UserProfile userProfile, byte[] avatarBytes, byte[] smallAvatarBytes, String mimeType) { - + private Image updateUserAvatar(UserProfile userProfile, byte[] avatarBytes, byte[] smallAvatarBytes, String mimeType) + throws IOException { var avatar = new Image(); userProfile.setAvatar(avatar); avatar.setImage(avatarBytes); avatar.setMimeType(mimeType); - try { - this.setUpAvatarDimensions(avatar); - } catch (IOException e) { - log.error("Exception thrown when setting up small avatar's metadata", e); - } + this.setUpAvatarDimensions(avatar); var smallAvatar = new Image(); userProfile.setSmallAvatar(smallAvatar); smallAvatar.setImage(smallAvatarBytes); smallAvatar.setMimeType("image/png"); - try { - this.setUpAvatarDimensions(smallAvatar); - } catch (IOException e) { - log.error("Exception thrown when setting up small avatar's metadata", e); - } - + this.setUpAvatarDimensions(smallAvatar); userProfile.setSmallAvatar(smallAvatar); userProfileDAO.update(userProfile); @@ -182,12 +165,10 @@ private Image updateUserAvatar(UserProfile userProfile, byte[] avatarBytes, byte } private byte[] convertToSmallImage(byte[] imgBytesArray) throws IOException { - return ImageUtilService.resizeImage(imgBytesArray, smallImgWidth, smallImgHeight); } private void setUpAvatarDimensions(Image avatar) throws IOException { - var image = ImageIO.read(new ByteArrayInputStream(avatar.getImage())); avatar.setHeight(image.getHeight()); avatar.setWidth(image.getWidth()); diff --git a/src/test/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestControllerTest.java b/src/test/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestControllerTest.java index 829db11..c1d4faf 100644 --- a/src/test/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestControllerTest.java +++ b/src/test/java/io/github/onecx/user/profile/rs/external/v1/controllers/AvatarV1RestControllerTest.java @@ -13,6 +13,7 @@ import org.tkit.quarkus.test.WithDBData; import gen.io.github.onecx.user.profile.rs.external.v1.model.ImageInfoDTO; +import gen.io.github.onecx.user.profile.rs.internal.model.ProblemDetailResponseDTO; import io.github.onecx.user.profile.test.AbstractTest; import io.quarkus.test.common.http.TestHTTPEndpoint; import io.quarkus.test.junit.QuarkusTest; @@ -24,6 +25,35 @@ public class AvatarV1RestControllerTest extends AbstractTest { @Test void testAvatarRestControler() throws URISyntaxException, IOException { + // test empty jpg image + File emptyAvatar = new File("src/test/resources/data/avatar_empty.jpg"); + var error = given() + .when() + .contentType("image/jpg") + .body(emptyAvatar) + .header(APM_HEADER_PARAM, createToken("user1", null)) + .put() + .then() + .statusCode(BAD_REQUEST.getStatusCode()) + .extract().as(ProblemDetailResponseDTO.class); + + assertThat(error).isNotNull(); + assertThat(error.getErrorCode()).isEqualTo("UPLOAD_ERROR"); + + var avatarInfo = given() + .when() + .contentType(APPLICATION_JSON) + .header(APM_HEADER_PARAM, createToken("user1", null)) + .get() + .then() + .statusCode(OK.getStatusCode()) + .extract().as(ImageInfoDTO.class); + + assertThat(avatarInfo).isNotNull(); + assertThat(avatarInfo.getImageUrl()).isNotNull(); + assertThat(avatarInfo.getSmallImageUrl()).isNotNull(); + + // add avatar File avatar = new File("src/test/resources/data/avatar_test.jpg"); var imageInfo = given() @@ -64,7 +94,7 @@ void testAvatarRestControler() throws URISyntaxException, IOException { assertThat(avatarByteArray).isNotNull(); - var avatarInfo = given() + avatarInfo = given() .when() .contentType(APPLICATION_JSON) .header(APM_HEADER_PARAM, createToken("user1", null)) diff --git a/src/test/resources/data/avatar_empty.jpg b/src/test/resources/data/avatar_empty.jpg new file mode 100644 index 0000000..e69de29 diff --git a/src/test/resources/data/testdata.xml b/src/test/resources/data/testdata.xml index 6f51e1f..a1dfbb5 100644 --- a/src/test/resources/data/testdata.xml +++ b/src/test/resources/data/testdata.xml @@ -2,9 +2,14 @@ + + + + + first_name="User" last_name="One" display_name="User One" email="user1@cap.de" type="MOBILE" number="123456" street="userstreet1" street_no="11" city="Muenchen" postal_code="12345" country="GERMANY" + avatar_guid="11-111" small_avatar_guid="11-112"/> +