Simplify code using Java 8 features
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / Bug3090MultiKeyList.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.controller.md.sal.binding.impl.test;
9
10 import com.google.common.collect.ImmutableSet;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Objects;
15 import java.util.Set;
16 import java.util.function.Function;
17 import java.util.stream.Collectors;
18 import org.junit.Test;
19 import org.opendaylight.controller.md.sal.binding.api.DataObjectModification.ModificationType;
20 import org.opendaylight.controller.md.sal.binding.api.ReadWriteTransaction;
21 import org.opendaylight.controller.md.sal.binding.test.AbstractDataTreeChangeListenerTest;
22 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
23 import org.opendaylight.mdsal.binding.spec.reflect.BindingReflections;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.opendaylight.test.bug._3090.rev160101.Root;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.opendaylight.test.bug._3090.rev160101.RootBuilder;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.opendaylight.test.bug._3090.rev160101.root.ListInRoot;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.opendaylight.test.bug._3090.rev160101.root.ListInRootBuilder;
28 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.binding.YangModuleInfo;
30
31 public class Bug3090MultiKeyList extends AbstractDataTreeChangeListenerTest {
32     private static final InstanceIdentifier<Root> ROOT_PATH = InstanceIdentifier.create(Root.class);
33
34     @Override
35     protected Iterable<YangModuleInfo> getModuleInfos() throws Exception {
36         return ImmutableSet.of(BindingReflections.getModuleInfo(Root.class));
37     }
38
39     @Test
40     public void listWithMultiKeyTest() {
41         final List<ListInRoot> listInRoots = new ArrayList<>();
42         for (int i = 0; i < 10; i++) {
43             listInRoots.add(new ListInRootBuilder()
44                 .setLeafA("leaf a" + i)
45                 .setLeafC("leaf c" + i)
46                 .setLeafB("leaf b" + i)
47                 .build()
48             );
49         }
50
51         final Root root = new RootBuilder().setListInRoot(listInRoots).build();
52
53         final TestListener<Root> listener = createListener(LogicalDatastoreType.CONFIGURATION, ROOT_PATH,
54                 match(ModificationType.WRITE, ROOT_PATH, Objects::isNull,
55                         (Function<Root, Boolean>) dataAfter -> checkData(root, dataAfter)));
56
57         final ReadWriteTransaction readWriteTransaction = getDataBroker().newReadWriteTransaction();
58         readWriteTransaction.put(LogicalDatastoreType.CONFIGURATION, ROOT_PATH, root);
59         assertCommit(readWriteTransaction.submit());
60
61         listener.verify();
62     }
63
64     private boolean checkData(Root expected, Root actual) {
65         if (actual == null) {
66             return false;
67         }
68
69         Set<ListInRoot> expListInRoot = new HashSet<>(expected.getListInRoot());
70         Set<ListInRoot> actualListInRoot = actual.getListInRoot().stream()
71                 .map(list -> new ListInRootBuilder(list).build()).collect(Collectors.toSet());
72         return expListInRoot.equals(actualListInRoot);
73     }
74 }