Test downloaded file name (FINERACT-1218)

This commit is contained in:
Michael Vorburger
2020-11-02 22:44:37 +01:00
committed by Aleksandar Vidakovic
parent e380df187d
commit 26cd903301
7 changed files with 88 additions and 12 deletions
@@ -19,8 +19,11 @@
package org.apache.fineract.client.util;
import com.google.common.truth.Truth;
import com.google.common.truth.Truth8;
import okhttp3.Headers;
import okhttp3.MediaType;
import org.junit.jupiter.api.Test;
import retrofit2.Response;
public class PartsTest {
@@ -43,4 +46,15 @@ public class PartsTest {
void nullMediaType() {
Truth.assertThat(Parts.mediaType(null)).isNull();
}
@Test
void fileName() {
Truth8.assertThat(Parts.fileName(Response.success(null, Headers.of("Content-Disposition", "attachment; filename=\"doc.pdf\""))))
.hasValue("doc.pdf");
}
@Test
void fileNameWithoutContentDisposition() {
Truth8.assertThat(Parts.fileName(Response.success(null))).isEmpty();
}
}
@@ -28,6 +28,7 @@ import org.apache.fineract.client.models.PostEntityTypeEntityIdDocumentsResponse
import org.apache.fineract.client.util.Parts;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import retrofit2.Response;
/**
* Integration Test for /documents API.
@@ -80,15 +81,18 @@ public class DocumentTest extends IntegrationTest {
@Test
@Order(4)
void downloadFile() throws IOException {
ResponseBody r = ok(fineract().documents.downloadFile("clients", clientId, documentId));
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(r.bytes().length).isEqualTo(testFile.length());
assertThat(r.contentLength()).isEqualTo(-1); // TODO testFile.length()
Response<ResponseBody> r = okR(fineract().documents.downloadFile("clients", clientId, documentId));
try (ResponseBody body = r.body()) {
assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(body.bytes().length).isEqualTo(testFile.length());
assertThat(body.contentLength()).isEqualTo(-1); // TODO testFile.length()
}
assertThat(Parts.fileName(r)).hasValue(testFile.getName());
}
@Test
@Order(10)
void updateDocument() {
void updateDocumentWithoutNewUpload() {
String newName = "Test changed name";
String newDescription = getClass().getName();
ok(fineract().documents.updateDocument("clients", clientId, documentId, null, newName, newDescription));
@@ -28,6 +28,7 @@ import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test;
import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET;
import retrofit2.http.Headers;
@@ -83,10 +84,15 @@ public class ImageTest extends IntegrationTest {
@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());
Response<ResponseBody> r = okR(fineract().images.get("staff", 1L, 3505, 1972, "inline_octet"));
try (ResponseBody body = r.body()) {
assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(body.bytes().length).isEqualTo(testImage.length());
assertThat(body.contentLength()).isEqualTo(testImage.length());
}
String expectedFileName = "test, testJPEG"; // without dot!
assertThat(Parts.fileName(r)).hasValue(expectedFileName);
}
@Test
@@ -43,6 +43,7 @@ import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestMethodOrder;
import retrofit2.Call;
import retrofit2.Response;
/**
* Integration Test for /documents API.
@@ -85,6 +86,10 @@ public abstract class IntegrationTest {
return Calls.ok(call);
}
protected <T> Response<T> okR(Call<T> call) {
return Calls.okR(call);
}
// as above, avoids import static CallSubject.assertThat
protected <T> CallSubject assertThat(Call<T> call) {
return CallSubject.assertThat(call);