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.InstanceIdentifier;
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, TestModel.createTestContext());
55 final ActorRef subject = getSystem().actorOf(props, "testReadData");
57 new Within(duration("1 seconds")) {
58 protected void run() {
61 new ReadData(InstanceIdentifier.builder().build()),
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 instanceof ReadDataReply) {
68 if (((ReadDataReply) in).getNormalizedNode()
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, TestModel.createTestContext());
95 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
97 new Within(duration("1 seconds")) {
98 protected void run() {
101 new ReadData(TestModel.TEST_PATH),
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 instanceof ReadDataReply) {
108 if (((ReadDataReply) in).getNormalizedNode()
117 }.get(); // this extracts the received message
119 assertEquals("match", out);
129 private void assertModification(final ActorRef subject,
130 final Class<? extends Modification> modificationType) {
131 new JavaTestKit(getSystem()) {{
132 new Within(duration("1 seconds")) {
133 protected void run() {
135 .tell(new ShardTransaction.GetCompositedModification(),
138 final CompositeModification compositeModification =
139 new ExpectMsg<CompositeModification>("match hint") {
140 // do not put code outside this method, will run afterwards
141 protected CompositeModification match(Object in) {
142 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
143 return ((ShardTransaction.GetCompositeModificationReply) in)
149 }.get(); // this extracts the received message
152 compositeModification.getModifications().size() == 1);
153 assertEquals(modificationType,
154 compositeModification.getModifications().get(0)
163 public void testOnReceiveWriteData() throws Exception {
164 new JavaTestKit(getSystem()) {{
165 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
167 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
168 final ActorRef subject =
169 getSystem().actorOf(props, "testWriteData");
171 new Within(duration("1 seconds")) {
172 protected void run() {
174 subject.tell(new WriteData(TestModel.TEST_PATH,
175 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
178 final String out = new ExpectMsg<String>("match hint") {
179 // do not put code outside this method, will run afterwards
180 protected String match(Object in) {
181 if (in instanceof WriteDataReply) {
187 }.get(); // this extracts the received message
189 assertEquals("match", out);
191 assertModification(subject, WriteModification.class);
201 public void testOnReceiveMergeData() throws Exception {
202 new JavaTestKit(getSystem()) {{
203 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
205 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
206 final ActorRef subject =
207 getSystem().actorOf(props, "testMergeData");
209 new Within(duration("1 seconds")) {
210 protected void run() {
212 subject.tell(new MergeData(TestModel.TEST_PATH,
213 ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
216 final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
217 // do not put code outside this method, will run afterwards
218 protected String match(Object in) {
219 if (in instanceof MergeDataReply) {
225 }.get(); // this extracts the received message
227 assertEquals("match", out);
229 assertModification(subject, MergeModification.class);
240 public void testOnReceiveDeleteData() throws Exception {
241 new JavaTestKit(getSystem()) {{
242 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
244 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
245 final ActorRef subject =
246 getSystem().actorOf(props, "testDeleteData");
248 new Within(duration("1 seconds")) {
249 protected void run() {
251 subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
253 final String out = new ExpectMsg<String>("match hint") {
254 // do not put code outside this method, will run afterwards
255 protected String match(Object in) {
256 if (in instanceof DeleteDataReply) {
262 }.get(); // this extracts the received message
264 assertEquals("match", out);
266 assertModification(subject, DeleteModification.class);
277 public void testOnReceiveReadyTransaction() throws Exception {
278 new JavaTestKit(getSystem()) {{
279 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
281 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
282 final ActorRef subject =
283 getSystem().actorOf(props, "testReadyTransaction");
285 new Within(duration("1 seconds")) {
286 protected void run() {
288 subject.tell(new ReadyTransaction(), getRef());
290 final String out = new ExpectMsg<String>("match hint") {
291 // do not put code outside this method, will run afterwards
292 protected String match(Object in) {
293 if (in instanceof ReadyTransactionReply) {
299 }.get(); // this extracts the received message
301 assertEquals("match", out);
313 public void testOnReceiveCloseTransaction() throws Exception {
314 new JavaTestKit(getSystem()) {{
315 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
317 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
318 final ActorRef subject =
319 getSystem().actorOf(props, "testCloseTransaction");
323 new Within(duration("2 seconds")) {
324 protected void run() {
326 subject.tell(new CloseTransaction(), getRef());
328 final String out = new ExpectMsg<String>("match hint") {
329 // do not put code outside this method, will run afterwards
330 protected String match(Object in) {
331 if (in instanceof CloseTransactionReply) {
337 }.get(); // this extracts the received message
339 assertEquals("match", out);
341 final String termination = new ExpectMsg<String>("match hint") {
342 // do not put code outside this method, will run afterwards
343 protected String match(Object in) {
344 if (in instanceof Terminated) {
350 }.get(); // this extracts the received message