Skip to content

Commit

Permalink
Fix lambda applicationContexts
Browse files Browse the repository at this point in the history
  • Loading branch information
blacelle committed Dec 31, 2021
1 parent 17de7a1 commit 65a5ed2
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
* @author Benoit Lacelle
*/
public class GithubWebhookHandlerFactory implements IGitWebhookHandlerFactory {
// private static final Logger LOGGER = LoggerFactory.getLogger(GithubWebhookHandlerFactory.class);

public static final String ENV_GITHUB_APP_PRIVATE_JWK = "github.app.private-jwk";

// public static final String GITHUB_APP_PRIVATE_JWK_FORUNITTESTS = "forUnitTests";

// https://github.com/organizations/solven-eu/settings/apps/cleanthat
// https://github.com/apps/cleanthat
Expand Down Expand Up @@ -63,7 +68,14 @@ public IGithubWebhookHandler makeWithFreshAuth() throws IOException {

// https://connect2id.com/products/nimbus-jose-jwt/examples/jwt-with-rsa-signature
public String makeJWT() throws JOSEException {
String rawJwk = env.getRequiredProperty("github.app.private-jwk");
String rawJwk = env.getRequiredProperty(ENV_GITHUB_APP_PRIVATE_JWK);

// if (rawJwk.equals(GithubWebhookHandlerFactory.GITHUB_APP_PRIVATE_JWK_FORUNITTESTS)
// && GCInspector.inUnitTest()) {
// LOGGER.info("We are in a unit-test");
// return GithubWebhookHandlerFactory.GITHUB_APP_PRIVATE_JWK_FORUNITTESTS;
// }

RSAKey rsaJWK;
try {
rsaJWK = RSAKey.parse(rawJwk);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@

import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;

import eu.solven.cleanthat.code_provider.github.event.GithubWebhookHandlerFactory;
import eu.solven.cleanthat.code_provider.github.event.IGithubWebhookHandler;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandler;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandlerFactory;
import eu.solven.cleanthat.code_provider.github.event.pojo.CleanThatWebhookEvent;
import eu.solven.cleanthat.code_provider.github.event.pojo.GithubWebhookEvent;
import eu.solven.cleanthat.codeprovider.git.GitWebhookRelevancyResult;
Expand All @@ -37,10 +37,10 @@ public static void main(String[] args) {

@Override
protected Map<String, ?> unsafeProcessOneEvent(IWebhookEvent input) {
GithubWebhookHandlerFactory githubFactory = getAppContext().getBean(GithubWebhookHandlerFactory.class);
IGitWebhookHandlerFactory githubFactory = getAppContext().getBean(IGitWebhookHandlerFactory.class);

// TODO Cache the Github instance for the JWT duration
IGithubWebhookHandler makeWithFreshJwt;
IGitWebhookHandler makeWithFreshJwt;
try {
makeWithFreshJwt = githubFactory.makeWithFreshAuth();
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.amazonaws.services.dynamodbv2.AmazonDynamoDB;
import com.fasterxml.jackson.databind.ObjectMapper;

import eu.solven.cleanthat.code_provider.github.event.CompositeCodeCleanerFactory;
import eu.solven.cleanthat.code_provider.github.event.ICodeCleanerFactory;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandler;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandlerFactory;
Expand Down Expand Up @@ -39,7 +40,7 @@ public AmazonDynamoDB makeDynamoDbClient() {
protected Map<String, ?> unsafeProcessOneEvent(IWebhookEvent input) {
IGitWebhookHandler makeWithFreshJwt = extracted(getAppContext());

ICodeCleanerFactory cleanerFactory = getAppContext().getBean(ICodeCleanerFactory.class);
ICodeCleanerFactory cleanerFactory = getAppContext().getBean(CompositeCodeCleanerFactory.class);

WebhookRelevancyResult processAnswer =
makeWithFreshJwt.filterWebhookEventTargetRelevantBranch(cleanerFactory, input);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package eu.solven.cleanthat.lambda.step0_checkwebhook;

import java.util.Map;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.google.common.collect.ImmutableMap;

import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandler;
import eu.solven.cleanthat.code_provider.github.event.pojo.GithubWebhookEvent;
import eu.solven.cleanthat.codeprovider.git.GitWebhookRelevancyResult;
import eu.solven.cleanthat.lambda.step1_checkconfiguration.TestCheckConfigWebhooksLambdaFunctionAutonomy;

@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { CheckWebhooksLambdaFunction.class,
TestCheckConfigWebhooksLambdaFunctionAutonomy.CheckConfigWebhooksLambdaFunctionComplement.class })
public class TestCheckWebhooksLambdaFunctionAutonomy {

@Autowired
ApplicationContext appContext;

@Autowired
CheckWebhooksLambdaFunction function;

@Test
public void testAppRun() {
// When
{
IGitWebhookHandler gitWebhookHandler = appContext.getBean(IGitWebhookHandler.class);

GitWebhookRelevancyResult value =
new GitWebhookRelevancyResult(false, false, Optional.empty(), Optional.empty(), Optional.empty());
Mockito.when(gitWebhookHandler.filterWebhookEventRelevant(Mockito.any(I3rdPartyWebhookEvent.class)))
.thenReturn(value);
}

// Then
Map<String, ?> payload = ImmutableMap.<String, Object>builder().build();
function.unsafeProcessOneEvent(new GithubWebhookEvent(payload));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package eu.solven.cleanthat.lambda.step1_checkconfiguration;

import java.io.IOException;
import java.util.Map;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.google.common.collect.ImmutableMap;

import eu.solven.cleanthat.code_provider.github.event.ICodeCleanerFactory;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandler;
import eu.solven.cleanthat.code_provider.github.event.IGitWebhookHandlerFactory;
import eu.solven.cleanthat.code_provider.github.event.pojo.CleanThatWebhookEvent;
import eu.solven.cleanthat.code_provider.github.event.pojo.WebhookRelevancyResult;
import eu.solven.cleanthat.lambda.step0_checkwebhook.IWebhookEvent;

@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { CheckConfigWebhooksLambdaFunction.class,
TestCheckConfigWebhooksLambdaFunctionAutonomy.CheckConfigWebhooksLambdaFunctionComplement.class })
public class TestCheckConfigWebhooksLambdaFunctionAutonomy {

@Configuration
public static class CheckConfigWebhooksLambdaFunctionComplement {

@Primary
@Bean
public IGitWebhookHandler gitWebhookHandler() throws IOException {
return Mockito.mock(IGitWebhookHandler.class);
}

// Override GithubWebhookHandlerFactory which will try actually connecting to GH
@Primary
@Bean
public IGitWebhookHandlerFactory gitWebhookHandlerFactory(IGitWebhookHandler gitWebhookHandler)
throws IOException {
IGitWebhookHandlerFactory factory = Mockito.mock(IGitWebhookHandlerFactory.class);

Mockito.when(factory.makeWithFreshAuth()).thenReturn(gitWebhookHandler);

return factory;
}
}

@Autowired
ApplicationContext appContext;

@Autowired
CheckConfigWebhooksLambdaFunction function;

@Test
public void testAppRun() {
// When
{
IGitWebhookHandler gitWebhookHandler = appContext.getBean(IGitWebhookHandler.class);

WebhookRelevancyResult value =
new WebhookRelevancyResult(Optional.empty(), Optional.of("someUnitTestReason"));
Mockito.when(
gitWebhookHandler.filterWebhookEventTargetRelevantBranch(Mockito.any(ICodeCleanerFactory.class),
Mockito.any(IWebhookEvent.class)))
.thenReturn(value);
}

// Then
Map<String, ?> payload = ImmutableMap.<String, Object>builder().build();
Map<String, Object> headers = Map.of();
function.unsafeProcessOneEvent(new CleanThatWebhookEvent(headers, payload));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package eu.solven.cleanthat.lambda.step2_executeclean;

import java.util.Map;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.google.common.collect.ImmutableMap;

import eu.solven.cleanthat.code_provider.github.event.pojo.CleanThatWebhookEvent;
import eu.solven.cleanthat.lambda.step1_checkconfiguration.TestCheckConfigWebhooksLambdaFunctionAutonomy;

@SpringBootTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { ExecuteCleaningWebhooksLambdaFunction.class,
TestCheckConfigWebhooksLambdaFunctionAutonomy.CheckConfigWebhooksLambdaFunctionComplement.class })
public class TestExecuteCleaningWebhooksLambdaFunctionAutonomy {

@Autowired
ApplicationContext appContext;

@Autowired
ExecuteCleaningWebhooksLambdaFunction function;

@Test
public void testAppRun() {
// When
// {
// IGitWebhookHandler gitWebhookHandler = appContext.getBean(IGitWebhookHandler.class);
//
// WebhookRelevancyResult value =
// new WebhookRelevancyResult(Optional.empty(), Optional.of("someUnitTestReason"));
// Mockito.when(
// gitWebhookHandler.filterWebhookEventTargetRelevantBranch(Mockito.any(ICodeCleanerFactory.class),
// Mockito.any(IWebhookEvent.class)))
// .thenReturn(value);
// }

// Then
Map<String, ?> payload = ImmutableMap.<String, Object>builder().build();
Map<String, Object> headers = Map.of();
function.unsafeProcessOneEvent(new CleanThatWebhookEvent(headers, payload));
}
}

0 comments on commit 65a5ed2

Please sign in to comment.