diff --git a/pom.xml b/pom.xml index 121bf2cc..0e242c25 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ com.coveo push-api-client.java - 2.0.0 + 2.1.0 ${project.groupId}:${project.artifactId} jar Coveo Push API client. See more on https://github.com/coveo/push-api-client.java diff --git a/src/main/java/com/coveo/pushapiclient/BatchIdentity.java b/src/main/java/com/coveo/pushapiclient/BatchIdentity.java new file mode 100644 index 00000000..f7d376b8 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/BatchIdentity.java @@ -0,0 +1,42 @@ +package com.coveo.pushapiclient; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +import java.util.List; + +/** + * See [Manage Batches of Security Identities](https://docs.coveo.com/en/55) + */ +public class BatchIdentity { + + private List members; + private List mappings; + private List deleted; + + public BatchIdentity(List members, List mappings, List deleted) { + this.members = members; + this.mappings = mappings; + this.deleted = deleted; + } + + public BatchIdentityRecord marshal() { + return new BatchIdentityRecord( + this.members.stream().map(s -> new Gson().toJsonTree(s).getAsJsonObject()).toArray(JsonObject[]::new), + this.mappings.stream().map(s -> new Gson().toJsonTree(s).getAsJsonObject()).toArray(JsonObject[]::new), + this.deleted.stream().map(s -> new Gson().toJsonTree(s).getAsJsonObject()).toArray(JsonObject[]::new) + ); + } + + public List getMembers() { + return members; + } + + public List getMappings() { + return mappings; + } + + public List getDeleted() { + return deleted; + } +} diff --git a/src/main/java/com/coveo/pushapiclient/BatchIdentityRecord.java b/src/main/java/com/coveo/pushapiclient/BatchIdentityRecord.java new file mode 100644 index 00000000..c807cc1d --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/BatchIdentityRecord.java @@ -0,0 +1,31 @@ +package com.coveo.pushapiclient; + +import com.google.gson.JsonObject; + +/** + * See [BatchIdentityBody](https://docs.coveo.com/en/139#batchidentitybody) + */ +public class BatchIdentityRecord { + + private JsonObject[] members; + private JsonObject[] mappings; + private JsonObject[] deleted; + + public BatchIdentityRecord(JsonObject[] members, JsonObject[] mappings, JsonObject[] deleted) { + this.members = members; + this.mappings = mappings; + this.deleted = deleted; + } + + public JsonObject[] getMembers() { + return members; + } + + public JsonObject[] getMappings() { + return mappings; + } + + public JsonObject[] getDeleted() { + return deleted; + } +} diff --git a/src/main/java/com/coveo/pushapiclient/BatchUpdate.java b/src/main/java/com/coveo/pushapiclient/BatchUpdate.java index 1b057ce7..2a7efa6d 100644 --- a/src/main/java/com/coveo/pushapiclient/BatchUpdate.java +++ b/src/main/java/com/coveo/pushapiclient/BatchUpdate.java @@ -11,9 +11,9 @@ public class BatchUpdate { private final List addOrUpdate; - private final List delete; + private final List delete; - public BatchUpdate(List addOrUpdate, List delete) { + public BatchUpdate(List addOrUpdate, List delete) { this.addOrUpdate = addOrUpdate; this.delete = delete; } @@ -21,7 +21,7 @@ public BatchUpdate(List addOrUpdate, List dele public BatchUpdateRecord marshal() { return new BatchUpdateRecord( this.addOrUpdate.stream().map(DocumentBuilder::marshalJsonObject).toArray(JsonObject[]::new), - this.delete.stream().map(DocumentBuilder::marshalJsonObject).toArray(JsonObject[]::new) + this.delete.stream().map(DeleteDocument::marshalJsonObject).toArray(JsonObject[]::new) ); } @@ -29,7 +29,7 @@ public List getAddOrUpdate() { return addOrUpdate; } - public List getDelete() { + public List getDelete() { return delete; } diff --git a/src/main/java/com/coveo/pushapiclient/DeleteDocument.java b/src/main/java/com/coveo/pushapiclient/DeleteDocument.java new file mode 100644 index 00000000..200d7c42 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/DeleteDocument.java @@ -0,0 +1,31 @@ +package com.coveo.pushapiclient; + +import com.google.gson.Gson; +import com.google.gson.JsonObject; + +public class DeleteDocument { + + /** + * The documentId of the document. + */ + public String documentId; + + /** + * Flag to delete children of the document. + */ + public boolean deleteChildren; + + public DeleteDocument(String documentId) { + this.documentId = documentId; + this.deleteChildren = false; + } + + public DeleteDocument(String documentId, boolean deleteChildren) { + this.documentId = documentId; + this.deleteChildren = deleteChildren; + } + + public JsonObject marshalJsonObject() { + return new Gson().toJsonTree(this).getAsJsonObject(); + } +} diff --git a/src/main/java/com/coveo/pushapiclient/Document.java b/src/main/java/com/coveo/pushapiclient/Document.java index cc94e3d3..30161dec 100644 --- a/src/main/java/com/coveo/pushapiclient/Document.java +++ b/src/main/java/com/coveo/pushapiclient/Document.java @@ -13,6 +13,7 @@ public class Document { * See https://docs.coveo.com/en/115 for more information. */ public final HashMap metadata; + /** * The list of permission sets for this item. *

@@ -20,7 +21,8 @@ public class Document { *

* See https://docs.coveo.com/en/107 for more information. */ - public final DocumentPermissions[] permissions; + public DocumentPermissions[] permissions; + /** * The Uniform Resource Identifier (URI) that uniquely identifies the document in a Coveo index. *

@@ -29,36 +31,48 @@ public class Document { * - `file://folder/text.txt` */ public String uri; + + /** + * The documentId of the document. + */ + public String documentId; + /** * The title of the document. */ public String title; + /** * The clickable URI associated with the document. */ public String clickableUri; + /** * The author of the document. */ public String author; + /** * The date of the document, represented as an ISO string. *

* Optional, will default to indexation date. */ public String date; + /** * The modified date of the document, represented as an ISO string. *

* Optional, will default to indexation date. */ public String modifiedDate; + /** * The permanent identifier of a document that does not change over time. *

* Optional, will be derived from the document URI. */ public String permanentId; + /** * The unique identifier (URI) of the parent item. *

@@ -67,6 +81,7 @@ public class Document { * This value also ensures that a parent and all of its attachments will be routed in the same index slice. */ public String parentId; + /** * The textual (non-binary) content of the item. *

@@ -79,18 +94,33 @@ public class Document { * Example: `This is a simple string that will be used for searchability as well as to generate excerpt and summaries for the document.` */ public String data; + /** * The original binary item content, compressed using one of the supported compression types (Deflate, GZip, LZMA, Uncompressed, or ZLib), and then Base64 encoded. *

* You can use this parameter when you're pushing a compressed binary item (such as XML/HTML, PDF, Word, or binary) whose size is less than 5 MB. *

- * Whenever you're pushing an item whose size is 5 MB or more, use the CompressedBinaryDataFileIdproperty instead. + * Whenever you're pushing an item whose size is 5 MB or more, use the CompressedBinaryDataFileId property instead. *

* If you're pushing less than 5 MB of textual (non-binary) content, you can use the data property instead. *

* See https://docs.coveo.com/en/73 for more information. */ public CompressedBinaryData compressedBinaryData; + + /** + * The fileId from the content that has been uploaded to the S3 via a FileContainer. The file is compressed using one of the supported compression types (Deflate, GZip, LZMA, Uncompressed, or ZLib). + *

+ * You can use this parameter when you're pushing a compressed binary item (such as XML/HTML, PDF, Word, or binary) whose size is greater than 5 MB. + *

+ * Whenever you're pushing an item whose size is less than 5 MB, use the CompressedBinaryData property instead. + *

+ * If you're pushing less than 5 MB of textual (non-binary) content, you can use the data property instead. + *

+ * See https://docs.coveo.com/en/73 for more information. + */ + public String compressedBinaryDataFileId; + /** * The file extension of the data you're pushing. *

diff --git a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java index 5cd1eaa7..dd3bbd36 100644 --- a/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java +++ b/src/main/java/com/coveo/pushapiclient/DocumentBuilder.java @@ -169,6 +169,17 @@ public DocumentBuilder withCompressedBinaryData(CompressedBinaryData compressedB return this; } + /** + * Set the file container file ID for the compressed binary data of the document. See {@link Document#compressedBinaryDataFileId} + * + * @param compressedBinaryDataFileId + * @return + */ + public DocumentBuilder withCompressedBinaryDataFileId(String compressedBinaryDataFileId) { + this.document.compressedBinaryDataFileId = compressedBinaryDataFileId; + return this; + } + /** * Set the file extension on the document. See {@link Document#fileExtension} * @@ -306,6 +317,16 @@ public DocumentBuilder withAllowAnonymousUsers(Boolean allowAnonymous) { return this; } + /** + * Set the fully built out DocumentPermissions array. See {@Link Document#permissions} + * @param documentPermissions + * @return + */ + public DocumentBuilder withDocumentPermissions(DocumentPermissions[] documentPermissions) { + this.document.permissions = documentPermissions; + return this; + } + /** * Marshal the document into a JSON string accepted by the push API. * diff --git a/src/main/java/com/coveo/pushapiclient/Environment.java b/src/main/java/com/coveo/pushapiclient/Environment.java new file mode 100644 index 00000000..ccf1c4f8 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/Environment.java @@ -0,0 +1,21 @@ +package com.coveo.pushapiclient; + +/** + * Available environments to use as the host for the PushAPI. + */ +public enum Environment { + PRODUCTION( "https://api.cloud.coveo.com"), + HIPAA("https://apihipaa.cloud.coveo.com"), + DEVELOPMENT("https://apidev.cloud.coveo.com"), + STAGING("https://apiqa.cloud.coveo.com"); + + private String host; + + Environment(String host) { + this.host = host; + } + + public String getHost() { + return this.host; + } +} diff --git a/src/main/java/com/coveo/pushapiclient/PlatformClient.java b/src/main/java/com/coveo/pushapiclient/PlatformClient.java index 8e4134e9..2d9df7ba 100644 --- a/src/main/java/com/coveo/pushapiclient/PlatformClient.java +++ b/src/main/java/com/coveo/pushapiclient/PlatformClient.java @@ -19,6 +19,7 @@ public class PlatformClient { private final String apiKey; private final String organizationId; private final HttpClient httpClient; + private final Environment environment; /** * Construct a PlatformClient @@ -30,12 +31,35 @@ public PlatformClient(String apiKey, String organizationId) { this.apiKey = apiKey; this.organizationId = organizationId; this.httpClient = HttpClient.newHttpClient(); + this.environment = Environment.PRODUCTION; } + /** + * Construct a PlatformClient + * + * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param organizationId The Coveo Organization identifier. + * @param httpClient The HttpClient. + */ public PlatformClient(String apiKey, String organizationId, HttpClient httpClient) { this.apiKey = apiKey; this.organizationId = organizationId; this.httpClient = httpClient; + this.environment = Environment.PRODUCTION; + } + + /** + * Construct a PlatformClient + * + * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param organizationId The Coveo Organization identifier. + * @param environment The Environment to be used. + */ + public PlatformClient(String apiKey, String organizationId, Environment environment) { + this.apiKey = apiKey; + this.organizationId = organizationId; + this.httpClient = HttpClient.newHttpClient(); + this.environment = environment; } /** @@ -149,7 +173,7 @@ public HttpResponse deleteSecurityIdentity(String securityProviderId, Se */ public HttpResponse deleteOldSecurityIdentities(String securityProviderId, SecurityIdentityDeleteOptions batchDelete) throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); - URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/olderthan?orderingId=%s&queueDelay=%s", batchDelete.getOrderingId(), batchDelete.getQueueDelay())); + URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/olderthan?queueDelay=%s%s", batchDelete.getQueueDelay(), appendOrderingId(batchDelete.getOrderingId()))); HttpRequest request = HttpRequest.newBuilder() .headers(headers) @@ -160,6 +184,19 @@ public HttpResponse deleteOldSecurityIdentities(String securityProviderI return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } + /** + * Returns the orderingId for the query string only when a valid orderingId is available. + * + * @param orderingId + * @return + */ + public String appendOrderingId(long orderingId) { + if (orderingId > 0) { + return String.format("&orderingId=%s", orderingId); + } + return ""; + } + /** * Manage batches of security identities. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55). * @@ -171,7 +208,7 @@ public HttpResponse deleteOldSecurityIdentities(String securityProviderI */ public HttpResponse manageSecurityIdentities(String securityProviderId, SecurityIdentityBatchConfig batchConfig) throws IOException, InterruptedException { String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); - URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/batch?fileId=%s&orderingId=%s", batchConfig.getFileId(), batchConfig.getOrderingId())); + URI uri = URI.create(this.getBaseProviderURL(securityProviderId) + String.format("/permissions/batch?fileId=%s%s", batchConfig.getFileId(), appendOrderingId(batchConfig.getOrderingId()))); HttpRequest request = HttpRequest.newBuilder() .headers(headers) @@ -249,17 +286,37 @@ public HttpResponse createFileContainer() throws IOException, Interrupte return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } + /** + * Update the status of a Push source. See [Updating the Status of a Push Source](https://docs.coveo.com/en/35). + * + * @param status + * @return + * @throws IOException + * @throws InterruptedException + */ + public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) throws IOException, InterruptedException { + String[] headers = this.getHeaders(this.getAuthorizationHeader(), this.getContentTypeApplicationJSONHeader()); + URI uri = URI.create(this.getBasePushURL() + String.format("/sources/%s/status?statusType=%s", sourceId, status.toString())); + + HttpRequest request = HttpRequest.newBuilder() + .headers(headers) + .uri(uri) + .POST(HttpRequest.BodyPublishers.ofString("")) + .build(); + + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + /** * Upload content update into a file container. See [Upload the Content Update into the File Container](https://docs.coveo.com/en/90/index-content/manage-batches-of-items-in-a-push-source#step-2-upload-the-content-update-into-the-file-container). * - * @param sourceId * @param fileContainer - * @param batchUpdate + * @param batchUpdateJson * @return * @throws IOException * @throws InterruptedException */ - public HttpResponse uploadContentToFileContainer(String sourceId, FileContainer fileContainer, BatchUpdateRecord batchUpdate) throws IOException, InterruptedException { + public HttpResponse uploadContentToFileContainer(FileContainer fileContainer, String batchUpdateJson) throws IOException, InterruptedException { String[] headers = fileContainer.requiredHeaders.entrySet() .stream() .flatMap(entry -> Stream.of(entry.getKey(), entry.getValue())) @@ -270,7 +327,7 @@ public HttpResponse uploadContentToFileContainer(String sourceId, FileCo HttpRequest request = HttpRequest.newBuilder() .headers(headers) .uri(uri) - .PUT(HttpRequest.BodyPublishers.ofString(new Gson().toJson(batchUpdate))) + .PUT(HttpRequest.BodyPublishers.ofString(batchUpdateJson)) .build(); return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); @@ -298,6 +355,28 @@ public HttpResponse pushFileContainerContent(String sourceId, FileContai return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); } + /** + * Push a binary to a File Container. See [Upload the Item Data Into the File Container](https://docs.coveo.com/en/69#step-2-upload-the-item-data-into-the-file-container) + * + * @param fileContainer + * @param fileAsBytes + * @return + * @throws IOException + * @throws InterruptedException + */ + public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException { + String[] headers = this.getHeaders(this.getAes256Header(), this.getContentTypeApplicationOctetStreamHeader()); + URI uri = URI.create(fileContainer.uploadUri); + + HttpRequest request = HttpRequest.newBuilder() + .headers(headers) + .uri(uri) + .PUT(HttpRequest.BodyPublishers.ofByteArray(fileAsBytes)) + .build(); + + return this.httpClient.send(request, HttpResponse.BodyHandlers.ofString()); + } + private String getBaseSourceURL() { return String.format("%s/sources", this.getBasePlatformURL()); } @@ -307,7 +386,7 @@ private String getBasePlatformURL() { } private String getBasePushURL() { - return String.format("https://api.cloud.coveo.com/push/v1/organizations/%s", this.organizationId); + return String.format("%s/push/v1/organizations/%s", this.environment.getHost(), this.organizationId); } private String getBaseProviderURL(String providerId) { @@ -331,6 +410,14 @@ private String[] getContentTypeApplicationJSONHeader() { return new String[]{"Content-Type", "application/json", "Accept", "application/json"}; } + private String[] getAes256Header() { + return new String[]{"x-amz-server-side-encryption", "AES256"}; + } + + private String[] getContentTypeApplicationOctetStreamHeader() { + return new String[]{"Content-Type", "application/octet-stream"}; + } + private String toJSON(HashMap hashMap) { return new Gson().toJson(hashMap, new TypeToken>() { }.getType()); diff --git a/src/main/java/com/coveo/pushapiclient/PushAPIStatus.java b/src/main/java/com/coveo/pushapiclient/PushAPIStatus.java new file mode 100644 index 00000000..7b6a90ee --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/PushAPIStatus.java @@ -0,0 +1,11 @@ +package com.coveo.pushapiclient; + +/** + * Enum for possible PushAPI statuses. See [Updating the Status of a Push Source](https://docs.coveo.com/en/35). + */ +public enum PushAPIStatus { + IDLE, + REBUILD, + INCREMENTAL, + REFRESH; +} diff --git a/src/main/java/com/coveo/pushapiclient/SecurityIdentityBatchResponse.java b/src/main/java/com/coveo/pushapiclient/SecurityIdentityBatchResponse.java new file mode 100644 index 00000000..58694f34 --- /dev/null +++ b/src/main/java/com/coveo/pushapiclient/SecurityIdentityBatchResponse.java @@ -0,0 +1,28 @@ +package com.coveo.pushapiclient; + +import java.net.http.HttpResponse; + +/** + * Used for the responses when pushing batches of Security Identities. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55) + */ +public class SecurityIdentityBatchResponse { + + protected HttpResponse s3Response; + protected HttpResponse batchResponse; + + public HttpResponse getS3Response() { + return s3Response; + } + + public void setS3Response(HttpResponse s3Response) { + this.s3Response = s3Response; + } + + public HttpResponse getBatchResponse() { + return batchResponse; + } + + public void setBatchResponse(HttpResponse batchResponse) { + this.batchResponse = batchResponse; + } +} diff --git a/src/main/java/com/coveo/pushapiclient/Source.java b/src/main/java/com/coveo/pushapiclient/Source.java index ab65b4b9..f374ddce 100644 --- a/src/main/java/com/coveo/pushapiclient/Source.java +++ b/src/main/java/com/coveo/pushapiclient/Source.java @@ -16,6 +16,15 @@ public Source(String apiKey, String organizationId) { this.platformClient = new PlatformClient(apiKey, organizationId); } + /** + * @param apiKey An apiKey capable of pushing documents and managing sources in a Coveo organization. See [Manage API Keys](https://docs.coveo.com/en/1718). + * @param organizationId The Coveo Organization identifier. + * @param environment The Environment to be used. + */ + public Source(String apiKey, String organizationId, Environment environment) { + this.platformClient = new PlatformClient(apiKey, organizationId, environment); + } + /** * Create a new push source. * @@ -68,6 +77,19 @@ public HttpResponse deleteSecurityIdentity(String securityProviderId, Se return this.platformClient.deleteSecurityIdentity(securityProviderId, securityIdentityDelete); } + /** + * Update the status of a Push source. See [Updating the Status of a Push Source](https://docs.coveo.com/en/35). + * + * @param sourceId + * @param status + * @return + * @throws IOException + * @throws InterruptedException + */ + public HttpResponse updateSourceStatus(String sourceId, PushAPIStatus status) throws IOException, InterruptedException { + return this.platformClient.updateSourceStatus(sourceId, status); + } + /** * Delete old security identities. See [Disabling Old Security Identities](https://docs.coveo.com/en/33). * @@ -134,7 +156,54 @@ public HttpResponse deleteDocument(String sourceId, String documentId, B public HttpResponse batchUpdateDocuments(String sourceId, BatchUpdate batchUpdate) throws IOException, InterruptedException { HttpResponse resFileContainer = this.platformClient.createFileContainer(); FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); - this.platformClient.uploadContentToFileContainer(sourceId, fileContainer, batchUpdate.marshal()); + this.platformClient.uploadContentToFileContainer(fileContainer, new Gson().toJson(batchUpdate.marshal())); return this.platformClient.pushFileContainerContent(sourceId, fileContainer); } + + /** + * Manages pushing batches of Security Identities to a File Container, then into Coveo. See [Manage Batches of Security Identities](https://docs.coveo.com/en/55) + * + * @param securityProviderId + * @param batchIdentity + * @return + * @throws IOException + * @throws InterruptedException + */ + public SecurityIdentityBatchResponse batchUpdateSecurityIdentities(String securityProviderId, BatchIdentity batchIdentity) throws IOException, InterruptedException { + SecurityIdentityBatchResponse securityIdentityBatchResponse = new SecurityIdentityBatchResponse(); + HttpResponse resFileContainer = this.platformClient.createFileContainer(); + FileContainer fileContainer = new Gson().fromJson(resFileContainer.body(), FileContainer.class); + String batchIdJson = new Gson().toJson(batchIdentity.marshal()); + securityIdentityBatchResponse.s3Response = this.platformClient.uploadContentToFileContainer(fileContainer, batchIdJson); + if (securityIdentityBatchResponse.s3Response.statusCode() >= 200 && securityIdentityBatchResponse.s3Response.statusCode() <= 299) { //maybe just 200 or 202 + SecurityIdentityBatchConfig batchConfig = new SecurityIdentityBatchConfig(fileContainer.fileId, 0l); + securityIdentityBatchResponse.batchResponse = this.manageSecurityIdentities(securityProviderId, batchConfig); + } + return securityIdentityBatchResponse; + } + + /** + * Creates a File Container. [Creating a File Container](https://docs.coveo.com/en/43) + * + * @return + * @throws IOException + * @throws InterruptedException + */ + public FileContainer createFileContainer() throws IOException, InterruptedException { + HttpResponse resFileContainer = this.platformClient.createFileContainer(); + return new Gson().fromJson(resFileContainer.body(), FileContainer.class); + } + + /** + * Push file to a File Container. [Using the compressedBinaryDataFileId Property](https://docs.coveo.com/en/69) + * + * @param fileContainer + * @param fileAsBytes + * @return + * @throws IOException + * @throws InterruptedException + */ + public HttpResponse pushBinaryToFileContainer(FileContainer fileContainer, byte[] fileAsBytes) throws IOException, InterruptedException { + return this.platformClient.pushBinaryToFileContainer(fileContainer, fileAsBytes); + } } diff --git a/src/test/java/com/coveo/pushapiclient/BatchUpdateTest.java b/src/test/java/com/coveo/pushapiclient/BatchUpdateTest.java index f2c8d2d8..e7383bf9 100644 --- a/src/test/java/com/coveo/pushapiclient/BatchUpdateTest.java +++ b/src/test/java/com/coveo/pushapiclient/BatchUpdateTest.java @@ -38,9 +38,21 @@ public void setUp() { list3.add(db3); list3.add(db4); - batch1 = new BatchUpdate(list1, list2); - batch2 = new BatchUpdate(list1, list2); - batch3 = new BatchUpdate(list2, list3); + DeleteDocument del1 = new DeleteDocument("123"); + DeleteDocument del2 = new DeleteDocument("456"); + DeleteDocument del3 = new DeleteDocument("789"); + + List delList1 = new ArrayList<>(); + delList1.add(del1); + delList1.add(del2); + + List delList2 = new ArrayList<>(); + delList2.add(del2); + delList2.add(del3); + + batch1 = new BatchUpdate(list1, delList1); + batch2 = new BatchUpdate(list1, delList1); + batch3 = new BatchUpdate(list2, delList2); batch4 = batch1; } diff --git a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java index b6d5e4fd..8acf472b 100644 --- a/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java +++ b/src/test/java/com/coveo/pushapiclient/PlatformClientTest.java @@ -1,5 +1,6 @@ package com.coveo.pushapiclient; +import com.google.gson.Gson; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; @@ -14,7 +15,9 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.*; +import static org.mockito.Mockito.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class PlatformClientTest { private PlatformClient client; @@ -43,13 +46,17 @@ public SecurityIdentityDelete securityIdentityDelete() { } public SecurityIdentityBatchConfig securityIdentityBatchConfig() { - return new SecurityIdentityBatchConfig("the_file_id", 1234l); + return new SecurityIdentityBatchConfig("the_file_id", 1234L); } public DocumentBuilder documentBuilder() { return new DocumentBuilder("the_uri", "the_title"); } + public DeleteDocument deleteDocument() { + return new DeleteDocument("12345"); + } + public Document document() { return documentBuilder().getDocument(); } @@ -72,7 +79,7 @@ public BatchUpdateRecord batchUpdateRecord() { BatchUpdate batchUpdate = new BatchUpdate(new ArrayList<>() {{ add(documentBuilder()); }}, new ArrayList<>() {{ - add(documentBuilder()); + add(deleteDocument()); }}); return batchUpdate.marshal(); } @@ -194,6 +201,15 @@ public void testManageSecurityIdentities() throws IOException, InterruptedExcept assertApplicationJsonHeader(); } + @Test + public void testAppendOrderingId() throws IOException, InterruptedException { + String standardOrderingParam = client.appendOrderingId(1234L); + String noOrderingParam = client.appendOrderingId(0L); + + assertTrue(standardOrderingParam.contains(String.format("orderingId=%s", "1234"))); + assertEquals("", noOrderingParam); + } + @Test public void testPushDocument() throws IOException, InterruptedException { client.pushDocument("my_source", documentString(), document().uri, CompressionType.UNCOMPRESSED); @@ -223,7 +239,7 @@ public void testCreateFileContainer() throws IOException, InterruptedException { @Test public void testUploadContentToFileContainer() throws IOException, InterruptedException { - client.uploadContentToFileContainer("my_source", fileContainer(), batchUpdateRecord()); + client.uploadContentToFileContainer(fileContainer(), new Gson().toJson(batchUpdateRecord())); verify(httpClient).send(argument.capture(), any(HttpResponse.BodyHandlers.ofString().getClass())); assertEquals("PUT", argument.getValue().method()); @@ -235,7 +251,7 @@ public void testUploadContentToFileContainer() throws IOException, InterruptedEx ArrayList delete = (ArrayList) requestBody.get("delete"); assertEquals(document().uri, addOrUpdate.get(0).get("documentId")); - assertEquals(document().uri, delete.get(0).get("documentId")); + assertEquals(deleteDocument().documentId, delete.get(0).get("documentId")); } @Test