2 * Copyright (c) 2014, 2015 Hewlett-Packard Development Company, L.P. and others. All rights reserved.
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
9 package org.opendaylight.aaa.federation;
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;
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;
35 * A unit test for federation endpoint.
41 public class FederationEndpointTest {
42 private static final long TOKEN_TIMEOUT_SECS = 10;
43 private static final String CONTEXT = "/oauth2/federation";
45 private final static ServletTester server = new ServletTester();
46 private static final Claim claim = new ClaimBuilder().setUser("bob").setUserId("1234")
47 .addRole("admin").build();
50 public static void init() throws Exception {
52 server.setContextPath(CONTEXT);
54 // Add our servlet under test
55 server.addServlet(FederationEndpoint.class, "/*");
57 // Add ClaimAuth filter
58 server.addFilter(ClaimAuthFilter.class, "/*", 0);
65 public static void shutdown() throws Exception {
72 when(ServiceLocator.getInstance().getTokenStore().tokenExpiration()).thenReturn(
77 public void teardown() {
78 ServiceLocator.getInstance().getClaimAuthCollection().clear();
82 @SuppressWarnings("unchecked")
83 public void testFederation() throws Exception {
84 when(ServiceLocator.getInstance().getClaimAuthCollection().get(0).transform(anyMap()))
86 when(ServiceLocator.getInstance().getIdmService().listDomains(anyString())).thenReturn(
87 Arrays.asList("pepsi", "coke"));
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)));
94 HttpTester req = new HttpTester();
95 req.setMethod("POST");
96 req.setURI(CONTEXT + "/");
97 req.setVersion("HTTP/1.0");
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"));
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");
113 HttpTester resp = new HttpTester();
114 resp.parse(server.getResponses(req.generate()));
115 assertEquals(401, resp.getStatus());
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));