540df28752ceef5a91115559db57719b616eb203
[moon.git] /
1 /*
2  * Copyright (c) 2014, 2015 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
9 package org.opendaylight.aaa;
10
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;
16
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;
29
30 public class AuthenticationManagerTest {
31     @Test
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();
36
37         assertNotNull(as);
38
39         as.set(auth);
40         assertEquals(auth, as.get());
41
42         as.clear();
43         assertNull(as.get());
44     }
45
46     @Test
47     public void testAuthenticationCrudSpawnedThread() throws InterruptedException,
48             ExecutionException {
49         AuthenticationService as = AuthenticationManager.instance();
50         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
51                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
52
53         as.set(auth);
54         Future<Authentication> f = Executors.newSingleThreadExecutor().submit(new Worker());
55         assertEquals(auth, f.get());
56
57         as.clear();
58         f = Executors.newSingleThreadExecutor().submit(new Worker());
59         assertNull(f.get());
60     }
61
62     @Test
63     public void testAuthenticationCrudSpawnedThreadPool() throws InterruptedException,
64             ExecutionException {
65         AuthenticationService as = AuthenticationManager.instance();
66         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("Bob")
67                 .setUserId("1234").addRole("admin").addRole("guest").build()).build();
68
69         as.set(auth);
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());
74         }
75
76         as.clear();
77         fs = Executors.newFixedThreadPool(2).invokeAll(Arrays.asList(new Worker(), new Worker()));
78         for (Future<Authentication> f : fs) {
79             assertNull(f.get());
80         }
81     }
82
83     @Test
84     public void testUpdatedValid() throws ConfigurationException {
85         Dictionary<String, String> props = new Hashtable<>();
86         AuthenticationManager as = AuthenticationManager.instance();
87
88         assertFalse(as.isAuthEnabled());
89
90         props.put(AuthenticationManager.AUTH_ENABLED, "TrUe");
91         as.updated(props);
92         assertTrue(as.isAuthEnabled());
93
94         props.put(AuthenticationManager.AUTH_ENABLED, "FaLsE");
95         as.updated(props);
96         assertFalse(as.isAuthEnabled());
97     }
98
99     @Test
100     public void testUpdatedNullProperty() throws ConfigurationException {
101         AuthenticationManager as = AuthenticationManager.instance();
102
103         assertFalse(as.isAuthEnabled());
104         as.updated(null);
105         assertFalse(as.isAuthEnabled());
106     }
107
108     @Test(expected = ConfigurationException.class)
109     public void testUpdatedInvalidValue() throws ConfigurationException {
110         AuthenticationManager as = AuthenticationManager.instance();
111         Dictionary<String, String> props = new Hashtable<>();
112
113         props.put(AuthenticationManager.AUTH_ENABLED, "yes");
114         as.updated(props);
115     }
116
117     @Test(expected = ConfigurationException.class)
118     public void testUpdatedInvalidKey() throws ConfigurationException {
119         AuthenticationManager as = AuthenticationManager.instance();
120         Dictionary<String, String> props = new Hashtable<>();
121
122         props.put("Invalid Key", "true");
123         as.updated(props);
124     }
125
126     private class Worker implements Callable<Authentication> {
127         @Override
128         public Authentication call() throws Exception {
129             AuthenticationService as = AuthenticationManager.instance();
130             return as.get();
131         }
132     }
133 }