BUG-8618: eliminate SimpleShardDataTreeCohort subclasses
[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
31 public class DataChangeListenerProxyTest extends AbstractActorTest {
32
33     private static class MockDataChangedEvent
34             implements AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> {
35         Map<YangInstanceIdentifier,NormalizedNode<?,?>> createdData = new HashMap<>();
36         Map<YangInstanceIdentifier,NormalizedNode<?,?>> updatedData = new HashMap<>();
37         Map<YangInstanceIdentifier,NormalizedNode<?,?>> originalData = new HashMap<>();
38
39         @Override
40         public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getCreatedData() {
41             createdData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createDocumentOne(
42                     CompositeModel.createTestContext()));
43             return createdData;
44         }
45
46         @Override
47         public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getUpdatedData() {
48             updatedData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createTestContainer());
49             return updatedData;
50
51         }
52
53         @Override
54         public Set<YangInstanceIdentifier> getRemovedPaths() {
55             Set<YangInstanceIdentifier> ids = new HashSet<>();
56             ids.add(CompositeModel.TEST_PATH);
57             return ids;
58         }
59
60         @Override
61         public Map<YangInstanceIdentifier, NormalizedNode<?, ?>> getOriginalData() {
62             originalData.put(YangInstanceIdentifier.EMPTY, CompositeModel.createFamily());
63             return originalData;
64         }
65
66         @Override public NormalizedNode<?, ?> getOriginalSubtree() {
67             return CompositeModel.createFamily() ;
68         }
69
70         @Override public NormalizedNode<?, ?> getUpdatedSubtree() {
71             return CompositeModel.createTestContainer();
72         }
73     }
74
75
76     @Test
77     public void testOnDataChanged() throws Exception {
78         final Props props = Props.create(MessageCollectorActor.class);
79         final ActorRef actorRef = getSystem().actorOf(props);
80
81         DataChangeListenerProxy dataChangeListenerProxy = new DataChangeListenerProxy(
82                 getSystem().actorSelection(actorRef.path()));
83
84         dataChangeListenerProxy.onDataChanged(new MockDataChangedEvent());
85
86         //Check if it was received by the remote actor
87         ActorContext testContext = new ActorContext(getSystem(), getSystem().actorOf(
88                 Props.create(DoNothingActor.class)), new MockClusterWrapper(), new MockConfiguration());
89         Object messages = testContext
90             .executeOperation(actorRef, MessageCollectorActor.GET_ALL_MESSAGES);
91
92         Assert.assertNotNull(messages);
93
94         Assert.assertTrue(messages instanceof List);
95
96         List<?> listMessages = (List<?>) messages;
97
98         Assert.assertEquals(1, listMessages.size());
99
100         Assert.assertTrue(listMessages.get(0).getClass().equals(DataChanged.class));
101
102     }
103 }