Skip to content

Commit

Permalink
upgraded Ersatz
Browse files Browse the repository at this point in the history
  • Loading branch information
musketyr committed Dec 12, 2023
1 parent 9b742ea commit 5c850ed
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand All @@ -58,9 +64,16 @@ public class TestFacebookApplication implements FacebookApplication, Closeable {
private final DefaultFacebookApplication fallback;
private final FacebookApplicationConfiguration configuration;

public TestFacebookApplication(Optional<FacebookApplicationConfiguration> 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
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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')
}
}
Expand Down

0 comments on commit 5c850ed

Please sign in to comment.