001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.model.occurrence;
015
016import org.gbif.api.model.occurrence.geo.DistanceUnit;
017
018import org.junit.jupiter.api.Test;
019
020import static  org.hamcrest.MatcherAssert.assertThat;
021import static org.hamcrest.Matchers.equalToObject;
022import static org.junit.jupiter.api.Assertions.assertEquals;
023import static org.junit.jupiter.api.Assertions.assertThrows;
024
025/**
026 * Tests for DistanceUnit class.
027 */
028public class DistanceUnitTests {
029
030  @Test
031  public void testDistanceUnitParseNames() {
032    //Test all unit names
033    for (DistanceUnit unit : DistanceUnit.values()) {
034      for (String name : unit.getNames()) {
035        DistanceUnit.parseDistance("1" + name);
036      }
037    }
038  }
039
040  @Test
041  public void testDistanceUnitParseWrongNames() {
042    assertThrows(IllegalArgumentException.class,
043                () -> DistanceUnit.parseDistance("100wt")); //Wrong unit
044  }
045
046  @Test
047  public void testDistanceUnitConversions() {
048    //Simple conversion test
049    DistanceUnit.Distance oneKm = DistanceUnit.parseDistance("1km");
050    DistanceUnit.Distance tenThousandCm = DistanceUnit.parseDistance("100000cm");
051    assertThat(oneKm, equalToObject(tenThousandCm));
052  }
053
054
055  @Test
056  public void testGeoDistanceStringParsing() {
057    //Simple conversion test
058    String rawGeoDistance = "40.2, 100.2, 1.1km";
059    DistanceUnit.GeoDistance oneKmDistance = DistanceUnit.GeoDistance.parseGeoDistance(rawGeoDistance);
060
061    assertEquals(rawGeoDistance, oneKmDistance.toGeoDistanceString());
062  }
063
064}