Merge "Bug-835:Reserve ports should be logical ports"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
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;
8 import org.junit.Test;
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.md.cluster.datastore.model.TestModel;
22 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
23 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
25
26 import static org.junit.Assert.assertEquals;
27
28 public class ShardTransactionTest extends AbstractActorTest {
29   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
30
31   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
32
33   static {
34     store.onGlobalContextUpdated(TestModel.createTestContext());
35   }
36
37   @Test
38   public void testOnReceiveReadData() throws Exception {
39     new JavaTestKit(getSystem()) {{
40       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
41       final ActorRef subject = getSystem().actorOf(props, "testReadData");
42
43       new Within(duration("1 seconds")) {
44         protected void run() {
45
46           subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
47
48           final String out = new ExpectMsg<String>("match hint") {
49             // do not put code outside this method, will run afterwards
50             protected String match(Object in) {
51               if (in instanceof ReadDataReply) {
52                 if (((ReadDataReply) in).getNormalizedNode() != null) {
53                   return "match";
54                 }
55                 return null;
56               } else {
57                 throw noMatch();
58               }
59             }
60           }.get(); // this extracts the received message
61
62           assertEquals("match", out);
63
64           expectNoMsg();
65         }
66
67
68       };
69     }};
70   }
71
72   @Test
73   public void testOnReceiveWriteData() throws Exception {
74     new JavaTestKit(getSystem()) {{
75       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
76       final ActorRef subject = getSystem().actorOf(props, "testWriteData");
77
78       new Within(duration("1 seconds")) {
79         protected void run() {
80
81           subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
82
83           final String out = new ExpectMsg<String>("match hint") {
84             // do not put code outside this method, will run afterwards
85             protected String match(Object in) {
86               if (in instanceof WriteDataReply) {
87                 return "match";
88               } else {
89                 throw noMatch();
90               }
91             }
92           }.get(); // this extracts the received message
93
94           assertEquals("match", out);
95
96           expectNoMsg();
97         }
98
99
100       };
101     }};
102   }
103
104   @Test
105   public void testOnReceiveMergeData() throws Exception {
106     new JavaTestKit(getSystem()) {{
107       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
108       final ActorRef subject = getSystem().actorOf(props, "testMergeData");
109
110       new Within(duration("1 seconds")) {
111         protected void run() {
112
113           subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
114
115           final String out = new ExpectMsg<String>("match hint") {
116             // do not put code outside this method, will run afterwards
117             protected String match(Object in) {
118               if (in instanceof MergeDataReply) {
119                 return "match";
120               } else {
121                 throw noMatch();
122               }
123             }
124           }.get(); // this extracts the received message
125
126           assertEquals("match", out);
127
128           expectNoMsg();
129         }
130
131
132       };
133     }};
134   }
135
136   @Test
137   public void testOnReceiveDeleteData() throws Exception {
138     new JavaTestKit(getSystem()) {{
139       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
140       final ActorRef subject = getSystem().actorOf(props, "testDeleteData");
141
142       new Within(duration("1 seconds")) {
143         protected void run() {
144
145           subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
146
147           final String out = new ExpectMsg<String>("match hint") {
148             // do not put code outside this method, will run afterwards
149             protected String match(Object in) {
150               if (in instanceof DeleteDataReply) {
151                 return "match";
152               } else {
153                 throw noMatch();
154               }
155             }
156           }.get(); // this extracts the received message
157
158           assertEquals("match", out);
159
160           expectNoMsg();
161         }
162
163
164       };
165     }};
166   }
167
168
169   @Test
170   public void testOnReceiveReadyTransaction() throws Exception {
171     new JavaTestKit(getSystem()) {{
172       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
173       final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");
174
175       new Within(duration("1 seconds")) {
176         protected void run() {
177
178           subject.tell(new ReadyTransaction(), getRef());
179
180           final String out = new ExpectMsg<String>("match hint") {
181             // do not put code outside this method, will run afterwards
182             protected String match(Object in) {
183               if (in instanceof ReadyTransactionReply) {
184                 return "match";
185               } else {
186                 throw noMatch();
187               }
188             }
189           }.get(); // this extracts the received message
190
191           assertEquals("match", out);
192
193           expectNoMsg();
194         }
195
196
197       };
198     }};
199
200   }
201
202   @Test
203   public void testOnReceiveCloseTransaction() throws Exception {
204     new JavaTestKit(getSystem()) {{
205       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
206       final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");
207
208       new Within(duration("1 seconds")) {
209         protected void run() {
210
211           subject.tell(new CloseTransaction(), getRef());
212
213           final String out = new ExpectMsg<String>("match hint") {
214             // do not put code outside this method, will run afterwards
215             protected String match(Object in) {
216               if (in instanceof CloseTransactionReply) {
217                 return "match";
218               } else {
219                 throw noMatch();
220               }
221             }
222           }.get(); // this extracts the received message
223
224           assertEquals("match", out);
225
226           expectNoMsg();
227         }
228
229
230       };
231     }};
232
233   }
234
235
236 }