Skip to content

Commit 1b376a6

Browse files
authored
feat: add support for permanentid generation (#12)
https://coveord.atlassian.net/browse/CDX-437
1 parent 0e878fe commit 1b376a6

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@
2929
<version>4.12</version>
3030
<scope>test</scope>
3131
</dependency>
32+
<dependency>
33+
<groupId>commons-codec</groupId>
34+
<artifactId>commons-codec</artifactId>
35+
<version>1.9</version>
36+
</dependency>
3237

3338

3439
</dependencies>

src/main/java/com/coveo/pushapiclient/DocumentBuilder.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.gson.Gson;
44
import com.google.gson.JsonObject;
5+
import org.apache.commons.codec.digest.DigestUtils;
56
import org.joda.time.DateTime;
67
import org.joda.time.format.ISODateTimeFormat;
78

@@ -161,6 +162,8 @@ public String marshal() {
161162
}
162163

163164
public JsonObject marshalJsonObject() {
165+
this.generatePermanentId();
166+
164167
JsonObject jsonDocument = new Gson().toJsonTree(this.document).getAsJsonObject();
165168
this.document.metadata.forEach((key, value) -> {
166169
jsonDocument.add(key, new Gson().toJsonTree(value));
@@ -195,4 +198,10 @@ private void validateReservedMetadataKeyNames(String key) {
195198
throw new RuntimeException(String.format("Cannot use %s as a metadata key: It is a reserved keynames. See https://docs.coveo.com/en/78/index-content/push-api-reference#json-document-reserved-key-names", key));
196199
}
197200
}
201+
202+
private void generatePermanentId() {
203+
if (this.document.permanentId == null) {
204+
this.document.permanentId = DigestUtils.sha256Hex(this.document.uri);
205+
}
206+
}
198207
}

src/test/java/com/coveo/pushapiclient/DocumentBuilderTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ public void testWithPermanentId() {
132132

133133
@Test
134134
public void testWithPermanentIdGeneration() {
135-
// TODO
135+
assertTrue(
136+
"permanentId should be generated automatically if not set",
137+
docBuilder.marshalJsonObject().get("permanentId").getAsString().length() > 0
138+
);
136139
}
137140

138141
@Test

0 commit comments

Comments
 (0)