f583a302b8892ca699e7839d1f13126113f2ab0c
[moon.git] /
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. 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.h2.persistence;
10
11 import java.io.File;
12 import java.sql.SQLException;
13
14 import org.junit.AfterClass;
15 import org.junit.Assert;
16 import org.junit.Before;
17 import org.junit.BeforeClass;
18 import org.junit.Test;
19 import org.opendaylight.aaa.api.IDMStoreUtil;
20 import org.opendaylight.aaa.api.IIDMStore;
21 import org.opendaylight.aaa.api.model.Domain;
22 import org.opendaylight.aaa.api.model.Grant;
23 import org.opendaylight.aaa.api.model.Role;
24 import org.opendaylight.aaa.api.model.User;
25
26 public class H2StoreTest {
27     @BeforeClass
28     public static void start() {
29         File f = new File("idmlight.db.mv.db");
30         if (f.exists()) {
31             f.delete();
32         }
33         f = new File("idmlight.db.trace.db");
34         if (f.exists()) {
35             f.delete();
36         }
37     }
38
39     @AfterClass
40     public static void end() {
41         File f = new File("idmlight.db.mv.db");
42         if (f.exists()) {
43             f.delete();
44         }
45         f = new File("idmlight.db.trace.db");
46         if (f.exists()) {
47             f.delete();
48         }
49     }
50
51     @Before
52     public void before() throws StoreException, SQLException {
53         UserStore us = new UserStore();
54         us.dbClean();
55         DomainStore ds = new DomainStore();
56         ds.dbClean();
57         RoleStore rs = new RoleStore();
58         rs.dbClean();
59         GrantStore gs = new GrantStore();
60         gs.dbClean();
61     }
62
63     @Test
64     public void testCreateDefaultDomain() throws StoreException {
65         Domain d = new Domain();
66         Assert.assertEquals(true, d != null);
67         DomainStore ds = new DomainStore();
68         d.setName(IIDMStore.DEFAULT_DOMAIN);
69         d.setEnabled(true);
70         d = ds.createDomain(d);
71         Assert.assertEquals(true, d != null);
72     }
73
74     @Test
75     public void testCreateTempRole() throws StoreException {
76         Role role = H2Store.createRole("temp", "temp domain", "Temp Testing role");
77         Assert.assertEquals(true, role != null);
78     }
79
80     @Test
81     public void testCreateUser() throws StoreException {
82         User user = H2Store.createUser("test", "pass", "domain", "desc", "email", true, "SALT");
83         Assert.assertEquals(true, user != null);
84     }
85
86     @Test
87     public void testCreateGrant() throws StoreException {
88         Domain d = H2Store.createDomain("sdn", true);
89         Role role = H2Store.createRole("temp", "temp domain", "Temp Testing role");
90         User user = H2Store.createUser("test", "pass", "domain", "desc", "email", true, "SALT");
91         Grant g = H2Store.createGrant(d.getDomainid(), user.getUserid(), role.getRoleid());
92         Assert.assertEquals(true, g != null);
93     }
94
95     @Test
96     public void testUpdatingUserEmail() throws StoreException {
97         UserStore us = new UserStore();
98         Domain d = H2Store.createDomain("sdn", true);
99         User user = H2Store.createUser("test", "pass", d.getDomainid(), "desc", "email", true,
100                 "SALT");
101
102         user.setName("test");
103         user = us.putUser(user);
104         Assert.assertEquals(true, user != null);
105
106         user.setEmail("Test@Test.com");
107         user = us.putUser(user);
108
109         user = new User();
110         user.setName("test");
111         user.setDomainid(d.getDomainid());
112         user = us.getUser(IDMStoreUtil.createUserid(user.getName(), user.getDomainid()));
113
114         Assert.assertEquals("Test@Test.com", user.getEmail());
115     }
116     /*
117      * @Test public void testCreateUserViaAPI() throws StoreException { Domain d
118      * = StoreBuilder.createDomain("sdn",true);
119      *
120      * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
121      * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
122      * h.createUser(null, user);
123      *
124      * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
125      * UserStore us = new UserStore(); u =
126      * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
127      *
128      * Assert.assertEquals(true, u != null); }
129      *
130      * @Test public void testUpdateUserViaAPI() throws StoreException { Domain d
131      * = StoreBuilder.createDomain("sdn",true);
132      *
133      * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
134      * user.setDomainid(d.getDomainid()); UserHandler h = new UserHandler();
135      * h.createUser(null, user);
136      *
137      * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
138      * h.putUser(null, user, "" + user.getUserid());
139      *
140      * UserStore us = new UserStore();
141      *
142      * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
143      * u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
144      *
145      * Assert.assertEquals("Hello@Hello.com", u.getEmail());
146      *
147      * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
148      * Assert.assertEquals(u.getPassword(), hash); }
149      *
150      * @Test public void testUpdateUserRoleViaAPI() throws StoreException {
151      * Domain d = StoreBuilder.createDomain("sdn",true); Role role1 =
152      * StoreBuilder.createRole("temp1",d.getDomainid(),"Temp Testing role");
153      * Role role2 =
154      * StoreBuilder.createRole("temp2",d.getDomainid(),"Temp Testing role");
155      *
156      * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
157      * user.setDomainid(d.getDomainid());
158      *
159      * UserHandler h = new UserHandler(); h.createUser(null, user);
160      *
161      * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
162      * h.putUser(null, user, user.getUserid());
163      *
164      * Grant g = new Grant(); g.setUserid(user.getUserid());
165      * g.setDomainid(d.getDomainid()); g.setRoleid(role1.getRoleid());
166      * GrantStore gs = new GrantStore(); g = gs.createGrant(g);
167      *
168      * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
169      * role1.getRoleid());
170      *
171      * g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(),
172      * d.getDomainid(), role1.getRoleid())); g.setRoleid(role2.getRoleid()); g =
173      * gs.createGrant(g);
174      *
175      * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
176      * role2.getRoleid());
177      *
178      * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
179      * UserStore us = new UserStore(); u =
180      * us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
181      *
182      * Assert.assertEquals("Hello@Hello.com", u.getEmail());
183      *
184      * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
185      * Assert.assertEquals(true, hash.equals(u.getPassword())); }
186      */
187 }