From 5c850edd024455719c0cfa5e831f35577126b71b Mon Sep 17 00:00:00 2001 From: musketyr Date: Tue, 12 Dec 2023 15:14:57 +0100 Subject: [PATCH] upgraded Ersatz --- gradle.properties | 2 +- .../micronaut-facebook-sdk-mock.gradle | 7 +-- .../facebooksdk/mock/FacebookApiResponse.java | 4 +- .../mock/TestFacebookApplication.java | 43 +++++++++++++------ .../TestFacebookApplicationExtensions.java | 4 +- .../mock/TestFacebookApplicationSpec.groovy | 4 +- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/gradle.properties b/gradle.properties index 17e7cfb..f50f5bf 100644 --- a/gradle.properties +++ b/gradle.properties @@ -31,6 +31,6 @@ micronautGradlePluginVersion = 4.2.0 restfbVersion=2023.3.0 gruVersion=2.0.4 -ersatzVersion=1.9.0 +ersatzVersion=4.0.0 fixtVersion=0.2.3 groovyClosureSupportVersion=1.0.1 diff --git a/libs/micronaut-facebook-sdk-mock/micronaut-facebook-sdk-mock.gradle b/libs/micronaut-facebook-sdk-mock/micronaut-facebook-sdk-mock.gradle index 81b5112..d3979ac 100644 --- a/libs/micronaut-facebook-sdk-mock/micronaut-facebook-sdk-mock.gradle +++ b/libs/micronaut-facebook-sdk-mock/micronaut-facebook-sdk-mock.gradle @@ -17,14 +17,11 @@ */ dependencies { api project(':micronaut-facebook-sdk') - api "com.stehno.ersatz:ersatz:$ersatzVersion" + api "io.github.cjstehno.ersatz:ersatz-groovy:$ersatzVersion" + api "io.github.cjstehno.ersatz:ersatz:$ersatzVersion" api "space.jasan:groovy-closure-support:$groovyClosureSupportVersion" implementation 'io.micronaut:micronaut-jackson-databind' - // dependency of ersatz - implementation 'io.undertow:undertow-core:2.0.28.Final' - implementation 'javax.activation:activation:1.1.1' - testImplementation "com.agorapulse.testing:fixt:$fixtVersion" } diff --git a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/FacebookApiResponse.java b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/FacebookApiResponse.java index 286486a..8887267 100644 --- a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/FacebookApiResponse.java +++ b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/FacebookApiResponse.java @@ -19,8 +19,8 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; -import com.stehno.ersatz.ContentType; -import com.stehno.ersatz.Response; +import io.github.cjstehno.ersatz.cfg.ContentType; +import io.github.cjstehno.ersatz.cfg.Response; import java.util.Collections; import java.util.LinkedHashMap; diff --git a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplication.java b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplication.java index 27881ad..8e94310 100644 --- a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplication.java +++ b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplication.java @@ -27,17 +27,21 @@ import com.restfb.JsonMapper; import com.restfb.Version; import com.restfb.WebRequestor; -import com.stehno.ersatz.ContentType; -import com.stehno.ersatz.ErsatzServer; -import com.stehno.ersatz.Expectations; +import io.github.cjstehno.ersatz.ErsatzServer; +import io.github.cjstehno.ersatz.cfg.ContentType; +import io.github.cjstehno.ersatz.cfg.Expectations; +import io.micronaut.context.BeanContext; import io.micronaut.context.annotation.Replaces; +import io.micronaut.context.exceptions.BeanInstantiationException; import jakarta.annotation.PreDestroy; import jakarta.inject.Singleton; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import java.io.Closeable; import java.nio.charset.StandardCharsets; import java.util.Collections; -import java.util.Optional; import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -48,6 +52,8 @@ @Singleton @Replaces(DefaultFacebookApplication.class) public class TestFacebookApplication implements FacebookApplication, Closeable { + + private static final Logger LOGGER = LoggerFactory.getLogger(TestFacebookApplication.class); public static final String TOKEN = "TOKEN"; @@ -58,9 +64,16 @@ public class TestFacebookApplication implements FacebookApplication, Closeable { private final DefaultFacebookApplication fallback; private final FacebookApplicationConfiguration configuration; - public TestFacebookApplication(Optional configuration) { - this.configuration = configuration.orElseGet(FacebookApplicationConfiguration::new); + public TestFacebookApplication(BeanContext context) { + FacebookApplicationConfiguration maybeConfiguration = null; + try { + maybeConfiguration = context.containsBean(FacebookApplicationConfiguration.class) ? context.getBean(FacebookApplicationConfiguration.class) : new FacebookApplicationConfiguration(); + } catch (BeanInstantiationException ex){ + LOGGER.trace("FacebookApplicationConfiguration not found, using default configuration", ex); + } + this.configuration = maybeConfiguration == null ? new FacebookApplicationConfiguration() : maybeConfiguration; this.fallback = new DefaultFacebookApplication(this.configuration); + } @Override @@ -103,15 +116,17 @@ public void close() { } private static ErsatzServer prepareServer() { - ErsatzServer server = new ErsatzServer(); - server.reportToConsole(true); - server.https(true); - server.decoder(ContentType.APPLICATION_URLENCODED, (body, ctx) -> { - if (body == null || body.length == 0) { - return null; - } - return parseQueryString(new String(body, StandardCharsets.UTF_8), StandardCharsets.UTF_8.name()); + ErsatzServer server = new ErsatzServer(serverConfig -> { + serverConfig.reportToConsole(true); + serverConfig.https(true); + serverConfig.decoder(ContentType.APPLICATION_URLENCODED, (body, ctx) -> { + if (body == null || body.length == 0) { + return null; + } + return parseQueryString(new String(body, StandardCharsets.UTF_8), StandardCharsets.UTF_8.name()); + }); }); + return server; } diff --git a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/groovy/TestFacebookApplicationExtensions.java b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/groovy/TestFacebookApplicationExtensions.java index abc95a1..e99ebc0 100644 --- a/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/groovy/TestFacebookApplicationExtensions.java +++ b/libs/micronaut-facebook-sdk-mock/src/main/java/com/agorapulse/micronaut/facebooksdk/mock/groovy/TestFacebookApplicationExtensions.java @@ -19,12 +19,12 @@ import com.agorapulse.micronaut.facebooksdk.mock.FacebookApiResponse; import com.agorapulse.micronaut.facebooksdk.mock.TestFacebookApplication; -import com.stehno.ersatz.Expectations; -import com.stehno.ersatz.Request; import groovy.lang.Closure; import groovy.lang.DelegatesTo; import groovy.transform.stc.ClosureParams; import groovy.transform.stc.SimpleType; +import io.github.cjstehno.ersatz.cfg.Expectations; +import io.github.cjstehno.ersatz.cfg.Request; import space.jasan.support.groovy.closure.ConsumerWithDelegate; import static groovy.lang.Closure.DELEGATE_FIRST; diff --git a/libs/micronaut-facebook-sdk-mock/src/test/groovy/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplicationSpec.groovy b/libs/micronaut-facebook-sdk-mock/src/test/groovy/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplicationSpec.groovy index af6bf32..7553019 100644 --- a/libs/micronaut-facebook-sdk-mock/src/test/groovy/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplicationSpec.groovy +++ b/libs/micronaut-facebook-sdk-mock/src/test/groovy/com/agorapulse/micronaut/facebooksdk/mock/TestFacebookApplicationSpec.groovy @@ -20,7 +20,7 @@ package com.agorapulse.micronaut.facebooksdk.mock import com.agorapulse.testing.fixt.Fixt import com.restfb.types.User import groovy.transform.CompileDynamic -import io.micronaut.test.extensions.junit5.annotation.MicronautTest +import io.micronaut.test.extensions.spock.annotation.MicronautTest import spock.lang.AutoCleanup import spock.lang.Specification @@ -37,7 +37,7 @@ class TestFacebookApplicationSpec extends Specification { void 'get me'() { given: facebook.mockApi { - get('/v16.0/me') { + GET('/v16.0/me') { just fixt.readText('me.json') } }