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;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertFalse;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertTrue;
17 import java.util.Arrays;
18 import java.util.Dictionary;
19 import java.util.Hashtable;
20 import java.util.List;
21 import java.util.concurrent.Callable;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.Executors;
24 import java.util.concurrent.Future;
25 import org.junit.Test;
26 import org.opendaylight.aaa.api.Authentication;
27 import org.opendaylight.aaa.api.AuthenticationService;
28 import org.osgi.service.cm.ConfigurationException;
30 public class AuthenticationManagerTest {
32 public void testAuthenticationCrudSameThread() {
33 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
34 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
35 AuthenticationService as = AuthenticationManager.instance();
40 assertEquals(auth, as.get());
47 public void testAuthenticationCrudSpawnedThread() throws InterruptedException,
49 AuthenticationService as = AuthenticationManager.instance();
50 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
51 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
54 Future<Authentication> f = Executors.newSingleThreadExecutor().submit(new Worker());
55 assertEquals(auth, f.get());
58 f = Executors.newSingleThreadExecutor().submit(new Worker());
63 public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException,
65 AuthenticationService as = AuthenticationManager.instance();
66 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
67 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
70 List<Future<Authentication>> fs = Executors.newFixedThreadPool(2).invokeAll(
71 Arrays.asList(new Worker(), new Worker()));
72 for (Future<Authentication> f : fs) {
73 assertEquals(auth, f.get());
77 fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker()));
78 for (Future<Authentication> f : fs) {
84 public void testUpdatedValid() throws ConfigurationException {
85 Dictionary<String, String> props = new Hashtable<>();
86 AuthenticationManager as = AuthenticationManager.instance();
88 assertFalse(as.isAuthEnabled());
90 props.put(AuthenticationManager.AUTH_ENABLED, "TrUe");
92 assertTrue(as.isAuthEnabled());
94 props.put(AuthenticationManager.AUTH_ENABLED, "FaLsE");
96 assertFalse(as.isAuthEnabled());
100 public void testUpdatedNullProperty() throws ConfigurationException {
101 AuthenticationManager as = AuthenticationManager.instance();
103 assertFalse(as.isAuthEnabled());
105 assertFalse(as.isAuthEnabled());
108 @Test(expected = ConfigurationException.class)
109 public void testUpdatedInvalidValue() throws ConfigurationException {
110 AuthenticationManager as = AuthenticationManager.instance();
111 Dictionary<String, String> props = new Hashtable<>();
113 props.put(AuthenticationManager.AUTH_ENABLED, "yes");
117 @Test(expected = ConfigurationException.class)
118 public void testUpdatedInvalidKey() throws ConfigurationException {
119 AuthenticationManager as = AuthenticationManager.instance();
120 Dictionary<String, String> props = new Hashtable<>();
122 props.put("Invalid Key", "true");
126 private class Worker implements Callable<Authentication> {
128 public Authentication call() throws Exception {
129 AuthenticationService as = AuthenticationManager.instance();