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
@@ -23,7 +23,7 @@ import retrofit2.Call;
import retrofit2.Response; import retrofit2.Response;
/** /**
* Exception thrown by {@link Calls#ok(Call)} when {@link Call}s fail. * Exception thrown by {@link Calls} utility when {@link Call}s fail.
* *
* @author Michael Vorburger.ch * @author Michael Vorburger.ch
*/ */
@@ -32,7 +32,8 @@ public final class Calls {
private Calls() {} private Calls() {}
/** /**
* Execute a Call, expecting success. * Execute a Call, expecting success, returning strongly typed body. This covers the most typical use and is thus
* most used.
* *
* @param call * @param call
* the Call to execute * the Call to execute
@@ -42,13 +43,38 @@ public final class Calls {
* [200..300) successful * [200..300) successful
*/ */
public static <T> T ok(Call<T> call) throws CallFailedRuntimeException { public static <T> T ok(Call<T> call) throws CallFailedRuntimeException {
return okR(call).body();
}
/**
* Execute a Call, expecting success, returning Response. This is rarely used, and only useful if you need access to
* e.g. the headers of the response.
*
* @param call
* the Call to execute
* @return the Response of the successful call (never null)
* @throws CallFailedRuntimeException
* thrown either if a problem occurred talking to the server, or the HTTP response code was not
* [200..300) successful
*/
public static <T> Response<T> okR(Call<T> call) throws CallFailedRuntimeException {
Response<T> response = executeU(call); Response<T> response = executeU(call);
if (response.isSuccessful()) { if (response.isSuccessful()) {
return response.body(); return response;
} }
throw new CallFailedRuntimeException(call, response); throw new CallFailedRuntimeException(call, response);
} }
/**
* {@link Call#execute()} mapping {@link IOException} to {@link CallFailedRuntimeException}. This can be useful for
* code which would like to handle non-success HTTP responses, notably tests asserting non-200 results.
*
* @param call
* the Call to execute
* @return the Response, which may or may not have been successful.
* @throws CallFailedRuntimeException
* thrown if a problem occurred talking to the server
*/
public static <T> Response<T> executeU(Call<T> call) throws CallFailedRuntimeException { public static <T> Response<T> executeU(Call<T> call) throws CallFailedRuntimeException {
try { try {
return call.execute(); return call.execute();
@@ -19,13 +19,22 @@
package org.apache.fineract.client.util; package org.apache.fineract.client.util;
import java.io.File; import java.io.File;
import java.util.Optional;
import okhttp3.MediaType; import okhttp3.MediaType;
import okhttp3.MultipartBody.Part; import okhttp3.MultipartBody.Part;
import okhttp3.RequestBody; import okhttp3.RequestBody;
import org.apache.fineract.client.services.DocumentsApiFixed;
import org.apache.fineract.client.services.ImagesApi;
import retrofit2.Response;
/** /**
* Convenience Factory for {@link Part} (including {@link RequestBody}). * Convenience Factory for {@link Part} (including {@link RequestBody}).
* *
* {@link Part} is the argument of operations for binary uploads like
* {@link DocumentsApiFixed#createDocument(String, Long, Part, String, String)},
* {@link DocumentsApiFixed#updateDocument(String, Long, Long, Part, String, String)} and
* {@link ImagesApi#create(String, Long, Part)} and {@link ImagesApi#update(String, Long, Part)}.
*
* @author Michael Vorburger.ch * @author Michael Vorburger.ch
*/ */
public final class Parts { public final class Parts {
@@ -85,4 +94,16 @@ public final class Parts {
return null; return null;
} }
} }
public static Optional<String> fileName(Response<?> response) {
String contentDisposition = response.headers().get("Content-Disposition");
if (contentDisposition == null) {
return Optional.empty();
}
int i = contentDisposition.indexOf("; filename=\"");
if (i == -1) {
return Optional.empty();
}
return Optional.of(contentDisposition.substring(i + "; filename=\"".length(), contentDisposition.length() - 1));
}
} }
@@ -19,8 +19,11 @@
package org.apache.fineract.client.util; package org.apache.fineract.client.util;
import com.google.common.truth.Truth; import com.google.common.truth.Truth;
import com.google.common.truth.Truth8;
import okhttp3.Headers;
import okhttp3.MediaType; import okhttp3.MediaType;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import retrofit2.Response;
public class PartsTest { public class PartsTest {
@@ -43,4 +46,15 @@ public class PartsTest {
void nullMediaType() { void nullMediaType() {
Truth.assertThat(Parts.mediaType(null)).isNull(); 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.apache.fineract.client.util.Parts;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import retrofit2.Response;
/** /**
* Integration Test for /documents API. * Integration Test for /documents API.
@@ -80,15 +81,18 @@ public class DocumentTest extends IntegrationTest {
@Test @Test
@Order(4) @Order(4)
void downloadFile() throws IOException { void downloadFile() throws IOException {
ResponseBody r = ok(fineract().documents.downloadFile("clients", clientId, documentId)); Response<ResponseBody> r = okR(fineract().documents.downloadFile("clients", clientId, documentId));
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); try (ResponseBody body = r.body()) {
assertThat(r.bytes().length).isEqualTo(testFile.length()); assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(r.contentLength()).isEqualTo(-1); // TODO testFile.length() assertThat(body.bytes().length).isEqualTo(testFile.length());
assertThat(body.contentLength()).isEqualTo(-1); // TODO testFile.length()
}
assertThat(Parts.fileName(r)).hasValue(testFile.getName());
} }
@Test @Test
@Order(10) @Order(10)
void updateDocument() { void updateDocumentWithoutNewUpload() {
String newName = "Test changed name"; String newName = "Test changed name";
String newDescription = getClass().getName(); String newDescription = getClass().getName();
ok(fineract().documents.updateDocument("clients", clientId, documentId, null, newName, newDescription)); 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.Order;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response;
import retrofit2.http.GET; import retrofit2.http.GET;
import retrofit2.http.Headers; import retrofit2.http.Headers;
@@ -83,10 +84,15 @@ public class ImageTest extends IntegrationTest {
@Order(5) @Order(5)
void getInlineOctetOutput() throws IOException { void getInlineOctetOutput() throws IOException {
// 3505x1972 is the exact original size of testFile // 3505x1972 is the exact original size of testFile
ResponseBody r = ok(fineract().images.get("staff", 1L, 3505, 1972, "inline_octet")); Response<ResponseBody> r = okR(fineract().images.get("staff", 1L, 3505, 1972, "inline_octet"));
assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg")); try (ResponseBody body = r.body()) {
assertThat(r.bytes().length).isEqualTo(testImage.length()); assertThat(body.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(r.contentLength()).isEqualTo(testImage.length()); 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 @Test
@@ -43,6 +43,7 @@ import org.junit.jupiter.api.TestInstance;
import org.junit.jupiter.api.TestInstance.Lifecycle; import org.junit.jupiter.api.TestInstance.Lifecycle;
import org.junit.jupiter.api.TestMethodOrder; import org.junit.jupiter.api.TestMethodOrder;
import retrofit2.Call; import retrofit2.Call;
import retrofit2.Response;
/** /**
* Integration Test for /documents API. * Integration Test for /documents API.
@@ -85,6 +86,10 @@ public abstract class IntegrationTest {
return Calls.ok(call); return Calls.ok(call);
} }
protected <T> Response<T> okR(Call<T> call) {
return Calls.okR(call);
}
// as above, avoids import static CallSubject.assertThat // as above, avoids import static CallSubject.assertThat
protected <T> CallSubject assertThat(Call<T> call) { protected <T> CallSubject assertThat(Call<T> call) {
return CallSubject.assertThat(call); return CallSubject.assertThat(call);