001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.util;
017
018import org.gbif.api.vocabulary.IdentifierType;
019
020import java.util.stream.Stream;
021
022import org.junit.jupiter.api.Test;
023import org.junit.jupiter.params.ParameterizedTest;
024import org.junit.jupiter.params.provider.Arguments;
025import org.junit.jupiter.params.provider.MethodSource;
026
027import static org.junit.jupiter.api.Assertions.assertEquals;
028import static org.junit.jupiter.api.Assertions.assertFalse;
029import static org.junit.jupiter.api.Assertions.assertNull;
030import static org.junit.jupiter.api.Assertions.assertTrue;
031
032public class IdentifierUtilsTest {
033
034  /**
035   * A test is run for each set of parameters. To add a new test, just add a new set of parameters.
036   */
037  public static Stream<Arguments> getTestParameters() {
038    return Stream.of(
039      Arguments.of(null, "49c5b4ac-e3bf-401b-94b1-c94a2ad5c8d6", IdentifierType.UUID),
040      Arguments.of("https://www.gbif.org/dataset/ds1", "ds1", IdentifierType.GBIF_PORTAL),
041      Arguments.of(null, "local_1:dx:4", IdentifierType.UNKNOWN),
042      Arguments.of("http://en.wikipedia.org/wiki/Handle_1", "http://en.wikipedia.org/wiki/Handle_1", IdentifierType.HANDLER),
043      Arguments.of("urn:ds:acns:1", "urn:ds:acns:1", IdentifierType.URI),
044      Arguments.of("http://ipt.gbif.org/resource.do?r=ds1", "http://ipt.gbif.org/resource.do?r=ds1", IdentifierType.URL),
045      Arguments.of("ftp://ftp.gbif.org", "ftp://ftp.gbif.org", IdentifierType.FTP),
046      Arguments.of("https://doi.org/10.1016/S1097-2765(03)00225-9", "10.1016/S1097-2765(03)00225-9", IdentifierType.DOI),
047      Arguments.of("http://www.lsid.info/132187", "132187", IdentifierType.LSID),
048      Arguments.of("https://ror.org/02mhbdp94.","https://ror.org/02mhbdp94.", IdentifierType.ROR),
049      Arguments.of("https://www.checklistbank.org/dataset/1008","1008", IdentifierType.CLB_DATASET_KEY)
050    );
051  }
052
053  @ParameterizedTest
054  @MethodSource("getTestParameters")
055  public void testGetIdentifierLink(String expected, String identifier, IdentifierType type) {
056    assertEquals(expected, IdentifierUtils.getIdentifierLink(identifier, type));
057  }
058
059  @SuppressWarnings({"ConstantConditions", "unused"})
060  @ParameterizedTest
061  @MethodSource("getTestParameters")
062  public void testGetIdentifierLinkWithoutIdentifer(String expected, String identifier, IdentifierType type) {
063    assertNull(IdentifierUtils.getIdentifierLink(null, type));
064  }
065
066  @SuppressWarnings({"ConstantConditions", "unused"})
067  @ParameterizedTest
068  @MethodSource("getTestParameters")
069  public void testGetIdentifierLinkWithoutType(String expected, String identifier, IdentifierType type) {
070    assertNull(IdentifierUtils.getIdentifierLink(identifier, null));
071  }
072
073  @Test
074  public void wikidataValidatorTest() {
075    assertTrue(IdentifierUtils.isValidWikidataIdentifier("http://www.wikidata.org/entity/Q1528756"));
076    assertTrue(IdentifierUtils.isValidWikidataIdentifier("https://www.wikidata.org/entity/q1528756"));
077    assertFalse(IdentifierUtils.isValidWikidataIdentifier("https://www.wikidata.org/entity/1528756"));
078    assertFalse(IdentifierUtils.isValidWikidataIdentifier("https://www.wikidata.org/entity/q15287h56"));
079    assertFalse(IdentifierUtils.isValidWikidataIdentifier("https://www.wikidata.org/entity/1528756q"));
080  }
081
082  @Test
083  public void rorIDValidatorTest() {
084    assertTrue(IdentifierUtils.isValidRORIdentifier("https://ror.org/0566bfb96"));
085    assertFalse(IdentifierUtils.isValidRORIdentifier("https://ror.org/https://ror.org/0566bfb96"));
086    assertFalse(IdentifierUtils.isValidRORIdentifier("0566bfb96"));
087    assertFalse(IdentifierUtils.isValidRORIdentifier("https://ror.org/2566bfb96"));
088  }
089
090  @Test
091  public void CLBDatasetKeyValidatorTest() {
092    assertTrue(IdentifierUtils.isValidCLBDatasetKey("123"));
093    assertFalse(IdentifierUtils.isValidCLBDatasetKey("0123"));
094    assertFalse(IdentifierUtils.isValidCLBDatasetKey("0"));
095    assertFalse(IdentifierUtils.isValidCLBDatasetKey("-123"));
096    assertFalse(IdentifierUtils.isValidCLBDatasetKey("abc123"));
097  }
098}