}
@Test
public void shouldCopyNodesFromOneWorkspaceToAnotherAndGenerateNewUuids() {
// Populate the workspace with some content ..
MapNode root = workspace.getRoot();
MapNode node_a = workspace.createNode(context, root, nameFactory.create("a"), null);
MapNode node_b = workspace.createNode(context, node_a, nameFactory.create("b"), null);
MapNode node_c = workspace.createNode(context, node_b, nameFactory.create("c"), null);
MapNode node_d = workspace.createNode(context, root, nameFactory.create("d"), null);
MapNode node_e = workspace.createNode(context, node_d, nameFactory.create("e"), null);
MapNode node_b2 = workspace.createNode(context, node_d, nameFactory.create("b"), null);
ValueFactory<String> stringFactory = valueFactories.getStringFactory();
Name propertyName = nameFactory.create("something");
Property property = propertyFactory.create(propertyName, stringFactory.create("Worth the wait"));
node_b.setProperty(property);
assertThat(workspace.size(), is(7));
assertThat(workspace.getNode(pathFactory.create("/")), is(sameInstance(workspace.getRoot())));
assertThat(workspace.getNode(pathFactory.create("/a")), is(sameInstance(node_a)));
assertThat(workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(node_b)));
assertThat(workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(node_c)));
assertThat(workspace.getNode(pathFactory.create("/d")), is(sameInstance(node_d)));
assertThat(workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(node_e)));
assertThat(workspace.getNode(pathFactory.create("/d/b")), is(sameInstance(node_b2)));
assertThat(workspace.getNode(pathFactory.create("/a/b")).getProperty(propertyName), is(property));
// Create the second workspace and populate it with some content ..
InMemoryRepository.Workspace new_workspace = (InMemoryRepository.Workspace) repository.createWorkspace(context, "Second Workspace", CreateConflictBehavior.DO_NOT_CREATE);
assertThat(new_workspace, is(notNullValue()));
MapNode new_root = new_workspace.getRoot();
MapNode new_node_a = new_workspace.createNode(context, new_root, nameFactory.create("a"), null);
MapNode new_node_b = new_workspace.createNode(context, new_node_a, nameFactory.create("b"), null);
MapNode new_node_c = new_workspace.createNode(context, new_node_b, nameFactory.create("c"), null);
MapNode new_node_d = new_workspace.createNode(context, new_root, nameFactory.create("d"), null);
MapNode new_node_e = new_workspace.createNode(context, new_node_d, nameFactory.create("e"), null);
MapNode new_node_b2 = new_workspace.createNode(context, new_node_d, nameFactory.create("b"), null);
assertThat(new_workspace.size(), is(7));
assertThat(new_workspace.getNode(pathFactory.create("/")), is(sameInstance(new_root)));
assertThat(new_workspace.getNode(pathFactory.create("/a")), is(sameInstance(new_node_a)));
assertThat(new_workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(new_node_b)));
assertThat(new_workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(new_node_c)));
assertThat(new_workspace.getNode(pathFactory.create("/d")), is(sameInstance(new_node_d)));
assertThat(new_workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(new_node_e)));
assertThat(new_workspace.getNode(pathFactory.create("/d/b")), is(sameInstance(new_node_b2)));
// Copy 'workspace::/a/b' into 'newWorkspace::/d'
Map<UUID, UUID> oldToNewUuids = new HashMap<UUID, UUID>();
workspace.copyNode(context, node_b, new_workspace, new_node_d, null, true, oldToNewUuids);
assertThat(workspace.size(), is(7));
assertThat(workspace.getNode(pathFactory.create("/")), is(sameInstance(workspace.getRoot())));
assertThat(workspace.getNode(pathFactory.create("/a")), is(sameInstance(node_a)));
assertThat(workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(node_b)));
assertThat(workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(node_c)));
assertThat(workspace.getNode(pathFactory.create("/d")), is(sameInstance(node_d)));
assertThat(workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(node_e)));
assertThat(workspace.getNode(pathFactory.create("/d/b")), is(sameInstance(node_b2)));
assertThat(workspace.getNode(pathFactory.create("/a/b")).getProperty(propertyName), is(property));
assertThat(new_workspace.size(), is(9));
assertThat(new_workspace.getNode(pathFactory.create("/")), is(sameInstance(new_root)));
assertThat(new_workspace.getNode(pathFactory.create("/a")), is(sameInstance(new_node_a)));
assertThat(new_workspace.getNode(pathFactory.create("/a/b")), is(sameInstance(new_node_b)));
assertThat(new_workspace.getNode(pathFactory.create("/a/b/c")), is(sameInstance(new_node_c)));
assertThat(new_workspace.getNode(pathFactory.create("/d")), is(sameInstance(new_node_d)));
assertThat(new_workspace.getNode(pathFactory.create("/d/e")), is(sameInstance(new_node_e)));
assertThat(new_workspace.getNode(pathFactory.create("/d/b[1]")), is(sameInstance(new_node_b2)));
assertThat(new_workspace.getNode(pathFactory.create("/d/b[2]")), is(notNullValue()));
assertThat(new_workspace.getNode(pathFactory.create("/d/b[2]/c")), is(notNullValue()));
assertThat(new_workspace.getNode(pathFactory.create("/d/b[2]")).getProperty(propertyName), is(property));
// The new copy should have different UUIDs than in the original, since we did specify a UUID map ..
MapNode new_copy_b = new_workspace.getNode(pathFactory.create("/d/b[2]"));
MapNode new_copy_c = new_workspace.getNode(pathFactory.create("/d/b[2]/c"));
assertThat(new_copy_b, is(notNullValue()));
assertThat(new_copy_c, is(notNullValue()));
assertThat(new_copy_b.getUuid(), is(not(node_b.getUuid())));
assertThat(new_copy_c.getUuid(), is(not(node_c.getUuid())));
assertThat(new_copy_b.getUuid(), is(oldToNewUuids.get(node_b.getUuid())));
assertThat(new_copy_c.getUuid(), is(oldToNewUuids.get(node_c.getUuid())));
}