View Javadoc
1   /*
2    * Copyright 2016 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *       http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.openehealth.ipf.commons.ihe.xds.core.validate;
18  
19  import org.junit.Test;
20  import org.openehealth.ipf.commons.core.URN;
21  
22  import java.net.URI;
23  import java.net.URISyntaxException;
24  import java.util.UUID;
25  
26  import static org.junit.Assert.assertEquals;
27  import static org.junit.Assert.assertFalse;
28  import static org.junit.Assert.assertTrue;
29  import static org.junit.Assert.fail;
30  
31  /**
32   *
33   */
34  public class UUIDValidatorTest {
35  
36      private static final UUIDValidator validator = new UUIDValidator();
37  
38      @Test
39      public void testValidateGoodCases() throws XDSMetaDataException {
40          validator.validate(UUID.randomUUID().toString());
41      }
42  
43      @Test
44      public void testValidateBadCases() throws XDSMetaDataException {
45          assertFails("");
46          assertFails("1");
47      }
48  
49      @Test
50      public void testGetAsUUID() throws URISyntaxException {
51          UUID random = UUID.randomUUID();
52          assertEquals(random, validator.getAsUUID("urn:uuid:" + random.toString()).get());
53          assertEquals(random, validator.getAsUUID(new URI("urn", "uuid", random.toString())).get());
54          assertEquals(random, validator.getAsUUID(new URN(random)).get());
55          assertFalse(validator.getAsUUID("gablorg").isPresent());
56      }
57  
58      @Test
59      public void testGetSymbolic() throws URISyntaxException {
60          assertFalse(validator.getSymbolicId(new URI("urn", "uuid", UUID.randomUUID().toString()).toString()).isPresent());
61          assertTrue(validator.getSymbolicId("gablorg").isPresent());
62      }
63  
64      private static void assertFails(String value) {
65          try {
66              validator.validate(value);
67              fail("Expected exception: " + XDSMetaDataException.class + " for " + value);
68          } catch (XDSMetaDataException e) {
69              // Expected
70          }
71      }
72  }