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.eclipse.jetty.testing.HttpTester;
21 import org.eclipse.jetty.testing.ServletTester;
22 import org.junit.After;
23 import org.junit.AfterClass;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.opendaylight.aaa.ClaimBuilder;
28 import org.opendaylight.aaa.api.Claim;
29 import org.opendaylight.aaa.api.ClaimAuth;
30 import org.opendaylight.aaa.api.IdMService;
31 import org.opendaylight.aaa.api.TokenStore;
34 * A unit test for federation endpoint.
39 public class FederationEndpointTest {
40 private static final long TOKEN_TIMEOUT_SECS = 10;
41 private static final String CONTEXT = "/oauth2/federation";
43 private final static ServletTester server = new ServletTester();
44 private static final Claim claim = new ClaimBuilder().setUser("bob").setUserId("1234")
45 .addRole("admin").build();
48 public static void init() throws Exception {
50 server.setContextPath(CONTEXT);
52 // Add our servlet under test
53 server.addServlet(FederationEndpoint.class, "/*");
55 // Add ClaimAuth filter
56 server.addFilter(ClaimAuthFilter.class, "/*", 0);
63 public static void shutdown() throws Exception {
70 when(ServiceLocator.getInstance().getTokenStore().tokenExpiration()).thenReturn(
75 public void teardown() {
76 ServiceLocator.getInstance().getClaimAuthCollection().clear();
80 public void testFederationUnconfiguredProxyPort() throws Exception {
81 HttpTester req = new HttpTester();
82 req.setMethod("POST");
83 req.setURI(CONTEXT + "/");
84 req.setVersion("HTTP/1.0");
86 HttpTester resp = new HttpTester();
87 resp.parse(server.getResponses(req.generate()));
88 assertEquals(401, resp.getStatus());
92 @SuppressWarnings("unchecked")
93 public void testFederation() throws Exception {
94 when(ServiceLocator.getInstance().getClaimAuthCollection().get(0).transform(anyMap()))
96 when(ServiceLocator.getInstance().getIdmService().listDomains(anyString())).thenReturn(
97 Arrays.asList("pepsi", "coke"));
99 // Configure secure port (of zero)
100 FederationConfiguration.instance = mock(FederationConfiguration.class);
101 when(FederationConfiguration.instance.secureProxyPorts()).thenReturn(
102 new TreeSet<Integer>(Arrays.asList(0)));
104 HttpTester req = new HttpTester();
105 req.setMethod("POST");
106 req.setURI(CONTEXT + "/");
107 req.setVersion("HTTP/1.0");
109 HttpTester resp = new HttpTester();
110 resp.parse(server.getResponses(req.generate()));
111 assertEquals(201, resp.getStatus());
112 String content = resp.getContent();
113 assertTrue(content.contains("pepsi coke"));
116 private static void mockServiceLocator() {
117 ServiceLocator.getInstance().setIdmService(mock(IdMService.class));
118 ServiceLocator.getInstance().setTokenStore(mock(TokenStore.class));
119 ServiceLocator.getInstance().getClaimAuthCollection().add(mock(ClaimAuth.class));