Search in sources :

Example 56 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class FileInfoTests method testInvalidFieldsInFromXContent.

public void testInvalidFieldsInFromXContent() throws IOException {
    final int iters = scaledRandomIntBetween(1, 10);
    for (int iter = 0; iter < iters; iter++) {
        final BytesRef hash = new BytesRef(scaledRandomIntBetween(0, 1024 * 1024));
        hash.length = hash.bytes.length;
        for (int i = 0; i < hash.length; i++) {
            hash.bytes[i] = randomByte();
        }
        String name = "foobar";
        String physicalName = "_foobar";
        String failure = null;
        long length = Math.max(0, Math.abs(randomLong()));
        // random corruption
        switch(randomIntBetween(0, 3)) {
            case 0:
                name = "foo,bar";
                failure = "missing or invalid file name";
                break;
            case 1:
                physicalName = "_foo,bar";
                failure = "missing or invalid physical file name";
                break;
            case 2:
                length = -Math.abs(randomLong());
                failure = "missing or invalid file length";
                break;
            case 3:
                break;
            default:
                fail("shouldn't be here");
        }
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.startObject();
        builder.field(FileInfo.NAME, name);
        builder.field(FileInfo.PHYSICAL_NAME, physicalName);
        builder.field(FileInfo.LENGTH, length);
        builder.field(FileInfo.WRITTEN_BY, Version.LATEST.toString());
        builder.field(FileInfo.CHECKSUM, "666");
        builder.endObject();
        byte[] xContent = BytesReference.toBytes(BytesReference.bytes(builder));
        if (failure == null) {
            // No failures should read as usual
            final BlobStoreIndexShardSnapshot.FileInfo parsedInfo;
            try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
                parser.nextToken();
                parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
            }
            assertThat(name, equalTo(parsedInfo.name()));
            assertThat(physicalName, equalTo(parsedInfo.physicalName()));
            assertThat(length, equalTo(parsedInfo.length()));
            assertEquals("666", parsedInfo.checksum());
            assertEquals("666", parsedInfo.metadata().checksum());
            assertEquals(Version.LATEST, parsedInfo.metadata().writtenBy());
        } else {
            try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
                parser.nextToken();
                BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
                fail("Should have failed with [" + failure + "]");
            } catch (OpenSearchParseException ex) {
                assertThat(ex.getMessage(), containsString(failure));
            }
        }
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) Matchers.containsString(org.hamcrest.Matchers.containsString) FileInfo(org.opensearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo) BytesRef(org.apache.lucene.util.BytesRef) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 57 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class RepositoryDataTests method testIndexThatReferenceANullSnapshot.

public void testIndexThatReferenceANullSnapshot() throws IOException {
    final XContentBuilder builder = XContentBuilder.builder(randomFrom(XContentType.JSON).xContent());
    builder.startObject();
    {
        builder.startArray("snapshots");
        builder.value(new SnapshotId("_name", "_uuid"));
        builder.endArray();
        builder.startObject("indices");
        {
            builder.startObject("docs");
            {
                builder.field("id", "_id");
                builder.startArray("snapshots");
                {
                    builder.startObject();
                    if (randomBoolean()) {
                        builder.field("name", "_name");
                    }
                    builder.endObject();
                }
                builder.endArray();
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    try (XContentParser xParser = createParser(builder)) {
        OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> RepositoryData.snapshotsFromXContent(xParser, randomNonNegativeLong(), randomBoolean()));
        assertThat(e.getMessage(), equalTo("Detected a corrupted repository, " + "index [docs/_id] references an unknown snapshot uuid [null]"));
    }
}
Also used : SnapshotId(org.opensearch.snapshots.SnapshotId) OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 58 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GeoContextMappingTests method testMissingGeoField.

public void testMissingGeoField() throws Exception {
    XContentBuilder mapping = jsonBuilder();
    mapping.startObject();
    mapping.startObject("properties");
    mapping.startObject("suggestion");
    mapping.field("type", "completion");
    mapping.field("analyzer", "simple");
    mapping.startArray("contexts");
    mapping.startObject();
    mapping.field("name", "st");
    mapping.field("type", "geo");
    mapping.field("path", "pin");
    mapping.field("precision", 5);
    mapping.endObject();
    mapping.endArray();
    mapping.endObject();
    mapping.endObject();
    mapping.endObject();
    OpenSearchParseException ex = expectThrows(OpenSearchParseException.class, () -> createIndex("test", Settings.EMPTY, "type1", mapping));
    assertThat(ex.getMessage(), equalTo("field [pin] referenced in context [st] is not defined in the mapping"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Example 59 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class DateIndexNameFactoryTests method testRequiredFields.

public void testRequiredFields() throws Exception {
    DateIndexNameProcessor.Factory factory = new DateIndexNameProcessor.Factory(TestTemplateService.instance());
    Map<String, Object> config = new HashMap<>();
    config.put("date_rounding", "y");
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> factory.create(null, null, null, config));
    assertThat(e.getMessage(), Matchers.equalTo("[field] required property is missing"));
    config.clear();
    config.put("field", "_field");
    e = expectThrows(OpenSearchParseException.class, () -> factory.create(null, null, null, config));
    assertThat(e.getMessage(), Matchers.equalTo("[date_rounding] required property is missing"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) HashMap(java.util.HashMap)

Example 60 with OpenSearchParseException

use of org.opensearch.OpenSearchParseException in project OpenSearch by opensearch-project.

the class GrokProcessorFactoryTests method testBuildMissingField.

public void testBuildMissingField() throws Exception {
    GrokProcessor.Factory factory = new GrokProcessor.Factory(Collections.emptyMap(), MatcherWatchdog.noop());
    Map<String, Object> config = new HashMap<>();
    config.put("patterns", Collections.singletonList("(?<foo>\\w+)"));
    OpenSearchParseException e = expectThrows(OpenSearchParseException.class, () -> factory.create(null, null, null, config));
    assertThat(e.getMessage(), equalTo("[field] required property is missing"));
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) HashMap(java.util.HashMap)

Aggregations

OpenSearchParseException (org.opensearch.OpenSearchParseException)108 XContentParser (org.opensearch.common.xcontent.XContentParser)34 HashMap (java.util.HashMap)27 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)16 ArrayList (java.util.ArrayList)15 Map (java.util.Map)12 Matchers.containsString (org.hamcrest.Matchers.containsString)12 IOException (java.io.IOException)11 List (java.util.List)9 ParsingException (org.opensearch.common.ParsingException)5 GeoPoint (org.opensearch.common.geo.GeoPoint)5 Settings (org.opensearch.common.settings.Settings)5 Token (org.opensearch.common.xcontent.XContentParser.Token)5 MappedFieldType (org.opensearch.index.mapper.MappedFieldType)5 HashSet (java.util.HashSet)4 GeometryCollectionBuilder (org.opensearch.common.geo.builders.GeometryCollectionBuilder)4 UncheckedIOException (java.io.UncheckedIOException)3 DateTimeParseException (java.time.format.DateTimeParseException)3 CoordinatesBuilder (org.opensearch.common.geo.builders.CoordinatesBuilder)3 MultiPointBuilder (org.opensearch.common.geo.builders.MultiPointBuilder)3