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 Props props = ShardTransaction.props(store.newReadWriteTransaction());
47 final ActorRef subject = getSystem().actorOf(props, "testReadData");
49 new Within(duration("1 seconds")) {
50 protected void run() {
52 subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
54 final String out = new ExpectMsg<String>("match hint") {
55 // do not put code outside this method, will run afterwards
56 protected String match(Object in) {
57 if (in instanceof ReadDataReply) {
58 if (((ReadDataReply) in).getNormalizedNode() != null) {
66 }.get(); // this extracts the received message
68 assertEquals("match", out);
78 private void assertModification(final ActorRef subject, final Class<? extends Modification> modificationType){
79 new JavaTestKit(getSystem()) {{
80 new Within(duration("1 seconds")) {
81 protected void run() {
82 subject.tell(new ShardTransaction.GetCompositedModification(), getRef());
84 final CompositeModification compositeModification = new ExpectMsg<CompositeModification>("match hint") {
85 // do not put code outside this method, will run afterwards
86 protected CompositeModification match(Object in) {
87 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
88 return ((ShardTransaction.GetCompositeModificationReply) in).getModification();
93 }.get(); // this extracts the received message
95 assertTrue(compositeModification.getModifications().size() == 1);
96 assertEquals(modificationType, compositeModification.getModifications().get(0).getClass());
104 public void testOnReceiveWriteData() throws Exception {
105 new JavaTestKit(getSystem()) {{
106 final Props props = ShardTransaction.props(store.newReadWriteTransaction());
107 final ActorRef subject = getSystem().actorOf(props, "testWriteData");
109 new Within(duration("1 seconds")) {
110 protected void run() {
112 subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
114 final String out = new ExpectMsg<String>("match hint") {
115 // do not put code outside this method, will run afterwards
116 protected String match(Object in) {
117 if (in instanceof WriteDataReply) {
123 }.get(); // this extracts the received message
125 assertEquals("match", out);
127 assertModification(subject, WriteModification.class);
137 public void testOnReceiveMergeData() throws Exception {
138 new JavaTestKit(getSystem()) {{
139 final Props props = ShardTransaction.props(store.newReadWriteTransaction());
140 final ActorRef subject = getSystem().actorOf(props, "testMergeData");
142 new Within(duration("1 seconds")) {
143 protected void run() {
145 subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), 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 MergeDataReply) {
156 }.get(); // this extracts the received message
158 assertEquals("match", out);
160 assertModification(subject, MergeModification.class);
171 public void testOnReceiveDeleteData() throws Exception {
172 new JavaTestKit(getSystem()) {{
173 final Props props = ShardTransaction.props(store.newReadWriteTransaction());
174 final ActorRef subject = getSystem().actorOf(props, "testDeleteData");
176 new Within(duration("1 seconds")) {
177 protected void run() {
179 subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
181 final String out = new ExpectMsg<String>("match hint") {
182 // do not put code outside this method, will run afterwards
183 protected String match(Object in) {
184 if (in instanceof DeleteDataReply) {
190 }.get(); // this extracts the received message
192 assertEquals("match", out);
194 assertModification(subject, DeleteModification.class);
205 public void testOnReceiveReadyTransaction() throws Exception {
206 new JavaTestKit(getSystem()) {{
207 final Props props = ShardTransaction.props(store.newReadWriteTransaction());
208 final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");
210 new Within(duration("1 seconds")) {
211 protected void run() {
213 subject.tell(new ReadyTransaction(), getRef());
215 final String out = new ExpectMsg<String>("match hint") {
216 // do not put code outside this method, will run afterwards
217 protected String match(Object in) {
218 if (in instanceof ReadyTransactionReply) {
224 }.get(); // this extracts the received message
226 assertEquals("match", out);
238 public void testOnReceiveCloseTransaction() throws Exception {
239 new JavaTestKit(getSystem()) {{
240 final Props props = ShardTransaction.props(store.newReadWriteTransaction());
241 final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");
243 new Within(duration("1 seconds")) {
244 protected void run() {
246 subject.tell(new CloseTransaction(), getRef());
248 final String out = new ExpectMsg<String>("match hint") {
249 // do not put code outside this method, will run afterwards
250 protected String match(Object in) {
251 if (in instanceof CloseTransactionReply) {
257 }.get(); // this extracts the received message
259 assertEquals("match", out);