use of uk.gov.gchq.gaffer.types.TypeValue in project Gaffer by gchq.
the class TypeValueSerialiser method deserialise.
@Override
public TypeValue deserialise(final byte[] bytes) throws SerialisationException {
int lastDelimiter = 0;
TypeValue typeValue = new TypeValue();
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] == ByteArrayEscapeUtils.DELIMITER) {
if (i > 0) {
try {
typeValue.setType(new String(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(bytes, lastDelimiter, i)), CommonConstants.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new SerialisationException("Failed to deserialise the Type from TypeValue Object", e);
}
}
lastDelimiter = i + 1;
break;
}
}
if (bytes.length > lastDelimiter) {
try {
typeValue.setValue(new String(ByteArrayEscapeUtils.unEscape(Arrays.copyOfRange(bytes, lastDelimiter, bytes.length)), CommonConstants.UTF_8));
} catch (UnsupportedEncodingException e) {
throw new SerialisationException("Failed to deserialise the Value from TypeValue Object", e);
}
}
return typeValue;
}
use of uk.gov.gchq.gaffer.types.TypeValue in project Gaffer by gchq.
the class TypeValueSerialiserTest method testCanSerialiseDeSerialiseCorrectlyValueOnly.
@Test
public void testCanSerialiseDeSerialiseCorrectlyValueOnly() throws SerialisationException {
TypeValue typeValue = new TypeValue();
typeValue.setValue("testValue");
byte[] bytes = serialiser.serialise(typeValue);
String serialisedForm = new String(bytes);
assertEquals("\0testValue", serialisedForm);
TypeValue deSerialisedTypeValue = (TypeValue) serialiser.deserialise(bytes);
assertNull(deSerialisedTypeValue.getType());
assertEquals(typeValue.getValue(), deSerialisedTypeValue.getValue());
assertEquals(typeValue, deSerialisedTypeValue);
}
use of uk.gov.gchq.gaffer.types.TypeValue in project Gaffer by gchq.
the class TypeValueSerialiserTest method testCanSerialiseDeserialiseCorrectlyAndBeEscaped.
@Test
public void testCanSerialiseDeserialiseCorrectlyAndBeEscaped() throws SerialisationException {
TypeValue typeValue = new TypeValue("testType", "testValue");
byte[] bytes = ByteArrayEscapeUtils.escape(serialiser.serialise(typeValue));
String serialisedForm = new String(bytes);
assertEquals("testType\1\1testValue", serialisedForm);
TypeValue deSerialisedTypeValue = (TypeValue) serialiser.deserialise(ByteArrayEscapeUtils.unEscape(bytes));
assertEquals(typeValue.getType(), deSerialisedTypeValue.getType());
assertEquals(typeValue.getValue(), deSerialisedTypeValue.getValue());
assertEquals(typeValue, deSerialisedTypeValue);
}
use of uk.gov.gchq.gaffer.types.TypeValue in project Gaffer by gchq.
the class TypeValueSerialiserTest method testCanSerialiseDeSerialiseCorrectlyTypeOnly.
@Test
public void testCanSerialiseDeSerialiseCorrectlyTypeOnly() throws SerialisationException {
TypeValue typeValue = new TypeValue();
typeValue.setType("testType");
byte[] bytes = serialiser.serialise(typeValue);
String serialisedForm = new String(bytes);
assertEquals("testType\0", serialisedForm);
TypeValue deSerialisedTypeValue = (TypeValue) serialiser.deserialise(bytes);
assertEquals(typeValue.getType(), deSerialisedTypeValue.getType());
assertNull(typeValue.getValue(), deSerialisedTypeValue.getValue());
assertEquals(typeValue, deSerialisedTypeValue);
}
use of uk.gov.gchq.gaffer.types.TypeValue in project Gaffer by gchq.
the class TypeValueSerialiserTest method shouldDeserialiseEmptyBytes.
@Override
public void shouldDeserialiseEmptyBytes() throws SerialisationException {
// When
final TypeValue value = serialiser.deserialiseEmptyBytes();
// Then
assertNull(value);
}
Aggregations