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.vocabulary;
017
018import java.util.Arrays;
019import java.util.HashSet;
020import java.util.List;
021import java.util.Set;
022
023import org.junit.jupiter.api.Test;
024
025import static org.junit.jupiter.api.Assertions.assertFalse;
026import static org.junit.jupiter.api.Assertions.assertNotNull;
027import static org.junit.jupiter.api.Assertions.assertTrue;
028
029/**
030 * Checks InterpretationRemark implementations.
031 */
032public class InterpretationRemarkTest {
033
034  private static final String ROOT_PACKAGE = "org.gbif.api.vocabulary";
035
036  // All the implementations of InterpretationRemark
037  private static final List<Class<?>> IMPLEMENTING_CLASSES =
038    Arrays.asList(NameUsageIssue.class, OccurrenceIssue.class);
039
040  /**
041   * Checks all classes (enumerations) that implement {@link InterpretationRemark} to ensure they
042   * have unique entry names. {@link InterpretationRemark} implementations can be used as keys and
043   * can also be serialized as String. For this reason we want to ensure we have unique entries
044   * among implementations. This is mainly to avoid confusion when we do aggregation.
045   */
046  @Test
047  public void testInterpretationRemarkImplementations() {
048    Set<String> interpretationRemarks = new HashSet<>();
049    Set<String> interpretationRemarksId = new HashSet<>();
050    IMPLEMENTING_CLASSES
051      .forEach(cl -> Arrays.asList((InterpretationRemark[]) cl.getEnumConstants()).forEach(
052        ir -> {
053          assertTrue(interpretationRemarks.add(ir.toString()),
054              "Enumeration value " + ir + " is unique within all InterpretationRemark implementations.");
055          assertTrue(interpretationRemarksId.add(ir.getId()),
056              "Enumeration value " + ir + " with getID "
057                  + ir.getId() + " is unique within all InterpretationRemark implementations.");
058        }
059        )
060      );
061  }
062
063  @Test
064  public void testInterpretationRemarkNotNulls() {
065    IMPLEMENTING_CLASSES
066      .forEach(cl -> Arrays.asList((InterpretationRemark[]) cl.getEnumConstants()).forEach(
067        ir -> assertNotNull(
068            ir.getSeverity(),
069          "InterpretationRemark implementations return a not null value for getSeverity()")
070        )
071      );
072  }
073
074  @Test
075  public void testInterpretationRemarkDeprecated() {
076    assertTrue(OccurrenceIssue.COORDINATE_PRECISION_UNCERTAINTY_MISMATCH.isDeprecated());
077    assertFalse(OccurrenceIssue.BASIS_OF_RECORD_INVALID.isDeprecated());
078  }
079}