Use YangInstanceIdentifier.EMPTY
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / DataChangeListenerProxyTest.java
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorRef;
12 import akka.actor.Props;
13 import java.util.HashMap;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18 import org.junit.Assert;
19 import org.junit.Test;
20 import org.opendaylight.controller.cluster.datastore.messages.DataChanged;
21 import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
22 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
23 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
24 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
25 import org.opendaylight.controller.cluster.raft.utils.MessageCollectorActor;
26 import org.opendaylight.controller.md.cluster.datastore.model.CompositeModel;
27 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 public class DataChangeListenerProxyTest extends AbstractActorTest {
31
32   private static class MockDataChangedEvent implements AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> {
33     Map<YangInstanceIdentifier,NormalizedNode<?,?>> createdData = new HashMap<>();
34     Map<YangInstanceIdentifier,NormalizedNode<?,?>> updatedData = new HashMap<>();
35     Map<YangInstanceIdentifier,NormalizedNode<?,?>> originalData = new HashMap<>();
36
37
38
39     @Override
40     public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
41       createdData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createDocumentOne(CompositeModel.createTestContext()));
42       return createdData;
43     }
44
45     @Override
46     public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
47       updatedData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createTestContainer());
48       return updatedData;
49
50     }
51
52     @Override
53     public Set<YangInstanceIdentifier> getRemovedPaths() {
54       Set<YangInstanceIdentifier>ids = new HashSet<>();
55       ids.add( CompositeModel.TEST_PATH);
56       return ids;
57     }
58
59     @Override
60     public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getOriginalData() {
61       originalData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createFamily());
62       return originalData;
63     }
64
65     @Override public NormalizedNode<?, ?> getOriginalSubtree() {
66       return CompositeModel.createFamily() ;
67     }
68
69     @Override public NormalizedNode<?, ?> getUpdatedSubtree() {
70       return CompositeModel.createTestContainer();
71     }
72   }
73
74
75   @Test
76     public void testOnDataChanged() throws Exception {
77         final Props props = Props.create(MessageCollectorActor.class);
78         final ActorRef actorRef = getSystem().actorOf(props);
79
80         DataChangeListenerProxy dataChangeListenerProxy = new DataChangeListenerProxy(
81                 getSystem().actorSelection(actorRef.path()));
82
83         dataChangeListenerProxy.onDataChanged(new MockDataChangedEvent());
84
85         //Check if it was received by the remote actor
86         ActorContext
87             testContext = new ActorContext(getSystem(), getSystem().actorOf(Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
88         Object messages = testContext
89             .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
90
91         Assert.assertNotNull(messages);
92
93         Assert.assertTrue(messages instanceof List);
94
95         List<?> listMessages = (List<?>) messages;
96
97         Assert.assertEquals(1, listMessages.size());
98
99         Assert.assertTrue(listMessages.get(0).getClass().equals(DataChanged.class));
100
101     }
102 }

©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.