1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.testkit.JavaTestKit;
6 import com.google.common.util.concurrent.ListeningExecutorService;
7 import com.google.common.util.concurrent.MoreExecutors;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
11 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
12 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
13 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
14 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
15 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
16 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
20 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
21 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
22 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
23 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.Modification;
25 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
26 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
27 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
34 public class ShardTransactionTest extends AbstractActorTest {
35 private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
37 private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
40 store.onGlobalContextUpdated(TestModel.createTestContext());
44 public void testOnReceiveReadData() throws Exception {
45 new JavaTestKit(getSystem()) {{
46 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
47 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
48 final ActorRef subject = getSystem().actorOf(props, "testReadData");
50 new Within(duration("1 seconds")) {
51 protected void run() {
53 subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
55 final String out = new ExpectMsg<String>("match hint") {
56 // do not put code outside this method, will run afterwards
57 protected String match(Object in) {
58 if (in instanceof ReadDataReply) {
59 if (((ReadDataReply) in).getNormalizedNode() != null) {
67 }.get(); // this extracts the received message
69 assertEquals("match", out);
79 private void assertModification(final ActorRef subject, final Class<? extends Modification> modificationType){
80 new JavaTestKit(getSystem()) {{
81 new Within(duration("1 seconds")) {
82 protected void run() {
83 subject.tell(new ShardTransaction.GetCompositedModification(), getRef());
85 final CompositeModification compositeModification = new ExpectMsg<CompositeModification>("match hint") {
86 // do not put code outside this method, will run afterwards
87 protected CompositeModification match(Object in) {
88 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
89 return ((ShardTransaction.GetCompositeModificationReply) in).getModification();
94 }.get(); // this extracts the received message
96 assertTrue(compositeModification.getModifications().size() == 1);
97 assertEquals(modificationType, compositeModification.getModifications().get(0).getClass());
105 public void testOnReceiveWriteData() throws Exception {
106 new JavaTestKit(getSystem()) {{
107 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
108 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
109 final ActorRef subject = getSystem().actorOf(props, "testWriteData");
111 new Within(duration("1 seconds")) {
112 protected void run() {
114 subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
116 final String out = new ExpectMsg<String>("match hint") {
117 // do not put code outside this method, will run afterwards
118 protected String match(Object in) {
119 if (in instanceof WriteDataReply) {
125 }.get(); // this extracts the received message
127 assertEquals("match", out);
129 assertModification(subject, WriteModification.class);
139 public void testOnReceiveMergeData() throws Exception {
140 new JavaTestKit(getSystem()) {{
141 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
142 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
143 final ActorRef subject = getSystem().actorOf(props, "testMergeData");
145 new Within(duration("1 seconds")) {
146 protected void run() {
148 subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
150 final String out = new ExpectMsg<String>("match hint") {
151 // do not put code outside this method, will run afterwards
152 protected String match(Object in) {
153 if (in instanceof MergeDataReply) {
159 }.get(); // this extracts the received message
161 assertEquals("match", out);
163 assertModification(subject, MergeModification.class);
174 public void testOnReceiveDeleteData() throws Exception {
175 new JavaTestKit(getSystem()) {{
176 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
177 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
178 final ActorRef subject = getSystem().actorOf(props, "testDeleteData");
180 new Within(duration("1 seconds")) {
181 protected void run() {
183 subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
185 final String out = new ExpectMsg<String>("match hint") {
186 // do not put code outside this method, will run afterwards
187 protected String match(Object in) {
188 if (in instanceof DeleteDataReply) {
194 }.get(); // this extracts the received message
196 assertEquals("match", out);
198 assertModification(subject, DeleteModification.class);
209 public void testOnReceiveReadyTransaction() throws Exception {
210 new JavaTestKit(getSystem()) {{
211 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
212 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
213 final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");
215 new Within(duration("1 seconds")) {
216 protected void run() {
218 subject.tell(new ReadyTransaction(), getRef());
220 final String out = new ExpectMsg<String>("match hint") {
221 // do not put code outside this method, will run afterwards
222 protected String match(Object in) {
223 if (in instanceof ReadyTransactionReply) {
229 }.get(); // this extracts the received message
231 assertEquals("match", out);
243 public void testOnReceiveCloseTransaction() throws Exception {
244 new JavaTestKit(getSystem()) {{
245 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
246 final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
247 final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");
249 new Within(duration("1 seconds")) {
250 protected void run() {
252 subject.tell(new CloseTransaction(), getRef());
254 final String out = new ExpectMsg<String>("match hint") {
255 // do not put code outside this method, will run afterwards
256 protected String match(Object in) {
257 if (in instanceof CloseTransactionReply) {
263 }.get(); // this extracts the received message
265 assertEquals("match", out);