use of org.apache.atlas.model.notification.AtlasNotificationMessage in project atlas by apache.
the class AbstractNotificationConsumerTest method testReceive.
@Test
public void testReceive() throws Exception {
Logger logger = mock(Logger.class);
TestMessage testMessage1 = new TestMessage("sValue1", 99);
TestMessage testMessage2 = new TestMessage("sValue2", 98);
TestMessage testMessage3 = new TestMessage("sValue3", 97);
TestMessage testMessage4 = new TestMessage("sValue4", 96);
List jsonList = new LinkedList<>();
jsonList.add(AtlasType.toV1Json(new AtlasNotificationMessage<>(new MessageVersion("1.0.0"), testMessage1)));
jsonList.add(AtlasType.toV1Json(new AtlasNotificationMessage<>(new MessageVersion("1.0.0"), testMessage2)));
jsonList.add(AtlasType.toV1Json(new AtlasNotificationMessage<>(new MessageVersion("1.0.0"), testMessage3)));
jsonList.add(AtlasType.toV1Json(new AtlasNotificationMessage<>(new MessageVersion("1.0.0"), testMessage4)));
NotificationConsumer<TestMessage> consumer = new TestNotificationConsumer(jsonList, logger);
List<AtlasKafkaMessage<TestMessage>> messageList = consumer.receive();
assertFalse(messageList.isEmpty());
assertEquals(messageList.get(0).getMessage(), testMessage1);
assertEquals(messageList.get(1).getMessage(), testMessage2);
assertEquals(messageList.get(2).getMessage(), testMessage3);
assertEquals(messageList.get(3).getMessage(), testMessage4);
}
use of org.apache.atlas.model.notification.AtlasNotificationMessage in project atlas by apache.
the class AtlasNotificationMessageTest method testCompareVersion.
@Test
public void testCompareVersion() throws Exception {
MessageVersion version1 = new MessageVersion("1.0.0");
MessageVersion version2 = new MessageVersion("2.0.0");
MessageVersion version3 = new MessageVersion("0.5.0");
AtlasNotificationMessage<String> atlasNotificationMessage = new AtlasNotificationMessage<>(version1, "a");
assertTrue(atlasNotificationMessage.compareVersion(version1) == 0);
assertTrue(atlasNotificationMessage.compareVersion(version2) < 0);
assertTrue(atlasNotificationMessage.compareVersion(version3) > 0);
}
use of org.apache.atlas.model.notification.AtlasNotificationMessage in project atlas by apache.
the class AbstractNotification method createNotificationMessages.
/**
* Get the notification message JSON from the given object.
*
* @param message the message in object form
*
* @return the message as a JSON string
*/
public static void createNotificationMessages(Object message, List<String> msgJsonList) {
AtlasNotificationMessage<?> notificationMsg = new AtlasNotificationMessage<>(CURRENT_MESSAGE_VERSION, message, getHostAddress(), getCurrentUser());
String msgJson = AtlasType.toV1Json(notificationMsg);
boolean msgLengthExceedsLimit = (msgJson.length() * MAX_BYTES_PER_CHAR) > MESSAGE_MAX_LENGTH_BYTES;
if (msgLengthExceedsLimit) {
// get utf-8 bytes for msgJson and check for length limit again
byte[] msgBytes = AtlasNotificationBaseMessage.getBytesUtf8(msgJson);
msgLengthExceedsLimit = msgBytes.length > MESSAGE_MAX_LENGTH_BYTES;
if (msgLengthExceedsLimit) {
String msgId = getNextMessageId();
CompressionKind compressionKind = CompressionKind.NONE;
if (MESSAGE_COMPRESSION_ENABLED) {
byte[] encodedBytes = AtlasNotificationBaseMessage.gzipCompressAndEncodeBase64(msgBytes);
compressionKind = CompressionKind.GZIP;
LOG.info("Compressed large message: msgID={}, uncompressed={} bytes, compressed={} bytes", msgId, msgBytes.length, encodedBytes.length);
msgLengthExceedsLimit = encodedBytes.length > MESSAGE_MAX_LENGTH_BYTES;
if (!msgLengthExceedsLimit) {
// no need to split
AtlasNotificationStringMessage compressedMsg = new AtlasNotificationStringMessage(encodedBytes, msgId, compressionKind);
// msgJson will not have multi-byte characters here, due to use of encodeBase64() above
msgJson = AtlasType.toV1Json(compressedMsg);
// not used after this point
msgBytes = null;
} else {
// encodedBytes will be split
// not used after this point
msgJson = null;
msgBytes = encodedBytes;
}
}
if (msgLengthExceedsLimit) {
// compressed messages are already base64-encoded
byte[] encodedBytes = MESSAGE_COMPRESSION_ENABLED ? msgBytes : AtlasNotificationBaseMessage.encodeBase64(msgBytes);
int splitCount = encodedBytes.length / MESSAGE_MAX_LENGTH_BYTES;
if ((encodedBytes.length % MESSAGE_MAX_LENGTH_BYTES) != 0) {
splitCount++;
}
for (int i = 0, offset = 0; i < splitCount; i++) {
int length = MESSAGE_MAX_LENGTH_BYTES;
if ((offset + length) > encodedBytes.length) {
length = encodedBytes.length - offset;
}
AtlasNotificationStringMessage splitMsg = new AtlasNotificationStringMessage(encodedBytes, offset, length, msgId, compressionKind, i, splitCount);
String splitMsgJson = AtlasType.toV1Json(splitMsg);
msgJsonList.add(splitMsgJson);
offset += length;
}
LOG.info("Split large message: msgID={}, splitCount={}, length={} bytes", msgId, splitCount, encodedBytes.length);
}
}
}
if (!msgLengthExceedsLimit) {
msgJsonList.add(msgJson);
}
}
use of org.apache.atlas.model.notification.AtlasNotificationMessage in project atlas by apache.
the class AtlasNotificationMessageTest method testGetMessage.
@Test
public void testGetMessage() throws Exception {
String message = "a";
MessageVersion version = new MessageVersion("1.0.0");
AtlasNotificationMessage<String> atlasNotificationMessage = new AtlasNotificationMessage<>(version, message);
assertEquals(atlasNotificationMessage.getMessage(), message);
}
use of org.apache.atlas.model.notification.AtlasNotificationMessage in project atlas by apache.
the class AtlasNotificationMessageTest method testGetVersion.
@Test
public void testGetVersion() throws Exception {
MessageVersion version = new MessageVersion("1.0.0");
AtlasNotificationMessage<String> atlasNotificationMessage = new AtlasNotificationMessage<>(version, "a");
assertEquals(atlasNotificationMessage.getVersion(), version);
}
Aggregations