2 * Copyright (c) 2020 PANTHEON.tech, 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.mdsal.binding.dom.codec.impl;
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;
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;
27 public class LazyBindingListTest extends AbstractBindingCodecTest {
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());
34 final TopLevelList expected = new TopLevelListBuilder()
36 .setNestedList(nested)
38 final TopLevelList actual = thereAndBackAgain(
39 InstanceIdentifier.create(Top.class).child(TopLevelList.class, expected.key()), expected);
41 final List<NestedList> list = actual.getNestedList();
42 assertThat(list, instanceOf(LazyBindingList.class));
44 // Equality does all the right things to check happy paths
45 assertEquals(expected.getNestedList(), list);
46 assertEquals(expected.getNestedList().hashCode(), list.hashCode());
48 // Make sure the list performs proper caching
49 assertSame(list.get(LazyBindingList.LAZY_CUTOFF), list.get(LazyBindingList.LAZY_CUTOFF));
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());
64 public void testSingletonList() {
65 final TopLevelList expected = new TopLevelListBuilder()
67 .setNestedList(List.of(new NestedListBuilder().setName(String.valueOf("one")).build()))
69 final TopLevelList actual = thereAndBackAgain(
70 InstanceIdentifier.create(Top.class).child(TopLevelList.class, expected.key()), expected);
72 final List<NestedList> list = actual.getNestedList();
73 assertThat(list, not(instanceOf(LazyBindingList.class)));
74 assertEquals(expected.getNestedList(), list);