e5c837bff4f18ebc4b05526b4a3aa2260f3dc4eb
[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.store;
10
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13 import static org.mockito.Mockito.mock;
14 import static org.opendaylight.aaa.store.DefaultTokenStore.MAX_CACHED_DISK;
15 import static org.opendaylight.aaa.store.DefaultTokenStore.MAX_CACHED_MEMORY;
16 import static org.opendaylight.aaa.store.DefaultTokenStore.SECS_TO_IDLE;
17 import static org.opendaylight.aaa.store.DefaultTokenStore.SECS_TO_LIVE;
18
19 import java.util.Dictionary;
20 import java.util.Hashtable;
21 import org.apache.felix.dm.Component;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.opendaylight.aaa.AuthenticationBuilder;
26 import org.opendaylight.aaa.ClaimBuilder;
27 import org.opendaylight.aaa.api.Authentication;
28 import org.osgi.service.cm.ConfigurationException;
29
30 public class DefaultTokenStoreTest {
31     private static final String FOO_TOKEN = "foo_token";
32     private final DefaultTokenStore dts = new DefaultTokenStore();
33     private static final Dictionary<String, String> config = new Hashtable<>();
34     static {
35         config.put(MAX_CACHED_MEMORY, Long.toString(3));
36         config.put(MAX_CACHED_DISK, Long.toString(3));
37         config.put(SECS_TO_IDLE, Long.toString(1));
38         config.put(SECS_TO_LIVE, Long.toString(1));
39     }
40
41     @Before
42     public void setup() throws ConfigurationException {
43         dts.init(mock(Component.class));
44         dts.updated(config);
45     }
46
47     @After
48     public void teardown() {
49         dts.destroy();
50     }
51
52     @Test
53     public void testCache() throws InterruptedException {
54         Authentication auth = new AuthenticationBuilder(new ClaimBuilder().setUser("foo")
55                                                                           .setUserId("1234")
56                                                                           .addRole("admin").build()).build();
57         dts.put(FOO_TOKEN, auth);
58         assertEquals(auth, dts.get(FOO_TOKEN));
59         dts.delete(FOO_TOKEN);
60         assertNull(dts.get(FOO_TOKEN));
61         dts.put(FOO_TOKEN, auth);
62         Thread.sleep(1200);
63         assertNull(dts.get(FOO_TOKEN));
64     }
65
66 }