|
| 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 | +package org.elasticsearch.common.geo; |
| 20 | + |
| 21 | +import org.elasticsearch.common.CheckedConsumer; |
| 22 | +import org.elasticsearch.common.bytes.BytesReference; |
| 23 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 24 | +import org.elasticsearch.common.xcontent.XContentParser; |
| 25 | +import org.elasticsearch.common.xcontent.json.JsonXContent; |
| 26 | +import org.elasticsearch.test.ESTestCase; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | + |
| 30 | +import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; |
| 31 | + |
| 32 | +public class GeoUtilTests extends ESTestCase { |
| 33 | + |
| 34 | + public void testPrecisionParser() throws IOException { |
| 35 | + assertEquals(10, parsePrecision(builder -> builder.field("test", 10))); |
| 36 | + assertEquals(10, parsePrecision(builder -> builder.field("test", 10.2))); |
| 37 | + assertEquals(6, parsePrecision(builder -> builder.field("test", "6"))); |
| 38 | + assertEquals(7, parsePrecision(builder -> builder.field("test", "1km"))); |
| 39 | + assertEquals(7, parsePrecision(builder -> builder.field("test", "1.1km"))); |
| 40 | + } |
| 41 | + |
| 42 | + public void testIncorrectPrecisionParser() { |
| 43 | + expectThrows(NumberFormatException.class, () -> parsePrecision(builder -> builder.field("test", "10.1.1.1"))); |
| 44 | + expectThrows(NumberFormatException.class, () -> parsePrecision(builder -> builder.field("test", "364.4smoots"))); |
| 45 | + assertEquals( |
| 46 | + "precision too high [0.01mm]", |
| 47 | + expectThrows(IllegalArgumentException.class, () -> parsePrecision(builder -> builder.field("test", "0.01mm"))).getMessage() |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + /** |
| 52 | + * Invokes GeoUtils.parsePrecision parser on the value generated by tokenGenerator |
| 53 | + * <p> |
| 54 | + * The supplied tokenGenerator should generate a single field that contains the precision in |
| 55 | + * one of the supported formats or malformed precision value if error handling is tested. The |
| 56 | + * method return the parsed value or throws an exception, if precision value is malformed. |
| 57 | + */ |
| 58 | + private int parsePrecision(CheckedConsumer<XContentBuilder, IOException> tokenGenerator) throws IOException { |
| 59 | + XContentBuilder builder = jsonBuilder().startObject(); |
| 60 | + tokenGenerator.accept(builder); |
| 61 | + builder.endObject(); |
| 62 | + XContentParser parser = createParser(JsonXContent.jsonXContent, BytesReference.bytes(builder)); |
| 63 | + assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken()); // { |
| 64 | + assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken()); // field name |
| 65 | + assertTrue(parser.nextToken().isValue()); // field value |
| 66 | + int precision = GeoUtils.parsePrecision(parser); |
| 67 | + assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken()); // } |
| 68 | + assertNull(parser.nextToken()); // no more tokens |
| 69 | + return precision; |
| 70 | + } |
| 71 | +} |
0 commit comments