Skip to content

Commit

Permalink
Merge pull request quarkusio#9502 from patriot1burke/lambda-clientcon…
Browse files Browse the repository at this point in the history
…text

Read Lambda ClientContext correctly, Issue quarkusio#9395
  • Loading branch information
geoand authored May 21, 2020
2 parents 3d44c8d + 259758b commit 6141a5a
Show file tree
Hide file tree
Showing 7 changed files with 229 additions and 4 deletions.
5 changes: 5 additions & 0 deletions extensions/amazon-lambda/common-runtime/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import org.jboss.logging.Logger;

import com.amazonaws.services.lambda.runtime.ClientContext;
import com.amazonaws.services.lambda.runtime.CognitoIdentity;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
Expand Down Expand Up @@ -30,7 +29,7 @@ public void initObjectMapper() {

public void initContextReaders() {
cognitoIdReader = objectMapper.readerFor(CognitoIdentity.class);
clientCtxReader = objectMapper.readerFor(ClientContext.class);
clientCtxReader = objectMapper.readerFor(ClientContextImpl.class);

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.quarkus.amazon.lambda.runtime;

import java.util.Map;

import com.amazonaws.services.lambda.runtime.Client;
import com.amazonaws.services.lambda.runtime.ClientContext;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;

public class ClientContextImpl implements ClientContext {
private ClientImpl impl;
private Map<String, String> cust;
private Map<String, String> env;

@JsonGetter("client")
public ClientImpl getImpl() {
return impl;
}

@JsonSetter("client")
public void setImpl(ClientImpl impl) {
this.impl = impl;
}

@JsonGetter("custom")
public Map<String, String> getCust() {
return cust;
}

@JsonSetter("custom")
public void setCust(Map<String, String> cust) {
this.cust = cust;
}

@JsonGetter("env")
public Map<String, String> getEnv() {
return env;
}

@JsonSetter("env")
public void setEnv(Map<String, String> env) {
this.env = env;
}

@Override
@JsonIgnore
public Client getClient() {
return impl;
}

@Override
@JsonIgnore
public Map<String, String> getCustom() {
return cust;
}

@Override
@JsonIgnore
public Map<String, String> getEnvironment() {
return env;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package io.quarkus.amazon.lambda.runtime;

import com.amazonaws.services.lambda.runtime.Client;
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSetter;

public class ClientImpl implements Client {
private String id;
private String title;
private String versionName;
private String versionCode;
private String packageName;

@JsonGetter("client_id")
public String getId() {
return id;
}

@JsonSetter("client_id")
public void setId(String id) {
this.id = id;
}

@JsonGetter("app_title")
public String getTitle() {
return title;
}

@JsonSetter("app_title")
public void setTitle(String title) {
this.title = title;
}

@JsonGetter("app_version_name")
public String getVersionName() {
return versionName;
}

@JsonSetter("app_version_name")
public void setVersionName(String versionName) {
this.versionName = versionName;
}

@JsonGetter("app_version_code")
public String getVersionCode() {
return versionCode;
}

@JsonSetter("app_version_code")
public void setVersionCode(String versionCode) {
this.versionCode = versionCode;
}

@JsonGetter("app_package_name")
public String getPackageName() {
return packageName;
}

@JsonSetter("app_package_name")
public void setPackageName(String packageName) {
this.packageName = packageName;
}

@Override
@JsonIgnore
public String getInstallationId() {
return id;
}

@Override
@JsonIgnore
public String getAppTitle() {
return title;
}

@Override
@JsonIgnore
public String getAppVersionName() {
return versionName;
}

@Override
@JsonIgnore
public String getAppVersionCode() {
return versionCode;
}

@Override
@JsonIgnore
public String getAppPackageName() {
return packageName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package io.quarkus.amazon.lambda.test;

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

import com.amazonaws.services.lambda.runtime.ClientContext;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;

import io.quarkus.amazon.lambda.runtime.ClientContextImpl;

public class ClientContextTest {

static final String ctx = "{\n" +
" \"client\": {\n" +
" \"client_id\":\"<client_id>\",\n" +
" \"app_title\":\"<app_title>\",\n" +
" \"app_version_name\":\"<app_version_name>\",\n" +
" \"app_version_code\":\"<app_version_code>\",\n" +
" \"app_package_name\":\"<app_package_name>\"\n" +
" },\n" +
" \n" +
" \"custom\": { \"hello\": \"world\"},\n" +
" \n" +
" \"env\":{\n" +
" \"platform\":\"<platform>\",\n" +
" \"model\":\"<model>\",\n" +
" \"make\":\"<make>\",\n" +
" \"platform_version\":\"<platform_version>\",\n" +
" \"locale\":\"<locale>\"\n" +
" },\n" +
"\n" +
" \"services\": { \n" +
" \"mobile_analytics\": {\n" +
" \"app_id\":\"<mobile_analytics_app_id>\"\n" +
" }\n" +
" }\n" +
" }";

@Test
public void testContextMarshalling() throws Exception {
ObjectMapper mapper = new ObjectMapper().configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

ObjectReader reader = mapper.readerFor(ClientContextImpl.class);

ClientContext clientContext = reader.readValue(ctx);
Assertions.assertNotNull(clientContext.getClient());
Assertions.assertNotNull(clientContext.getCustom());
Assertions.assertNotNull(clientContext.getEnvironment());

Assertions.assertEquals("<client_id>", clientContext.getClient().getInstallationId());
Assertions.assertEquals("<app_title>", clientContext.getClient().getAppTitle());
Assertions.assertEquals("<app_version_name>", clientContext.getClient().getAppVersionName());
Assertions.assertEquals("<app_version_code>", clientContext.getClient().getAppVersionCode());
Assertions.assertEquals("<app_package_name>", clientContext.getClient().getAppPackageName());

Assertions.assertEquals("world", clientContext.getCustom().get("hello"));
Assertions.assertEquals("<platform>", clientContext.getEnvironment().get("platform"));

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void chooseHandlerClass(List<Class<? extends RequestHandler<?, ?>>> uname
@SuppressWarnings("rawtypes")
public void startPollLoop(ShutdownContext context) {
AbstractLambdaPollLoop loop = new AbstractLambdaPollLoop(AmazonLambdaMapperRecorder.objectMapper,
AmazonLambdaMapperRecorder.cognitoIdReader, AmazonLambdaMapperRecorder.cognitoIdReader) {
AmazonLambdaMapperRecorder.cognitoIdReader, AmazonLambdaMapperRecorder.clientCtxReader) {

@Override
protected Object processRequest(Object input, AmazonLambdaContext context) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static void handle(InputStream inputStream, OutputStream outputStream, Co
@SuppressWarnings("rawtypes")
public void startPollLoop(ShutdownContext context) {
AbstractLambdaPollLoop loop = new AbstractLambdaPollLoop(AmazonLambdaMapperRecorder.objectMapper,
AmazonLambdaMapperRecorder.cognitoIdReader, AmazonLambdaMapperRecorder.cognitoIdReader) {
AmazonLambdaMapperRecorder.cognitoIdReader, AmazonLambdaMapperRecorder.clientCtxReader) {

@Override
protected Object processRequest(Object input, AmazonLambdaContext context) throws Exception {
Expand Down

0 comments on commit 6141a5a

Please sign in to comment.