1 package org.opendaylight.aaa.h2.persistence;
4 import java.sql.SQLException;
6 import org.junit.AfterClass;
7 import org.junit.Assert;
8 import org.junit.Before;
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 import org.opendaylight.aaa.api.IDMStoreUtil;
12 import org.opendaylight.aaa.api.IIDMStore;
13 import org.opendaylight.aaa.api.model.Domain;
14 import org.opendaylight.aaa.api.model.Grant;
15 import org.opendaylight.aaa.api.model.Role;
16 import org.opendaylight.aaa.api.model.User;
18 public class H2StoreTest {
20 public static void start() {
21 File f = new File("idmlight.db.mv.db");
25 f = new File("idmlight.db.trace.db");
32 public static void end() {
33 File f = new File("idmlight.db.mv.db");
37 f = new File("idmlight.db.trace.db");
44 public void before() throws StoreException, SQLException{
45 UserStore us = new UserStore();
47 DomainStore ds = new DomainStore();
49 RoleStore rs = new RoleStore();
51 GrantStore gs = new GrantStore();
56 public void testCreateDefaultDomain() {
57 Domain d = new Domain();
58 Assert.assertEquals(true, d != null);
59 DomainStore ds = new DomainStore();
60 d.setName(IIDMStore.DEFAULT_DOMAIN);
63 d = ds.createDomain(d);
64 } catch (StoreException e) {
67 Assert.assertEquals(true, d != null);
71 public void testCreateTempRole() throws StoreException {
72 Role role = H2Store.createRole("temp","temp domain","Temp Testing role");
73 Assert.assertEquals(true, role != null);
77 public void testCreateUser() throws StoreException {
78 User user = H2Store.createUser("test","pass","domain","desc","email",true,"SALT");
79 Assert.assertEquals(true, user != null);
83 public void testCreateGrant() throws StoreException {
84 Domain d = H2Store.createDomain("sdn",true);
85 Role role = H2Store.createRole("temp","temp domain","Temp Testing role");
86 User user = H2Store.createUser("test","pass","domain","desc","email",true,"SALT");
87 Grant g = H2Store.createGrant(d.getDomainid(), user.getUserid(), role.getRoleid());
88 Assert.assertEquals(true, g != null);
92 public void testUpdatingUserEmail() throws StoreException {
93 UserStore us = new UserStore();
94 Domain d = H2Store.createDomain("sdn",true);
95 User user = H2Store.createUser("test","pass",d.getDomainid(),"desc","email",true,"SALT");
98 user = us.putUser(user);
99 Assert.assertEquals(true, user != null);
101 user.setEmail("Test@Test.com");
102 user = us.putUser(user);
105 user.setName("test");
106 user.setDomainid(d.getDomainid());
107 user = us.getUser(IDMStoreUtil.createUserid(user.getName(),user.getDomainid()));
109 Assert.assertEquals("Test@Test.com", user.getEmail());
113 public void testCreateUserViaAPI() throws StoreException {
114 Domain d = StoreBuilder.createDomain("sdn",true);
116 User user = new User();
117 user.setName("Hello");
118 user.setPassword("Hello");
119 user.setDomainid(d.getDomainid());
120 UserHandler h = new UserHandler();
121 h.createUser(null, user);
125 u.setDomainid(d.getDomainid());
126 UserStore us = new UserStore();
127 u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
129 Assert.assertEquals(true, u != null);
133 public void testUpdateUserViaAPI() throws StoreException {
134 Domain d = StoreBuilder.createDomain("sdn",true);
136 User user = new User();
137 user.setName("Hello");
138 user.setPassword("Hello");
139 user.setDomainid(d.getDomainid());
140 UserHandler h = new UserHandler();
141 h.createUser(null, user);
143 user.setEmail("Hello@Hello.com");
144 user.setPassword("Test123");
145 h.putUser(null, user, "" + user.getUserid());
147 UserStore us = new UserStore();
151 u.setDomainid(d.getDomainid());
152 u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
154 Assert.assertEquals("Hello@Hello.com", u.getEmail());
156 String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
157 Assert.assertEquals(u.getPassword(), hash);
161 public void testUpdateUserRoleViaAPI() throws StoreException {
162 Domain d = StoreBuilder.createDomain("sdn",true);
163 Role role1 = StoreBuilder.createRole("temp1",d.getDomainid(),"Temp Testing role");
164 Role role2 = StoreBuilder.createRole("temp2",d.getDomainid(),"Temp Testing role");
166 User user = new User();
167 user.setName("Hello");
168 user.setPassword("Hello");
169 user.setDomainid(d.getDomainid());
171 UserHandler h = new UserHandler();
172 h.createUser(null, user);
174 user.setEmail("Hello@Hello.com");
175 user.setPassword("Test123");
176 h.putUser(null, user, user.getUserid());
178 Grant g = new Grant();
179 g.setUserid(user.getUserid());
180 g.setDomainid(d.getDomainid());
181 g.setRoleid(role1.getRoleid());
182 GrantStore gs = new GrantStore();
183 g = gs.createGrant(g);
185 Assert.assertEquals(true, g != null);
186 Assert.assertEquals(g.getRoleid(), role1.getRoleid());
188 g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(), d.getDomainid(), role1.getRoleid()));
189 g.setRoleid(role2.getRoleid());
190 g = gs.createGrant(g);
192 Assert.assertEquals(true, g != null);
193 Assert.assertEquals(g.getRoleid(), role2.getRoleid());
197 u.setDomainid(d.getDomainid());
198 UserStore us = new UserStore();
199 u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
201 Assert.assertEquals("Hello@Hello.com", u.getEmail());
203 String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
204 Assert.assertEquals(true, hash.equals(u.getPassword()));