DbusEvent { public abstract boolean isValid();  abstract boolean isExtReplicatedEvent(); abstract boolean isValid(); abstract boolean isControlMessage(); abstract boolean isPrivateControlMessage(); abstract boolean isCheckpointMessage(); abstract boolean isKeyNumber(); abstract boolean isKeyString(); abstract boolean isKeySchema(); abstract boolean isSCNRegressMessage(); abstract boolean isTraceEnabled(); abstract boolean isEndOfPeriodMarker(); abstract DbusOpcode getOpcode(); abstract long timestampInNanos(); abstract int size(); abstract long sequence(); abstract long key(); @Deprecated abstract int keyLength(); abstract byte[] keyBytes(); @Deprecated abstract short srcId(); abstract int getSourceId(); @Deprecated abstract short physicalPartitionId(); @Deprecated abstract short logicalPartitionId(); abstract short getPartitionId(); abstract byte[] schemaId(); abstract void schemaId(byte[] md5); abstract int valueLength(); abstract ByteBuffer value(); abstract ByteBuffer getRawBytes(); abstract DbusEventPart getPayloadPart(); abstract DbusEventPart getPayloadMetadataPart(); abstract DbusEventPart getKeyPart(); final static int MD5_DIGEST_LEN; final static int CRC32_DIGEST_LEN; }
@Test public void testIsValid() { String randomValue = RngUtils.randomString(100); ByteBuffer serializationBuffer = ByteBuffer.allocate(1000).order(_eventV1Factory.getByteOrder()); try { DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 0L, (short)0, partitionId, timeStamp, srcId, schemaId, randomValue.getBytes(Charset.defaultCharset()), false, false); eventInfo.setEventSerializationVersion(DbusEventFactory.DBUS_EVENT_V1); DbusEventFactory.serializeEvent(new DbusEventKey(key), serializationBuffer, eventInfo); } catch (KeyTypeNotImplementedException e2) { fail(e2.getLocalizedMessage()); } DbusEventInternalReadable e = null; try { e = DbusEventFactoryForTesting.createReadOnlyDbusEventFromBuffer(serializationBuffer, 0, 200L, DbusEventFactory.DBUS_EVENT_V2); } catch (Exception e1) { fail("Not supposed to throw exception"); } assertTrue(e.isValid()); try { DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.LENGTH,e); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.ERR); assertFalse(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.LENGTH,e); assertTrue(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.HEADERCRC,e); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.ERR); assertFalse(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.HEADERCRC,e); assertTrue(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.PAYLOADCRC,e); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.ERR); assertFalse(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.PAYLOADCRC,e); assertTrue(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.PAYLOAD,e); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.OK); assertTrue(e.scanEvent()== DbusEventInternalReadable.EventScanStatus.ERR); assertFalse(e.isValid()); DbusEventCorrupter.toggleEventCorruption(EventCorruptionType.PAYLOAD,e); assertTrue(e.isValid()); } catch (InvalidEventException e1) { fail("Not supposed to throw exception"); } int origLimit = serializationBuffer.limit(); serializationBuffer.limit(30); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.PARTIAL); assertTrue(e.isPartial()); assertFalse(e.isValid()); serializationBuffer.limit(origLimit); assertTrue(e.isValid()); serializationBuffer.limit(90); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.OK); assertTrue(e.scanEvent()== DbusEventInternalReadable.EventScanStatus.PARTIAL); assertTrue(e.isPartial()); assertFalse(e.isValid()); serializationBuffer.limit(origLimit); assertTrue(e.isValid()); int headerSize = 56; int preambleSize = 5; serializationBuffer.limit(headerSize); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.PARTIAL); assertTrue(e.isPartial()); serializationBuffer.limit(headerSize+preambleSize); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.OK); assertTrue(e.isPartial()); serializationBuffer.limit(origLimit); assertTrue(e.isValid()); serializationBuffer.limit(headerSize+preambleSize-1); assertTrue(e.scanHeader()== DbusEventInternalReadable.HeaderScanStatus.PARTIAL); assertTrue(e.isPartial()); serializationBuffer.limit(origLimit); assertTrue(e.isValid()); }
@Test public void testHeaderCrcLongKeyWithDirectBuffer() throws KeyTypeNotImplementedException { String randomValue = RngUtils.randomString(20); ByteBuffer directBuffer = ByteBuffer.allocateDirect(5000).order(_eventV1Factory.getByteOrder()); DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 0L, (short)0, partitionId, timeStamp, srcId, schemaId, randomValue.getBytes(Charset.defaultCharset()), false, false); eventInfo.setEventSerializationVersion(DbusEventFactory.DBUS_EVENT_V1); DbusEventFactory.serializeEvent(new DbusEventKey(key), directBuffer, eventInfo); DbusEventInternalReadable e = null; try { e = DbusEventFactoryForTesting.createReadOnlyDbusEventFromBuffer(directBuffer, 0, 200L, DbusEventFactory.DBUS_EVENT_V2); } catch (Exception e1) { fail("Not supposed to throw exception"); } assertTrue(e.isValid()); }
@Test public void testHeaderCrcStringKeyWithDirectBuffer() throws KeyTypeNotImplementedException { String randomValue = RngUtils.randomString(20); String randomKey = RngUtils.randomString(10); ByteBuffer directBuffer = ByteBuffer.allocateDirect(5000).order(_eventV1Factory.getByteOrder()); DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 0L, (short)0, partitionId, timeStamp, srcId, schemaId, randomValue.getBytes(Charset.defaultCharset()), false, false); eventInfo.setEventSerializationVersion(DbusEventFactory.DBUS_EVENT_V1); DbusEventFactory.serializeEvent(new DbusEventKey(randomKey.getBytes(Charset.defaultCharset())), directBuffer, eventInfo); DbusEventInternalReadable e = null; try { e = DbusEventFactoryForTesting.createReadOnlyDbusEventFromBuffer(directBuffer, 0, 200L, DbusEventFactory.DBUS_EVENT_V2); } catch (Exception e1) { fail("Not supposed to throw exception"); } assertTrue(e.isValid()); }
@Test public void testValueCrc() throws Exception { String randomValue = RngUtils.randomString(20); ByteBuffer directBuffer = ByteBuffer.allocateDirect(5000).order(_eventV1Factory.getByteOrder()); DbusEventInfo eventInfo = new DbusEventInfo(DbusOpcode.UPSERT, 0L, (short)0, partitionId, timeStamp, srcId, schemaId, randomValue.getBytes(Charset.defaultCharset()), false, false); eventInfo.setEventSerializationVersion(DbusEventFactory.DBUS_EVENT_V1); DbusEventFactory.serializeEvent(new DbusEventKey(key), directBuffer, eventInfo); DbusEventInternalReadable e = _eventV1Factory.createReadOnlyDbusEventFromBuffer(directBuffer, 0); DbusEventInternalWritable writableEvent = DbusEventCorrupter.makeWritable(e); writableEvent.setSequence(200L); long calcValueCRC = 0; try { calcValueCRC = e.getCalculatedValueCrc(); writableEvent.setValueCrc(calcValueCRC); writableEvent.applyCrc(); } catch (Exception e1) { fail("Not supposed to throw exception"); } assertTrue(e.isValid()); long actualValueCRC = e.bodyCrc(); assertEquals("value CRCs should match", calcValueCRC, actualValueCRC); try { writableEvent.setValueCrc(calcValueCRC + 1); writableEvent.applyCrc(); } catch (Exception e1) { fail("Not supposed to throw exception"); } actualValueCRC = e.bodyCrc(); assertEquals("value CRCs should match", calcValueCRC+1, actualValueCRC); assertTrue(!e.isValid()); }