This is an automated email from the ASF dual-hosted git repository.
ptuomola pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/fineract.git
The following commit(s) were added to refs/heads/develop by this push:
new 5f42112 client: Fix mediaType MIME code (FINERACT-1218)
5f42112 is described below
commit 5f4211227814fedea46448076d159f7b40e97aee
Author: Michael Vorburger <[email protected]>
AuthorDate: Thu Oct 22 01:12:20 2020 +0200
client: Fix mediaType MIME code (FINERACT-1218)
---
.../org/apache/fineract/client/util/Parts.java | 14 ++++---
.../org/apache/fineract/client/util/PartsTest.java | 46 ++++++++++++++++++++++
.../integrationtests/newstyle/DocumentTest.java | 14 +++----
3 files changed, 60 insertions(+), 14 deletions(-)
diff --git
a/fineract-client/src/main/java/org/apache/fineract/client/util/Parts.java
b/fineract-client/src/main/java/org/apache/fineract/client/util/Parts.java
index d54fac5..486842f 100644
--- a/fineract-client/src/main/java/org/apache/fineract/client/util/Parts.java
+++ b/fineract-client/src/main/java/org/apache/fineract/client/util/Parts.java
@@ -33,24 +33,26 @@ public final class Parts {
private Parts() {}
public static Part fromFile(File file) {
- RequestBody rb = RequestBody.create(file,
getMediaType(file.getName()));
+ RequestBody rb = RequestBody.create(file, mediaType(file.getName()));
return Part.createFormData("file", file.getName(), rb);
}
public static Part fromFile(String fileName, byte[] bytes) {
- RequestBody rb = RequestBody.create(bytes, getMediaType(fileName));
+ RequestBody rb = RequestBody.create(bytes, mediaType(fileName));
return Part.createFormData("file", fileName, rb);
}
+ // package local, for unit testing
// TODO this logic should be on the Server, not have to be done by the
client...
- // There actually does seem to be some code related to MIME type guessing
in Fineract, but test shows it doesn't
- // work :(
- private static MediaType getMediaType(String fileName) {
+ static MediaType mediaType(String fileName) {
+ if (fileName == null) {
+ return null;
+ }
int dotPos = fileName.lastIndexOf('.');
if (dotPos == -1) {
return null;
}
- String ext = fileName.substring(dotPos);
+ String ext = fileName.substring(dotPos + 1);
//
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
switch (ext) {
case "jpg":
diff --git
a/fineract-client/src/test/java/org/apache/fineract/client/util/PartsTest.java
b/fineract-client/src/test/java/org/apache/fineract/client/util/PartsTest.java
new file mode 100644
index 0000000..a312ff8
--- /dev/null
+++
b/fineract-client/src/test/java/org/apache/fineract/client/util/PartsTest.java
@@ -0,0 +1,46 @@
+/**
+ * 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.util;
+
+import com.google.common.truth.Truth;
+import okhttp3.MediaType;
+import org.junit.jupiter.api.Test;
+
+public class PartsTest {
+
+ @Test
+ void validMediaType() {
+
Truth.assertThat(Parts.mediaType("test.jpg")).isEqualTo(MediaType.get("image/jpeg"));
+ }
+
+ @Test
+ void dotMediaType() {
+ Truth.assertThat(Parts.mediaType("test.")).isNull();
+ }
+
+ @Test
+ void emptyMediaType() {
+ Truth.assertThat(Parts.mediaType("")).isNull();
+ }
+
+ @Test
+ void nullMediaType() {
+ Truth.assertThat(Parts.mediaType(null)).isNull();
+ }
+}
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 991ad13..c1d5d5d 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
@@ -49,7 +49,7 @@ public class DocumentTest extends IntegrationTest {
@Test
@Order(2)
- void createDocument() throws IOException {
+ void createDocument() {
String name = "Test";
Part part = Parts.fromFile(testFile);
String description = null;
@@ -63,7 +63,7 @@ public class DocumentTest extends IntegrationTest {
@Test
@Order(3)
- void getDocument() throws IOException {
+ void getDocument() {
GetEntityTypeEntityIdDocumentsResponse doc =
ok(fineract().documents.getDocument("clients", clientId, documentId));
assertThat(doc.getName()).isEqualTo("Test");
assertThat(doc.getFileName()).isEqualTo(testFile.getName());
@@ -72,9 +72,8 @@ public class DocumentTest extends IntegrationTest {
assertThat(doc.getParentEntityType()).isEqualTo("clients");
assertThat(doc.getParentEntityId()).isEqualTo(clientId);
// TODO huh?! It's more than uploaded file; seems like a bug - it's
including create body, not just file size
- assertThat(doc.getSize()).isEqualTo(testFile.length() + 385);
- // TODO huh?! MIME is always text/plain instead of image/jpeg... :(
- assertThat(doc.getType()).isEqualTo("text/plain");
+ assertThat(doc.getSize()).isEqualTo(testFile.length() + 411);
+ assertThat(doc.getType()).isEqualTo("image/jpeg");
// TODO doc.getStorageType() shouldn't be exposed by the API?!
}
@@ -82,15 +81,14 @@ public class DocumentTest extends IntegrationTest {
@Order(4)
void downloadFile() throws IOException {
ResponseBody r = ok(fineract().documents.downloadFile("clients",
clientId, documentId));
- assertThat(r.contentType()).isEqualTo(MediaType.get("text/plain")); //
TODO wrong, bug; needs to be "image/jpeg"
- //
(as above)
+ assertThat(r.contentType()).isEqualTo(MediaType.get("image/jpeg"));
assertThat(r.bytes().length).isEqualTo(testFile.length());
// NOK: assertThat(r.contentLength()).isEqualTo(testFile.length());
}
@Test
@Order(10)
- void updateDocument() throws IOException {
+ void updateDocument() {
String newName = "Test changed name";
String newDescription = getClass().getName();
ok(fineract().documents.updateDocument("clients", clientId,
documentId, null, newName, newDescription));