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.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;
26 import static org.junit.Assert.assertEquals;
28 public class ShardTransactionTest extends AbstractActorTest {
29 private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
31 private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
34 store.onGlobalContextUpdated(TestModel.createTestContext());
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");
43 new Within(duration("1 seconds")) {
44 protected void run() {
46 subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
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) {
60 }.get(); // this extracts the received message
62 assertEquals("match", out);
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");
78 new Within(duration("1 seconds")) {
79 protected void run() {
81 subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
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) {
92 }.get(); // this extracts the received message
94 assertEquals("match", out);
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");
110 new Within(duration("1 seconds")) {
111 protected void run() {
113 subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
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) {
124 }.get(); // this extracts the received message
126 assertEquals("match", out);
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");
142 new Within(duration("1 seconds")) {
143 protected void run() {
145 subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
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) {
156 }.get(); // this extracts the received message
158 assertEquals("match", out);
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");
175 new Within(duration("1 seconds")) {
176 protected void run() {
178 subject.tell(new ReadyTransaction(), getRef());
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) {
189 }.get(); // this extracts the received message
191 assertEquals("match", out);
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");
208 new Within(duration("1 seconds")) {
209 protected void run() {
211 subject.tell(new CloseTransaction(), getRef());
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) {
222 }.get(); // this extracts the received message
224 assertEquals("match", out);