7c1e156f3d3576795ae456011ca9e6fb62db0404
[facebook-errors] / src / test / java / de / juplo / facebook / errors / FacebookErrorsOAuth2AutoConfigurationTest.java
1 package de.juplo.facebook.errors;
2
3
4 import java.util.HashMap;
5 import java.util.Map;
6 import java.util.Map.Entry;
7 import org.junit.After;
8 import org.junit.Test;
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.security.oauth2.OAuth2AutoConfiguration;
15 import org.springframework.context.ConfigurableApplicationContext;
16 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
17 import org.springframework.mock.env.MockEnvironment;
18 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
19 import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
20
21
22
23 public class FacebookErrorsOAuth2AutoConfigurationTest
24 {
25   private final Logger LOG =
26       LoggerFactory.getLogger(FacebookErrorsOAuth2AutoConfigurationTest.class);
27
28
29   private ConfigurableApplicationContext context;
30
31
32   @After
33   public void tearDown()
34   {
35     if (this.context != null)
36       this.context.close();
37   }
38
39
40   @Test
41   public void defaultNonWebConfiguration()
42   {
43     LOG.info("<-- Start Of New Test-Case!");
44     Map<String, String> properties = new HashMap<>();
45     properties.put("security.oauth2.client.client-id", "CLIENT_ID");
46     context = loadNonWebApplicationContext(EmptyConfiguration.class, properties);
47     OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class);
48     assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass());
49   }
50
51   @Test
52   public void defaultWebConfiguration()
53   {
54     LOG.info("<-- Start Of New Test-Case!");
55     Map<String, String> properties = new HashMap<>();
56     properties.put("security.oauth2.client.client-id", "CLIENT_ID");
57     context = loadWebApplicationContext(EmptyConfiguration.class, properties);
58     OAuth2RestTemplate template = context.getBean(OAuth2RestTemplate.class);
59     assertEquals(OAuth2GraphApiErrorHandler.class, template.getErrorHandler().getClass());
60   }
61
62
63   @Configuration
64   static class EmptyConfiguration
65   {
66   }
67
68
69   private ConfigurableApplicationContext loadNonWebApplicationContext(
70       Class<?> config,
71       Map<String, String> properties
72       )
73   {
74     AnnotationConfigApplicationContext ctx =
75         new AnnotationConfigApplicationContext();
76     if (properties != null)
77     {
78       MockEnvironment env = new MockEnvironment();
79       for (Entry<String, String> entry : properties.entrySet())
80         env.withProperty(entry.getKey(), entry.getValue());
81       ctx.setEnvironment(env);
82     }
83     ctx.register(FacebookErrorsOAuth2AutoConfiguration.class);
84     ctx.register(OAuth2AutoConfiguration.class);
85     ctx.register(config);
86     AutoConfigurationReportLoggingInitializer report =
87         new AutoConfigurationReportLoggingInitializer();
88     report.initialize(ctx);
89     ctx.refresh();
90     return ctx;
91   }
92
93   private ConfigurableApplicationContext loadWebApplicationContext(
94       Class<?> config,
95       Map<String, String> properties
96       )
97   {
98     AnnotationConfigWebApplicationContext ctx =
99         new AnnotationConfigWebApplicationContext();
100     if (properties != null)
101     {
102       MockEnvironment env = new MockEnvironment();
103       for (Entry<String, String> entry : properties.entrySet())
104         env.withProperty(entry.getKey(), entry.getValue());
105       ctx.setEnvironment(env);
106     }
107     ctx.register(FacebookErrorsOAuth2AutoConfiguration.class);
108     ctx.register(OAuth2AutoConfiguration.class);
109     ctx.register(config);
110     AutoConfigurationReportLoggingInitializer report =
111         new AutoConfigurationReportLoggingInitializer();
112     report.initialize(ctx);
113     ctx.refresh();
114     return ctx;
115   }
116 }