|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.ingest.common; |
| 21 | + |
| 22 | +import org.elasticsearch.ElasticsearchException; |
| 23 | +import org.elasticsearch.common.unit.ByteSizeUnit; |
| 24 | +import org.elasticsearch.common.unit.ByteSizeValue; |
| 25 | +import org.elasticsearch.ingest.IngestDocument; |
| 26 | +import org.elasticsearch.ingest.Processor; |
| 27 | +import org.elasticsearch.ingest.RandomDocumentPicks; |
| 28 | +import org.hamcrest.CoreMatchers; |
| 29 | + |
| 30 | +import static org.hamcrest.Matchers.equalTo; |
| 31 | + |
| 32 | +public class BytesProcessorTests extends AbstractStringProcessorTestCase { |
| 33 | + |
| 34 | + private String modifiedInput; |
| 35 | + |
| 36 | + @Override |
| 37 | + protected AbstractStringProcessor newProcessor(String field, boolean ignoreMissing, String targetField) { |
| 38 | + return new BytesProcessor(randomAlphaOfLength(10), field, ignoreMissing, targetField); |
| 39 | + } |
| 40 | + |
| 41 | + @Override |
| 42 | + protected String modifyInput(String input) { |
| 43 | + //largest value that allows all results < Long.MAX_VALUE bytes |
| 44 | + long randomNumber = randomLongBetween(1, Long.MAX_VALUE / ByteSizeUnit.PB.toBytes(1)); |
| 45 | + ByteSizeUnit randomUnit = randomFrom(ByteSizeUnit.values()); |
| 46 | + modifiedInput = randomNumber + randomUnit.getSuffix(); |
| 47 | + return modifiedInput; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + protected Long expectedResult(String input) { |
| 52 | + return ByteSizeValue.parseBytesSizeValue(modifiedInput, null, "").getBytes(); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + protected Class<Long> expectedResultType() { |
| 57 | + return Long.class; |
| 58 | + } |
| 59 | + |
| 60 | + public void testTooLarge() { |
| 61 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 62 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "8912pb"); |
| 63 | + Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); |
| 64 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); |
| 65 | + assertThat(exception.getMessage(), |
| 66 | + CoreMatchers.equalTo("failed to parse setting [" + fieldName + "] with value [8912pb] as a size in bytes")); |
| 67 | + assertThat(exception.getCause().getMessage(), |
| 68 | + CoreMatchers.containsString("Values greater than 9223372036854775807 bytes are not supported")); |
| 69 | + } |
| 70 | + |
| 71 | + public void testNotBytes() { |
| 72 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 73 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "junk"); |
| 74 | + Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); |
| 75 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); |
| 76 | + assertThat(exception.getMessage(), |
| 77 | + CoreMatchers.equalTo("failed to parse [junk]")); |
| 78 | + } |
| 79 | + |
| 80 | + public void testMissingUnits() { |
| 81 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 82 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "1"); |
| 83 | + Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); |
| 84 | + ElasticsearchException exception = expectThrows(ElasticsearchException.class, () -> processor.execute(ingestDocument)); |
| 85 | + assertThat(exception.getMessage(), |
| 86 | + CoreMatchers.containsString("unit is missing or unrecognized")); |
| 87 | + } |
| 88 | + |
| 89 | + public void testFractional() throws Exception { |
| 90 | + IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random()); |
| 91 | + String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "1.1kb"); |
| 92 | + Processor processor = newProcessor(fieldName, randomBoolean(), fieldName); |
| 93 | + processor.execute(ingestDocument); |
| 94 | + assertThat(ingestDocument.getFieldValue(fieldName, expectedResultType()), equalTo(1126L)); |
| 95 | + assertWarnings("Fractional bytes values are deprecated. Use non-fractional bytes values instead: [1.1kb] found for setting " + |
| 96 | + "[" + fieldName + "]"); |
| 97 | + } |
| 98 | +} |
0 commit comments