Skip to content

Commit

Permalink
Merge pull request #37 from veryfi/feature/PLAT-5496-support-tags
Browse files Browse the repository at this point in the history
Add replace and add tags functions
  • Loading branch information
Kaevan89 authored Jan 10, 2024
2 parents f3b5a11 + efcd5c3 commit d8ae397
Show file tree
Hide file tree
Showing 8 changed files with 195 additions and 26 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ Install the package using Maven:
<dependency>
<groupId>com.veryfi</groupId>
<artifactId>veryfi-java</artifactId>
<version>1.0.9</version>
<version>1.0.11</version>
</dependency>
```
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
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
<version>20231013</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/veryfi/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,38 @@ CompletableFuture<String> processDocumentUrlAsync(String fileUrl, List<String> f
*/
CompletableFuture<String> 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<String> 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<String>}
*/
CompletableFuture<String> replaceTagsAsync(String documentId, List<String> 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<String> 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<String>}
*/
CompletableFuture<String> addTagsAsync(String documentId, List<String> tags);

/**
* Define new time out for the requests in seconds
* @param timeOut of the http requests in seconds
Expand Down
61 changes: 61 additions & 0 deletions src/main/java/veryfi/ClientImpl.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package veryfi;

import org.json.JSONArray;
import org.json.JSONObject;
import veryfi.models.AddLineItem;
import veryfi.models.NotValidModelException;
Expand Down Expand Up @@ -701,6 +702,66 @@ public CompletableFuture<String> 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<String>}
*/
@Override
public String replaceTags(String documentId, List<String> 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<String>}
*/
@Override
public CompletableFuture<String> replaceTagsAsync(String documentId, List<String> 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<String>}
*/
@Override
public String addTags(String documentId, List<String> 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<String>}
*/
@Override
public CompletableFuture<String> addTagsAsync(String documentId, List<String> 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
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/veryfi/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
112 changes: 90 additions & 22 deletions src/test/java/ClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -546,74 +546,142 @@ 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<String> httpResponse = mock(HttpResponse.class);
when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>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<String> httpResponse = mock(HttpResponse.class);
CompletableFuture<HttpResponse<String>> jsonResponseFutureMock = CompletableFuture.completedFuture(httpResponse);
when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>any())).thenReturn(jsonResponseFutureMock);
when(httpResponse.statusCode()).thenReturn(200);
when(httpResponse.body()).thenReturn(result);
CompletableFuture<String> 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<String> httpResponse = mock(HttpResponse.class);
when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>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<String> httpResponse = mock(HttpResponse.class);
CompletableFuture<HttpResponse<String>> jsonResponseFutureMock = CompletableFuture.completedFuture(httpResponse);
when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>any())).thenReturn(jsonResponseFutureMock);
when(httpResponse.statusCode()).thenReturn(200);
when(httpResponse.body()).thenReturn(result);
CompletableFuture<String> 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<String> 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<String> httpResponse = mock(HttpResponse.class);
when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>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<String> httpResponse = mock(HttpResponse.class);
when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>any())).thenReturn(httpResponse);
CompletableFuture<HttpResponse<String>> jsonResponseFutureMock = CompletableFuture.completedFuture(httpResponse);
when(httpClient.sendAsync(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>any())).thenReturn(jsonResponseFutureMock);
when(httpResponse.statusCode()).thenReturn(200);
when(httpResponse.body()).thenReturn(result);
}
String response = client.deleteLineItems(documentId);
CompletableFuture<String> 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<String> httpResponse = mock(HttpResponse.class);
when(httpClient.send(any(HttpRequest.class), ArgumentMatchers.<HttpResponse.BodyHandler<String>>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<String> httpResponse = mock(HttpResponse.class);
Expand All @@ -622,7 +690,7 @@ void deleteLineItemsAsyncTest() throws IOException, ExecutionException, Interrup
when(httpResponse.statusCode()).thenReturn(200);
when(httpResponse.body()).thenReturn(result);
}
CompletableFuture<String> responseFuture = client.deleteLineItemsAsync(documentId);
CompletableFuture<String> responseFuture = client.addTagsAsync(documentId, List.of("TAG1", "TAG2", "TAG3"));
String response = responseFuture.get();
JSONObject jsonResponse = new JSONObject(response);
Assertions.assertEquals("ok", jsonResponse.getString("status"));
Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/addTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status":"ok",
"message":"Tags have been added"
}
4 changes: 4 additions & 0 deletions src/test/resources/replaceTags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"status":"ok",
"message":"Tags have been added"
}

0 comments on commit d8ae397

Please sign in to comment.