82da42786da078480ad920c039ce4a9432fce166
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / test / Bug4513Test.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc., Inocybe Technologies 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 static org.junit.Assert.assertEquals;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.timeout;
13 import static org.mockito.Mockito.verify;
14
15 import java.util.Arrays;
16 import java.util.Collection;
17 import org.junit.Test;
18 import org.mockito.ArgumentCaptor;
19 import org.opendaylight.controller.md.sal.binding.api.DataBroker;
20 import org.opendaylight.controller.md.sal.binding.api.DataChangeListener;
21 import org.opendaylight.controller.md.sal.binding.api.DataTreeChangeListener;
22 import org.opendaylight.controller.md.sal.binding.api.DataTreeIdentifier;
23 import org.opendaylight.controller.md.sal.binding.api.DataTreeModification;
24 import org.opendaylight.controller.md.sal.binding.api.WriteTransaction;
25 import org.opendaylight.controller.md.sal.binding.test.AbstractDataBrokerTest;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataBroker;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
28 import org.opendaylight.controller.md.sal.common.api.data.LogicalDatastoreType;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.listener.rev150825.ListenerTest;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.listener.rev150825.ListenerTestBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.listener.rev150825.listener.test.ListItem;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.controller.md.sal.test.listener.rev150825.listener.test.ListItemBuilder;
33 import org.opendaylight.yangtools.concepts.ListenerRegistration;
34 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
35
36 /**
37  * Regression test suite for https://bugs.opendaylight.org/show_bug.cgi?id=4513 - Change event is empty when
38  * Homogeneous composite key is used homogeneous composite key is used.
39  */
40 public class Bug4513Test extends AbstractDataBrokerTest {
41     @SuppressWarnings({ "rawtypes", "unchecked" })
42     @Test
43     public void testDataChangeListener() throws Exception {
44         DataChangeListener listener = mock(DataChangeListener.class);
45         InstanceIdentifier<ListItem> wildCard = InstanceIdentifier.builder(ListenerTest.class)
46                 .child(ListItem.class).build();
47         ListenerRegistration<DataChangeListener> reg = getDataBroker().registerDataChangeListener(
48                 LogicalDatastoreType.OPERATIONAL, wildCard, listener, AsyncDataBroker.DataChangeScope.SUBTREE);
49
50         final ListItem item = writeListItem();
51
52         ArgumentCaptor<AsyncDataChangeEvent> captor = ArgumentCaptor.forClass(AsyncDataChangeEvent.class);
53
54         verify(listener, timeout(100)).onDataChanged(captor.capture());
55
56         AsyncDataChangeEvent event = captor.getValue();
57         assertEquals("createdData", 1, event.getCreatedData().size());
58         assertEquals("ListItem", item, event.getCreatedData().values().iterator().next());
59     }
60
61     @SuppressWarnings({ "rawtypes", "unchecked" })
62     @Test
63     public void testDataTreeChangeListener() throws Exception {
64         DataBroker dataBroker = getDataBroker();
65
66         DataTreeChangeListener<ListItem> listener = mock(DataTreeChangeListener.class);
67         InstanceIdentifier<ListItem> wildCard = InstanceIdentifier.builder(ListenerTest.class)
68                 .child(ListItem.class).build();
69         ListenerRegistration<DataTreeChangeListener<ListItem>> reg = dataBroker.registerDataTreeChangeListener(
70                 new DataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, wildCard), listener);
71
72         final ListItem item = writeListItem();
73
74         ArgumentCaptor<Collection> captor = ArgumentCaptor.forClass(Collection.class);
75
76         verify(listener, timeout(100)).onDataTreeChanged(captor.capture());
77
78         Collection<DataTreeModification<ListItem>> mods = captor.getValue();
79         assertEquals("ListItem", item, mods.iterator().next().getRootNode().getDataAfter());
80     }
81
82     private ListItem writeListItem() {
83         WriteTransaction writeTransaction = getDataBroker().newWriteOnlyTransaction();
84         final ListItem item = new ListItemBuilder().setSip("name").setOp(43L).build();
85         ListenerTestBuilder builder = new ListenerTestBuilder().setListItem(Arrays.asList(item));
86         writeTransaction.put(LogicalDatastoreType.OPERATIONAL, InstanceIdentifier.builder(
87                 ListenerTest.class).build(), builder.build());
88         assertCommit(writeTransaction.submit());
89         return item;
90     }
91 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.