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 static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
36 public class ShardTransactionTest extends AbstractActorTest {
37 private static ListeningExecutorService storeExecutor =
38 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
40 private static final InMemoryDOMDataStore store =
41 new InMemoryDOMDataStore("OPER", storeExecutor);
43 private static final SchemaContext testSchemaContext = TestModel.createTestContext();
46 store.onGlobalContextUpdated(testSchemaContext);
50 public void testOnReceiveReadData() throws Exception {
51 new JavaTestKit(getSystem()) {{
52 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
54 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
55 final ActorRef subject = getSystem().actorOf(props, "testReadData");
57 new Within(duration("1 seconds")) {
58 protected void run() {
61 new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
64 final String out = new ExpectMsg<String>("match hint") {
65 // do not put code outside this method, will run afterwards
66 protected String match(Object in) {
67 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
68 if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
69 .getNormalizedNode()!= null) {
77 }.get(); // this extracts the received message
79 assertEquals("match", out);
90 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
91 new JavaTestKit(getSystem()) {{
92 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
94 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
95 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
97 new Within(duration("1 seconds")) {
98 protected void run() {
101 new ReadData(TestModel.TEST_PATH).toSerializable(),
104 final String out = new ExpectMsg<String>("match hint") {
105 // do not put code outside this method, will run afterwards
106 protected String match(Object in) {
107 if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
108 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
118 }.get(); // this extracts the received message
120 assertEquals("match", out);
130 private void assertModification(final ActorRef subject,
131 final Class<? extends Modification> modificationType) {
132 new JavaTestKit(getSystem()) {{
133 new Within(duration("1 seconds")) {
134 protected void run() {
136 .tell(new ShardTransaction.GetCompositedModification(),
139 final CompositeModification compositeModification =
140 new ExpectMsg<CompositeModification>("match hint") {
141 // do not put code outside this method, will run afterwards
142 protected CompositeModification match(Object in) {
143 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
144 return ((ShardTransaction.GetCompositeModificationReply) in)
150 }.get(); // this extracts the received message
153 compositeModification.getModifications().size() == 1);
154 assertEquals(modificationType,
155 compositeModification.getModifications().get(0)
164 public void testOnReceiveWriteData() throws Exception {
165 new JavaTestKit(getSystem()) {{
166 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
168 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
169 final ActorRef subject =
170 getSystem().actorOf(props, "testWriteData");
172 new Within(duration("1 seconds")) {
173 protected void run() {
175 subject.tell(new WriteData(TestModel.TEST_PATH,
176 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
179 final String out = new ExpectMsg<String>("match hint") {
180 // do not put code outside this method, will run afterwards
181 protected String match(Object in) {
182 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
188 }.get(); // this extracts the received message
190 assertEquals("match", out);
192 assertModification(subject, WriteModification.class);
202 public void testOnReceiveMergeData() throws Exception {
203 new JavaTestKit(getSystem()) {{
204 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
206 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
207 final ActorRef subject =
208 getSystem().actorOf(props, "testMergeData");
210 new Within(duration("1 seconds")) {
211 protected void run() {
213 subject.tell(new MergeData(TestModel.TEST_PATH,
214 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
217 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
218 // do not put code outside this method, will run afterwards
219 protected String match(Object in) {
220 if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
226 }.get(); // this extracts the received message
228 assertEquals("match", out);
230 assertModification(subject, MergeModification.class);
241 public void testOnReceiveDeleteData() throws Exception {
242 new JavaTestKit(getSystem()) {{
243 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
245 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
246 final ActorRef subject =
247 getSystem().actorOf(props, "testDeleteData");
249 new Within(duration("1 seconds")) {
250 protected void run() {
252 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), 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.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
263 }.get(); // this extracts the received message
265 assertEquals("match", out);
267 assertModification(subject, DeleteModification.class);
278 public void testOnReceiveReadyTransaction() throws Exception {
279 new JavaTestKit(getSystem()) {{
280 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
282 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
283 final ActorRef subject =
284 getSystem().actorOf(props, "testReadyTransaction");
286 new Within(duration("1 seconds")) {
287 protected void run() {
289 subject.tell(new ReadyTransaction().toSerializable(), getRef());
291 final String out = new ExpectMsg<String>("match hint") {
292 // do not put code outside this method, will run afterwards
293 protected String match(Object in) {
294 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
300 }.get(); // this extracts the received message
302 assertEquals("match", out);
314 public void testOnReceiveCloseTransaction() throws Exception {
315 new JavaTestKit(getSystem()) {{
316 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
318 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
319 final ActorRef subject =
320 getSystem().actorOf(props, "testCloseTransaction");
324 new Within(duration("2 seconds")) {
325 protected void run() {
327 subject.tell(new CloseTransaction().toSerializable(), getRef());
329 final String out = new ExpectMsg<String>("match hint") {
330 // do not put code outside this method, will run afterwards
331 protected String match(Object in) {
332 if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
338 }.get(); // this extracts the received message
340 assertEquals("match", out);
342 final String termination = new ExpectMsg<String>("match hint") {
343 // do not put code outside this method, will run afterwards
344 protected String match(Object in) {
345 if (in instanceof Terminated) {
351 }.get(); // this extracts the received message