From efcd5c3dd32326cf1e9924778f44f9ce8c507dfa Mon Sep 17 00:00:00 2001 From: Alejandro Uribe Date: Wed, 10 Jan 2024 11:27:08 -0500 Subject: [PATCH] Add replace and add tags functions --- README.md | 4 +- pom.xml | 2 +- src/main/java/veryfi/Client.java | 32 ++++++++ src/main/java/veryfi/ClientImpl.java | 61 +++++++++++++++ src/main/java/veryfi/Constants.java | 2 +- src/test/java/ClientTest.java | 112 +++++++++++++++++++++------ src/test/resources/addTags.json | 4 + src/test/resources/replaceTags.json | 4 + 8 files changed, 195 insertions(+), 26 deletions(-) create mode 100644 src/test/resources/addTags.json create mode 100644 src/test/resources/replaceTags.json diff --git a/README.md b/README.md index 39635e6..200b935 100755 --- a/README.md +++ b/README.md @@ -18,12 +18,12 @@ Install the package using Maven: com.veryfi veryfi-java - 1.0.9 + 1.0.11 ``` Install the package using Gradle: ```bash -implementation group: 'com.veryfi', name: 'veryfi-java', version: '1.0.10' +implementation group: 'com.veryfi', name: 'veryfi-java', version: '1.0.11' ``` ## Getting Started diff --git a/pom.xml b/pom.xml index e85dc19..7fd75e3 100644 --- a/pom.xml +++ b/pom.xml @@ -63,7 +63,7 @@ org.json json - 20230227 + 20231013 org.junit.jupiter diff --git a/src/main/java/veryfi/Client.java b/src/main/java/veryfi/Client.java index 31e7aae..71e3981 100644 --- a/src/main/java/veryfi/Client.java +++ b/src/main/java/veryfi/Client.java @@ -221,6 +221,38 @@ CompletableFuture processDocumentUrlAsync(String fileUrl, List f */ CompletableFuture deleteLineItemAsync(String documentId, String lineItemId); + /** + * Replace multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link String} + */ + String replaceTags(String documentId, List tags); + + /** + * Replace multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + CompletableFuture replaceTagsAsync(String documentId, List tags); + + /** + * Add multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link String} + */ + String addTags(String documentId, List tags); + + /** + * Add multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + CompletableFuture addTagsAsync(String documentId, List tags); + /** * Define new time out for the requests in seconds * @param timeOut of the http requests in seconds diff --git a/src/main/java/veryfi/ClientImpl.java b/src/main/java/veryfi/ClientImpl.java index 29266d0..b135e05 100644 --- a/src/main/java/veryfi/ClientImpl.java +++ b/src/main/java/veryfi/ClientImpl.java @@ -1,5 +1,6 @@ package veryfi; +import org.json.JSONArray; import org.json.JSONObject; import veryfi.models.AddLineItem; import veryfi.models.NotValidModelException; @@ -701,6 +702,66 @@ public CompletableFuture deleteLineItemAsync(String documentId, String l return requestAsync(HttpMethod.DELETE, endpointName, requestArguments); } + /** + * Replace multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + @Override + public String replaceTags(String documentId, List tags) { + String endpointName = "/documents/" + documentId + "/"; + JSONObject requestArguments = new JSONObject(); + JSONArray jsonArrayTags = new JSONArray(tags); + requestArguments.put("tags", jsonArrayTags); + return request(HttpMethod.PUT, endpointName, requestArguments); + } + + /** + * Replace multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + @Override + public CompletableFuture replaceTagsAsync(String documentId, List tags) { + String endpointName = "/documents/" + documentId + "/"; + JSONObject requestArguments = new JSONObject(); + JSONArray jsonArrayTags = new JSONArray(tags); + requestArguments.put("tags", jsonArrayTags); + return requestAsync(HttpMethod.PUT, endpointName, requestArguments); + } + + /** + * Add multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + @Override + public String addTags(String documentId, List tags) { + String endpointName = "/documents/" + documentId + "/tags/"; + JSONObject requestArguments = new JSONObject(); + JSONArray jsonArrayTags = new JSONArray(tags); + requestArguments.put("tags", jsonArrayTags); + return request(HttpMethod.POST, endpointName, requestArguments); + } + + /** + * Add multiple tags on an existing document. + * @param documentId ID of the document you'd like to update. + * @param tags tags array of tags to be added. + * @return the response data. {@link CompletableFuture} + */ + @Override + public CompletableFuture addTagsAsync(String documentId, List tags) { + String endpointName = "/documents/" + documentId + "/tags/"; + JSONObject requestArguments = new JSONObject(); + JSONArray jsonArrayTags = new JSONArray(tags); + requestArguments.put("tags", jsonArrayTags); + return requestAsync(HttpMethod.POST, endpointName, requestArguments); + } + /** * Define new time out for the requests in seconds * diff --git a/src/main/java/veryfi/Constants.java b/src/main/java/veryfi/Constants.java index b814c44..54bed54 100644 --- a/src/main/java/veryfi/Constants.java +++ b/src/main/java/veryfi/Constants.java @@ -21,7 +21,7 @@ private Constants() { /** * header for HttpRequest */ - public static final String USER_AGENT_JAVA = "Java Veryfi-Java/1.0.10"; + public static final String USER_AGENT_JAVA = "Java Veryfi-Java/1.0.11"; /** * header for HttpRequest */ diff --git a/src/test/java/ClientTest.java b/src/test/java/ClientTest.java index 5b9cdd8..a34ac6b 100644 --- a/src/test/java/ClientTest.java +++ b/src/test/java/ClientTest.java @@ -546,32 +546,66 @@ void addLineItemAsyncTest() throws IOException, InterruptedException, NotValidMo void deleteLineItemTest() throws IOException, InterruptedException { String documentId = "125344108"; String lineItemId = "189951682"; + HttpClient httpClient = mock(HttpClient.class); + client.setHttpClient(httpClient); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItem.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + String response = client.deleteLineItem(documentId, lineItemId); + JSONObject jsonResponse = new JSONObject(response); + Assertions.assertEquals("ok", jsonResponse.getString("status")); + } + + @Test + void deleteLineItemAsyncTest() throws IOException, InterruptedException, ExecutionException { + String documentId = "125344108"; + String lineItemId = "189951682"; + HttpClient httpClient = mock(HttpClient.class); + client.setHttpClient(httpClient); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItem.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + CompletableFuture> jsonResponseFutureMock = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFutureMock); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + CompletableFuture jsonResponseFuture = client.deleteLineItemAsync(documentId, lineItemId); + String jsonResponse = jsonResponseFuture.get(); + JSONObject deleteResponse = new JSONObject(jsonResponse); + Assertions.assertEquals("ok", deleteResponse.getString("status")); + } + + @Test + void deleteLineItemsTest() throws IOException, InterruptedException { + String documentId = "125344108"; if (mockResponses) { HttpClient httpClient = mock(HttpClient.class); client.setHttpClient(httpClient); - InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItem.json"); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItems.json"); assert fileStream != null; String result = new String(fileStream.readAllBytes()); HttpResponse httpResponse = mock(HttpResponse.class); when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn(result); - String response = client.deleteLineItem(documentId, lineItemId); - JSONObject jsonResponse = new JSONObject(response); - Assertions.assertEquals("ok", jsonResponse.getString("status")); - } else { - Assertions.assertTrue(true); // Tested before. } + String response = client.deleteLineItems(documentId); + JSONObject jsonResponse = new JSONObject(response); + Assertions.assertEquals("ok", jsonResponse.getString("status")); } @Test - void deleteLineItemAsyncTest() throws IOException, InterruptedException, ExecutionException { + void deleteLineItemsAsyncTest() throws IOException, ExecutionException, InterruptedException { String documentId = "125344108"; - String lineItemId = "189951682"; if (mockResponses) { HttpClient httpClient = mock(HttpClient.class); client.setHttpClient(httpClient); - InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItem.json"); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItems.json"); assert fileStream != null; String result = new String(fileStream.readAllBytes()); HttpResponse httpResponse = mock(HttpResponse.class); @@ -579,41 +613,75 @@ void deleteLineItemAsyncTest() throws IOException, InterruptedException, Executi when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFutureMock); when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn(result); - CompletableFuture jsonResponseFuture = client.deleteLineItemAsync(documentId, lineItemId); - String jsonResponse = jsonResponseFuture.get(); - JSONObject deleteResponse = new JSONObject(jsonResponse); - Assertions.assertEquals("ok", deleteResponse.getString("status")); - } else { - Assertions.assertTrue(true); // Tested before. } + CompletableFuture responseFuture = client.deleteLineItemsAsync(documentId); + String response = responseFuture.get(); + JSONObject jsonResponse = new JSONObject(response); + Assertions.assertEquals("ok", jsonResponse.getString("status")); } @Test - void deleteLineItemsTest() throws IOException, InterruptedException { + void replaceTagsTest() throws IOException, InterruptedException { + String documentId = "125344108"; + HttpClient httpClient = mock(HttpClient.class); + client.setHttpClient(httpClient); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("replaceTags.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + String response = client.replaceTags(documentId, List.of("TAG1", "TAG2", "TAG3")); + JSONObject jsonResponse = new JSONObject(response); + Assertions.assertEquals("ok", jsonResponse.getString("status")); + } + + @Test + void replaceTagsAsyncTest() throws IOException, ExecutionException, InterruptedException { String documentId = "125344108"; if (mockResponses) { HttpClient httpClient = mock(HttpClient.class); client.setHttpClient(httpClient); - InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItems.json"); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("replaceTags.json"); assert fileStream != null; String result = new String(fileStream.readAllBytes()); HttpResponse httpResponse = mock(HttpResponse.class); - when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + CompletableFuture> jsonResponseFutureMock = CompletableFuture.completedFuture(httpResponse); + when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(jsonResponseFutureMock); when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn(result); } - String response = client.deleteLineItems(documentId); + CompletableFuture responseFuture = client.replaceTagsAsync(documentId, List.of("TAG1", "TAG2", "TAG3")); + String response = responseFuture.get(); JSONObject jsonResponse = new JSONObject(response); Assertions.assertEquals("ok", jsonResponse.getString("status")); } @Test - void deleteLineItemsAsyncTest() throws IOException, ExecutionException, InterruptedException { + void addTagsTest() throws IOException, InterruptedException { + String documentId = "125344108"; + HttpClient httpClient = mock(HttpClient.class); + client.setHttpClient(httpClient); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("addTags.json"); + assert fileStream != null; + String result = new String(fileStream.readAllBytes()); + HttpResponse httpResponse = mock(HttpResponse.class); + when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.>any())).thenReturn(httpResponse); + when(httpResponse.statusCode()).thenReturn(200); + when(httpResponse.body()).thenReturn(result); + String response = client.addTags(documentId, List.of("TAG1", "TAG2", "TAG3")); + JSONObject jsonResponse = new JSONObject(response); + Assertions.assertEquals("ok", jsonResponse.getString("status")); + } + + @Test + void addTagsAsyncTest() throws IOException, ExecutionException, InterruptedException { String documentId = "125344108"; if (mockResponses) { HttpClient httpClient = mock(HttpClient.class); client.setHttpClient(httpClient); - InputStream fileStream = ClassLoader.getSystemResourceAsStream("deleteLineItems.json"); + InputStream fileStream = ClassLoader.getSystemResourceAsStream("addTags.json"); assert fileStream != null; String result = new String(fileStream.readAllBytes()); HttpResponse httpResponse = mock(HttpResponse.class); @@ -622,7 +690,7 @@ void deleteLineItemsAsyncTest() throws IOException, ExecutionException, Interrup when(httpResponse.statusCode()).thenReturn(200); when(httpResponse.body()).thenReturn(result); } - CompletableFuture responseFuture = client.deleteLineItemsAsync(documentId); + CompletableFuture responseFuture = client.addTagsAsync(documentId, List.of("TAG1", "TAG2", "TAG3")); String response = responseFuture.get(); JSONObject jsonResponse = new JSONObject(response); Assertions.assertEquals("ok", jsonResponse.getString("status")); diff --git a/src/test/resources/addTags.json b/src/test/resources/addTags.json new file mode 100644 index 0000000..f57569a --- /dev/null +++ b/src/test/resources/addTags.json @@ -0,0 +1,4 @@ +{ + "status":"ok", + "message":"Tags have been added" +} diff --git a/src/test/resources/replaceTags.json b/src/test/resources/replaceTags.json new file mode 100644 index 0000000..f57569a --- /dev/null +++ b/src/test/resources/replaceTags.json @@ -0,0 +1,4 @@ +{ + "status":"ok", + "message":"Tags have been added" +}