Fix the compilation error
[aaa.git] / aaa-authn-federation / src / test / java / org / opendaylight / aaa / federation / FederationEndpointTest.java
1 /*
2  * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.aaa.federation;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.Matchers.anyMap;
14 import static org.mockito.Matchers.anyString;
15 import static org.mockito.Mockito.mock;
16 import static org.mockito.Mockito.when;
17
18 import java.util.Arrays;
19 import java.util.TreeSet;
20 import org.junit.After;
21 import org.junit.AfterClass;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.mortbay.jetty.testing.HttpTester;
27 import org.mortbay.jetty.testing.ServletTester;
28 import org.opendaylight.aaa.ClaimBuilder;
29 import org.opendaylight.aaa.api.Claim;
30 import org.opendaylight.aaa.api.ClaimAuth;
31 import org.opendaylight.aaa.api.IdMService;
32 import org.opendaylight.aaa.api.TokenStore;
33
34 /**
35  * A unit test for federation endpoint.
36  *
37  * @author liemmn
38  *
39  */
40 @Ignore
41 public class FederationEndpointTest {
42     private static final long TOKEN_TIMEOUT_SECS = 10;
43     private static final String CONTEXT = "/oauth2/federation";
44
45     private final static ServletTester server = new ServletTester();
46     private static final Claim claim = new ClaimBuilder().setUser("bob").setUserId("1234")
47             .addRole("admin").build();
48
49     @BeforeClass
50     public static void init() throws Exception {
51         // Set up server
52         server.setContextPath(CONTEXT);
53
54         // Add our servlet under test
55         server.addServlet(FederationEndpoint.class, "/*");
56
57         // Add ClaimAuth filter
58         server.addFilter(ClaimAuthFilter.class, "/*", 0);
59
60         // Let's do dis
61         server.start();
62     }
63
64     @AfterClass
65     public static void shutdown() throws Exception {
66         server.stop();
67     }
68
69     @Before
70     public void setup() {
71         mockServiceLocator();
72         when(ServiceLocator.getInstance().getTokenStore().tokenExpiration()).thenReturn(
73                 TOKEN_TIMEOUT_SECS);
74     }
75
76     @After
77     public void teardown() {
78         ServiceLocator.getInstance().getClaimAuthCollection().clear();
79     }
80
81     @Test
82     @SuppressWarnings("unchecked")
83     public void testFederation() throws Exception {
84         when(ServiceLocator.getInstance().getClaimAuthCollection().get(0).transform(anyMap()))
85                 .thenReturn(claim);
86         when(ServiceLocator.getInstance().getIdmService().listDomains(anyString())).thenReturn(
87                 Arrays.asList("pepsi", "coke"));
88
89         // Configure secure port (of zero)
90         FederationConfiguration.instance = mock(FederationConfiguration.class);
91         when(FederationConfiguration.instance.secureProxyPorts()).thenReturn(
92                 new TreeSet<Integer>(Arrays.asList(0)));
93
94         HttpTester req = new HttpTester();
95         req.setMethod("POST");
96         req.setURI(CONTEXT + "/");
97         req.setVersion("HTTP/1.0");
98
99         HttpTester resp = new HttpTester();
100         resp.parse(server.getResponses(req.generate()));
101         assertEquals(201, resp.getStatus());
102         String content = resp.getContent();
103         assertTrue(content.contains("pepsi coke"));
104     }
105
106     @Test
107     public void testFederationUnconfiguredProxyPort() throws Exception {
108         HttpTester req = new HttpTester();
109         req.setMethod("POST");
110         req.setURI(CONTEXT + "/");
111         req.setVersion("HTTP/1.0");
112
113         HttpTester resp = new HttpTester();
114         resp.parse(server.getResponses(req.generate()));
115         assertEquals(401, resp.getStatus());
116     }
117
118     private static void mockServiceLocator() {
119         ServiceLocator.getInstance().setIdmService(mock(IdMService.class));
120         ServiceLocator.getInstance().setTokenStore(mock(TokenStore.class));
121         ServiceLocator.getInstance().getClaimAuthCollection().add(mock(ClaimAuth.class));
122     }
123 }