28b56436d521a7b6958bbabfd31118c316ea279f
[yangtools.git] / common / yang-common / src / test / java / org / opendaylight / yangtools / yang / common / BiMapYangNamespaceContextTest.java
1 /*
2  * Copyright (c) 2019 Pantheon Technologies, s.r.o.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.common;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
13
14 import com.google.common.collect.ImmutableBiMap;
15 import java.io.ByteArrayInputStream;
16 import java.io.ByteArrayOutputStream;
17 import java.io.DataInputStream;
18 import java.io.DataOutputStream;
19 import java.io.IOException;
20 import java.util.Optional;
21 import org.junit.Test;
22
23 public class BiMapYangNamespaceContextTest {
24     private static final QNameModule FOO = QNameModule.create(XMLNamespace.of("foo"));
25     private static final QNameModule BAR = QNameModule.create(XMLNamespace.of("bar"));
26     private static final QNameModule BAZ = QNameModule.create(XMLNamespace.of("baz"));
27
28     private final BiMapYangNamespaceContext context = new BiMapYangNamespaceContext(
29         ImmutableBiMap.of("foo", FOO, "bar", BAR));
30
31     @Test
32     public void testEquals() {
33         assertTrue(context.equals(context));
34         assertTrue(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO, "bar", BAR))));
35         assertFalse(context.equals(null));
36         assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO))));
37         assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("bar", BAR))));
38     }
39
40     @Test
41     public void testPrefixForNamespace() {
42         assertEquals(Optional.of("foo"), context.findPrefixForNamespace(FOO));
43         assertEquals(Optional.of("bar"), context.findPrefixForNamespace(BAR));
44         assertEquals(Optional.empty(), context.findPrefixForNamespace(BAZ));
45     }
46
47     @Test
48     public void testNamespaceForPrefix() {
49         assertEquals(Optional.of(FOO), context.findNamespaceForPrefix("foo"));
50         assertEquals(Optional.of(BAR), context.findNamespaceForPrefix("bar"));
51         assertEquals(Optional.empty(), context.findNamespaceForPrefix("baz"));
52     }
53
54     @Test
55     public void testReadWrite() throws IOException {
56         final byte[] bytes;
57         try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
58             try (DataOutputStream dos = new DataOutputStream(bos)) {
59                 context.writeTo(dos);
60             }
61             bytes = bos.toByteArray();
62         }
63
64         final BiMapYangNamespaceContext other;
65         try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
66             other = BiMapYangNamespaceContext.readFrom(dis);
67         }
68
69         assertEquals(context, other);
70     }
71
72     @Test
73     public void testCreateQName() {
74         assertEquals(QName.create(FOO, "some"), context.createQName("foo", "some"));
75     }
76 }