Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-tokenauthrealm / src / test / java / org / opendaylight / aaa / tokenauthrealm / auth / AuthenticationManagerTest.java
1 /*
2  * Copyright (c) 2014, 2017 Hewlett-Packard Development Company, L.P. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.aaa.tokenauthrealm.auth;
9
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;
16
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;
28
29 @RunWith(MockitoJUnitRunner.StrictStubs.class)
30 public class AuthenticationManagerTest {
31     @Mock
32     private AuthenticationManager.Configuration configuration;
33
34     private final AuthenticationManager authManager = new AuthenticationManager();
35
36     @Test
37     public void testAuthenticationCrudSameThread() {
38         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
39                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
40
41         assertNotNull(authManager);
42
43         authManager.set(auth);
44         assertEquals(auth, authManager.get());
45
46         authManager.clear();
47         assertNull(authManager.get());
48     }
49
50     @Test
51     public void testAuthenticationCrudSpawnedThread() throws InterruptedException,
52             ExecutionException {
53         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
54                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
55
56         authManager.set(auth);
57         Future<Authentication> future = Executors.newSingleThreadExecutor().submit(new Worker());
58         assertEquals(auth, future.get());
59
60         authManager.clear();
61         future = Executors.newSingleThreadExecutor().submit(new Worker());
62         assertNull(future.get());
63     }
64
65     @Test
66     public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException,
67             ExecutionException {
68         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
69                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
70
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());
76         }
77
78         authManager.clear();
79         fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker()));
80         for (Future<Authentication> f : fs) {
81             assertNull(f.get());
82         }
83     }
84
85     @Test
86     public void testUpdatedValid() {
87         assertFalse(authManager.isAuthEnabled());
88
89         doReturn(true).when(configuration).authEnabled();
90         authManager.modified(configuration);
91         assertTrue(authManager.isAuthEnabled());
92
93         doReturn(false).when(configuration).authEnabled();
94         authManager.modified(configuration);
95         assertFalse(authManager.isAuthEnabled());
96     }
97
98     private class Worker implements Callable<Authentication> {
99         @Override
100         public Authentication call() throws Exception {
101             return authManager.get();
102         }
103     }
104 }