001/* 002 * Licensed under the Apache License, Version 2.0 (the "License"); 003 * you may not use this file except in compliance with the License. 004 * You may obtain a copy of the License at 005 * 006 * http://www.apache.org/licenses/LICENSE-2.0 007 * 008 * Unless required by applicable law or agreed to in writing, software 009 * distributed under the License is distributed on an "AS IS" BASIS, 010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 011 * See the License for the specific language governing permissions and 012 * limitations under the License. 013 */ 014package org.gbif.dwc.extensions; 015 016import org.gbif.dwc.digester.ThesaurusHandlingRule; 017import org.gbif.dwc.terms.DcTerm; 018import org.gbif.dwc.xml.SAXUtils; 019import org.gbif.utils.file.FileUtils; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.net.URL; 024import java.util.List; 025import java.util.Map; 026 027import javax.xml.parsers.ParserConfigurationException; 028 029import org.junit.jupiter.api.Test; 030import org.xml.sax.SAXException; 031 032import static org.junit.jupiter.api.Assertions.assertNotNull; 033 034public class ExtensionFactoryTest { 035 036 @Test 037 public void testExtensionFactory() throws IOException, ParserConfigurationException, SAXException { 038 String vocabUrl = "http://rs.gbif.org/vocabulary/gbif/description_type.xml"; 039 InputStream vocab = FileUtils.classpathStream("extension/vocabulary_gbif_description_type.xml"); 040 InputStream extension = FileUtils.classpathStream("extension/extension_gbif_1.0_description.xml"); 041 042 VocabularyFactory vocabFactory = new VocabularyFactory(SAXUtils.getNsAwareSaxParserFactory()); 043 044 ThesaurusHandlingRule thr = new ThesaurusHandlingRule(new MockVocabulariesManager(vocabUrl, vocabFactory.build(vocab))); 045 ExtensionFactory factory = new ExtensionFactory(thr, SAXUtils.getNsAwareSaxParserFactory()); 046 047 Extension ext = factory.build(extension, new URL(vocabUrl), false); 048 049 assertNotNull(ext); 050 assertNotNull(ext.getProperty(DcTerm.type).getVocabulary()); 051 } 052 053 /** 054 * Mock VocabulariesManager that support only 1 vocabulary 055 */ 056 private static class MockVocabulariesManager implements VocabulariesManager{ 057 058 private final String vocabUrl; 059 private final Vocabulary vocab; 060 061 public MockVocabulariesManager(String vocabUrl, Vocabulary vocab){ 062 this.vocabUrl = vocabUrl; 063 this.vocab = vocab; 064 } 065 066 @Override 067 public Vocabulary get(String s) { 068 return null; 069 } 070 071 @Override 072 public Vocabulary get(URL url) { 073 if(vocabUrl.equalsIgnoreCase(url.toString())){ 074 return vocab; 075 } 076 return null; 077 } 078 079 @Override 080 public Map<String, String> getI18nVocab(String s, String s1) { 081 return null; 082 } 083 084 @Override 085 public List<Vocabulary> list() { 086 return null; 087 } 088 } 089}