Skip to content

Commit

Permalink
feat: initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
milan.horvath committed Jan 9, 2024
1 parent 6b69021 commit aecb45a
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -142,52 +139,36 @@ 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<ProblemDetailResponseDTO> 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);
return avatar;
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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()
Expand Down Expand Up @@ -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))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 7 additions & 1 deletion src/test/resources/data/testdata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@
<!-- This xml was generated by https://github.com/lorislab/dbx2x -->
<dataset>

<!-- Image -->
<IMAGE guid="11-111" optlock="0" modificationuser="manual" tenant_id="tenant-100" height="100" width="100" mime_type="png"/>
<IMAGE guid="11-112" optlock="0" modificationuser="manual" tenant_id="tenant-100" height="50" width="50" mime_type="png"/>

<!-- UserProfile -->
<USM_USER_PROFILE guid="11-111" optlock="0" user_id="user1" tenant_id="tenant-100" identity_provider="keycloak" identity_provider_id="keycloak" organization="Company1" hide_my_profile="false" locale="de" timezone="de" menu_mode="STATIC" color_scheme="AUTO"
first_name="User" last_name="One" display_name="User One" email="[email protected]" type="MOBILE" number="123456" street="userstreet1" street_no="11" city="Muenchen" postal_code="12345" country="GERMANY"/>
first_name="User" last_name="One" display_name="User One" email="[email protected]" 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"/>
<USM_USER_PROFILE guid="22-111" optlock="0" user_id="user2" tenant_id="tenant-100" identity_provider="keycloak" identity_provider_id="keycloak" organization="Company1" hide_my_profile="false" locale="en" timezone="en" menu_mode="HORIZONTAL" color_scheme="DARK"
first_name="User" last_name="Two" display_name="User Two" email="[email protected]" type="LANDLINE" number="987654" street="userstreet2" street_no="22" city="Muenchen" postal_code="12345" country="GERMANY"/>
<USM_USER_PROFILE guid="33-111" optlock="0" user_id="user3" tenant_id="tenant-200" identity_provider="keycloak" identity_provider_id="keycloak" organization="Company2" hide_my_profile="true" locale="de" timezone="de" menu_mode="SLIM" color_scheme="LIGHT"
Expand All @@ -17,4 +22,5 @@
<USM_PREFERENCE guid="11-112" optlock="0" tenant_id="tenant-100" user_id="user1" application_id="app1" name="preference2" description="Preference 2 app 1 for user 1" value="P2A1U1" />
<USM_PREFERENCE guid="11-113" optlock="0" tenant_id="tenant-100" user_id="user1" application_id="app2" name="preference1" description="Preference 1 app 2 for user 1" value="P1A2U1" />
<USM_PREFERENCE guid="11-114" optlock="0" tenant_id="tenant-100" user_id="user1" application_id="app2" name="preference2" description="Preference 2 app 2 for user 1" value="P2A2U1" />

</dataset>

0 comments on commit aecb45a

Please sign in to comment.