diff --git a/docs/services/iot.md b/docs/services/iot.md index de9c3b95ea..ec0da4f584 100644 --- a/docs/services/iot.md +++ b/docs/services/iot.md @@ -15,7 +15,7 @@ Supported MVP 1 behavior: - Tags for things, certificates, policies, and topic rules. - IoT Data retained messages: retained `Publish`, `GetRetainedMessage`, and paginated `ListRetainedMessages`. - Shadow null-delete and version-conflict behavior for HTTP and shared service paths. -- Topic rule duplicate/delete/replace semantics, plus `republish`, `sqs`, `sns`, `s3`, `dynamoDBv2`, `kinesis`, and `lambda` action dispatch. +- Topic rule duplicate/delete/replace semantics, plus `republish`, `sqs`, `sns`, `s3`, `dynamoDBv2`, `kinesis`, `lambda`, `firehose`, and `cloudwatchLogs` action dispatch. Current MVP 1 limitations: @@ -37,7 +37,7 @@ Supported MVP 2 behavior: - MQTT clients can use QoS 1 subscribe/publish paths with broker PUBACK and delivery behavior. - IoT Data connection APIs for live MQTT sessions: `GetConnection`, `DeleteConnection`, `ListSubscriptions`, and `SendDirectMessage`. - `DeleteConnection` closes active MQTT client sessions through the embedded broker and optionally purges broker session state for `cleanSession=true`. -- IoT rules can dispatch matching payloads to SQS, SNS, S3, DynamoDB v2, Kinesis, Lambda, and MQTT republish targets. +- IoT rules can dispatch matching payloads to SQS, SNS, S3, DynamoDB v2, Kinesis, Lambda, Kinesis Data Firehose, CloudWatch Logs, and MQTT republish targets. Current MVP 2 limitations: @@ -151,6 +151,8 @@ Supported rule behavior: - `dynamoDBv2` action writes JSON object payload fields as DynamoDB attribute values through Floci's DynamoDB service boundary; nested objects and arrays become maps and lists. - `kinesis` action puts the original payload into a Kinesis stream through Floci's Kinesis service boundary. - `lambda` action invokes the configured function ARN through Floci's Lambda service boundary. +- `firehose` action puts the original payload into a Kinesis Data Firehose delivery stream through Floci's Firehose service boundary, with `separator` appended to each record; the separator must be `\n`, `\t`, `\r\n` or `,`, as the API model requires, or the rule is rejected with `InvalidRequestException`. With `batchMode`, a JSON array payload becomes one record per element. +- `cloudwatchLogs` action writes the original payload as a log event through Floci's CloudWatch Logs service boundary, into a log stream named after the rule that is created in `logGroupName` on first use. The log group must exist. With `batchMode`, a JSON array payload becomes one event per element, and each element supplies its own `timestamp` (epoch milliseconds) and `message`, as the AWS message format for batched device logs requires; an element without them is logged as its JSON text at publish time. - One failing action never fails the publish or the other actions of the rule. The failure is logged, and once every action ran the rule's `errorAction` receives the AWS failure document: `ruleName`, `topic`, `base64OriginalPayload` and `failures` with `failedAction`, `failedResource` and `errorMessage` per failed action. - `GetTopicRule` returns `awsIotSqlVersion` and `errorAction` as they were given to `CreateTopicRule` or `ReplaceTopicRule`. diff --git a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java index d1ae8b156f..2d5d972ea3 100644 --- a/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java +++ b/src/main/java/io/github/hectorvent/floci/services/iot/IotService.java @@ -11,6 +11,7 @@ import io.github.hectorvent.floci.core.common.AwsArnUtils; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.StorageBackend; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; import io.github.hectorvent.floci.core.storage.StorageFactory; import io.github.hectorvent.floci.services.iot.model.IotCertificate; import io.github.hectorvent.floci.services.iot.model.IotJob; @@ -23,6 +24,8 @@ import io.github.hectorvent.floci.services.iot.model.IotThingType; import io.github.hectorvent.floci.services.iot.model.IotTopicRule; import io.github.hectorvent.floci.services.iot.model.Thing; import io.github.hectorvent.floci.services.dynamodb.DynamoDbService; +import io.github.hectorvent.floci.services.firehose.FirehoseService; +import io.github.hectorvent.floci.services.firehose.model.Record; import io.github.hectorvent.floci.services.kinesis.KinesisService; import io.github.hectorvent.floci.services.lambda.LambdaService; import io.github.hectorvent.floci.services.lambda.model.InvocationType; @@ -33,10 +36,12 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import org.jboss.logging.Logger; +import java.io.IOException; import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.ArrayList; +import java.util.Arrays; import java.util.Base64; import java.util.Comparator; import java.util.HashSet; @@ -58,6 +63,8 @@ public class IotService { static final String DEFAULT_ENDPOINT_TYPE = "iot:Data-ATS"; private static final Pattern THING_NAME_PATTERN = Pattern.compile("[a-zA-Z0-9:_-]{1,128}"); + /** The {@code FirehoseSeparator} shape of the IoT API model. */ + private static final Pattern FIREHOSE_SEPARATOR = Pattern.compile("([\\n\\t])|(\\r\\n)|(,)"); private final StorageBackend thingStore; private final StorageBackend certificateStore; @@ -83,6 +90,8 @@ public class IotService { private final KinesisService kinesisService; private final DynamoDbService dynamoDbService; private final LambdaService lambdaService; + private final FirehoseService firehoseService; + private final CloudWatchLogsService cloudWatchLogsService; @Inject public IotService(StorageFactory storageFactory, @@ -96,7 +105,9 @@ public class IotService { S3Service s3Service, KinesisService kinesisService, DynamoDbService dynamoDbService, - LambdaService lambdaService) { + LambdaService lambdaService, + FirehoseService firehoseService, + CloudWatchLogsService cloudWatchLogsService) { this(storageFactory.create("iot", "iot-things.json", new TypeReference>() {}), storageFactory.create("iot", "iot-certificates.json", new TypeReference>() {}), storageFactory.create("iot", "iot-policies.json", new TypeReference>() {}), @@ -111,7 +122,7 @@ public class IotService { storageFactory.create("iot", "iot-thing-groups.json", new TypeReference>() {}), storageFactory.create("iot", "iot-thing-group-memberships.json", new TypeReference>>() {}), config, regionResolver, objectMapper, publishEventRecorder, mqttBrokerService, sqsService, snsService, - s3Service, kinesisService, dynamoDbService, lambdaService); + s3Service, kinesisService, dynamoDbService, lambdaService, firehoseService, cloudWatchLogsService); } IotService(StorageBackend thingStore, @@ -137,7 +148,9 @@ public class IotService { S3Service s3Service, KinesisService kinesisService, DynamoDbService dynamoDbService, - LambdaService lambdaService) { + LambdaService lambdaService, + FirehoseService firehoseService, + CloudWatchLogsService cloudWatchLogsService) { this.thingStore = thingStore; this.certificateStore = certificateStore; this.policyStore = policyStore; @@ -162,6 +175,8 @@ public class IotService { this.kinesisService = kinesisService; this.dynamoDbService = dynamoDbService; this.lambdaService = lambdaService; + this.firehoseService = firehoseService; + this.cloudWatchLogsService = cloudWatchLogsService; } public String describeEndpoint(String endpointType) { @@ -867,15 +882,29 @@ public class IotService { rule.setDescription(payload.path("description").asText(null)); rule.setRuleDisabled(payload.path("ruleDisabled").asBoolean(false)); JsonNode actions = payload.path("actions"); + actions.forEach(IotService::validateAction); rule.setActionsJson(actions.isArray() ? actions.toString() : "[]"); rule.setAwsIotSqlVersion(payload.path("awsIotSqlVersion").asText(null)); JsonNode errorAction = payload.path("errorAction"); + validateAction(errorAction); rule.setErrorActionJson(errorAction.isObject() ? errorAction.toString() : null); rule.setCreatedAt(createdAt == null ? Instant.now() : createdAt); topicRuleStore.put(topicRuleKey(region, ruleName), rule); return rule; } + /** The API model restricts the firehose {@code separator} to a newline, tab, Windows newline or comma. */ + private static void validateAction(JsonNode action) { + JsonNode separator = action.path("firehose").path("separator"); + if (separator.isMissingNode() || separator.isNull()) { + return; + } + if (!FIREHOSE_SEPARATOR.matcher(separator.asText()).matches()) { + throw new AwsException("InvalidRequestException", + "Invalid firehose separator. Valid values are: '\\n' (newline), '\\t' (tab), '\\r\\n' (Windows newline), ',' (comma)", 400); + } + } + public IotTopicRule getTopicRule(String ruleName, String region) { return topicRuleStore.get(topicRuleKey(region, ruleName)) .orElseThrow(() -> new AwsException("ResourceNotFoundException", "Topic rule not found: " + ruleName, 404)); @@ -1025,7 +1054,7 @@ public class IotService { } JsonNode config = action.get(type); try { - runAction(type, config, payload, ruleRegion); + runAction(rule, type, config, payload, ruleRegion); } catch (RuntimeException e) { LOG.warnv(e, "Action {0} of topic rule {1} failed", type, rule.getRuleName()); failures.add(failure(type, config, e)); @@ -1041,7 +1070,7 @@ public class IotService { return; } try { - runAction(type, errorAction.get(type), failureDocument(rule, topic, payload, failures), ruleRegion); + runAction(rule, type, errorAction.get(type), failureDocument(rule, topic, payload, failures), ruleRegion); } catch (RuntimeException e) { LOG.warnv(e, "Error action {0} of topic rule {1} failed", type, rule.getRuleName()); } @@ -1071,7 +1100,7 @@ public class IotService { return null; } - private void runAction(String type, JsonNode action, byte[] payload, String region) { + private void runAction(IotTopicRule rule, String type, JsonNode action, byte[] payload, String region) { switch (type) { case "republish" -> { String targetTopic = action.path("topic").asText(null); @@ -1121,6 +1150,18 @@ public class IotService { lambdaService.invoke(region, functionArn, payload, InvocationType.Event); } } + case "firehose" -> { + String deliveryStreamName = action.path("deliveryStreamName").asText(null); + if (deliveryStreamName != null && !deliveryStreamName.isBlank()) { + deliverToFirehose(deliveryStreamName, action, payload); + } + } + case "cloudwatchLogs" -> { + String logGroupName = action.path("logGroupName").asText(null); + if (logGroupName != null && !logGroupName.isBlank()) { + putLogEvents(logGroupName, rule.getRuleName(), action, payload, region); + } + } default -> LOG.debugv("Topic rule action {0} is not supported and was skipped", type); } } @@ -1143,6 +1184,8 @@ public class IotService { case "kinesis" -> action.path("streamName").asText(""); case "dynamoDBv2" -> action.path("putItem").path("tableName").asText(""); case "lambda" -> action.path("functionArn").asText(""); + case "firehose" -> action.path("deliveryStreamName").asText(""); + case "cloudwatchLogs" -> action.path("logGroupName").asText(""); default -> ""; }; } @@ -1157,6 +1200,90 @@ public class IotService { return document.toString().getBytes(StandardCharsets.UTF_8); } + /** + * The firehose action appends {@code separator} to every record. With {@code batchMode} a JSON + * array payload becomes one record per element, as the rule's SQL result would on AWS; any other + * payload is one record either way. + */ + private void deliverToFirehose(String deliveryStreamName, JsonNode action, byte[] payload) { + byte[] separator = action.path("separator").asText("").getBytes(StandardCharsets.UTF_8); + JsonNode batch = action.path("batchMode").asBoolean(false) ? jsonArrayOrNull(payload) : null; + if (batch == null) { + firehoseService.putRecord(deliveryStreamName, new Record(withSeparator(payload, separator))); + return; + } + List records = new ArrayList<>(); + for (JsonNode element : batch) { + records.add(new Record(withSeparator(element.toString().getBytes(StandardCharsets.UTF_8), separator))); + } + if (!records.isEmpty()) { + firehoseService.putRecordBatch(deliveryStreamName, records); + } + } + + /** + * The cloudwatchLogs action writes the payload as one log event to a stream named after the + * rule, creating the stream in the given group on first use. The group itself must exist, as + * on AWS. With {@code batchMode} a JSON array payload becomes one event per element, each + * element carrying its own {@code timestamp} and {@code message} as the AWS message format + * for batched device logs requires. + */ + private void putLogEvents(String logGroupName, String logStreamName, JsonNode action, byte[] payload, String region) { + JsonNode batch = action.path("batchMode").asBoolean(false) ? jsonArrayOrNull(payload) : null; + long now = Instant.now().toEpochMilli(); + List> events = new ArrayList<>(); + if (batch == null) { + events.add(Map.of("timestamp", now, "message", new String(payload, StandardCharsets.UTF_8))); + } else { + batch.forEach(element -> events.add(batchedLogEvent(element, now))); + } + if (events.isEmpty()) { + return; + } + try { + cloudWatchLogsService.createLogStream(logGroupName, logStreamName, region); + } catch (AwsException e) { + if (!"ResourceAlreadyExistsException".equals(e.getErrorCode())) { + throw e; + } + LOG.debugv("Log stream {0}/{1} already exists", logGroupName, logStreamName); + } + cloudWatchLogsService.putLogEvents(logGroupName, logStreamName, events, region); + } + + /** + * One batched element is {@code {"timestamp": , "message": ""}}. An + * element without a numeric timestamp is stamped with the publish time and one without a + * message is logged as its own JSON text, so a batch that ignores the format still reaches + * the log group instead of failing the action. + */ + private static Map batchedLogEvent(JsonNode element, long now) { + JsonNode timestamp = element.path("timestamp"); + JsonNode message = element.path("message"); + String text = message.isMissingNode() ? element.toString() + : message.isValueNode() ? message.asText() : message.toString(); + return Map.of("timestamp", timestamp.isNumber() ? timestamp.asLong() : now, "message", text); + } + + private JsonNode jsonArrayOrNull(byte[] payload) { + try { + JsonNode node = objectMapper.readTree(payload); + return node != null && node.isArray() ? node : null; + } catch (IOException e) { + LOG.debugv("Payload is not JSON and is delivered as one record: {0}", e.getMessage()); + return null; + } + } + + private static byte[] withSeparator(byte[] data, byte[] separator) { + if (separator.length == 0) { + return data; + } + byte[] record = Arrays.copyOf(data, data.length + separator.length); + System.arraycopy(separator, 0, record, data.length, separator.length); + return record; + } + private ObjectNode toDynamoDbItem(byte[] payload) { try { JsonNode document = objectMapper.readTree(payload); diff --git a/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceTest.java b/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceTest.java index 86f776fa38..073f0a8dde 100644 --- a/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceTest.java +++ b/src/test/java/io/github/hectorvent/floci/services/iot/IotServiceTest.java @@ -7,7 +7,10 @@ import io.github.hectorvent.floci.config.EmulatorConfig; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.RegionResolver; import io.github.hectorvent.floci.core.storage.AccountAwareStorageBackend; +import io.github.hectorvent.floci.services.cloudwatch.logs.CloudWatchLogsService; import io.github.hectorvent.floci.services.dynamodb.DynamoDbService; +import io.github.hectorvent.floci.services.firehose.FirehoseService; +import io.github.hectorvent.floci.services.firehose.model.Record; import io.github.hectorvent.floci.services.iot.model.IotTopicRule; import io.github.hectorvent.floci.services.kinesis.KinesisService; import io.github.hectorvent.floci.services.lambda.LambdaService; @@ -21,15 +24,19 @@ import org.mockito.ArgumentCaptor; import java.nio.charset.StandardCharsets; import java.util.Base64; +import java.util.List; +import java.util.Map; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -38,8 +45,8 @@ import static org.mockito.Mockito.when; /** * Rules engine behaviour of {@link IotService} with in-memory stores and mocked action targets: - * one failing action never fails the publish or the other actions, and the error action receives - * the failure document. + * one failing action never fails the publish or the other actions, the error action receives the + * failure document, and the {@code firehose} and {@code cloudwatchLogs} actions deliver the payload. */ class IotServiceTest { @@ -54,6 +61,8 @@ class IotServiceTest { private final SqsService sqs = mock(SqsService.class); private final LambdaService lambda = mock(LambdaService.class); private final DynamoDbService dynamoDb = mock(DynamoDbService.class); + private final FirehoseService firehose = mock(FirehoseService.class); + private final CloudWatchLogsService logs = mock(CloudWatchLogsService.class); private IotService service; @BeforeEach @@ -84,7 +93,9 @@ class IotServiceTest { mock(S3Service.class), mock(KinesisService.class), dynamoDb, - lambda); + lambda, + firehose, + logs); } private IotTopicRule createRule(String name, String payloadJson) throws Exception { @@ -199,6 +210,194 @@ class IotServiceTest { assertEquals("metrics", failures.get(1).get("failedResource").asText()); } + private static String firehoseRule(String separator, boolean batchMode) { + String separatorMember = separator == null ? "" : ", \"separator\": \"" + separator + "\""; + return """ + { + "sql": "SELECT * FROM 'devices/+/metrics'", + "actions": [{"firehose": {"deliveryStreamName": "metrics", "roleArn": "arn:aws:iam::000000000000:role/rule"%s, "batchMode": %s}}] + } + """.formatted(separatorMember, batchMode); + } + + private static String text(Record record) { + return new String(record.getData(), StandardCharsets.UTF_8); + } + + @Test + void firehoseActionPutsThePayloadWithTheSeparatorAppended() throws Exception { + createRule("metricsRule", firehoseRule("\\n", false)); + + publish("{\"v\":1}"); + + ArgumentCaptor record = ArgumentCaptor.forClass(Record.class); + verify(firehose).putRecord(eq("metrics"), record.capture()); + assertEquals("{\"v\":1}\n", text(record.getValue())); + } + + @Test + void firehoseActionWithoutASeparatorPutsThePayloadAsIs() throws Exception { + createRule("metricsRule", firehoseRule(null, false)); + + publish("{\"v\":1}"); + + ArgumentCaptor record = ArgumentCaptor.forClass(Record.class); + verify(firehose).putRecord(eq("metrics"), record.capture()); + assertEquals("{\"v\":1}", text(record.getValue())); + } + + @Test + void firehoseActionInBatchModeDeliversEachElementOfAJsonArrayAsOneRecord() throws Exception { + createRule("metricsRule", firehoseRule("\\n", true)); + + publish("[{\"v\":1},{\"v\":2}]"); + + @SuppressWarnings("unchecked") + ArgumentCaptor> records = ArgumentCaptor.forClass(List.class); + verify(firehose).putRecordBatch(eq("metrics"), records.capture()); + assertEquals(List.of("{\"v\":1}\n", "{\"v\":2}\n"), records.getValue().stream().map(IotServiceTest::text).toList()); + verify(firehose, never()).putRecord(anyString(), any()); + } + + @Test + void firehoseActionInBatchModeDeliversAnythingElseAsOneRecord() throws Exception { + createRule("metricsRule", firehoseRule(null, true)); + + publish("plain text"); + + ArgumentCaptor record = ArgumentCaptor.forClass(Record.class); + verify(firehose).putRecord(eq("metrics"), record.capture()); + assertEquals("plain text", text(record.getValue())); + } + + @Test + void firehoseActionAcceptsEverySeparatorTheApiAllows() throws Exception { + List separators = List.of("\\n", "\\t", "\\r\\n", ","); + for (int i = 0; i < separators.size(); i++) { + createRule("rule" + i, firehoseRule(separators.get(i), false)); + } + } + + @Test + void firehoseActionRejectsAnySeparatorTheApiDoesNotAllow() { + for (String separator : List.of("|", ";", "", " ", "\\n\\n")) { + AwsException e = assertThrows(AwsException.class, () -> createRule("metricsRule", firehoseRule(separator, false))); + assertEquals("InvalidRequestException", e.getErrorCode(), separator); + assertEquals(400, e.getHttpStatus()); + } + assertThrows(AwsException.class, () -> createRule("metricsRule", """ + {"sql": "SELECT * FROM 'devices/+/metrics'", "actions": [], + "errorAction": {"firehose": {"deliveryStreamName": "errors", "roleArn": "arn:aws:iam::000000000000:role/rule", "separator": "|"}}} + """)); + } + + @Test + void firehoseActionFailureIsReportedWithTheDeliveryStreamName() throws Exception { + createRule("metricsRule", """ + {"sql": "SELECT * FROM 'devices/+/metrics'", + "actions": [{"firehose": {"deliveryStreamName": "metrics", "roleArn": "arn:aws:iam::000000000000:role/rule"}}], + "errorAction": %s} + """.formatted(lambdaErrorAction())); + doThrow(new AwsException("ResourceNotFoundException", "Delivery stream not found: metrics", 400)) + .when(firehose).putRecord(eq("metrics"), any()); + + publish("{\"v\":1}"); + + JsonNode failure = capturedInvocationPayload(ERROR_FUNCTION_ARN).get("failures").get(0); + assertEquals("FirehoseAction", failure.get("failedAction").asText()); + assertEquals("metrics", failure.get("failedResource").asText()); + } + + private static String cloudwatchLogsRule(boolean batchMode) { + return """ + { + "sql": "SELECT * FROM 'devices/+/metrics'", + "actions": [{"cloudwatchLogs": {"logGroupName": "/iot/metrics", "roleArn": "arn:aws:iam::000000000000:role/rule", "batchMode": %s}}] + } + """.formatted(batchMode); + } + + private List> capturedLogEvents() { + @SuppressWarnings("unchecked") + ArgumentCaptor>> events = ArgumentCaptor.forClass(List.class); + verify(logs).putLogEvents(eq("/iot/metrics"), eq("metricsRule"), events.capture(), eq(REGION)); + return events.getValue(); + } + + @Test + void cloudwatchLogsActionWritesThePayloadToAStreamNamedAfterTheRule() throws Exception { + createRule("metricsRule", cloudwatchLogsRule(false)); + + publish("{\"v\":1}"); + + verify(logs).createLogStream("/iot/metrics", "metricsRule", REGION); + List> events = capturedLogEvents(); + assertEquals(1, events.size()); + assertEquals("{\"v\":1}", events.get(0).get("message")); + assertTrue(events.get(0).get("timestamp") instanceof Long); + } + + @Test + void cloudwatchLogsActionReusesAnExistingStream() throws Exception { + createRule("metricsRule", cloudwatchLogsRule(false)); + doThrow(new AwsException("ResourceAlreadyExistsException", "The specified log stream already exists", 400)) + .when(logs).createLogStream("/iot/metrics", "metricsRule", REGION); + + assertDoesNotThrow(() -> publish("{\"v\":1}")); + + assertEquals("{\"v\":1}", capturedLogEvents().get(0).get("message")); + } + + @Test + void cloudwatchLogsActionInBatchModeTakesTimestampAndMessageFromEachArrayElement() throws Exception { + createRule("metricsRule", cloudwatchLogsRule(true)); + + publish(""" + [ + {"timestamp": 1673520691093, "message": "Test message 1"}, + {"timestamp": 1673520692879, "message": "Test message 2"}, + {"timestamp": 1673520693442, "message": "Test message 3"} + ] + """); + + List> events = capturedLogEvents(); + assertEquals(List.of("Test message 1", "Test message 2", "Test message 3"), + events.stream().map(event -> event.get("message")).toList()); + assertEquals(List.of(1673520691093L, 1673520692879L, 1673520693442L), + events.stream().map(event -> event.get("timestamp")).toList()); + } + + @Test + void cloudwatchLogsActionInBatchModeFallsBackToThePublishTimeAndTheElementText() throws Exception { + createRule("metricsRule", cloudwatchLogsRule(true)); + long before = System.currentTimeMillis(); + + publish("[{\"v\":1}, {\"timestamp\": \"1673520691093\", \"message\": {\"nested\": true}}, \"plain\"]"); + + List> events = capturedLogEvents(); + assertEquals(List.of("{\"v\":1}", "{\"nested\":true}", "\"plain\""), + events.stream().map(event -> event.get("message")).toList()); + events.forEach(event -> assertTrue((Long) event.get("timestamp") >= before)); + } + + @Test + void cloudwatchLogsActionFailsWhenTheLogGroupDoesNotExist() throws Exception { + createRule("metricsRule", """ + {"sql": "SELECT * FROM 'devices/+/metrics'", + "actions": [{"cloudwatchLogs": {"logGroupName": "/iot/missing", "roleArn": "arn:aws:iam::000000000000:role/rule"}}], + "errorAction": %s} + """.formatted(lambdaErrorAction())); + doThrow(new AwsException("ResourceNotFoundException", "The specified log group does not exist: /iot/missing", 400)) + .when(logs).createLogStream("/iot/missing", "metricsRule", REGION); + + assertDoesNotThrow(() -> publish("{\"v\":1}")); + + JsonNode failure = capturedInvocationPayload(ERROR_FUNCTION_ARN).get("failures").get(0); + assertEquals("CloudwatchLogsAction", failure.get("failedAction").asText()); + assertEquals("/iot/missing", failure.get("failedResource").asText()); + verify(logs, never()).putLogEvents(anyString(), anyString(), any(), anyString()); + } + @Test void topicRuleKeepsTheSqlVersionAndTheErrorAction() throws Exception { createRule("versionedRule", """