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;
015
016import org.gbif.dwc.record.Record;
017import org.gbif.dwc.terms.DwcTerm;
018import org.gbif.dwc.terms.Term;
019import org.gbif.utils.file.FileUtils;
020
021import java.io.IOException;
022import java.util.List;
023import java.util.Map;
024import java.util.Optional;
025
026import org.junit.jupiter.api.Test;
027
028import static org.junit.jupiter.api.Assertions.assertEquals;
029import static org.junit.jupiter.api.Assertions.assertFalse;
030import static org.junit.jupiter.api.Assertions.assertNotNull;
031import static org.junit.jupiter.api.Assertions.assertTrue;
032
033/**
034 * Unit test related to {@link ArchiveFile}.
035 */
036public class ArchiveFileTest {
037
038  @Test
039  public void testIterator() throws UnsupportedArchiveException, IOException {
040    // test proper archive
041    Archive arch = DwcFiles.fromLocation(FileUtils.getClasspathFile("archive-dwc/DarwinCore.txt").toPath());
042    ArchiveFile af = arch.getCore();
043
044    assertNotNull(af);
045    assertNotNull(af.getId());
046    assertTrue(af.hasTerm(DwcTerm.scientificName));
047
048    // test iterator
049    int counter = 0;
050    Record last = null;
051    for (Record rec : af) {
052      counter++;
053      last = rec;
054      if (counter == 1) {
055        assertEquals("1559060", rec.id());
056      }
057    }
058    assertEquals(3248, counter);
059    assertNotNull(last);
060    assertEquals("3082", last.id());
061  }
062
063  @Test
064  public void testRowTypeEquivalence() {
065    ArchiveFile af = new ArchiveFile();
066    af.setRowType(DwcTerm.Occurrence);
067    assertEquals(af.getRowType(), DwcTerm.Occurrence);
068    assertEquals(DwcTerm.Occurrence, af.getRowType());
069    assertEquals(DwcTerm.Occurrence.qualifiedName(), af.getRowType().qualifiedName());
070  }
071
072  @Test
073  public void testGetHeader() throws UnsupportedArchiveException, IOException {
074    ArchiveFile core = getCore("archive-dwc/DarwinCore.txt");
075    List<List<Term>> header = core.getHeader();
076    assertEquals(12, header.size());
077    assertEquals(DwcTerm.scientificName, header.get(2).get(0));
078  }
079
080  @Test
081  public void testIdWithTermAssociated() throws UnsupportedArchiveException, IOException {
082    ArchiveFile core = getCore("meta-xml-variants/dwca-id-with-term");
083    List<List<Term>> header = core.getHeader();
084    assertEquals(6, header.size());
085    assertEquals(DwcTerm.occurrenceID, header.get(0).get(0));
086    assertEquals(0, header.get(3).size());
087  }
088
089  @Test
090  public void testDefaultValues() throws UnsupportedArchiveException, IOException {
091    ArchiveFile core = getCore("meta-xml-variants/dwca-id-with-term");
092    Optional<Map<Term, String>> defaultValues = core.getDefaultValues();
093    assertTrue(defaultValues.isPresent());
094    assertEquals("Plantae", defaultValues.get().get(DwcTerm.kingdom));
095    //declared with an index
096    assertEquals("XYZ", defaultValues.get().get(DwcTerm.nomenclaturalCode));
097  }
098
099  @Test
100  public void testEmptyArchiveFile() {
101    ArchiveFile archiveFile = new ArchiveFile();
102    List<List<Term>> header = archiveFile.getHeader();
103    assertEquals(0, header.size());
104    assertFalse(archiveFile.getDefaultValues().isPresent());
105  }
106
107  private ArchiveFile getCore(String testFilePath) throws IOException {
108    Archive arch = DwcFiles.fromLocation(FileUtils.getClasspathFile(testFilePath).toPath());
109    return arch.getCore();
110  }
111  
112}