Add new DocumentTest (FINERACT-1218)
This commit is contained in:
committed by
Petri Tuomola
parent
6992102035
commit
20daf59a3e
+147
@@ -0,0 +1,147 @@
|
||||
/**
|
||||
* 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 java.util.List;
|
||||
import okhttp3.ResponseBody;
|
||||
import org.apache.fineract.client.models.DeleteEntityTypeEntityIdDocumentsResponse;
|
||||
import org.apache.fineract.client.models.GetEntityTypeEntityIdDocumentsResponse;
|
||||
import org.apache.fineract.client.models.PostEntityTypeEntityIdDocumentsResponse;
|
||||
import org.apache.fineract.client.models.PutEntityTypeEntityIdDocumentsResponse;
|
||||
import retrofit2.Call;
|
||||
import retrofit2.http.DELETE;
|
||||
import retrofit2.http.GET;
|
||||
import retrofit2.http.POST;
|
||||
import retrofit2.http.PUT;
|
||||
|
||||
// This class was originally generated by OpenAPI Generator (https://openapi-generator.tech),
|
||||
// but then had to be manually edited by Michael Vorburger.ch to manually fix https://issues.apache.org/jira/browse/FINERACT-1227.
|
||||
// If we could fix our OpenAPI / Swagger YAML generation from the JAX RS and OpenAPI annotation to have the correct notation for binary document files and images, then this could be removed again.
|
||||
public interface DocumentsApiFixed {
|
||||
|
||||
/**
|
||||
* Create a Document Note: A document is created using a Multi-part form upload Body Parts name : Name or summary of
|
||||
* the document description : Description of the document file : The file to be uploaded Mandatory Fields : file and
|
||||
* description
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @param file
|
||||
* (required)
|
||||
* @param name
|
||||
* name (optional)
|
||||
* @param description
|
||||
* description (optional)
|
||||
* @return Call<PostEntityTypeEntityIdDocumentsResponse>
|
||||
*/
|
||||
@retrofit2.http.Multipart
|
||||
@POST("{entityType}/{entityId}/documents")
|
||||
Call<PostEntityTypeEntityIdDocumentsResponse> createDocument(@retrofit2.http.Path("entityType") String entityType,
|
||||
@retrofit2.http.Path("entityId") Long entityId, @retrofit2.http.Part okhttp3.MultipartBody.Part file,
|
||||
@retrofit2.http.Part("name") String name, @retrofit2.http.Part("description") String description);
|
||||
|
||||
/**
|
||||
* Remove a Document
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @param documentId
|
||||
* documentId (required)
|
||||
* @return Call<DeleteEntityTypeEntityIdDocumentsResponse>
|
||||
*/
|
||||
@DELETE("{entityType}/{entityId}/documents/{documentId}")
|
||||
Call<DeleteEntityTypeEntityIdDocumentsResponse> deleteDocument(@retrofit2.http.Path("entityType") String entityType,
|
||||
@retrofit2.http.Path("entityId") Long entityId, @retrofit2.http.Path("documentId") Long documentId);
|
||||
|
||||
/**
|
||||
* Retrieve Binary File associated with Document Request used to download the file associated with the document
|
||||
* Example Requests: clients/1/documents/1/attachment loans/1/documents/1/attachment
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @param documentId
|
||||
* documentId (required)
|
||||
* @return Call<Void>
|
||||
*/
|
||||
@GET("{entityType}/{entityId}/documents/{documentId}/attachment")
|
||||
Call<ResponseBody> downloadFile(@retrofit2.http.Path("entityType") String entityType, @retrofit2.http.Path("entityId") Long entityId,
|
||||
@retrofit2.http.Path("documentId") Long documentId);
|
||||
|
||||
/**
|
||||
* Retrieve a Document Example Requests: clients/1/documents/1 loans/1/documents/1
|
||||
* client_identifiers/1/documents/1?fields=name,description
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @param documentId
|
||||
* documentId (required)
|
||||
* @return Call<GetEntityTypeEntityIdDocumentsResponse>
|
||||
*/
|
||||
@GET("{entityType}/{entityId}/documents/{documentId}")
|
||||
Call<GetEntityTypeEntityIdDocumentsResponse> getDocument(@retrofit2.http.Path("entityType") String entityType,
|
||||
@retrofit2.http.Path("entityId") Long entityId, @retrofit2.http.Path("documentId") Long documentId);
|
||||
|
||||
/**
|
||||
* List documents Example Requests: clients/1/documents client_identifiers/1/documents
|
||||
* loans/1/documents?fields=name,description
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @return Call<List<GetEntityTypeEntityIdDocumentsResponse>>
|
||||
*/
|
||||
@GET("{entityType}/{entityId}/documents")
|
||||
Call<List<GetEntityTypeEntityIdDocumentsResponse>> retrieveAllDocuments(@retrofit2.http.Path("entityType") String entityType,
|
||||
@retrofit2.http.Path("entityId") Long entityId);
|
||||
|
||||
/**
|
||||
* Update a Document Note: A document is updated using a Multi-part form upload Body Parts name Name or summary of
|
||||
* the document description Description of the document file The file to be uploaded
|
||||
*
|
||||
* @param entityType
|
||||
* entityType (required)
|
||||
* @param entityId
|
||||
* entityId (required)
|
||||
* @param documentId
|
||||
* documentId (required)
|
||||
* @param file
|
||||
* (optional)
|
||||
* @param name
|
||||
* name (optional)
|
||||
* @param description
|
||||
* description (optional)
|
||||
* @return Call<PutEntityTypeEntityIdDocumentsResponse>
|
||||
*/
|
||||
@retrofit2.http.Multipart
|
||||
@PUT("{entityType}/{entityId}/documents/{documentId}")
|
||||
Call<PutEntityTypeEntityIdDocumentsResponse> updateDocument(@retrofit2.http.Path("entityType") String entityType,
|
||||
@retrofit2.http.Path("entityId") Long entityId, @retrofit2.http.Path("documentId") Long documentId,
|
||||
@retrofit2.http.Part okhttp3.MultipartBody.Part file, @retrofit2.http.Part("name") String name,
|
||||
@retrofit2.http.Part("description") String description);
|
||||
|
||||
}
|
||||
@@ -56,7 +56,7 @@ import org.apache.fineract.client.services.CodesApi;
|
||||
import org.apache.fineract.client.services.CurrencyApi;
|
||||
import org.apache.fineract.client.services.DataTablesApi;
|
||||
import org.apache.fineract.client.services.DefaultApi;
|
||||
import org.apache.fineract.client.services.DocumentsApi;
|
||||
import org.apache.fineract.client.services.DocumentsApiFixed;
|
||||
import org.apache.fineract.client.services.EntityDataTableApi;
|
||||
import org.apache.fineract.client.services.EntityFieldConfigurationApi;
|
||||
import org.apache.fineract.client.services.ExternalServicesApi;
|
||||
@@ -181,7 +181,7 @@ public final class FineractClient {
|
||||
public final CurrencyApi currencies;
|
||||
public final DataTablesApi dataTables;
|
||||
public final @Deprecated DefaultApi legacy; // TODO FINERACT-1222
|
||||
public final DocumentsApi documents;
|
||||
public final DocumentsApiFixed documents;
|
||||
public final EntityDataTableApi entityDatatableChecks;
|
||||
public final EntityFieldConfigurationApi entityFieldConfigurations;
|
||||
public final ExternalServicesApi externalServices;
|
||||
@@ -288,7 +288,7 @@ public final class FineractClient {
|
||||
currencies = retrofit.create(CurrencyApi.class);
|
||||
dataTables = retrofit.create(DataTablesApi.class);
|
||||
legacy = retrofit.create(DefaultApi.class);
|
||||
documents = retrofit.create(DocumentsApi.class);
|
||||
documents = retrofit.create(DocumentsApiFixed.class);
|
||||
entityDatatableChecks = retrofit.create(EntityDataTableApi.class);
|
||||
entityFieldConfigurations = retrofit.create(EntityFieldConfigurationApi.class);
|
||||
externalServices = retrofit.create(ExternalServicesApi.class);
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
/**
|
||||
* 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 java.io.File;
|
||||
import okhttp3.MediaType;
|
||||
import okhttp3.MultipartBody.Part;
|
||||
import okhttp3.RequestBody;
|
||||
|
||||
/**
|
||||
* Convenience Factory for {@link Part} (including {@link RequestBody}).
|
||||
*
|
||||
* @author Michael Vorburger.ch
|
||||
*/
|
||||
public final class Parts {
|
||||
|
||||
private Parts() {}
|
||||
|
||||
public static Part fromFile(File file) {
|
||||
RequestBody rb = RequestBody.create(file, getMediaType(file.getName()));
|
||||
return Part.createFormData("file", file.getName(), rb);
|
||||
}
|
||||
|
||||
public static Part fromFile(String fileName, byte[] bytes) {
|
||||
RequestBody rb = RequestBody.create(bytes, getMediaType(fileName));
|
||||
return Part.createFormData("file", fileName, rb);
|
||||
}
|
||||
|
||||
// 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) {
|
||||
int dotPos = fileName.lastIndexOf('.');
|
||||
if (dotPos == -1) {
|
||||
return null;
|
||||
}
|
||||
String ext = fileName.substring(dotPos);
|
||||
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
|
||||
switch (ext) {
|
||||
case "jpg":
|
||||
case "jpeg":
|
||||
return MediaType.get("image/jpeg");
|
||||
case "png":
|
||||
return MediaType.get("image/png");
|
||||
case "tif":
|
||||
case "tiff":
|
||||
return MediaType.get("image/tiff");
|
||||
case "gif":
|
||||
return MediaType.get("image/gif");
|
||||
case "pdf":
|
||||
return MediaType.get("application/pdf");
|
||||
case "docx":
|
||||
return MediaType.get("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
|
||||
case "doc":
|
||||
return MediaType.get("application/msword");
|
||||
case "xlsx":
|
||||
return MediaType.get("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
|
||||
case "xls":
|
||||
return MediaType.get("application/vnd.ms-excel");
|
||||
case "odt":
|
||||
return MediaType.get("application/vnd.oasis.opendocument.text");
|
||||
case "ods":
|
||||
return MediaType.get("application/vnd.oasis.opendocument.spreadsheet");
|
||||
case "txt":
|
||||
return MediaType.get("text/plain");
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user