View Javadoc
1   /*
2    * Copyright 2018 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.fhir.support.translation;
18  
19  import org.junit.Before;
20  import org.junit.Test;
21  import org.openehealth.ipf.commons.ihe.fhir.translation.DefaultUriMapper;
22  import org.openehealth.ipf.commons.map.BidiMappingService;
23  
24  import static org.junit.Assert.assertEquals;
25  import static org.junit.Assert.assertFalse;
26  
27  /**
28   *
29   */
30  public class DefaultUriMapperTest {
31  
32      private BidiMappingService mappingService;
33      private DefaultUriMapper uriMapper;
34  
35      @Before
36      public void setup() {
37          mappingService = new BidiMappingService();
38          mappingService.setMappingScript(getClass().getResource("/mapping.map"));
39          uriMapper = new DefaultUriMapper(mappingService, "uriToOid", "uriToNamespace");
40      }
41  
42      @Test
43      public void testTranslateOidUrn() throws Exception {
44          String oid = "1.2.3.4.5.6.7.8.9";
45          assertEquals(oid, uriMapper.uriToOid("urn:oid:" + oid).get());
46      }
47  
48      @Test
49      public void testTranslateUriToOid() throws Exception {
50          String uri = "http://org.openehealth/ipf/commons/ihe/fhir/1";
51          assertEquals("1.2.3.4", uriMapper.uriToOid(uri).get());
52      }
53  
54      @Test
55      public void testTranslateUriToOidFails() throws Exception {
56          String uri = "http://org.openehealth/ipf/commons/ihe/fhir/9";
57          assertFalse(uriMapper.uriToOid(uri).isPresent());
58      }
59  
60      @Test
61      public void testTranslatePinUrn() throws Exception {
62          String namespace = "namespace";
63          assertEquals(namespace, uriMapper.uriToNamespace("urn:pin:" + namespace).get());
64      }
65  
66      @Test
67      public void testTranslateUriToNamespace() throws Exception {
68          String uri = "http://org.openehealth/ipf/commons/ihe/fhir/1";
69          assertEquals("fhir1", uriMapper.uriToNamespace(uri).get());
70          uri = "http://org.openehealth/ipf/commons/ihe/fhir/9";
71          assertFalse(uriMapper.uriToNamespace(uri).isPresent());
72      }
73  
74      @Test
75      public void testTranslateNamespaceToUri() throws Exception {
76          String namespace = "fhir1";
77          assertEquals("http://org.openehealth/ipf/commons/ihe/fhir/1", uriMapper.namespaceToUri(namespace));
78      }
79  }