1 package de.juplo.facebook.errors;
4 import java.util.HashMap;
6 import java.util.Map.Entry;
7 import org.junit.After;
9 import org.springframework.context.annotation.Configuration;
10 import static org.junit.Assert.*;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
13 import org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer;
14 import org.springframework.boot.autoconfigure.social.FacebookAutoConfiguration;
15 import org.springframework.boot.autoconfigure.social.SocialWebAutoConfiguration;
16 import org.springframework.context.ConfigurableApplicationContext;
17 import org.springframework.context.annotation.Bean;
18 import org.springframework.context.annotation.Scope;
19 import org.springframework.context.annotation.ScopedProxyMode;
20 import org.springframework.mock.env.MockEnvironment;
21 import org.springframework.mock.web.MockHttpServletRequest;
22 import org.springframework.social.connect.ConnectionRepository;
23 import org.springframework.social.facebook.api.Facebook;
24 import org.springframework.social.facebook.api.impl.FacebookTemplate;
25 import org.springframework.web.client.RestTemplate;
26 import org.springframework.web.context.request.RequestContextHolder;
27 import org.springframework.web.context.request.ServletRequestAttributes;
28 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
32 public class FacebookErrorsSpringSocialAutoConfigurationTest
34 private final Logger LOG =
35 LoggerFactory.getLogger(FacebookErrorsSpringSocialAutoConfigurationTest.class);
38 private ConfigurableApplicationContext context;
42 public void tearDown()
44 if (this.context != null)
50 public void defaultConfiguration()
52 LOG.info("<-- Start Of New Test-Case!");
53 Map<String, String> properties = new HashMap<>();
54 properties.put("spring.social.facebook.app-id", "APP_ID");
55 properties.put("spring.social.facebook.app-secret", "APP_SECRET");
56 context = loadWebApplicationContext(EmptyConfiguration.class, properties);
57 Facebook facebook = context.getBean("facebook", Facebook.class);
58 RestTemplate template = (RestTemplate) facebook.restOperations();
59 assertEquals(GraphApiErrorHandler.class, template.getErrorHandler().getClass());
64 static class EmptyConfiguration
67 * We have to provide this scoped bean for all our tests.
68 * Otherwise, the tests will fail, if we do not set up a complete
69 * environment with a connection-repository and a properly mocked request,
70 * that holds appropriate information to fetch a connection from that
76 @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES)
77 public Facebook facebook(ConnectionRepository repository)
79 return new FacebookTemplate("ACCESS_TOKEN");
84 private ConfigurableApplicationContext loadWebApplicationContext(
86 Map<String, String> properties
89 AnnotationConfigWebApplicationContext ctx =
90 new AnnotationConfigWebApplicationContext();
91 if (properties != null)
93 MockEnvironment env = new MockEnvironment();
94 for (Entry<String, String> entry : properties.entrySet())
95 env.withProperty(entry.getKey(), entry.getValue());
96 ctx.setEnvironment(env);
98 ctx.register(FacebookErrorsSpringSocialAutoConfiguration.class);
99 ctx.register(FacebookAutoConfiguration.class);
100 ctx.register(SocialWebAutoConfiguration.class);
101 ctx.register(config);
102 AutoConfigurationReportLoggingInitializer report =
103 new AutoConfigurationReportLoggingInitializer();
104 report.initialize(ctx);
105 MockHttpServletRequest request = new MockHttpServletRequest();
106 ServletRequestAttributes attributes = new ServletRequestAttributes(request);
107 RequestContextHolder.setRequestAttributes(attributes);