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 org.junit.jupiter.api.Test;
019
020import static org.junit.jupiter.api.Assertions.assertEquals;
021import static org.junit.jupiter.api.Assertions.assertFalse;
022import static org.junit.jupiter.api.Assertions.assertNull;
023import static org.junit.jupiter.api.Assertions.assertTrue;
024
025public class LicenseTest {
026
027  @Test
028  public void testFromString() {
029    // positive matches
030    assertEquals(License.CC0_1_0, License.fromLicenseUrl("http://creativecommons.org/publicdomain/zero/1.0/legalcode").get());
031    assertEquals(License.CC0_1_0, License.fromLicenseUrl("HTTP://creativecommons.org/publicdomain/zero/1.0/legalcode").get());
032    assertEquals(License.CC0_1_0, License.fromLicenseUrl(" http://creativecommons.org/publicdomain/zero/1.0/legalcode ").get());
033    assertEquals(License.CC0_1_0, License.fromLicenseUrl("http://creativecommons.org/publicdomain/zero/1.0/legalcode/").get());
034    assertEquals(License.CC0_1_0, License.fromLicenseUrl("http://creativecommons.org/publicdomain/zero/1.0").get());
035    assertEquals(License.CC0_1_0, License.fromLicenseUrl("http://creativecommons.org/publicdomain/zero/1.0/").get());
036    assertEquals(License.CC0_1_0, License.fromLicenseUrl("HTTPS://CREATIVECOMMONS.ORG/PUBLICDOMAIN/ZERO/1.0/").get());
037    assertEquals(License.CC_BY_4_0, License.fromLicenseUrl("http://creativecommons.org/licenses/by/4.0/legalcode").get());
038    assertEquals(License.CC_BY_NC_4_0, License.fromLicenseUrl("http://creativecommons.org/licenses/by-nc/4.0/legalcode").get());
039
040    // negative matches
041    assertNull(License.fromLicenseUrl("CC0").orElse(null));
042    assertNull(License.fromLicenseUrl("https://creativecommons.org/licenses/by/3.0/legalcode").orElse(null));
043    assertNull(License.fromLicenseUrl("https://creativecommons.org/licenses/by/3.0/").orElse(null));
044    assertNull(License.fromLicenseUrl("https://creativecommons.org/licenses/by-nc/2.0/legalcode").orElse(null));
045    assertNull(License.fromLicenseUrl("https://creativecommons.org/licenses/by-nc/2.0/").orElse(null));
046  }
047
048  @Test
049  public void testConcreteLicense() {
050    assertFalse(License.UNSPECIFIED.isConcrete());
051    assertFalse(License.UNSUPPORTED.isConcrete());
052    assertTrue(License.CC0_1_0.isConcrete());
053  }
054
055  @Test
056  public void testGetMostRestrictive(){
057    assertEquals(License.CC_BY_4_0, License.getMostRestrictive(License.CC0_1_0, License.CC_BY_4_0, License.UNSPECIFIED));
058    assertEquals(License.CC_BY_4_0, License.getMostRestrictive(License.CC_BY_4_0, License.CC0_1_0, License.UNSPECIFIED));
059
060    assertEquals(License.CC_BY_NC_4_0, License.getMostRestrictive(License.CC_BY_4_0, License.CC_BY_NC_4_0, License.UNSPECIFIED));
061
062    assertEquals(License.UNSPECIFIED, License.getMostRestrictive(License.UNSUPPORTED, License.CC0_1_0, License.UNSPECIFIED));
063    assertEquals(License.UNSPECIFIED, License.getMostRestrictive(License.UNSUPPORTED, License.UNSPECIFIED, License.UNSPECIFIED));
064    assertEquals(License.UNSPECIFIED, License.getMostRestrictive(License.CC_BY_NC_4_0, null, License.UNSPECIFIED));
065  }
066
067  @Test
068  public void testLicenseOrdinalNumber() {
069    int numberOfConcreteLicense = 0;
070    for (License currLicense : License.values()) {
071      if (currLicense.isConcrete()) {
072        numberOfConcreteLicense++;
073      }
074    }
075
076    //The ordinal number in the Enum implicitly defines the level of restriction.
077    assertEquals(3, numberOfConcreteLicense, "The License value ordinal test covers all values of the Concrete License");
078    assertTrue(License.CC0_1_0.ordinal() < License.CC_BY_4_0.ordinal());
079    assertTrue(License.CC_BY_4_0.ordinal() < License.CC_BY_NC_4_0.ordinal());
080  }
081}