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.HashSet;
025import java.util.Set;
026import java.util.UUID;
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    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
044    Validator validator = validatorFactory.getValidator();
045
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
053    // All mandatory fields missing include:
054    // ds.setType(?);
055    // ds.setInstallationKey(?);
056    // ds.setPublishingOrganizationKey(?);
057
058    // perform validation
059    Set<ConstraintViolation<Dataset>> violations = validator.validate(ds);
060    assertFalse(violations.isEmpty(), "Violations were expected");
061
062    // ensure all 6 expected violations are caught
063    Set<String> propertiesInViolation = new HashSet<>();
064    propertiesInViolation.add("title");
065    propertiesInViolation.add("homepage");
066    propertiesInViolation.add("logoUrl");
067    propertiesInViolation.add("type");
068    propertiesInViolation.add("installationKey");
069    propertiesInViolation.add("publishingOrganisationKey");
070
071    assertEquals(6, violations.size());
072
073    // fix non-mandatory fields that don't validate
074    ds.setTitle("Rooftop bugs");
075    ds.setHomepage(URI.create("http://www.gbif.org"));
076    ds.setLogoUrl(URI.create("http://www.gbif.org/logo.png"));
077
078    // add all mandatory fields that were missing
079    ds.setType(DatasetType.SAMPLING_EVENT);
080    ds.setInstallationKey(UUID.randomUUID());
081    ds.setPublishingOrganizationKey(UUID.randomUUID());
082
083    // perform validation again
084    violations = validator.validate(ds);
085    assertTrue(violations.isEmpty(), "No violations were expected");
086  }
087
088  @Test
089  public void testLenientEquals() {
090    Dataset ds1 = new Dataset();
091    ds1.setMaintenanceUpdateFrequency(MaintenanceUpdateFrequency.DAILY);
092    ds1.setMaintenanceDescription("Daily, except for holidays");
093    ds1.setLicense(License.CC_BY_4_0);
094    ds1.setCreated(new Date());
095
096    Dataset ds2 = new Dataset();
097    ds2.setMaintenanceUpdateFrequency(MaintenanceUpdateFrequency.DAILY);
098    ds2.setMaintenanceDescription("Daily, except for holidays");
099    ds2.setLicense(License.CC0_1_0); // different license
100    ds1.setCreated(new Date()); // different created date!
101
102    assertTrue(ds1.lenientEquals(ds2)); // true because lenient equals excludes
103  }
104}