2 * Copyright (c) 2014, 2017 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
8 package org.opendaylight.aaa.tokenauthrealm.auth;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.mockito.Mockito.doReturn;
17 import java.util.Arrays;
18 import java.util.List;
19 import java.util.concurrent.Callable;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.Executors;
22 import java.util.concurrent.Future;
23 import org.junit.Test;
24 import org.junit.runner.RunWith;
25 import org.mockito.Mock;
26 import org.mockito.junit.MockitoJUnitRunner;
27 import org.opendaylight.aaa.api.Authentication;
29 @RunWith(MockitoJUnitRunner.StrictStubs.class)
30 public class AuthenticationManagerTest {
32 private AuthenticationManager.Configuration configuration;
34 private final AuthenticationManager authManager = new AuthenticationManager();
37 public void testAuthenticationCrudSameThread() {
38 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
39 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
41 assertNotNull(authManager);
43 authManager.set(auth);
44 assertEquals(auth, authManager.get());
47 assertNull(authManager.get());
51 public void testAuthenticationCrudSpawnedThread() throws InterruptedException,
53 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
54 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
56 authManager.set(auth);
57 Future<Authentication> future = Executors.newSingleThreadExecutor().submit(new Worker());
58 assertEquals(auth, future.get());
61 future = Executors.newSingleThreadExecutor().submit(new Worker());
62 assertNull(future.get());
66 public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException,
68 Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
69 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
71 authManager.set(auth);
72 List<Future<Authentication>> fs = Executors.newFixedThreadPool(2).invokeAll(
73 Arrays.asList(new Worker(), new Worker()));
74 for (Future<Authentication> f : fs) {
75 assertEquals(auth, f.get());
79 fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker()));
80 for (Future<Authentication> f : fs) {
86 public void testUpdatedValid() {
87 assertFalse(authManager.isAuthEnabled());
89 doReturn(true).when(configuration).authEnabled();
90 authManager.modified(configuration);
91 assertTrue(authManager.isAuthEnabled());
93 doReturn(false).when(configuration).authEnabled();
94 authManager.modified(configuration);
95 assertFalse(authManager.isAuthEnabled());
98 private class Worker implements Callable<Authentication> {
100 public Authentication call() throws Exception {
101 return authManager.get();