@Test
  public void testApply() throws Exception {
    // Write something into the datastore
    DOMStoreReadWriteTransaction writeTransaction = store.newReadWriteTransaction();
    WriteModification writeModification =
        new WriteModification(
            TestModel.TEST_PATH,
            ImmutableNodes.containerNode(TestModel.TEST_QNAME),
            TestModel.createTestContext());
    writeModification.apply(writeTransaction);
    commitTransaction(writeTransaction);

    // Check if it's in the datastore
    Optional<NormalizedNode<?, ?>> data = readData(TestModel.TEST_PATH);
    Assert.assertTrue(data.isPresent());

    // Delete stuff from the datastore
    DOMStoreWriteTransaction deleteTransaction = store.newWriteOnlyTransaction();
    DeleteModification deleteModification = new DeleteModification(TestModel.TEST_PATH);
    deleteModification.apply(deleteTransaction);
    commitTransaction(deleteTransaction);

    data = readData(TestModel.TEST_PATH);
    Assert.assertFalse(data.isPresent());
  }
  @Before
  public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);

    schemaContext = TestModel.createTestContext();

    doReturn(schemaContext).when(actorContext).getSchemaContext();
    doReturn(DatastoreContext.newBuilder().build()).when(actorContext).getDatastoreContext();
  }
Example #3
0
  @Test
  public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
    try {

      final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
      final Props props =
          ShardTransaction.props(
              store.newReadOnlyTransaction(), shard, TestModel.createTestContext());
      final TestActorRef subject = TestActorRef.apply(props, getSystem());

      subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
      Assert.assertFalse(true);

    } catch (Exception cs) {
      assertEquals(cs.getClass().getSimpleName(), Exception.class.getSimpleName());
      assertTrue(
          cs.getMessage().startsWith("ShardTransaction:handleRecieve received an unknown message"));
    }
  }
 static {
   store.onGlobalContextUpdated(TestModel.createTestContext());
 }
Example #5
0
public class ShardTransactionTest extends AbstractActorTest {
  private static ListeningExecutorService storeExecutor =
      MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());

  private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);

  private static final SchemaContext testSchemaContext = TestModel.createTestContext();

  static {
    store.onGlobalContextUpdated(testSchemaContext);
  }

  @Test
  public void testOnReceiveReadData() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(store.newReadOnlyTransaction(), shard, testSchemaContext);
        final ActorRef subject = getSystem().actorOf(props, "testReadData");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(
                new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(), getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
                      if (ReadDataReply.fromSerializable(
                                  testSchemaContext, YangInstanceIdentifier.builder().build(), in)
                              .getNormalizedNode()
                          != null) {
                        return "match";
                      }
                      return null;
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(store.newReadOnlyTransaction(), shard, testSchemaContext);
        final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(new ReadData(TestModel.TEST_PATH).toSerializable(), getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
                      if (ReadDataReply.fromSerializable(testSchemaContext, TestModel.TEST_PATH, in)
                              .getNormalizedNode()
                          == null) {
                        return "match";
                      }
                      return null;
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            expectNoMsg();
          }
        };
      }
    };
  }

  private void assertModification(
      final ActorRef subject, final Class<? extends Modification> modificationType) {
    new JavaTestKit(getSystem()) {
      {
        new Within(duration("1 seconds")) {
          protected void run() {
            subject.tell(new ShardTransaction.GetCompositedModification(), getRef());

            final CompositeModification compositeModification =
                new ExpectMsg<CompositeModification>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected CompositeModification match(Object in) {
                    if (in instanceof ShardTransaction.GetCompositeModificationReply) {
                      return ((ShardTransaction.GetCompositeModificationReply) in)
                          .getModification();
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertTrue(compositeModification.getModifications().size() == 1);
            assertEquals(
                modificationType, compositeModification.getModifications().get(0).getClass());
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveWriteData() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(
                store.newWriteOnlyTransaction(), shard, TestModel.createTestContext());
        final ActorRef subject = getSystem().actorOf(props, "testWriteData");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(
                new WriteData(
                        TestModel.TEST_PATH,
                        ImmutableNodes.containerNode(TestModel.TEST_QNAME),
                        TestModel.createTestContext())
                    .toSerializable(),
                getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            assertModification(subject, WriteModification.class);
            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveMergeData() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
        final ActorRef subject = getSystem().actorOf(props, "testMergeData");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(
                new MergeData(
                        TestModel.TEST_PATH,
                        ImmutableNodes.containerNode(TestModel.TEST_QNAME),
                        testSchemaContext)
                    .toSerializable(),
                getRef());

            final String out =
                new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            assertModification(subject, MergeModification.class);

            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveDeleteData() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(
                store.newWriteOnlyTransaction(), shard, TestModel.createTestContext());
        final ActorRef subject = getSystem().actorOf(props, "testDeleteData");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            assertModification(subject, DeleteModification.class);
            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveReadyTransaction() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(
                store.newReadWriteTransaction(), shard, TestModel.createTestContext());
        final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");

        new Within(duration("1 seconds")) {
          protected void run() {

            subject.tell(new ReadyTransaction().toSerializable(), getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testOnReceiveCloseTransaction() throws Exception {
    new JavaTestKit(getSystem()) {
      {
        final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
        final Props props =
            ShardTransaction.props(
                store.newReadWriteTransaction(), shard, TestModel.createTestContext());
        final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");

        watch(subject);

        new Within(duration("2 seconds")) {
          protected void run() {

            subject.tell(new CloseTransaction().toSerializable(), getRef());

            final String out =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            assertEquals("match", out);

            final String termination =
                new ExpectMsg<String>(duration("1 seconds"), "match hint") {
                  // do not put code outside this method, will run afterwards
                  protected String match(Object in) {
                    if (in instanceof Terminated) {
                      return "match";
                    } else {
                      throw noMatch();
                    }
                  }
                }.get(); // this extracts the received message

            expectNoMsg();
          }
        };
      }
    };
  }

  @Test
  public void testNegativePerformingWriteOperationOnReadTransaction() throws Exception {
    try {

      final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
      final Props props =
          ShardTransaction.props(
              store.newReadOnlyTransaction(), shard, TestModel.createTestContext());
      final TestActorRef subject = TestActorRef.apply(props, getSystem());

      subject.receive(new DeleteData(TestModel.TEST_PATH).toSerializable(), ActorRef.noSender());
      Assert.assertFalse(true);

    } catch (Exception cs) {
      assertEquals(cs.getClass().getSimpleName(), Exception.class.getSimpleName());
      assertTrue(
          cs.getMessage().startsWith("ShardTransaction:handleRecieve received an unknown message"));
    }
  }
}