Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New ScheduledV2Event implementation #480

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions aws-lambda-java-events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
* `S3BatchResponse`
* `S3Event`
* `ScheduledEvent`
* `ScheduledV2Event`
* `SecretsManagerRotationEvent`
* `SimpleIAMPolicyResponse`
* `SNSEvent`
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please update the header to

/*
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package com.amazonaws.services.lambda.runtime.events;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.joda.time.DateTime;

import java.io.Serializable;
import java.util.List;

/**
* Represents a Scheduled V2 event sent to Lambda
* <a href="https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-function-schedulev2.html">ScheduleV2</a>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a link to the wrong page.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one should be more valid. 😉

*/
@Data
@Builder(setterPrefix = "with")
@NoArgsConstructor
@AllArgsConstructor
public class ScheduledV2Event implements Serializable, Cloneable {
msailes marked this conversation as resolved.
Show resolved Hide resolved

private static final long serialVersionUID = -463442139623175611L;

private String version;

private String account;

private String region;

private String detail;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we (AWS) needs to validate that there will be no events where detail will be anything other than a String for scheduled task use cases.


private String detailType;

private String source;

private String id;

private DateTime time;

private List<String> resources;

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.amazonaws.services.lambda.runtime.events;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Locale;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand All @@ -12,6 +14,11 @@ public class APIGatewayV2CustomAuthorizerEventTest {
private static final long TIME_EPOCH = 1601306426515L;
private static final String TIME = "28/Sep/2020:15:14:43 +0000";

@BeforeAll
static void beforeAll() {
Locale.setDefault(new Locale("en", "US"));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for this

}

@Test
public void testEpochLongAsAnInstant() {
APIGatewayV2CustomAuthorizerEvent customAuthorizerEvent = APIGatewayV2CustomAuthorizerEvent.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ public class LambdaEventSerializers {
KinesisTimeWindowEventMixin.class),
new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.ScheduledEvent",
ScheduledEventMixin.class),
new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.ScheduledV2Event",
ScheduledEventMixin.class),
new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.SecretsManagerRotationEvent",
SecretsManagerRotationEventMixin.class),
new SimpleEntry<>("com.amazonaws.services.lambda.runtime.events.SNSEvent",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
/* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
package com.amazonaws.services.lambda.runtime.tests;

import com.amazonaws.services.lambda.runtime.events.*;
import com.amazonaws.services.lambda.runtime.serialization.PojoSerializer;
import com.amazonaws.services.lambda.runtime.serialization.events.LambdaEventSerializers;

import java.io.*;
peniakoff marked this conversation as resolved.
Show resolved Hide resolved

import com.amazonaws.services.lambda.runtime.events.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
* Load events from json files and serialize them in Events
Expand Down Expand Up @@ -101,6 +103,10 @@ public static ScheduledEvent loadScheduledEvent(String filename) {
return loadEvent(filename, ScheduledEvent.class);
}

public static ScheduledV2Event loadScheduledV2Event(String filename) {
return loadEvent(filename, ScheduledV2Event.class);
}

public static SNSEvent loadSNSEvent(String filename) {
return loadEvent(filename, SNSEvent.class);
}
Expand Down Expand Up @@ -131,7 +137,7 @@ public static <T> T loadEvent(String filename, Class<T> targetClass) {
}
if (stream == null) {
try {
stream = new FileInputStream(new File(filename));
stream = new FileInputStream(filename);
} catch (FileNotFoundException e) {
throw new EventLoadingException("Cannot load " + filename, e);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
/* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved. */
package com.amazonaws.services.lambda.runtime.tests;

import com.amazonaws.services.lambda.runtime.events.*;
import com.amazonaws.services.lambda.runtime.events.models.dynamodb.AttributeValue;
import com.amazonaws.services.lambda.runtime.events.models.dynamodb.Record;
import com.amazonaws.services.lambda.runtime.events.models.dynamodb.StreamRecord;
import org.joda.time.DateTime;
import org.junit.jupiter.api.Test;

import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.*;

import static java.time.Instant.ofEpochSecond;
import static org.assertj.core.api.Assertions.*;

import com.amazonaws.services.lambda.runtime.events.*;

public class EventLoaderTest {
Expand Down Expand Up @@ -363,6 +361,23 @@ public void testLoadRabbitMQEvent() {
assertThat((Integer) headers.get("numberInHeader")).isEqualTo(10);
}

@Test
public void testLoadScheduledV2Event() {
msailes marked this conversation as resolved.
Show resolved Hide resolved
ScheduledV2Event event = EventLoader.loadScheduledV2Event("scheduler_event.json");

assertThat(event).isNotNull();
assertThat(event.getVersion()).isEqualTo("0");
assertThat(event.getId()).isEqualTo("4e6638b7-b892-4482-9762-8c58d4e71ecc");
assertThat(event.getDetailType()).isEqualTo("Scheduled Event");
assertThat(event.getSource()).isEqualTo("aws.scheduler");
assertThat(event.getAccount()).isEqualTo("123456789012");
assertThat(event.getTime()).isEqualTo(DateTime.parse("2024-05-07T15:58:34Z"));
assertThat(event.getRegion()).isEqualTo("eu-central-1");
assertThat(event.getResources()).isNotEmpty();
assertThat(event.getResources()).isEqualTo(Collections.singletonList("arn:aws:scheduler:eu-central-1:123456789012:schedule/default/demoschedule"));
assertThat(event.getDetail()).isEqualTo("{}");
}

@Test
public void testLoadCognitoUserPoolPreTokenGenerationEventV2() {
CognitoUserPoolPreTokenGenerationEventV2 event = EventLoader.loadCognitoUserPoolPreTokenGenerationEventV2("cognito_user_pool_pre_token_generation_event_v2.json");
Expand Down
13 changes: 13 additions & 0 deletions aws-lambda-java-tests/src/test/resources/scheduler_event.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "0",
"id": "4e6638b7-b892-4482-9762-8c58d4e71ecc",
"detail-type": "Scheduled Event",
"source": "aws.scheduler",
"account": "123456789012",
"time": "2024-05-07T15:58:34Z",
"region": "eu-central-1",
"resources": [
"arn:aws:scheduler:eu-central-1:123456789012:schedule/default/demoschedule"
],
"detail": "{}"
}
Loading