use of org.vertexium.test.util.LargeStringInputStream in project vertexium by visallo.
the class GraphTestBase method testStreamingPropertyValueMarkResetLargeReads.
@Test
public void testStreamingPropertyValueMarkResetLargeReads() throws IOException {
assumeTrue("InputStream mark/reset is not supported", isInputStreamMarkResetSupported());
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
byte[] expectedLargeValueBytes = expectedLargeValue.getBytes();
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
graph.prepareVertex("v1", VISIBILITY_A).setProperty("propLarge", propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A);
StreamingPropertyValue prop = (StreamingPropertyValue) v1.getPropertyValue("propLarge");
InputStream in = prop.getInputStream();
int amountToRead = expectedLargeValueBytes.length - 8;
byte[] buffer = null;
while (amountToRead > 0) {
buffer = new byte[amountToRead];
amountToRead -= in.read(buffer);
}
assertEquals(expectedLargeValue.charAt(expectedLargeValue.length() - 9), (char) buffer[buffer.length - 1]);
in.mark(32);
buffer = new byte[2];
int sizeRead = in.read(buffer);
assertEquals(2, sizeRead);
assertEquals(expectedLargeValue.charAt(expectedLargeValue.length() - 8), (char) buffer[0]);
assertEquals(expectedLargeValue.charAt(expectedLargeValue.length() - 7), (char) buffer[1]);
assertEquals(expectedLargeValue.charAt(expectedLargeValue.length() - 6), (char) in.read());
in.reset();
sizeRead = in.read(buffer);
assertEquals(2, sizeRead);
assertEquals(expectedLargeValue.charAt(expectedLargeValue.length() - 8), (char) buffer[0]);
}
use of org.vertexium.test.util.LargeStringInputStream in project vertexium by visallo.
the class GraphTestBase method testAddStreamingPropertyValue.
@SuppressWarnings("AssertEqualsBetweenInconvertibleTypes")
@Test
public void testAddStreamingPropertyValue() throws IOException, InterruptedException {
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
PropertyValue propSmall = StreamingPropertyValue.create(new ByteArrayInputStream("value1".getBytes()), String.class, 6L);
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class, null);
String largePropertyName = "propLarge/\\*!@#$%^&*()[]{}|";
Vertex v1 = graph.prepareVertex("v1", VISIBILITY_A).setProperty("propSmall", propSmall, VISIBILITY_A).setProperty(largePropertyName, propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Iterable<Object> propSmallValues = v1.getPropertyValues("propSmall");
Assert.assertEquals(1, count(propSmallValues));
Object propSmallValue = propSmallValues.iterator().next();
assertTrue("propSmallValue was " + propSmallValue.getClass().getName(), propSmallValue instanceof StreamingPropertyValue);
StreamingPropertyValue value = (StreamingPropertyValue) propSmallValue;
assertEquals(String.class, value.getValueType());
assertEquals("value1".getBytes().length, (long) value.getLength());
assertEquals("value1", IOUtils.toString(value.getInputStream()));
assertEquals("value1", IOUtils.toString(value.getInputStream()));
Iterable<Object> propLargeValues = v1.getPropertyValues(largePropertyName);
Assert.assertEquals(1, count(propLargeValues));
Object propLargeValue = propLargeValues.iterator().next();
assertTrue(largePropertyName + " was " + propLargeValue.getClass().getName(), propLargeValue instanceof StreamingPropertyValue);
value = (StreamingPropertyValue) propLargeValue;
assertEquals(String.class, value.getValueType());
assertEquals(expectedLargeValue.getBytes().length, (long) value.getLength());
assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream()));
assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream()));
graph.flush();
v1 = graph.getVertex("v1", AUTHORIZATIONS_A);
propSmallValues = v1.getPropertyValues("propSmall");
Assert.assertEquals(1, count(propSmallValues));
propSmallValue = propSmallValues.iterator().next();
assertTrue("propSmallValue was " + propSmallValue.getClass().getName(), propSmallValue instanceof StreamingPropertyValue);
value = (StreamingPropertyValue) propSmallValue;
assertEquals(String.class, value.getValueType());
assertEquals("value1".getBytes().length, (long) value.getLength());
assertEquals("value1", IOUtils.toString(value.getInputStream()));
assertEquals("value1", IOUtils.toString(value.getInputStream()));
propLargeValues = v1.getPropertyValues(largePropertyName);
Assert.assertEquals(1, count(propLargeValues));
propLargeValue = propLargeValues.iterator().next();
assertTrue(largePropertyName + " was " + propLargeValue.getClass().getName(), propLargeValue instanceof StreamingPropertyValue);
value = (StreamingPropertyValue) propLargeValue;
assertEquals(String.class, value.getValueType());
assertEquals(expectedLargeValue.getBytes().length, (long) value.getLength());
assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream()));
assertEquals(expectedLargeValue, IOUtils.toString(value.getInputStream()));
}
use of org.vertexium.test.util.LargeStringInputStream in project vertexium by visallo.
the class GraphTestBase method testChangeVisibilityOnStreamingProperty.
@Test
public void testChangeVisibilityOnStreamingProperty() throws IOException {
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
PropertyValue propSmall = StreamingPropertyValue.create(new ByteArrayInputStream("value1".getBytes()), String.class);
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
String largePropertyName = "propLarge/\\*!@#$%^&*()[]{}|";
graph.prepareVertex("v1", VISIBILITY_A).setProperty("propSmall", propSmall, VISIBILITY_A).setProperty(largePropertyName, propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Assert.assertEquals(2, count(graph.getVertex("v1", AUTHORIZATIONS_A).getProperties()));
graph.getVertex("v1", FetchHints.ALL, AUTHORIZATIONS_A).prepareMutation().alterPropertyVisibility("propSmall", VISIBILITY_B).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Assert.assertEquals(1, count(graph.getVertex("v1", AUTHORIZATIONS_A).getProperties()));
graph.getVertex("v1", FetchHints.ALL, AUTHORIZATIONS_A).prepareMutation().alterPropertyVisibility(largePropertyName, VISIBILITY_B).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Assert.assertEquals(0, count(graph.getVertex("v1", AUTHORIZATIONS_A).getProperties()));
Assert.assertEquals(2, count(graph.getVertex("v1", AUTHORIZATIONS_A_AND_B).getProperties()));
}
use of org.vertexium.test.util.LargeStringInputStream in project vertexium by visallo.
the class GraphTestBase method testStreamingPropertyValueLargeReads.
@Test
public void testStreamingPropertyValueLargeReads() throws IOException {
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(LARGE_PROPERTY_VALUE_SIZE));
byte[] expectedLargeValueBytes = expectedLargeValue.getBytes();
PropertyValue propLarge = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
graph.prepareVertex("v1", VISIBILITY_A).setProperty("propLarge", propLarge, VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
graph.flush();
Vertex v1 = graph.getVertex("v1", AUTHORIZATIONS_A);
StreamingPropertyValue prop = (StreamingPropertyValue) v1.getPropertyValue("propLarge");
byte[] buffer = new byte[LARGE_PROPERTY_VALUE_SIZE * 2];
int leftToRead = expectedLargeValueBytes.length;
InputStream in = prop.getInputStream();
for (int expectedOffset = 0; expectedOffset < expectedLargeValueBytes.length; ) {
int sizeRead = in.read(buffer);
for (int j = 0; j < sizeRead; j++, expectedOffset++, leftToRead--) {
assertEquals("invalid data at offset " + expectedOffset, expectedLargeValueBytes[expectedOffset], buffer[j]);
}
}
assertEquals(0, leftToRead);
assertEquals(-1, in.read(buffer));
}
use of org.vertexium.test.util.LargeStringInputStream in project vertexium by visallo.
the class BackupRestoreTest method testSaveAndLoad.
@Test
public void testSaveAndLoad() throws IOException, ClassNotFoundException {
Graph graph = createGraph();
Metadata prop1Metadata = new Metadata();
prop1Metadata.add("metadata1", "metadata1Value", GraphTestBase.VISIBILITY_A);
int largePropertyValueSize = 1000;
String expectedLargeValue = IOUtils.toString(new LargeStringInputStream(largePropertyValueSize));
StreamingPropertyValue largeDataValue = StreamingPropertyValue.create(new ByteArrayInputStream(expectedLargeValue.getBytes()), String.class);
Vertex v1 = graph.prepareVertex("v1", GraphTestBase.VISIBILITY_A).addPropertyValue("id1a", "prop1", "value1a", prop1Metadata, GraphTestBase.VISIBILITY_A).addPropertyValue("id1b", "prop1", "value1b", GraphTestBase.VISIBILITY_A).addPropertyValue("id2", "prop2", "value2", GraphTestBase.VISIBILITY_B).setProperty("largeData", largeDataValue, GraphTestBase.VISIBILITY_A).save(AUTHORIZATIONS_A_AND_B);
Vertex v2 = graph.addVertex("v2", GraphTestBase.VISIBILITY_A, AUTHORIZATIONS_A);
Vertex v3 = graph.addVertex("v3", GraphTestBase.VISIBILITY_B, AUTHORIZATIONS_B);
graph.addEdge("e1to2", v1, v2, "label1", GraphTestBase.VISIBILITY_A, AUTHORIZATIONS_A);
graph.addEdge("e1to3", v1, v3, "label1", GraphTestBase.VISIBILITY_B, AUTHORIZATIONS_B);
File tmp = File.createTempFile(getClass().getName(), ".json");
try (FileOutputStream out = new FileOutputStream(tmp)) {
System.out.println("saving graph to: " + tmp);
GraphBackup graphBackup = new GraphBackup();
graphBackup.save(graph, out, AUTHORIZATIONS_A_AND_B);
}
try (FileInputStream in = new FileInputStream(tmp)) {
Graph loadedGraph = createGraph();
GraphRestore graphRestore = new GraphRestore();
graphRestore.restore(loadedGraph, in, AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(3, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_A_AND_B)));
Assert.assertEquals(2, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_A)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getVertices(AUTHORIZATIONS_B)));
Assert.assertEquals(2, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_A_AND_B)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_A)));
Assert.assertEquals(1, IterableUtils.count(loadedGraph.getEdges(AUTHORIZATIONS_B)));
v1 = loadedGraph.getVertex("v1", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(2, IterableUtils.count(v1.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
Iterable<Property> properties = v1.getProperties();
boolean prop1_id1a_found = false;
boolean prop1_id1b_found = false;
for (Property property : properties) {
if (property.getName().equals("prop1")) {
if (property.getKey().equals("id1a")) {
prop1_id1a_found = true;
assertEquals("value1a", property.getValue());
}
if (property.getKey().equals("id1b")) {
prop1_id1b_found = true;
assertEquals("value1b", property.getValue());
}
}
}
assertTrue("prop1[id1a] not found", prop1_id1a_found);
assertTrue("prop1[id1b] not found", prop1_id1b_found);
assertEquals("value2", v1.getPropertyValue("prop2", 0));
StreamingPropertyValue spv = (StreamingPropertyValue) v1.getPropertyValue("largeData", 0);
assertNotNull("largeData property not found", spv);
assertEquals(String.class, spv.getValueType());
assertEquals(expectedLargeValue, IOUtils.toString(spv.getInputStream()));
v2 = loadedGraph.getVertex("v2", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(1, IterableUtils.count(v2.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
v3 = loadedGraph.getVertex("v3", AUTHORIZATIONS_A_AND_B);
Assert.assertEquals(1, IterableUtils.count(v3.getEdges(Direction.BOTH, AUTHORIZATIONS_A_AND_B)));
}
tmp.delete();
}
Aggregations