1 package org.opendaylight.controller.cluster.datastore;
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.actor.Terminated;
6 import akka.testkit.JavaTestKit;
7 import com.google.common.util.concurrent.ListeningExecutorService;
8 import com.google.common.util.concurrent.MoreExecutors;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
11 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
12 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
13 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
14 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
15 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
16 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
22 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
23 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
24 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.Modification;
26 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
27 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
28 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
33 import java.util.Collections;
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertTrue;
38 public class ShardTransactionTest extends AbstractActorTest {
39 private static ListeningExecutorService storeExecutor =
40 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
42 private static final InMemoryDOMDataStore store =
43 new InMemoryDOMDataStore("OPER", storeExecutor);
45 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
48 store.onGlobalContextUpdated(testSchemaContext);
52 public void testOnReceiveReadData() throws Exception {
53 new JavaTestKit(getSystem()) {{
54 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
56 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
57 final ActorRef subject = getSystem().actorOf(props, "testReadData");
59 new Within(duration("1 seconds")) {
60 protected void run() {
63 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
66 final String out = new ExpectMsg<String>("match hint") {
67 // do not put code outside this method, will run afterwards
68 protected String match(Object in) {
69 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
70 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
71 .getNormalizedNode()!= null) {
79 }.get(); // this extracts the received message
81 assertEquals("match", out);
92 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
93 new JavaTestKit(getSystem()) {{
94 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
96 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
97 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
99 new Within(duration("1 seconds")) {
100 protected void run() {
103 new ReadData(TestModel.TEST_PATH).toSerializable(),
106 final String out = new ExpectMsg<String>("match hint") {
107 // do not put code outside this method, will run afterwards
108 protected String match(Object in) {
109 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
110 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
120 }.get(); // this extracts the received message
122 assertEquals("match", out);
132 private void assertModification(final ActorRef subject,
133 final Class<? extends Modification> modificationType) {
134 new JavaTestKit(getSystem()) {{
135 new Within(duration("1 seconds")) {
136 protected void run() {
138 .tell(new ShardTransaction.GetCompositedModification(),
141 final CompositeModification compositeModification =
142 new ExpectMsg<CompositeModification>("match hint") {
143 // do not put code outside this method, will run afterwards
144 protected CompositeModification match(Object in) {
145 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
146 return ((ShardTransaction.GetCompositeModificationReply) in)
152 }.get(); // this extracts the received message
155 compositeModification.getModifications().size() == 1);
156 assertEquals(modificationType,
157 compositeModification.getModifications().get(0)
166 public void testOnReceiveWriteData() throws Exception {
167 new JavaTestKit(getSystem()) {{
168 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
170 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
171 final ActorRef subject =
172 getSystem().actorOf(props, "testWriteData");
174 new Within(duration("1 seconds")) {
175 protected void run() {
177 subject.tell(new WriteData(TestModel.TEST_PATH,
178 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
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.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
190 }.get(); // this extracts the received message
192 assertEquals("match", out);
194 assertModification(subject, WriteModification.class);
204 public void testOnReceiveMergeData() throws Exception {
205 new JavaTestKit(getSystem()) {{
206 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
208 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
209 final ActorRef subject =
210 getSystem().actorOf(props, "testMergeData");
212 new Within(duration("1 seconds")) {
213 protected void run() {
215 subject.tell(new MergeData(TestModel.TEST_PATH,
216 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
219 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
220 // do not put code outside this method, will run afterwards
221 protected String match(Object in) {
222 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
228 }.get(); // this extracts the received message
230 assertEquals("match", out);
232 assertModification(subject, MergeModification.class);
243 public void testOnReceiveDeleteData() throws Exception {
244 new JavaTestKit(getSystem()) {{
245 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
247 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
248 final ActorRef subject =
249 getSystem().actorOf(props, "testDeleteData");
251 new Within(duration("1 seconds")) {
252 protected void run() {
254 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
256 final String out = new ExpectMsg<String>("match hint") {
257 // do not put code outside this method, will run afterwards
258 protected String match(Object in) {
259 if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
265 }.get(); // this extracts the received message
267 assertEquals("match", out);
269 assertModification(subject, DeleteModification.class);
280 public void testOnReceiveReadyTransaction() throws Exception {
281 new JavaTestKit(getSystem()) {{
282 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
284 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
285 final ActorRef subject =
286 getSystem().actorOf(props, "testReadyTransaction");
288 new Within(duration("1 seconds")) {
289 protected void run() {
291 subject.tell(new ReadyTransaction().toSerializable(), getRef());
293 final String out = new ExpectMsg<String>("match hint") {
294 // do not put code outside this method, will run afterwards
295 protected String match(Object in) {
296 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
302 }.get(); // this extracts the received message
304 assertEquals("match", out);
316 public void testOnReceiveCloseTransaction() throws Exception {
317 new JavaTestKit(getSystem()) {{
318 final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
320 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
321 final ActorRef subject =
322 getSystem().actorOf(props, "testCloseTransaction");
326 new Within(duration("2 seconds")) {
327 protected void run() {
329 subject.tell(new CloseTransaction().toSerializable(), getRef());
331 final String out = new ExpectMsg<String>("match hint") {
332 // do not put code outside this method, will run afterwards
333 protected String match(Object in) {
334 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
340 }.get(); // this extracts the received message
342 assertEquals("match", out);
344 final String termination = new ExpectMsg<String>("match hint") {
345 // do not put code outside this method, will run afterwards
346 protected String match(Object in) {
347 if (in instanceof Terminated) {
353 }.get(); // this extracts the received message