Formatting applied to aaa-authn-federation bundle
[aaa.git] / aaa-authn-federation / src / test / java / org / opendaylight / aaa / federation / FederationEndpointTest.java
1 /*
2  * Copyright (c) 2014 Hewlett-Packard Development Company, L.P. and others.
3  * All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
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.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;
32
33 /**
34  * A unit test for federation endpoint.
35  *
36  * @author liemmn
37  *
38  */
39 public class FederationEndpointTest {
40     private static final long TOKEN_TIMEOUT_SECS = 10;
41     private static final String CONTEXT = "/oauth2/federation";
42
43     private final static ServletTester server = new ServletTester();
44     private static final Claim claim = new ClaimBuilder().setUser("bob").setUserId("1234")
45             .addRole("admin").build();
46
47     @BeforeClass
48     public static void init() throws Exception {
49         // Set up server
50         server.setContextPath(CONTEXT);
51
52         // Add our servlet under test
53         server.addServlet(FederationEndpoint.class, "/*");
54
55         // Add ClaimAuth filter
56         server.addFilter(ClaimAuthFilter.class, "/*", 0);
57
58         // Let's do dis
59         server.start();
60     }
61
62     @AfterClass
63     public static void shutdown() throws Exception {
64         server.stop();
65     }
66
67     @Before
68     public void setup() {
69         mockServiceLocator();
70         when(ServiceLocator.getInstance().getTokenStore().tokenExpiration()).thenReturn(
71                 TOKEN_TIMEOUT_SECS);
72     }
73
74     @After
75     public void teardown() {
76         ServiceLocator.getInstance().getClaimAuthCollection().clear();
77     }
78
79     @Test
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");
85
86         HttpTester resp = new HttpTester();
87         resp.parse(server.getResponses(req.generate()));
88         assertEquals(401, resp.getStatus());
89     }
90
91     @Test
92     @SuppressWarnings("unchecked")
93     public void testFederation() throws Exception {
94         when(ServiceLocator.getInstance().getClaimAuthCollection().get(0).transform(anyMap()))
95                 .thenReturn(claim);
96         when(ServiceLocator.getInstance().getIdmService().listDomains(anyString())).thenReturn(
97                 Arrays.asList("pepsi", "coke"));
98
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)));
103
104         HttpTester req = new HttpTester();
105         req.setMethod("POST");
106         req.setURI(CONTEXT + "/");
107         req.setVersion("HTTP/1.0");
108
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"));
114     }
115
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));
120     }
121 }