Factor out {SharedSingleton,ImmutableOffset}MapTemplate
[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.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import static org.junit.Assert.fail;
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.Map;
18 import java.util.Set;
19 import org.junit.Test;
20
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);
27
28     @Test
29     public void testEmpty() {
30         ImmutableMapTemplate<?> template;
31         try {
32             template = ImmutableMapTemplate.ordered(ImmutableList.of());
33             fail("Returned template " + template);
34         } catch (IllegalArgumentException e) {
35             // expected
36         }
37
38         try {
39             template = ImmutableMapTemplate.unordered(ImmutableList.of());
40             fail("Returned template " + template);
41         } catch (IllegalArgumentException e) {
42             // expected
43         }
44     }
45
46     @Test
47     public void testOneKeyTemplate() {
48         assertOne(ImmutableMapTemplate.ordered(ONE_KEYSET), SharedSingletonMap.Ordered.class);
49         assertOne(ImmutableMapTemplate.unordered(ONE_KEYSET), SharedSingletonMap.Unordered.class);
50     }
51
52     @Test
53     public void testTwoKeyTemplate() {
54         assertTwo(ImmutableMapTemplate.ordered(TWO_KEYSET), ImmutableOffsetMap.Ordered.class);
55         assertTwo(ImmutableMapTemplate.unordered(TWO_KEYSET), ImmutableOffsetMap.Unordered.class);
56     }
57
58     private static void assertOne(final ImmutableMapTemplate<String> template, final Class<?> mapClass) {
59         assertEquals(ONE_KEYSET, template.keySet());
60         assertEquals(mapClass.getSimpleName() + "{keySet=[foo]}", template.toString());
61
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());
67
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());
72
73         // Null transformation
74         try {
75             map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> null);
76             fail("Returned map " + map);
77         } catch (IllegalArgumentException e) {
78             // expected
79         }
80
81         // Empty input
82         try {
83             map = template.instantiateWithValues();
84             fail("Returned map " + map);
85         } catch (IllegalArgumentException e) {
86             // expected
87         }
88         try {
89             map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
90             fail("Returned map " + map);
91         } catch (IllegalArgumentException e) {
92             // expected
93         }
94
95         // Two-item input
96         try {
97             map = template.instantiateWithValues(FOO, BAR);
98             fail("Returned map " + map);
99         } catch (IllegalArgumentException e) {
100             // expected
101         }
102         try {
103             map = template.instantiateTransformed(ImmutableMap.of(FOO, FOO, BAR, BAR), (key, value) -> key);
104             fail("Returned map " + map);
105         } catch (IllegalArgumentException e) {
106             // expected
107         }
108
109         // Mismatched input
110         try {
111             map = template.instantiateTransformed(ImmutableMap.of(BAR, FOO), (key, value) -> key);
112             fail("Returned map " + map);
113         } catch (IllegalArgumentException e) {
114             // expected
115         }
116     }
117
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());
121
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());
127
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());
132
133         // Empty input
134         try {
135             map = template.instantiateWithValues();
136             fail("Returned map " + map);
137         } catch (IllegalArgumentException e) {
138             // expected
139         }
140         try {
141             map = template.instantiateTransformed(ImmutableMap.of(), (key, value) -> key);
142             fail("Returned map " + map);
143         } catch (IllegalArgumentException e) {
144             // expected
145         }
146
147         // One-item input
148         try {
149             map = template.instantiateWithValues(FOO);
150             fail("Returned map " + map);
151         } catch (IllegalArgumentException e) {
152             // expected
153         }
154         try {
155             map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR), (key, value) -> key);
156             fail("Returned map " + map);
157         } catch (IllegalArgumentException e) {
158             // expected
159         }
160
161         // Mismatched input
162         try {
163             map = template.instantiateTransformed(ImmutableMap.of(FOO, BAR, BAZ, FOO), (key, value) -> key);
164             fail("Returned map " + map);
165         } catch (IllegalArgumentException e) {
166             // expected
167         }
168     }
169 }