Skip to content

Commit

Permalink
Create a new test mothod
Browse files Browse the repository at this point in the history
Signed-off-by: Gao Binlong <[email protected]>
  • Loading branch information
gaobinlong committed Dec 15, 2023
1 parent ab4c8fc commit 598c74a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ public IngestDocument execute(IngestDocument document) {
throw new IllegalArgumentException("cannot remove metadata field [" + path + "]");
}
// removing _id is disallowed when there's an external version specified in the request
if (path.equals(IngestDocument.Metadata.ID.getFieldName())) {
if (path.equals(IngestDocument.Metadata.ID.getFieldName())
&& document.hasField(IngestDocument.Metadata.VERSION_TYPE.getFieldName())) {
String versionType = document.getFieldValue(IngestDocument.Metadata.VERSION_TYPE.getFieldName(), String.class, true);
if (Objects.equals(versionType, VersionType.toString(VersionType.EXTERNAL))
|| Objects.equals(versionType, VersionType.toString(VersionType.EXTERNAL_GTE))) {
if (!Objects.equals(versionType, VersionType.toString(VersionType.INTERNAL))) {
Long version = document.getFieldValue(IngestDocument.Metadata.VERSION.getFieldName(), Long.class, true);
throw new IllegalArgumentException(
"cannot remove metadata field [_id] when specifying external version for the document, version: "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.ingest.common;

import org.opensearch.common.lucene.uid.Versions;
import org.opensearch.index.VersionType;
import org.opensearch.ingest.IngestDocument;
import org.opensearch.ingest.Processor;
Expand Down Expand Up @@ -180,8 +181,86 @@ public void testRemoveMetadataField() throws Exception {
assertThat(ingestDocument.hasField(metadataFieldName), equalTo(false));
}
}
}

public void testRemoveDocumentId() throws Exception {
Map<String, Object> config = new HashMap<>();
config.put("field", IngestDocument.Metadata.ID.getFieldName());
String processorTag = randomAlphaOfLength(10);

// test remove _id when _version_type is external
IngestDocument ingestDocumentWithExternalVersionType = new IngestDocument(
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
1L,
VersionType.EXTERNAL,
RandomDocumentPicks.randomSource(random())
);

Processor processorForExternalVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create(
null,
processorTag,
null,
config
);
assertThrows(
"cannot remove metadata field [_id] when specifying external version for the document, version: "
+ 1
+ ", version_type: "
+ VersionType.EXTERNAL,
IllegalArgumentException.class,
() -> processorForExternalVersionType.execute(ingestDocumentWithExternalVersionType)
);

// test remove _id when _version_type is external_gte
config.put("field", IngestDocument.Metadata.ID.getFieldName());
IngestDocument ingestDocumentWithExternalGTEVersionType = new IngestDocument(
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
1L,
VersionType.EXTERNAL_GTE,
RandomDocumentPicks.randomSource(random())
);

Processor processorForExternalGTEVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create(
null,
processorTag,
null,
config
);
assertThrows(
"cannot remove metadata field [_id] when specifying external version for the document, version: "
+ 1
+ ", version_type: "
+ VersionType.EXTERNAL_GTE,
IllegalArgumentException.class,
() -> processorForExternalGTEVersionType.execute(ingestDocumentWithExternalGTEVersionType)
);

// test remove _id when _version_type is internal
config.put("field", IngestDocument.Metadata.ID.getFieldName());
IngestDocument ingestDocumentWithInternalVersionType = new IngestDocument(
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
Versions.MATCH_ANY,
VersionType.INTERNAL,
RandomDocumentPicks.randomSource(random())
);

Processor processorForInternalVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create(
null,
processorTag,
null,
config
);
processorForInternalVersionType.execute(ingestDocumentWithInternalVersionType);
assertThat(ingestDocumentWithInternalVersionType.hasField(IngestDocument.Metadata.ID.getFieldName()), equalTo(false));

// test remove _id when _version_type is null
config.put("field", IngestDocument.Metadata.ID.getFieldName());
IngestDocument ingestDocumentWithNoVersionType = new IngestDocument(
RandomDocumentPicks.randomString(random()),
RandomDocumentPicks.randomString(random()),
Expand All @@ -190,11 +269,13 @@ public void testRemoveMetadataField() throws Exception {
null,
RandomDocumentPicks.randomSource(random())
);
Map<String, Object> config = new HashMap<>();
config.put("field", IngestDocument.Metadata.ID.getFieldName());
String processorTag = randomAlphaOfLength(10);
Processor processor = new RemoveProcessor.Factory(TestTemplateService.instance()).create(null, processorTag, null, config);
processor.execute(ingestDocumentWithNoVersionType);
Processor processorForNullVersionType = new RemoveProcessor.Factory(TestTemplateService.instance()).create(
null,
processorTag,
null,
config
);
processorForNullVersionType.execute(ingestDocumentWithNoVersionType);
assertThat(ingestDocumentWithNoVersionType.hasField(IngestDocument.Metadata.ID.getFieldName()), equalTo(false));
}
}

0 comments on commit 598c74a

Please sign in to comment.