aboutsummaryrefslogtreecommitdiffstats
path: root/upstream/odl-aaa-moon/aaa/aaa-h2-store/src/test/java/org/opendaylight/aaa/h2/persistence/H2StoreTest.java
blob: f583a302b8892ca699e7839d1f13126113f2ab0c (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * 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.h2.persistence;

import java.io.File;
import java.sql.SQLException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.aaa.api.IDMStoreUtil;
import org.opendaylight.aaa.api.IIDMStore;
import org.opendaylight.aaa.api.model.Domain;
import org.opendaylight.aaa.api.model.Grant;
import org.opendaylight.aaa.api.model.Role;
import org.opendaylight.aaa.api.model.User;

public class H2StoreTest {
    @BeforeClass
    public static void start() {
        File f = new File("idmlight.db.mv.db");
        if (f.exists()) {
            f.delete();
        }
        f = new File("idmlight.db.trace.db");
        if (f.exists()) {
            f.delete();
        }
    }

    @AfterClass
    public static void end() {
        File f = new File("idmlight.db.mv.db");
        if (f.exists()) {
            f.delete();
        }
        f = new File("idmlight.db.trace.db");
        if (f.exists()) {
            f.delete();
        }
    }

    @Before
    public void before() throws StoreException, SQLException {
        UserStore us = new UserStore();
        us.dbClean();
        DomainStore ds = new DomainStore();
        ds.dbClean();
        RoleStore rs = new RoleStore();
        rs.dbClean();
        GrantStore gs = new GrantStore();
        gs.dbClean();
    }

    @Test
    public void testCreateDefaultDomain() throws StoreException {
        Domain d = new Domain();
        Assert.assertEquals(true, d != null);
        DomainStore ds = new DomainStore();
        d.setName(IIDMStore.DEFAULT_DOMAIN);
        d.setEnabled(true);
        d = ds.createDomain(d);
        Assert.assertEquals(true, d != null);
    }

    @Test
    public void testCreateTempRole() throws StoreException {
        Role role = H2Store.createRole("temp", "temp domain", "Temp Testing role");
        Assert.assertEquals(true, role != null);
    }

    @Test
    public void testCreateUser() throws StoreException {
        User user = H2Store.createUser("test", "pass", "domain", "desc", "email", true, "SALT");
        Assert.assertEquals(true, user != null);
    }

    @Test
    public void testCreateGrant() throws StoreException {
        Domain d = H2Store.createDomain("sdn", true);
        Role role = H2Store.createRole("temp", "temp domain", "Temp Testing role");
        User user = H2Store.createUser("test", "pass", "domain", "desc", "email", true, "SALT");
        Grant g = H2Store.createGrant(d.getDomainid(), user.getUserid(), role.getRoleid());
        Assert.assertEquals(true, g != null);
    }

    @Test
    public void testUpdatingUserEmail() throws StoreException {
        UserStore us = new UserStore();
        Domain d = H2Store.createDomain("sdn", true);
        User user = H2Store.createUser("test", "pass", d.getDomainid(), "desc", "email", true,
                "SALT");

        user.setName("test");
        user = us.putUser(user);
        Assert.assertEquals(true, user != null);

        user.setEmail("Test@Test.com");
        user = us.putUser(user);

        user = new User();
        user.setName("test");
        user.setDomainid(d.getDomainid());
        user = us.getUser(IDMStoreUtil.createUserid(user.getName(), user.getDomainid()));

        Assert.assertEquals("Test@Test.com", user.getEmail());
    }
    /*
     * @Test public void testCreateUserViaAPI() throws StoreException { Domain d
     * = StoreBuilder.createDomain("sdn",true);
     *
     * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
     * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
     * h.createUser(null, user);
     *
     * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
     * UserStore us = new UserStore(); u =
     * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
     *
     * Assert.assertEquals(true, u != null); }
     *
     * @Test public void testUpdateUserViaAPI() throws StoreException { Domain d
     * = StoreBuilder.createDomain("sdn",true);
     *
     * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
     * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
     * h.createUser(null, user);
     *
     * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
     * h.putUser(null, user, "" + user.getUserid());
     *
     * UserStore us = new UserStore();
     *
     * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
     * u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
     *
     * Assert.assertEquals("Hello@Hello.com", u.getEmail());
     *
     * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
     * Assert.assertEquals(u.getPassword(), hash); }
     *
     * @Test public void testUpdateUserRoleViaAPI() throws StoreException {
     * Domain d = StoreBuilder.createDomain("sdn",true); Role role1 =
     * StoreBuilder.createRole("temp1",d.getDomainid(),"Temp Testing role");
     * Role role2 =
     * StoreBuilder.createRole("temp2",d.getDomainid(),"Temp Testing role");
     *
     * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
     * user.setDomainid(d.getDomainid());
     *
     * UserHandler h = new UserHandler(); h.createUser(null, user);
     *
     * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
     * h.putUser(null, user, user.getUserid());
     *
     * Grant g = new Grant(); g.setUserid(user.getUserid());
     * g.setDomainid(d.getDomainid()); g.setRoleid(role1.getRoleid());
     * GrantStore gs = new GrantStore(); g = gs.createGrant(g);
     *
     * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
     * role1.getRoleid());
     *
     * g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(),
     * d.getDomainid(), role1.getRoleid())); g.setRoleid(role2.getRoleid()); g =
     * gs.createGrant(g);
     *
     * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
     * role2.getRoleid());
     *
     * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
     * UserStore us = new UserStore(); u =
     * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
     *
     * Assert.assertEquals("Hello@Hello.com", u.getEmail());
     *
     * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
     * Assert.assertEquals(true, hash.equals(u.getPassword())); }
     */
}