2 * Copyright (c) 2019 Pantheon Technologies, s.r.o. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertTrue;
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;
21 import java.util.Optional;
22 import org.junit.Test;
24 public class BiMapYangNamespaceContextTest {
25 private static final QNameModule FOO = QNameModule.create(URI.create("foo"));
26 private static final QNameModule BAR = QNameModule.create(URI.create("bar"));
27 private static final QNameModule BAZ = QNameModule.create(URI.create("baz"));
29 private final BiMapYangNamespaceContext context = new BiMapYangNamespaceContext(
30 ImmutableBiMap.of("foo", FOO, "bar", BAR), FOO);
32 @Test(expected = IllegalArgumentException.class)
33 public void testUnmappedDefault() {
34 new BiMapYangNamespaceContext(ImmutableBiMap.of(), FOO);
38 public void testDefaultNamespace() {
39 assertEquals(Optional.of(FOO), context.getDefaultNamespace());
43 public void testEquals() {
44 assertTrue(context.equals(context));
45 assertTrue(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO, "bar", BAR), FOO)));
46 assertFalse(context.equals(null));
47 assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("foo", FOO), FOO)));
48 assertFalse(context.equals(new BiMapYangNamespaceContext(ImmutableBiMap.of("bar", BAR), BAR)));
52 public void testPrefixForNamespace() {
53 assertEquals(Optional.of("foo"), context.findPrefixForNamespace(FOO));
54 assertEquals(Optional.of("bar"), context.findPrefixForNamespace(BAR));
55 assertEquals(Optional.empty(), context.findPrefixForNamespace(BAZ));
59 public void testNamespaceForPrefix() {
60 assertEquals(Optional.of(FOO), context.findNamespaceForPrefix("foo"));
61 assertEquals(Optional.of(BAR), context.findNamespaceForPrefix("bar"));
62 assertEquals(Optional.empty(), context.findNamespaceForPrefix("baz"));
66 public void testReadWrite() throws IOException {
68 try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
69 try (DataOutputStream dos = new DataOutputStream(bos)) {
72 bytes = bos.toByteArray();
75 final BiMapYangNamespaceContext other;
76 try (DataInputStream dis = new DataInputStream(new ByteArrayInputStream(bytes))) {
77 other = BiMapYangNamespaceContext.readFrom(dis);
80 assertEquals(context, other);
84 public void testCreateQName() {
85 assertEquals(QName.create(FOO, "foo"), context.createQName("foo"));
86 assertEquals(QName.create(FOO, "some"), context.createQName("foo", "some"));