X-Git-Url: https://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FFacebookErrorsSpringSocialAutoConfigurationTest.java;fp=src%2Ftest%2Fjava%2Fde%2Fjuplo%2Ffacebook%2Ferrors%2FFacebookErrorsSpringSocialAutoConfigurationTest.java;h=651ced5736b9836964d587c90a3f9e725bbd96df;hb=c0c92426107de6a10754b4548833f6266afaa94a;hp=0000000000000000000000000000000000000000;hpb=12bc7ae4bfbae335955e1c046843162599b083dc;p=facebook-errors diff --git a/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java b/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java new file mode 100644 index 0000000..651ced5 --- /dev/null +++ b/src/test/java/de/juplo/facebook/errors/FacebookErrorsSpringSocialAutoConfigurationTest.java @@ -0,0 +1,111 @@ +package de.juplo.facebook.errors; + + +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.junit.After; +import org.junit.Test; +import org.springframework.context.annotation.Configuration; +import static org.junit.Assert.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer; +import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration; +import org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration; +import org.springframework.context.ConfigurableApplicationContext; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Scope; +import org.springframework.context.annotation.ScopedProxyMode; +import org.springframework.mock.env.MockEnvironment; +import org.springframework.mock.web.MockHttpServletRequest; +import org.springframework.social.connect.ConnectionRepository; +import org.springframework.social.facebook.api.Facebook; +import org.springframework.social.facebook.api.impl.FacebookTemplate; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.context.request.RequestContextHolder; +import org.springframework.web.context.request.ServletRequestAttributes; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; + + + +public class FacebookErrorsSpringSocialAutoConfigurationTest +{ + private final Logger LOG = + LoggerFactory.getLogger(FacebookErrorsSpringSocialAutoConfigurationTest.class); + + + private ConfigurableApplicationContext context; + + + @After + public void tearDown() + { + if (this.context != null) + this.context.close(); + } + + + @Test + public void defaultConfiguration() + { + LOG.info("<-- Start Of New Test-Case!"); + Map properties = new HashMap<>(); + properties.put("spring.social.facebook.app-id", "APP_ID"); + properties.put("spring.social.facebook.app-secret", "APP_SECRET"); + context = loadWebApplicationContext(EmptyConfiguration.class, properties); + Facebook facebook = context.getBean("facebook", Facebook.class); + RestTemplate template = (RestTemplate) facebook.restOperations(); + assertEquals(GraphApiErrorHandler.class, template.getErrorHandler().getClass()); + } + + + @Configuration + static class EmptyConfiguration + { + /** + * We have to provide this scoped bean for all our tests. + * Otherwise, the tests will fail, if we do not set up a complete + * environment with a connection-repository and a properly mocked request, + * that holds appropriate information to fetch a connection from that + * repository. + * @param repository + * @return + */ + @Bean + @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) + public Facebook facebook(ConnectionRepository repository) + { + return new FacebookTemplate("ACCESS_TOKEN"); + } + } + + + private ConfigurableApplicationContext loadWebApplicationContext( + Class config, + Map properties + ) + { + AnnotationConfigWebApplicationContext ctx = + new AnnotationConfigWebApplicationContext(); + if (properties != null) + { + MockEnvironment env = new MockEnvironment(); + for (Entry entry : properties.entrySet()) + env.withProperty(entry.getKey(), entry.getValue()); + ctx.setEnvironment(env); + } + ctx.register(FacebookErrorsSpringSocialAutoConfiguration.class); + ctx.register(FacebookAutoConfiguration.class); + ctx.register(SocialWebAutoConfiguration.class); + ctx.register(config); + AutoConfigurationReportLoggingInitializer report = + new AutoConfigurationReportLoggingInitializer(); + report.initialize(ctx); + MockHttpServletRequest request = new MockHttpServletRequest(); + ServletRequestAttributes attributes = new ServletRequestAttributes(request); + RequestContextHolder.setRequestAttributes(attributes); + ctx.refresh(); + return ctx; + } +}