final ByteArrayOutputStream outputStream = new ByteArrayOutputStream(1024);
// 1. create data and export
try (final Tx tx = app.tx()) {
final TestOne test1 = createTestNode(TestOne.class);
final TestOne test2 = createTestNode(TestOne.class);
final TestOne test3 = createTestNode(TestOne.class);
final TestOne test4 = createTestNode(TestOne.class);
test1.setProperty(TestOne.aString, string1);
test1.setProperty(TestOne.anInt, 1);
test2.setProperty(TestOne.aString, string2);
test2.setProperty(TestOne.anInt, 2);
test3.setProperty(TestOne.aString, string3);
test3.setProperty(TestOne.anInt, 3);
test4.setProperty(TestOne.aString, string4);
test4.setProperty(TestOne.anInt, 4);
SyncCommand.exportToStream(
outputStream,
app.nodeQuery(TestOne.class).getAsList(),
app.relationshipQuery(RelationshipInterface.class).getAsList(),
null,
false
);
tx.success();
}
// 2. clear database
try (final Tx tx = app.tx()) {
for (final TestOne test : app.nodeQuery(TestOne.class).getAsList()) {
app.delete(test);
}
tx.success();
}
// 3. verify that database is empty
try (final Tx tx = app.tx()) {
assertEquals("Database should contain not TestOne entities.", 0, app.nodeQuery(TestOne.class).getResult().size());
tx.success();
}
// 4. import data again
try (final Tx tx = app.tx()) {
SyncCommand.importFromStream(
app.getGraphDatabaseService(),
securityContext,
new ByteArrayInputStream(outputStream.toByteArray()),
true
);
tx.success();
}
// 5. check result
try (final Tx tx = app.tx()) {
final TestOne test1 = app.nodeQuery(TestOne.class).and(TestOne.anInt, 1).getFirst();
final TestOne test2 = app.nodeQuery(TestOne.class).and(TestOne.anInt, 2).getFirst();
final TestOne test3 = app.nodeQuery(TestOne.class).and(TestOne.anInt, 3).getFirst();
final TestOne test4 = app.nodeQuery(TestOne.class).and(TestOne.anInt, 4).getFirst();
assertEquals("Strings from exported and re-imported data should be equal", string1, test1.getProperty(TestOne.aString));
assertEquals("Strings from exported and re-imported data should be equal", string2, test2.getProperty(TestOne.aString));
assertEquals("Strings from exported and re-imported data should be equal", string3, test3.getProperty(TestOne.aString));
assertEquals("Strings from exported and re-imported data should be equal", string4, test4.getProperty(TestOne.aString));
tx.success();
}