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 java.util.UUID; 019 020import org.junit.jupiter.api.Test; 021 022import static org.junit.jupiter.api.Assertions.assertEquals; 023import static org.junit.jupiter.api.Assertions.assertFalse; 024import static org.junit.jupiter.api.Assertions.assertNull; 025import static org.junit.jupiter.api.Assertions.assertThrows; 026import static org.junit.jupiter.api.Assertions.assertTrue; 027 028public class DatasetKeyTest { 029 030 private static final String EXTERNAL_DATASET_KEY = 031 "7879e569-4a13-4643-b833-d1a564675b86:urn%3Alsid%3Aknb.ecoinformatics.org%3Aknb-lter-cdr%3A8133"; 032 private static final String INVALID_EXTERNAL_DATASET_KEY = 033 "7879e569:urn%3Alsid%3Aknb.ecoinformatics.org%3Aknb-lter-cdr%3A8133"; 034 private static final String SHORT_EXTERNAL_DATASET_KEY = "7879e569"; 035 private static final String VALID_UUID = "127da08e-5326-41ef-abbd-a27f65f3670d"; 036 private static final String VALID_UUID_MIXED_CASE = "127DA08E-5326-41ef-abbd-a27f65f3670d"; 037 038 @Test 039 public void testParseInvalid() { 040 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString("7879e569-4a13-4643-b833-d1a564675bzz")); 041 } 042 043 @Test 044 public void testParseInvalid2() { 045 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString("7879e569-4a13-4643-b833-d1a564675b861")); 046 } 047 048 @Test 049 public void testParseInvalid3() { 050 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString(VALID_UUID + ":")); 051 } 052 053 @Test 054 public void testParseInvalid4() { 055 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString("7879e569-4a13-4643-b833_d1a564675b85:aha")); 056 } 057 058 @Test 059 public void testParseInvalid5() { 060 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString("")); 061 } 062 063 @Test 064 public void testParseInvalid6() { 065 assertThrows(NullPointerException.class, () -> DatasetKey.fromString(null)); 066 } 067 068 @Test 069 public void testParseInvalidWhitespaceOnly() { 070 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString("7879e569-4a13-4643-b833_d1a564675b85: ")); 071 } 072 073 @Test 074 public void testInvalidConstructor() { 075 assertThrows(NullPointerException.class, () -> new DatasetKey(UUID.randomUUID(), null)); 076 } 077 078 @Test 079 public void testInvalidConstructor2() { 080 assertThrows(IllegalArgumentException.class, () -> new DatasetKey(UUID.randomUUID(), "")); 081 } 082 083 @Test 084 public void testInvalidConstructor3() { 085 assertThrows(IllegalArgumentException.class, () -> new DatasetKey(UUID.randomUUID(), " ")); 086 } 087 088 @Test 089 public void testDatasetIdWhitespace() { 090 DatasetKey key = new DatasetKey(UUID.randomUUID(), " 12 "); 091 assertEquals("12", key.getDatasetId()); 092 } 093 094 @Test 095 public void testParseValid() { 096 DatasetKey key = DatasetKey.fromString(VALID_UUID); 097 assertFalse(key.isExternalKey()); 098 assertNull(key.getDatasetId()); 099 assertEquals(VALID_UUID, key.getRegistryKey().toString()); 100 101 key = DatasetKey.fromString(VALID_UUID_MIXED_CASE); 102 assertFalse(key.isExternalKey()); 103 assertNull(key.getDatasetId()); 104 assertEquals(VALID_UUID, key.getRegistryKey().toString()); 105 106 key = DatasetKey.fromString(VALID_UUID + ":aha"); 107 assertEquals("aha", key.getDatasetId()); 108 109 key = DatasetKey.fromString(VALID_UUID + ":1"); 110 assertEquals("1", key.getDatasetId()); 111 112 key = DatasetKey.fromString(VALID_UUID + ":?:1-3"); 113 assertEquals("?:1-3", key.getDatasetId()); 114 115 key = DatasetKey.fromString(VALID_UUID + ":-"); 116 assertEquals("-", key.getDatasetId()); 117 } 118 119 @Test 120 public void testParseNetworkKeyInvalid() { 121 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString(INVALID_EXTERNAL_DATASET_KEY)); 122 } 123 124 @Test 125 public void testParseNetworkKeyShort() { 126 assertThrows(IllegalArgumentException.class, () -> DatasetKey.fromString(SHORT_EXTERNAL_DATASET_KEY)); 127 } 128 129 @Test 130 public void testParseNetworkKey() { 131 DatasetKey key = DatasetKey.fromString(EXTERNAL_DATASET_KEY); 132 assertTrue(key.isExternalKey()); 133 assertEquals(UUID.fromString("7879e569-4a13-4643-b833-d1a564675b86"), key.getRegistryKey()); 134 assertEquals("urn%3Alsid%3Aknb.ecoinformatics.org%3Aknb-lter-cdr%3A8133", key.getDatasetId()); 135 } 136 137 @Test 138 public void testParseNetworkKeyMixedCase() { 139 DatasetKey key = DatasetKey.fromString(VALID_UUID_MIXED_CASE); 140 assertFalse(key.isExternalKey()); 141 assertEquals(UUID.fromString(VALID_UUID_MIXED_CASE), key.getRegistryKey()); 142 } 143 144 @Test 145 public void testRoundtrip() { 146 DatasetKey key = DatasetKey.fromString(EXTERNAL_DATASET_KEY); 147 assertEquals(EXTERNAL_DATASET_KEY, key.toDatasetKey()); 148 149 key = new DatasetKey(UUID.fromString(VALID_UUID)); 150 assertEquals(VALID_UUID, key.toDatasetKey()); 151 } 152 153}