Migrate common/util to JUnit5
[yangtools.git] / common / util / src / test / java / org / opendaylight / yangtools / util / ImmutableMapTemplateTest.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  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.util;
9
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;
13
14 import com.google.common.collect.ImmutableList;
15 import com.google.common.collect.ImmutableMap;
16 import com.google.common.collect.ImmutableSet;
17 import java.util.Set;
18 import org.junit.jupiter.api.Test;
19
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);
26
27     @Test
28     void testEmpty() {
29         assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.ordered(ImmutableList.of()));
30         assertThrows(IllegalArgumentException.class, () -> ImmutableMapTemplate.unordered(ImmutableList.of()));
31     }
32
33     @Test
34     void testOneKeyTemplate() {
35         assertOne(ImmutableMapTemplate.ordered(ONE_KEYSET), SharedSingletonMap.Ordered.class);
36         assertOne(ImmutableMapTemplate.unordered(ONE_KEYSET), SharedSingletonMap.Unordered.class);
37     }
38
39     @Test
40     void testTwoKeyTemplate() {
41         assertTwo(ImmutableMapTemplate.ordered(TWO_KEYSET), ImmutableOffsetMap.Ordered.class);
42         assertTwo(ImmutableMapTemplate.unordered(TWO_KEYSET), ImmutableOffsetMap.Unordered.class);
43     }
44
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());
48
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());
54
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());
59
60         // Null transformation
61         assertThrows(IllegalArgumentException.class,
62             () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null));
63
64         // Empty input
65         assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
66         assertThrows(IllegalArgumentException.class,
67             () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
68
69         // Two-item input
70         assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO, BAR));
71         assertThrows(IllegalArgumentException.class,
72             () -> template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key));
73
74         // Mismatched input
75         assertThrows(IllegalArgumentException.class,
76             () -> template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key));
77     }
78
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());
82
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());
88
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());
93
94         // Empty input
95         assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues());
96         assertThrows(IllegalArgumentException.class,
97             () -> template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key));
98
99         // One-item input
100         assertThrows(IllegalArgumentException.class, () -> template.instantiateWithValues(FOO));
101         assertThrows(IllegalArgumentException.class,
102             () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key));
103
104         // Mismatched input
105         assertThrows(IllegalArgumentException.class,
106             () -> template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key));
107     }
108 }