From 91e77f5fe0a6da6fcafc412a9a94d99cf6708f49 Mon Sep 17 00:00:00 2001 From: Michael Vorburger Date: Thu, 22 Oct 2020 01:29:54 +0200 Subject: [PATCH] Add new ImageTest (FINERACT-1218) --- .../fineract/client/services/ImagesApi.java | 56 +++++++ .../fineract/client/util/FineractClient.java | 3 + .../newstyle/DocumentTest.java | 4 +- .../integrationtests/newstyle/ImageTest.java | 150 ++++++++++++++++++ .../integrationtests/StaffImageApiTest.java | 1 + .../integrationtests/common/ImageHelper.java | 1 + 6 files changed, 213 insertions(+), 2 deletions(-) create mode 100644 fineract-client/src/main/java/org/apache/fineract/client/services/ImagesApi.java create mode 100644 fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/ImageTest.java diff --git a/fineract-client/src/main/java/org/apache/fineract/client/services/ImagesApi.java b/fineract-client/src/main/java/org/apache/fineract/client/services/ImagesApi.java new file mode 100644 index 000000000..015c89214 --- /dev/null +++ b/fineract-client/src/main/java/org/apache/fineract/client/services/ImagesApi.java @@ -0,0 +1,56 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.client.services; + +import okhttp3.ResponseBody; +import retrofit2.Call; +import retrofit2.http.DELETE; +import retrofit2.http.GET; +import retrofit2.http.POST; +import retrofit2.http.PUT; + +/** + * Client API (Retrofit) for /images. + * + * This class entirely hand-written, inspired by DocumentsApiFixed, and from /images methods which currently end up in + * DefaultApi (see FINERACT-1222), but fixed for bugs + * in the code generation (see FINERACT-1227). + * + * @author Michael Vorburger.ch + */ +public interface ImagesApi { + + @POST("{entityType}/{entityId}/images") + @retrofit2.http.Multipart + Call create(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId, + @retrofit2.http.Part okhttp3.MultipartBody.Part file); + + @GET("{entityType}/{entityId}/images") + Call get(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId, + @retrofit2.http.Query("maxWidth") Integer maxWidth, @retrofit2.http.Query("maxHeight") Integer maxHeight, + @retrofit2.http.Query("output") String output); + + @PUT("{entityType}/{entityId}/images") + @retrofit2.http.Multipart + Call update(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId, + @retrofit2.http.Part okhttp3.MultipartBody.Part file); + + @DELETE("{entityType}/{entityId}/images") + Call delete(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId); +} diff --git a/fineract-client/src/main/java/org/apache/fineract/client/util/FineractClient.java b/fineract-client/src/main/java/org/apache/fineract/client/util/FineractClient.java index f81198bd1..5d18a54f1 100644 --- a/fineract-client/src/main/java/org/apache/fineract/client/util/FineractClient.java +++ b/fineract-client/src/main/java/org/apache/fineract/client/util/FineractClient.java @@ -69,6 +69,7 @@ import org.apache.fineract.client.services.GlobalConfigurationApi; import org.apache.fineract.client.services.GroupsApi; import org.apache.fineract.client.services.HolidaysApi; import org.apache.fineract.client.services.HooksApi; +import org.apache.fineract.client.services.ImagesApi; import org.apache.fineract.client.services.InterestRateChartApi; import org.apache.fineract.client.services.InterestRateSlabAKAInterestBandsApi; import org.apache.fineract.client.services.JournalEntriesApi; @@ -194,6 +195,7 @@ public final class FineractClient { public final GroupsApi groups; public final HolidaysApi holidays; public final HooksApi hooks; + public final ImagesApi images; public final InterestRateChartApi interestRateCharts; public final InterestRateSlabAKAInterestBandsApi interestRateChartLabs; public final JournalEntriesApi journalEntries; @@ -301,6 +303,7 @@ public final class FineractClient { groups = retrofit.create(GroupsApi.class); holidays = retrofit.create(HolidaysApi.class); hooks = retrofit.create(HooksApi.class); + images = retrofit.create(ImagesApi.class); interestRateCharts = retrofit.create(InterestRateChartApi.class); interestRateChartLabs = retrofit.create(InterestRateSlabAKAInterestBandsApi.class); journalEntries = retrofit.create(JournalEntriesApi.class); diff --git a/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/DocumentTest.java b/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/DocumentTest.java index aa690eaff..8ce7bf308 100644 --- a/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/DocumentTest.java +++ b/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/DocumentTest.java @@ -43,7 +43,7 @@ public class DocumentTest extends IntegrationTest { @Test @Order(1) - void retrieveAllDocuments() { + void retrieveAllDocuments() { assertThat(ok(fineract().documents.retrieveAllDocuments("clients", clientId))).isNotNull(); } @@ -83,7 +83,7 @@ public class DocumentTest extends IntegrationTest { ResponseBody r = ok(fineract().documents.downloadFile("clients", clientId, documentId)); assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); assertThat(r.bytes().length).isEqualTo(testFile.length()); - // NOK: assertThat(r.contentLength()).isEqualTo(testFile.length()); + assertThat(r.contentLength()).isEqualTo(-1); // TODO testFile.length() } @Test diff --git a/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/ImageTest.java b/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/ImageTest.java new file mode 100644 index 000000000..d2ea38d00 --- /dev/null +++ b/fineract-client/src/test/java/org/apache/fineract/integrationtests/newstyle/ImageTest.java @@ -0,0 +1,150 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.fineract.integrationtests.newstyle; + +import java.io.File; +import java.io.IOException; +import okhttp3.MediaType; +import okhttp3.ResponseBody; +import org.apache.fineract.client.services.ImagesApi; +import org.apache.fineract.client.util.Parts; +import org.junit.jupiter.api.Order; +import org.junit.jupiter.api.Test; +import retrofit2.Call; +import retrofit2.http.GET; +import retrofit2.http.Headers; + +/** + * Integration Test for /images API. + * + * @author Michael Vorburger.ch + */ +public class ImageTest extends IntegrationTest { + + // TODO This "new style" test is equivalent to the old StaffImageApiTest, so we could delete that (after + // FINERACT-1209) + + final File testImage = new File(getClass().getResource("/michael.vorburger-crepes.jpg").getFile()); + + Long clientId = new ClientTest().getClientId(); + // staffId is hard-coded to 1L below, because that always exists + + @Test + @Order(1) + void create() { + ok(fineract().images.create("staff", 1L, Parts.fromFile(testImage))); + ok(fineract().images.create("clients", clientId, Parts.fromFile(testImage))); + } + + @Test + @Order(2) + void getOriginalSize() throws IOException { + ResponseBody r = ok(fineract().images.get("staff", 1L, 3505, 1972, null)); + assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain")); + String encodedImage = r.string(); + assertThat(encodedImage).startsWith("data:image/jpeg;base64,"); + assertThat(encodedImage).hasLength(2846549); + assertThat(r.contentLength()).isEqualTo(-1); + } + + @Test + @Order(3) + void getSmallerSize() throws IOException { + ResponseBody r = ok(fineract().images.get("staff", 1L, 128, 128, null)); + assertThat(r.string()).hasLength(6591); + } + + @Test + @Order(4) + void getBiggerSize() throws IOException { + ResponseBody r = ok(fineract().images.get("staff", 1L, 9000, 6000, null)); + assertThat(r.string()).hasLength(2846549); + } + + @Test + @Order(5) + void getInlineOctetOutput() throws IOException { + // 3505x1972 is the exact original size of testFile + ResponseBody r = ok(fineract().images.get("staff", 1L, 3505, 1972, "inline_octet")); + assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); + assertThat(r.bytes().length).isEqualTo(testImage.length()); + assertThat(r.contentLength()).isEqualTo(testImage.length()); + } + + @Test + @Order(6) + void getOctetOutput() throws IOException { + ResponseBody r = ok(fineract().images.get("staff", 1L, 3505, 1972, "octet")); + assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); + assertThat(r.bytes().length).isEqualTo(testImage.length()); + assertThat(r.contentLength()).isEqualTo(testImage.length()); + } + + @Test + @Order(7) + void getAnotherOutput() throws IOException { + ResponseBody r = ok(fineract().images.get("staff", 1L, 3505, 1972, "abcd")); + assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain")); + assertThat(r.string()).startsWith("data:image/jpeg;base64,"); + } + + @Test + @Order(8) + void getText() throws IOException { + ResponseBody r = ok(fineract().createService(ImagesApiWithHeadersForTest.class).getText("staff", 1L, 3505, 1972, null)); + assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain")); + assertThat(r.string()).startsWith("data:image/jpeg;base64,"); + } + + @Test + @Order(9) + void getBytes() throws IOException { + ResponseBody r = ok(fineract().createService(ImagesApiWithHeadersForTest.class).getBytes("staff", 1L, 3505, 1972, null)); + assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); + assertThat(r.bytes().length).isEqualTo(testImage.length()); + } + + @Test + @Order(50) + void update() { + ok(fineract().images.update("staff", 1L, Parts.fromFile(testImage))); + } + + @Test + @Order(99) + void delete() { + ok(fineract().images.delete("staff", 1L)); + ok(fineract().images.delete("clients", clientId)); + } + + interface ImagesApiWithHeadersForTest extends ImagesApi { + + @Headers("Accept: text/plain") + @GET("{entityType}/{entityId}/images") + Call getText(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId, + @retrofit2.http.Query("maxWidth") Integer maxWidth, @retrofit2.http.Query("maxHeight") Integer maxHeight, + @retrofit2.http.Query("output") String output); + + @Headers("Accept: application/octet-stream") + @GET("{entityType}/{entityId}/images") + Call getBytes(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId, + @retrofit2.http.Query("maxWidth") Integer maxWidth, @retrofit2.http.Query("maxHeight") Integer maxHeight, + @retrofit2.http.Query("output") String output); + } +} diff --git a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/StaffImageApiTest.java b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/StaffImageApiTest.java index 9e1bd0e2f..54f7d598b 100644 --- a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/StaffImageApiTest.java +++ b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/StaffImageApiTest.java @@ -32,6 +32,7 @@ import org.apache.fineract.integrationtests.common.organisation.StaffHelper; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +@Deprecated // replaced by new ImageTest, can be deleted as FINERACT-1209 is fully implemented public class StaffImageApiTest { private RequestSpecification requestSpec; diff --git a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/ImageHelper.java b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/ImageHelper.java index ccf26bb0e..4a2f0b95e 100644 --- a/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/ImageHelper.java +++ b/fineract-provider/src/integrationTest/java/org/apache/fineract/integrationtests/common/ImageHelper.java @@ -24,6 +24,7 @@ import org.apache.http.HttpHeaders; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +@Deprecated // replaced by new ImageTest, can be deleted as FINERACT-1209 is fully implemented public final class ImageHelper { private ImageHelper() {