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.jupiter.api.Assertions.assertEquals;
11 import static org.junit.jupiter.api.Assertions.assertThrows;
12 import static org.junit.jupiter.api.Assertions.assertTrue;
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
18 import org.junit.jupiter.api.Test;
20 class ImmutableMapTemplateTest {
21 private static final String FOO = "foo";
22 private static final String BAR = "bar";
23 private static final String BAZ = "baz";
24 private static final Set<String> ONE_KEYSET = ImmutableSet.of(FOO);
25 private static final Set<String> TWO_KEYSET = ImmutableSet.of(FOO, BAR);
29 assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.ordered(ImmutableList.of()));
30 assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.unordered(ImmutableList.of()));
34 void testOneKeyTemplate() {
35 assertOne(ImmutableMapTemplate.ordered(ONE_KEYSET), SharedSingletonMap.Ordered.class);
36 assertOne(ImmutableMapTemplate.unordered(ONE_KEYSET), SharedSingletonMap.Unordered.class);
40 void testTwoKeyTemplate() {
41 assertTwo(ImmutableMapTemplate.ordered(TWO_KEYSET), ImmutableOffsetMap.Ordered.class);
42 assertTwo(ImmutableMapTemplate.unordered(TWO_KEYSET), ImmutableOffsetMap.Unordered.class);
45 private static void assertOne(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
46 assertEquals(ONE_KEYSET, template.keySet());
47 assertEquals(mapClass.getSimpleName() + "{keySet=[foo]}", template.toString());
49 // Successful instantiation
50 var map = template.instantiateWithValues(BAR);
51 assertTrue(mapClass.isInstance(map));
52 assertEquals(ImmutableMap.of(FOO, BAR), map);
53 assertEquals("{foo=bar}", map.toString());
55 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
56 assertTrue(mapClass.isInstance(map));
57 assertEquals(ImmutableMap.of(FOO, FOO), map);
58 assertEquals("{foo=foo}", map.toString());
60 // Null transformation
61 assertThrows(IllegalArgumentException.class,
62 () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null));
65 assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
66 assertThrows(IllegalArgumentException.class,
67 () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
70 assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO, BAR));
71 assertThrows(IllegalArgumentException.class,
72 () -> template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key));
75 assertThrows(IllegalArgumentException.class,
76 () -> template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key));
79 private static void assertTwo(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
80 assertEquals(TWO_KEYSET, template.keySet());
81 assertEquals(mapClass.getSimpleName() + "{offsets={foo=0, bar=1}}", template.toString());
83 // Successful instantiation
84 var map = template.instantiateWithValues(BAR, FOO);
85 assertTrue(mapClass.isInstance(map));
86 assertEquals(ImmutableMap.of(FOO, BAR, BAR, FOO), map);
87 assertEquals("{foo=bar, bar=foo}", map.toString());
89 map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAR, FOO), (key, value) -> key);
90 assertTrue(mapClass.isInstance(map));
91 assertEquals(ImmutableMap.of(FOO, FOO, BAR, BAR), map);
92 assertEquals("{foo=foo, bar=bar}", map.toString());
95 assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
96 assertThrows(IllegalArgumentException.class,
97 () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
100 assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO));
101 assertThrows(IllegalArgumentException.class,
102 () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key));
105 assertThrows(IllegalArgumentException.class,
106 () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key));