use of org.opensearch.common.xcontent.XContent in project OpenSearch by opensearch-project.
the class RepositoryDataTests method testIndexThatReferencesAnUnknownSnapshot.
public void testIndexThatReferencesAnUnknownSnapshot() throws IOException {
final XContent xContent = randomFrom(XContentType.values()).xContent();
final RepositoryData repositoryData = generateRandomRepoData();
XContentBuilder builder = XContentBuilder.builder(xContent);
repositoryData.snapshotsToXContent(builder, Version.CURRENT);
RepositoryData parsedRepositoryData;
try (XContentParser xParser = createParser(builder)) {
parsedRepositoryData = RepositoryData.snapshotsFromXContent(xParser, repositoryData.getGenId(), randomBoolean());
}
assertEquals(repositoryData, parsedRepositoryData);
Map<String, SnapshotId> snapshotIds = new HashMap<>();
Map<String, SnapshotState> snapshotStates = new HashMap<>();
Map<String, Version> snapshotVersions = new HashMap<>();
for (SnapshotId snapshotId : parsedRepositoryData.getSnapshotIds()) {
snapshotIds.put(snapshotId.getUUID(), snapshotId);
snapshotStates.put(snapshotId.getUUID(), parsedRepositoryData.getSnapshotState(snapshotId));
snapshotVersions.put(snapshotId.getUUID(), parsedRepositoryData.getVersion(snapshotId));
}
final IndexId corruptedIndexId = randomFrom(parsedRepositoryData.getIndices().values());
Map<IndexId, List<SnapshotId>> indexSnapshots = new HashMap<>();
final ShardGenerations.Builder shardGenBuilder = ShardGenerations.builder();
for (Map.Entry<String, IndexId> snapshottedIndex : parsedRepositoryData.getIndices().entrySet()) {
IndexId indexId = snapshottedIndex.getValue();
List<SnapshotId> snapshotsIds = new ArrayList<>(parsedRepositoryData.getSnapshots(indexId));
if (corruptedIndexId.equals(indexId)) {
snapshotsIds.add(new SnapshotId("_uuid", "_does_not_exist"));
}
indexSnapshots.put(indexId, snapshotsIds);
final int shardCount = randomIntBetween(1, 10);
for (int i = 0; i < shardCount; ++i) {
shardGenBuilder.put(indexId, i, UUIDs.randomBase64UUID(random()));
}
}
assertNotNull(corruptedIndexId);
RepositoryData corruptedRepositoryData = new RepositoryData(parsedRepositoryData.getGenId(), snapshotIds, snapshotStates, snapshotVersions, indexSnapshots, shardGenBuilder.build(), IndexMetaDataGenerations.EMPTY);
final XContentBuilder corruptedBuilder = XContentBuilder.builder(xContent);
corruptedRepositoryData.snapshotsToXContent(corruptedBuilder, Version.CURRENT);
try (XContentParser xParser = createParser(corruptedBuilder)) {
OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> RepositoryData.snapshotsFromXContent(xParser, corruptedRepositoryData.getGenId(), randomBoolean()));
assertThat(e.getMessage(), equalTo("Detected a corrupted repository, index " + corruptedIndexId + " references an unknown " + "snapshot uuid [_does_not_exist]"));
}
}
use of org.opensearch.common.xcontent.XContent in project OpenSearch by opensearch-project.
the class AggregationsTests method parseAndAssert.
/**
* Test that parsing works for a randomly created Aggregations object with a
* randomized aggregation tree. The test randomly chooses an
* {@link XContentType}, randomizes the order of the {@link XContent} fields
* and randomly sets the `humanReadable` flag when rendering the
* {@link XContent}.
*
* @param addRandomFields
* if set, this will also add random {@link XContent} fields to
* tests that the parsers are lenient to future additions to rest
* responses
*/
private void parseAndAssert(boolean addRandomFields) throws IOException {
XContentType xContentType = randomFrom(XContentType.values());
final ToXContent.Params params = new ToXContent.MapParams(singletonMap(RestSearchAction.TYPED_KEYS_PARAM, "true"));
Aggregations aggregations = createTestInstance();
BytesReference originalBytes = toShuffledXContent(aggregations, xContentType, params, randomBoolean());
BytesReference mutated;
if (addRandomFields) {
/*
* - don't insert into the root object because it should only contain the named aggregations to test
*
* - don't insert into the "meta" object, because we pass on everything we find there
*
* - we don't want to directly insert anything random into "buckets" objects, they are used with
* "keyed" aggregations and contain named bucket objects. Any new named object on this level should
* also be a bucket and be parsed as such.
*
* - we cannot insert randomly into VALUE or VALUES objects e.g. in Percentiles, the keys need to be numeric there
*
* - we cannot insert into ExtendedMatrixStats "covariance" or "correlation" fields, their syntax is strict
*
* - we cannot insert random values in top_hits, as all unknown fields
* on a root level of SearchHit are interpreted as meta-fields and will be kept
*
* - exclude "key", it can be an array of objects and we need strict values
*/
Predicate<String> excludes = path -> (path.isEmpty() || path.endsWith("aggregations") || path.endsWith(Aggregation.CommonFields.META.getPreferredName()) || path.endsWith(Aggregation.CommonFields.BUCKETS.getPreferredName()) || path.endsWith(CommonFields.VALUES.getPreferredName()) || path.endsWith("covariance") || path.endsWith("correlation") || path.contains(CommonFields.VALUE.getPreferredName()) || path.endsWith(CommonFields.KEY.getPreferredName())) || path.contains("top_hits");
mutated = insertRandomFields(xContentType, originalBytes, excludes, random());
} else {
mutated = originalBytes;
}
try (XContentParser parser = createParser(xContentType.xContent(), mutated)) {
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
assertEquals(Aggregations.AGGREGATIONS_FIELD, parser.currentName());
assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
Aggregations parsedAggregations = Aggregations.fromXContent(parser);
BytesReference parsedBytes = XContentHelper.toXContent(parsedAggregations, xContentType, randomBoolean());
OpenSearchAssertions.assertToXContentEquivalent(originalBytes, parsedBytes, xContentType);
}
}
use of org.opensearch.common.xcontent.XContent in project OpenSearch by opensearch-project.
the class AbstractFilteringTestCase method testRawField.
public void testRawField() throws Exception {
final Builder expectedRawField = builder -> builder.startObject().field("foo", 0).startObject("raw").field("content", "hello world!").endObject().endObject();
final Builder expectedRawFieldFiltered = builder -> builder.startObject().field("foo", 0).endObject();
final Builder expectedRawFieldNotFiltered = builder -> builder.startObject().startObject("raw").field("content", "hello world!").endObject().endObject();
Builder sampleWithRaw = builder -> {
BytesReference raw = BytesReference.bytes(XContentBuilder.builder(builder.contentType().xContent()).startObject().field("content", "hello world!").endObject());
return builder.startObject().field("foo", 0).rawField("raw", raw.streamInput()).endObject();
};
// Test method: rawField(String fieldName, BytesReference content)
testFilter(expectedRawField, sampleWithRaw, emptySet(), emptySet());
testFilter(expectedRawFieldFiltered, sampleWithRaw, singleton("f*"), emptySet());
testFilter(expectedRawFieldFiltered, sampleWithRaw, emptySet(), singleton("r*"));
testFilter(expectedRawFieldNotFiltered, sampleWithRaw, singleton("r*"), emptySet());
testFilter(expectedRawFieldNotFiltered, sampleWithRaw, emptySet(), singleton("f*"));
sampleWithRaw = builder -> {
BytesReference raw = BytesReference.bytes(XContentBuilder.builder(builder.contentType().xContent()).startObject().field("content", "hello world!").endObject());
return builder.startObject().field("foo", 0).rawField("raw", raw.streamInput()).endObject();
};
// Test method: rawField(String fieldName, InputStream content)
testFilter(expectedRawField, sampleWithRaw, emptySet(), emptySet());
testFilter(expectedRawFieldFiltered, sampleWithRaw, singleton("f*"), emptySet());
testFilter(expectedRawFieldFiltered, sampleWithRaw, emptySet(), singleton("r*"));
testFilter(expectedRawFieldNotFiltered, sampleWithRaw, singleton("r*"), emptySet());
testFilter(expectedRawFieldNotFiltered, sampleWithRaw, emptySet(), singleton("f*"));
}
Aggregations