aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-authn-mdsal-store/aaa-authn-mdsal-store-impl/src/test/java/org/opendaylight/aaa/authn/mdsal/store/util/AuthNStoreUtilTest.java
blob: 10c1879091f883b86c508bc486fd2696a9fc5426 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/*
 * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */

package org.opendaylight.aaa.authn.mdsal.store.util;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.opendaylight.aaa.api.Authentication;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.token_cache_times.token_list.UserTokens;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.Claims;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.ClaimsBuilder;
import org.opendaylight.yang.gen.v1.urn.aaa.yang.authn.claims.rev141029.tokencache.ClaimsKey;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
public class AuthNStoreUtilTest {

    private String token = "foo_token_test";
    private String userId = "123";
    private Long expire = new Long(365);
    @Mock
    private Authentication auth;
    @Mock
    private UserTokens tokens;
    @Mock
    private Claims claims;

    @Test
    public void testCreateInstIdentifierForTokencache() {
        assertTrue(AuthNStoreUtil.createInstIdentifierForTokencache("") == null);
        assertNotNull(AuthNStoreUtil.createInstIdentifierForTokencache(token));
    }

    @Test
    public void testCreateInstIdentifierUserTokens() {
        assertTrue(AuthNStoreUtil.createInstIdentifierUserTokens("", "") == null);
        assertNotNull(AuthNStoreUtil.createInstIdentifierUserTokens(userId, token));
    }

    @Test
    public void testCreateClaimsRecord() {
        assertTrue(AuthNStoreUtil.createClaimsRecord("", null) == null);
        assertNotNull(AuthNStoreUtil.createClaimsRecord(token, auth));
    }

    @Test
    public void testCreateUserTokens() {
        assertTrue(AuthNStoreUtil.createUserTokens("", null) == null);
        assertNotNull(AuthNStoreUtil.createUserTokens(token, expire));
    }

    @Test
    public void testCreateTokenList() {
        assertTrue(AuthNStoreUtil.createTokenList(null, "") == null);
        assertNotNull(AuthNStoreUtil.createTokenList(tokens, userId));
    }

    @Test
    public void testConvertClaimToAuthentication() {
        ClaimsKey claimsKey = new ClaimsKey(token);
        ClaimsBuilder claimsBuilder = new ClaimsBuilder();
        claimsBuilder.setClientId("123");
        claimsBuilder.setDomain("foo_domain");
        claimsBuilder.setKey(claimsKey);
        List<String> roles = new ArrayList<String>();
        roles.add("foo_role");
        claimsBuilder.setRoles(roles);
        claimsBuilder.setToken(token);
        claimsBuilder.setUser("foo_usr");
        claimsBuilder.setUserId(userId);
        Claims fooClaims = claimsBuilder.build();

        assertTrue(AuthNStoreUtil.convertClaimToAuthentication(null, expire) == null);
        assertNotNull(AuthNStoreUtil.convertClaimToAuthentication(fooClaims, expire));
    }

}