001/*
002 * Copyright 2021 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.dwc.terms;
017
018import org.junit.jupiter.api.Test;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022
023public class DcTermTest extends TermBaseTest<DcTerm> {
024
025  private static final TermFactory TERM_FACTORY = TermFactory.instance();
026
027  public DcTermTest() {
028    super(DcTerm.class);
029  }
030
031  @Test
032  @Override
033  public void testNames() {
034    for (DcTerm t : DcTerm.values()) {
035      assertFalse(t.simpleName().contains("_"), "Bad term: " + t.simpleName());
036      assertFalse(t.qualifiedName().contains("_"), "Bad term: " + t.qualifiedName());
037    }
038  }
039
040  /**
041   * Test that iterates over all DcTerm values and uses the simple name alone as input to the factory and then verifies
042   * the Term returned is the same as the original one.
043   */
044  @Test
045  public void testTermEquality() {
046    for (DcTerm t : DcTerm.values()) {
047      assertEquals(t, TERM_FACTORY.findTerm(t.qualifiedName()));
048      assertEquals(t, TERM_FACTORY.findTerm(t.simpleName(), t.isClass()));
049    }
050  }
051
052  /**
053   * A lookup for "http://purl.org/dc/terms/rights" shouldn't return "http://purl.org/dc/elements/1.1/rights".
054   */
055  @Test
056  public void testFindTermWithValidNamespace() {
057    String qualifiedName = "http://purl.org/dc/terms/rights";
058    Term term = TERM_FACTORY.findTerm(qualifiedName);
059    assertEquals(qualifiedName, term.qualifiedName());
060  }
061
062  /**
063   * There are 15 terms in DcElement that share the same name with DcTerm elements. This test ensures, that
064   * when a term lookup is done with the simple name only (no prefix, no namespace), the DcTerm under the /terms
065   * namespace is returned, not the DcElement under the /elements/1.1/rights namespace.
066   */
067  @Test
068  public void testFindTermWithNoNamespaceAndNoPrefix() {
069    String name = "rights";
070    Term term = TERM_FACTORY.findTerm(name);
071    assertEquals("http://purl.org/dc/terms/rights", term.qualifiedName());
072  }
073
074  /**
075   * There are 15 terms in DcElement that share the same name with DcTerm elements. This test ensures, that
076   * when a term lookup is done with the prefix "dcterms", the DcTerm under the /terms namespace
077   * is returned, not the DcElements term under the /elements/1.1/ namespace.
078   */
079  @Test
080  public void testFindTermWithDctermsPrefix() {
081    String name = "dcterms:rights";
082    Term term = TERM_FACTORY.findTerm(name);
083    assertEquals("http://purl.org/dc/terms/rights", term.qualifiedName());
084  }
085
086  /**
087   * There are 15 terms in DcElement that share the same name with DcTerm elements. This test ensures, that
088   * when a term lookup is done with the prefix "dct", the DcTerm under the /terms namespace
089   * is returned, not the DcElements term under the /elements/1.1/ namespace.
090   */
091  @Test
092  public void testFindTermWithDctPrefix() {
093    String name = "dct:rights";
094    Term term = TERM_FACTORY.findTerm(name);
095    assertEquals("http://purl.org/dc/terms/rights", term.qualifiedName());
096  }
097}