2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. 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.util;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
19 import org.junit.Test;
21 public class ImmutableMapTemplateTest {
22 private static final String FOO = "foo";
23 private static final String BAR = "bar";
24 private static final String BAZ = "baz";
25 private static final Set<String> ONE_KEYSET = ImmutableSet.of(FOO);
26 private static final Set<String> TWO_KEYSET = ImmutableSet.of(FOO, BAR);
29 public void testEmpty() {
30 ImmutableMapTemplate<?> template;
32 template = ImmutableMapTemplate.ordered(ImmutableList.of());
33 fail("Returned template " + template);
34 } catch (IllegalArgumentException e) {
39 template = ImmutableMapTemplate.unordered(ImmutableList.of());
40 fail("Returned template " + template);
41 } catch (IllegalArgumentException e) {
47 public void testOneKeyTemplate() {
48 assertOne(ImmutableMapTemplate.ordered(ONE_KEYSET), SharedSingletonMap.Ordered.class);
49 assertOne(ImmutableMapTemplate.unordered(ONE_KEYSET), SharedSingletonMap.Unordered.class);
53 public void testTwoKeyTemplate() {
54 assertTwo(ImmutableMapTemplate.ordered(TWO_KEYSET), ImmutableOffsetMap.Ordered.class);
55 assertTwo(ImmutableMapTemplate.unordered(TWO_KEYSET), ImmutableOffsetMap.Unordered.class);
58 private static void assertOne(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
59 assertEquals(ONE_KEYSET, template.keySet());
60 assertEquals("Single" + mapClass.getSimpleName() + "{keySet=[foo]}", template.toString());
62 // Successful instantiation
63 Map<String, String> map = template.instantiateWithValues(BAR);
64 assertTrue(mapClass.isInstance(map));
65 assertEquals(ImmutableMap.of(FOO, BAR), map);
66 assertEquals("{foo=bar}", map.toString());
68 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
69 assertTrue(mapClass.isInstance(map));
70 assertEquals(ImmutableMap.of(FOO, FOO), map);
71 assertEquals("{foo=foo}", map.toString());
73 // Null transformation
75 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null);
76 fail("Returned map " + map);
77 } catch (IllegalArgumentException e) {
83 map = template.instantiateWithValues();
84 fail("Returned map " + map);
85 } catch (IllegalArgumentException e) {
89 map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
90 fail("Returned map " + map);
91 } catch (IllegalArgumentException e) {
97 map = template.instantiateWithValues(FOO, BAR);
98 fail("Returned map " + map);
99 } catch (IllegalArgumentException e) {
103 map = template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key);
104 fail("Returned map " + map);
105 } catch (IllegalArgumentException e) {
111 map = template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key);
112 fail("Returned map " + map);
113 } catch (IllegalArgumentException e) {
118 private static void assertTwo(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
119 assertEquals(TWO_KEYSET, template.keySet());
120 assertEquals(mapClass.getSimpleName() + "{offsets={foo=0, bar=1}}", template.toString());
122 // Successful instantiation
123 Map<String, String> map = template.instantiateWithValues(BAR, FOO);
124 assertTrue(mapClass.isInstance(map));
125 assertEquals(ImmutableMap.of(FOO, BAR, BAR, FOO), map);
126 assertEquals("{foo=bar, bar=foo}", map.toString());
128 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAR, FOO), (key, value) -> key);
129 assertTrue(mapClass.isInstance(map));
130 assertEquals(ImmutableMap.of(FOO, FOO, BAR, BAR), map);
131 assertEquals("{foo=foo, bar=bar}", map.toString());
135 map = template.instantiateWithValues();
136 fail("Returned map " + map);
137 } catch (IllegalArgumentException e) {
141 map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
142 fail("Returned map " + map);
143 } catch (IllegalArgumentException e) {
149 map = template.instantiateWithValues(FOO);
150 fail("Returned map " + map);
151 } catch (IllegalArgumentException e) {
155 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
156 fail("Returned map " + map);
157 } catch (IllegalArgumentException e) {
163 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key);
164 fail("Returned map " + map);
165 } catch (IllegalArgumentException e) {