Add lazily-instantiated lists
[mdsal.git] / binding / mdsal-binding-dom-codec / src / test / java / org / opendaylight / mdsal / binding / dom / codec / impl / LazyBindingListTest.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, 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.mdsal.binding.dom.codec.impl;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.CoreMatchers.not;
12 import static org.hamcrest.MatcherAssert.assertThat;
13 import static org.junit.Assert.assertEquals;
14 import static org.junit.Assert.assertSame;
15 import static org.junit.Assert.assertThrows;
16
17 import java.util.ArrayList;
18 import java.util.List;
19 import org.junit.Test;
20 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.Top;
21 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelList;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.TopLevelListBuilder;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedList;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.mdsal.test.binding.rev140701.two.level.list.top.level.list.NestedListBuilder;
25 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
26
27 public class LazyBindingListTest extends AbstractBindingCodecTest {
28     @Test
29     public void testLazyList() {
30         final List<NestedList> nested = new ArrayList<>();
31         for (int i = 0; i < 2 * LazyBindingList.LAZY_CUTOFF; ++i) {
32             nested.add(new NestedListBuilder().setName(String.valueOf(i)).build());
33         }
34         final TopLevelList expected = new TopLevelListBuilder()
35                 .setName("test")
36                 .setNestedList(nested)
37                 .build();
38         final TopLevelList actual = thereAndBackAgain(
39             InstanceIdentifier.create(Top.class).child(TopLevelList.class, expected.key()), expected);
40
41         final List<NestedList> list = actual.getNestedList();
42         assertThat(list, instanceOf(LazyBindingList.class));
43
44         // Equality does all the right things to check happy paths
45         assertEquals(expected.getNestedList(), list);
46         assertEquals(expected.getNestedList().hashCode(), list.hashCode());
47
48         // Make sure the list performs proper caching
49         assertSame(list.get(LazyBindingList.LAZY_CUTOFF), list.get(LazyBindingList.LAZY_CUTOFF));
50
51         // Test throws, just for completeness' sake
52         assertThrows(UnsupportedOperationException.class, () -> list.add(null));
53         assertThrows(UnsupportedOperationException.class, () -> list.addAll(null));
54         assertThrows(UnsupportedOperationException.class, () -> list.addAll(0, null));
55         assertThrows(UnsupportedOperationException.class, () -> list.remove(null));
56         assertThrows(UnsupportedOperationException.class, () -> list.removeAll(null));
57         assertThrows(UnsupportedOperationException.class, () -> list.replaceAll(null));
58         assertThrows(UnsupportedOperationException.class, () -> list.retainAll(null));
59         assertThrows(UnsupportedOperationException.class, () -> list.sort(null));
60         assertThrows(UnsupportedOperationException.class, () -> list.clear());
61     }
62
63     @Test
64     public void testSingletonList() {
65         final TopLevelList expected = new TopLevelListBuilder()
66                 .setName("test")
67                 .setNestedList(List.of(new NestedListBuilder().setName(String.valueOf("one")).build()))
68                 .build();
69         final TopLevelList actual = thereAndBackAgain(
70             InstanceIdentifier.create(Top.class).child(TopLevelList.class, expected.key()), expected);
71
72         final List<NestedList> list = actual.getNestedList();
73         assertThat(list, not(instanceOf(LazyBindingList.class)));
74         assertEquals(expected.getNestedList(), list);
75     }
76 }