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.model.registry;
017
018import org.gbif.api.vocabulary.DatasetType;
019import org.gbif.api.vocabulary.License;
020import org.gbif.api.vocabulary.MaintenanceUpdateFrequency;
021
022import java.net.URI;
023import java.util.Date;
024import java.util.Set;
025import java.util.UUID;
026import java.util.stream.Collectors;
027
028import jakarta.validation.ConstraintViolation;
029import jakarta.validation.Validation;
030import jakarta.validation.Validator;
031import jakarta.validation.ValidatorFactory;
032
033import org.junit.jupiter.api.Test;
034
035import static org.junit.jupiter.api.Assertions.assertEquals;
036import static org.junit.jupiter.api.Assertions.assertFalse;
037import static org.junit.jupiter.api.Assertions.assertTrue;
038
039public class DatasetTest {
040
041  @Test
042  public void testValidations() {
043    Validator validator;
044    try (ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory()) {
045      validator = validatorFactory.getValidator();
046      Dataset ds = new Dataset();
047
048      // 3 non-mandatory fields that don't validate
049      ds.setTitle("B"); // too short
050      ds.setHomepage(URI.create("www.gbif.org")); // doesn't start with http or https
051      ds.setLogoUrl(URI.create("file:///tmp/aha")); // bad http URI
052      ds.setVersion("2026-05-19s"); // too long
053
054      // All mandatory fields missing include:
055      // type
056      // installationKey
057      // publishingOrganizationKey
058
059      // perform validation
060      Set<ConstraintViolation<Dataset>> violations = validator.validate(ds);
061      assertFalse(violations.isEmpty(), "Violations were expected");
062
063      // ensure all 7 expected violations are caught
064      Set<String> propertiesInViolation = Set.of(
065          "title", "homepage", "logoUrl", "type", "version",
066          "installationKey", "publishingOrganizationKey"
067      );
068
069      Set<String> actualProperties = violations.stream()
070          .map(v -> v.getPropertyPath().toString())
071          .collect(Collectors.toSet());
072
073      assertEquals(7, violations.size());
074      assertEquals(propertiesInViolation, actualProperties);
075
076      // fix non-mandatory fields that don't validate
077      ds.setTitle("Rooftop bugs");
078      ds.setHomepage(URI.create("https://www.gbif.org"));
079      ds.setLogoUrl(URI.create("https://www.gbif.org/logo.png"));
080
081      // add all mandatory fields that were missing
082      ds.setType(DatasetType.SAMPLING_EVENT);
083      ds.setInstallationKey(UUID.randomUUID());
084      ds.setPublishingOrganizationKey(UUID.randomUUID());
085      ds.setVersion("1");
086
087      // perform validation again
088      violations = validator.validate(ds);
089      assertTrue(violations.isEmpty(), "No violations were expected");
090    }
091  }
092
093  @Test
094  public void testLenientEquals() {
095    Dataset ds1 = new Dataset();
096    ds1.setMaintenanceUpdateFrequency(MaintenanceUpdateFrequency.DAILY);
097    ds1.setMaintenanceDescription("Daily, except for holidays");
098    ds1.setLicense(License.CC_BY_4_0);
099    ds1.setCreated(new Date());
100
101    Dataset ds2 = new Dataset();
102    ds2.setMaintenanceUpdateFrequency(MaintenanceUpdateFrequency.DAILY);
103    ds2.setMaintenanceDescription("Daily, except for holidays");
104    ds2.setLicense(License.CC0_1_0); // different license
105    ds1.setCreated(new Date()); // different created date!
106
107    assertTrue(ds1.lenientEquals(ds2)); // true because lenient equals excludes
108  }
109}