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.api.model.registry;
015
016import org.gbif.api.vocabulary.Country;
017import org.gbif.api.vocabulary.Language;
018
019import java.net.URI;
020import java.util.Collections;
021import java.util.HashSet;
022import java.util.Set;
023import java.util.UUID;
024
025import jakarta.validation.ConstraintViolation;
026import jakarta.validation.Validation;
027import jakarta.validation.Validator;
028import jakarta.validation.ValidatorFactory;
029
030import org.junit.jupiter.api.Test;
031
032import static org.junit.jupiter.api.Assertions.assertFalse;
033import static org.junit.jupiter.api.Assertions.assertTrue;
034
035public class OrganizationTest {
036
037  @Test
038  public void testValidations() {
039    ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
040    Validator validator = validatorFactory.getValidator();
041
042    Organization org = new Organization();
043    org.setTitle("a");  // too short
044    org.setHomepage(Collections.singletonList(URI.create("www.gbif.org"))); // doesn't start with http or https
045    org.setLogoUrl(URI.create("file:///tmp/aha")); // bad http URI
046
047    // perform validation
048    Set<ConstraintViolation<Organization>> violations = validator.validate(org);
049    assertFalse(violations.isEmpty(), "Violations were expected");
050
051    // ensure all expected properties are caught
052    Set<String> propertiesInViolation = new HashSet<>();
053    propertiesInViolation.add("title");
054    propertiesInViolation.add("logoUrl");
055    propertiesInViolation.add("country");
056
057    for (ConstraintViolation<?> cv : violations) {
058      propertiesInViolation.remove(cv.getPropertyPath().toString());
059    }
060    assertTrue(propertiesInViolation.isEmpty(), "Properties incorrectly passed validation " + propertiesInViolation);
061
062    // fix validation problems
063    org.setTitle("Academy of Natural Sciences");
064    org.setHomepage(Collections.singletonList(URI.create("http://www.gbif.org")));
065    org.setLogoUrl(URI.create("http://www.gbif.org/logo.png"));
066    org.setLanguage(Language.ENGLISH);
067    org.setEndorsingNodeKey(UUID.randomUUID());
068    org.setCountry(Country.DENMARK);
069
070    // perform validation again
071    violations = validator.validate(org);
072    assertTrue(violations.isEmpty(), "No violations were expected");
073  }
074}