+ 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"));
+ final Props props =
+ ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
+ final ActorRef subject = getSystem().actorOf(props, "testReadData");
+
+ new Within(duration("1 seconds")) {
+ protected void run() {
+
+ subject.tell(
+ new ReadData(InstanceIdentifier.builder().build()).toSerializable(),
+ getRef());
+
+ final String out = new ExpectMsg<String>("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,InstanceIdentifier.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"));
+ final Props props =
+ ShardTransaction.props(store.newReadWriteTransaction(), 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>("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>("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"));
+ final Props props =
+ ShardTransaction.props(store.newReadWriteTransaction(), 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>("match hint") {
+ // do not put code outside this method, will run afterwards
+ protected String match(Object in) {
+ if (in instanceof WriteDataReply) {
+ return "match";
+ } else {
+ throw noMatch();
+ }
+ }
+ }.get(); // this extracts the received message
+
+ assertEquals("match", out);
+
+ assertModification(subject, WriteModification.class);
+ expectNoMsg();
+ }
+