2 * Copyright (c) 2016, 2017 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.aaa.datastore.h2;
11 import java.sql.SQLException;
12 import org.junit.AfterClass;
13 import org.junit.Assert;
14 import org.junit.Before;
15 import org.junit.BeforeClass;
16 import org.junit.Test;
17 import org.opendaylight.aaa.api.IDMStoreUtil;
18 import org.opendaylight.aaa.api.IIDMStore;
19 import org.opendaylight.aaa.api.model.Domain;
20 import org.opendaylight.aaa.api.model.Grant;
21 import org.opendaylight.aaa.api.model.Role;
22 import org.opendaylight.aaa.api.model.User;
23 import org.opendaylight.aaa.api.password.service.PasswordHashService;
24 import org.opendaylight.aaa.impl.password.service.DefaultPasswordHashService;
26 public class H2StoreTest {
29 public static void start() {
30 File file = new File("idmlight.db.mv.db");
34 file = new File("idmlight.db.trace.db");
41 public static void end() {
42 File file = new File("idmlight.db.mv.db");
46 file = new File("idmlight.db.trace.db");
52 private H2Store h2Store;
53 private PasswordHashService passwordService = new DefaultPasswordHashService();
56 public void before() throws StoreException, SQLException {
57 IdmLightSimpleConnectionProvider dbConnectionFactory = new IdmLightSimpleConnectionProvider(
58 new IdmLightConfigBuilder().dbUser("foo").dbPwd("bar").build());
59 UserStore us = new UserStore(dbConnectionFactory, passwordService);
61 DomainStore ds = new DomainStore(dbConnectionFactory);
63 RoleStore rs = new RoleStore(dbConnectionFactory);
65 GrantStore gs = new GrantStore(dbConnectionFactory);
68 h2Store = new H2Store("foo", "bar", passwordService);
72 public void testCreateDefaultDomain() throws StoreException {
73 Domain domain = new Domain();
74 Assert.assertEquals(true, domain != null);
75 DomainStore ds = new DomainStore(
76 new IdmLightSimpleConnectionProvider(new IdmLightConfigBuilder().dbUser("foo").dbPwd("bar").build()));
77 domain.setName(IIDMStore.DEFAULT_DOMAIN);
78 domain.setEnabled(true);
79 domain = ds.createDomain(domain);
80 Assert.assertEquals(true, domain != null);
84 public void testCreateTempRole() throws StoreException {
85 Role role = h2Store.createRole("temp", "temp domain", "Temp Testing role");
86 Assert.assertEquals(true, role != null);
90 public void testCreateUser() throws StoreException {
91 User user = h2Store.createUser("test", "pass", "domain", "desc",
92 "email",true, "SALT");
93 Assert.assertEquals(true, user != null);
97 public void testCreateGrant() throws StoreException {
98 Domain domain = h2Store.createDomain("sdn", true);
99 Role role = h2Store.createRole("temp", "temp domain", "Temp Testing role");
100 User user = h2Store.createUser("test", "pass", "domain", "desc",
101 "email", true, "SALT");
102 Grant grant = h2Store.createGrant(domain.getDomainid(), user.getUserid(), role.getRoleid());
103 Assert.assertEquals(true, grant != null);
107 public void testUpdatingUserEmail() throws StoreException {
108 UserStore us = new UserStore(
109 new IdmLightSimpleConnectionProvider(
110 new IdmLightConfigBuilder().dbUser("foo").dbPwd("bar").build()), passwordService);
111 Domain domain = h2Store.createDomain("sdn", true);
112 User user = h2Store.createUser("test", "pass", domain.getDomainid(), "desc",
113 "email", true, "SALT");
115 user.setName("test");
116 user = us.putUser(user);
117 Assert.assertEquals(true, user != null);
119 user.setEmail("Test@Test.com");
120 user = us.putUser(user);
123 user.setName("test");
124 user.setDomainid(domain.getDomainid());
125 user = us.getUser(IDMStoreUtil.createUserid(user.getName(), user.getDomainid()));
127 Assert.assertEquals("Test@Test.com", user.getEmail());
131 * @Test public void testCreateUserViaAPI() throws StoreException { Domain d
132 * = StoreBuilder.createDomain("sdn",true);
134 * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
135 * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
136 * h.createUser(null, user);
138 * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
139 * UserStore us = new UserStore(); u =
140 * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
142 * Assert.assertEquals(true, u != null); }
144 * @Test public void testUpdateUserViaAPI() throws StoreException { Domain d
145 * = StoreBuilder.createDomain("sdn",true);
147 * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
148 * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
149 * h.createUser(null, user);
151 * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
152 * h.putUser(null, user, "" + user.getUserid());
154 * UserStore us = new UserStore();
156 * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
157 * u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
159 * Assert.assertEquals("Hello@Hello.com", u.getEmail());
161 * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
162 * Assert.assertEquals(u.getPassword(), hash); }
164 * @Test public void testUpdateUserRoleViaAPI() throws StoreException {
165 * Domain d = StoreBuilder.createDomain("sdn",true); Role role1 =
166 * StoreBuilder.createRole("temp1",d.getDomainid(),"Temp Testing role");
168 * StoreBuilder.createRole("temp2",d.getDomainid(),"Temp Testing role");
170 * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
171 * user.setDomainid(d.getDomainid());
173 * UserHandler h = new UserHandler(); h.createUser(null, user);
175 * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
176 * h.putUser(null, user, user.getUserid());
178 * Grant g = new Grant(); g.setUserid(user.getUserid());
179 * g.setDomainid(d.getDomainid()); g.setRoleid(role1.getRoleid());
180 * GrantStore gs = new GrantStore(); g = gs.createGrant(g);
182 * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
183 * role1.getRoleid());
185 * g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(),
186 * d.getDomainid(), role1.getRoleid())); g.setRoleid(role2.getRoleid()); g =
189 * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
190 * role2.getRoleid());
192 * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
193 * UserStore us = new UserStore(); u =
194 * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
196 * Assert.assertEquals("Hello@Hello.com", u.getEmail());
198 * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
199 * Assert.assertEquals(true, hash.equals(u.getPassword())); }