|
| 1 | +/* |
| 2 | + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one |
| 3 | + * or more contributor license agreements. Licensed under the Elastic License |
| 4 | + * 2.0; you may not use this file except in compliance with the Elastic License |
| 5 | + * 2.0. |
| 6 | + */ |
| 7 | + |
| 8 | +package org.elasticsearch.xpack.vectortile.rest; |
| 9 | + |
| 10 | +import com.wdtinc.mapbox_vector_tile.VectorTile; |
| 11 | +import com.wdtinc.mapbox_vector_tile.build.MvtLayerProps; |
| 12 | + |
| 13 | +import org.elasticsearch.test.ESTestCase; |
| 14 | + |
| 15 | +import java.util.stream.StreamSupport; |
| 16 | + |
| 17 | +import static org.hamcrest.Matchers.containsString; |
| 18 | + |
| 19 | +public class VectorTileUtilTests extends ESTestCase { |
| 20 | + |
| 21 | + public void testAddPropertyToFeature() { |
| 22 | + final MvtLayerProps layerProps = new MvtLayerProps(); |
| 23 | + final VectorTile.Tile.Feature.Builder featureBuilder = VectorTile.Tile.Feature.newBuilder(); |
| 24 | + // boolean |
| 25 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "bool", true); |
| 26 | + assertPropertyToFeature(layerProps, featureBuilder, 1); |
| 27 | + // byte |
| 28 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "byte", (byte) 1); |
| 29 | + assertPropertyToFeature(layerProps, featureBuilder, 2); |
| 30 | + // short |
| 31 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "short", (short) 2); |
| 32 | + assertPropertyToFeature(layerProps, featureBuilder, 3); |
| 33 | + // integer |
| 34 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "integer", 3); |
| 35 | + assertPropertyToFeature(layerProps, featureBuilder, 4); |
| 36 | + // long |
| 37 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "long", 4L); |
| 38 | + assertPropertyToFeature(layerProps, featureBuilder, 5); |
| 39 | + // float |
| 40 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "float", 5f); |
| 41 | + assertPropertyToFeature(layerProps, featureBuilder, 6); |
| 42 | + // double |
| 43 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "double", 6d); |
| 44 | + assertPropertyToFeature(layerProps, featureBuilder, 7); |
| 45 | + // string |
| 46 | + VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "string", "7"); |
| 47 | + assertPropertyToFeature(layerProps, featureBuilder, 8); |
| 48 | + // invalid |
| 49 | + IllegalArgumentException ex = expectThrows( |
| 50 | + IllegalArgumentException.class, |
| 51 | + () -> VectorTileUtils.addPropertyToFeature(featureBuilder, layerProps, "invalid", new Object()) |
| 52 | + ); |
| 53 | + assertThat(ex.getMessage(), containsString("Unsupported vector tile type for field [invalid]")); |
| 54 | + } |
| 55 | + |
| 56 | + private void assertPropertyToFeature(MvtLayerProps layerProps, VectorTile.Tile.Feature.Builder featureBuilder, int numProps) { |
| 57 | + assertEquals(numProps, StreamSupport.stream(layerProps.getKeys().spliterator(), false).count()); |
| 58 | + assertEquals(numProps, StreamSupport.stream(layerProps.getVals().spliterator(), false).count()); |
| 59 | + assertEquals(2 * numProps, featureBuilder.getTagsCount()); |
| 60 | + } |
| 61 | +} |
0 commit comments