MD-SAL Store for IDM
[aaa.git] / aaa-h2-store / src / test / java / org / opendaylight / aaa / h2 / persistence / H2StoreTest.java
1 package org.opendaylight.aaa.h2.persistence;
2
3 import java.io.File;
4 import java.sql.SQLException;
5
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;
17
18 public class H2StoreTest {
19     @BeforeClass
20     public static void start() {
21         File f = new File("idmlight.db.mv.db");
22         if(f.exists()){
23             f.delete();
24         }
25         f = new File("idmlight.db.trace.db");
26         if(f.exists()){
27             f.delete();
28         }
29     }
30
31     @AfterClass
32     public static void end() {
33         File f = new File("idmlight.db.mv.db");
34         if(f.exists()){
35             f.delete();
36         }
37         f = new File("idmlight.db.trace.db");
38         if(f.exists()){
39             f.delete();
40         }
41     }
42
43     @Before
44     public void before() throws StoreException, SQLException{
45         UserStore us = new UserStore();
46         us.dbClean();
47         DomainStore ds = new DomainStore();
48         ds.dbClean();
49         RoleStore rs = new RoleStore();
50         rs.dbClean();
51         GrantStore gs = new GrantStore();
52         gs.dbClean();
53     }
54
55     @Test
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);
61         d.setEnabled(true);
62         try {
63            d = ds.createDomain(d);
64         } catch (StoreException e) {
65            e.printStackTrace();
66         }
67         Assert.assertEquals(true, d != null);
68     }
69
70     @Test
71     public void testCreateTempRole() throws StoreException {
72         Role role = H2Store.createRole("temp","temp domain","Temp Testing role");
73         Assert.assertEquals(true, role != null);
74     }
75
76     @Test
77     public void testCreateUser() throws StoreException {
78         User user = H2Store.createUser("test","pass","domain","desc","email",true,"SALT");
79         Assert.assertEquals(true, user != null);
80     }
81
82     @Test
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);
89     }
90
91     @Test
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");
96
97         user.setName("test");
98         user = us.putUser(user);
99         Assert.assertEquals(true, user != null);
100
101         user.setEmail("Test@Test.com");
102         user = us.putUser(user);
103
104         user = new User();
105         user.setName("test");
106         user.setDomainid(d.getDomainid());
107         user = us.getUser(IDMStoreUtil.createUserid(user.getName(),user.getDomainid()));
108
109         Assert.assertEquals("Test@Test.com", user.getEmail());
110     }
111     /*
112     @Test
113     public void testCreateUserViaAPI() throws StoreException {
114         Domain d = StoreBuilder.createDomain("sdn",true);
115
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);
122
123         User u = new User();
124         u.setName("Hello");
125         u.setDomainid(d.getDomainid());
126         UserStore us = new UserStore();
127         u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
128
129         Assert.assertEquals(true, u != null);
130     }
131
132     @Test
133     public void testUpdateUserViaAPI() throws StoreException {
134         Domain d = StoreBuilder.createDomain("sdn",true);
135
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);
142
143         user.setEmail("Hello@Hello.com");
144         user.setPassword("Test123");
145         h.putUser(null, user, "" + user.getUserid());
146
147         UserStore us = new UserStore();
148
149         User u = new User();
150         u.setName("Hello");
151         u.setDomainid(d.getDomainid());
152         u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
153
154         Assert.assertEquals("Hello@Hello.com", u.getEmail());
155
156         String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
157         Assert.assertEquals(u.getPassword(), hash);
158     }
159
160     @Test
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");
165
166         User user = new User();
167         user.setName("Hello");
168         user.setPassword("Hello");
169         user.setDomainid(d.getDomainid());
170
171         UserHandler h = new UserHandler();
172         h.createUser(null, user);
173
174         user.setEmail("Hello@Hello.com");
175         user.setPassword("Test123");
176         h.putUser(null, user, user.getUserid());
177
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);
184
185         Assert.assertEquals(true, g != null);
186         Assert.assertEquals(g.getRoleid(), role1.getRoleid());
187
188         g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(), d.getDomainid(), role1.getRoleid()));
189         g.setRoleid(role2.getRoleid());
190         g = gs.createGrant(g);
191
192         Assert.assertEquals(true, g != null);
193         Assert.assertEquals(g.getRoleid(), role2.getRoleid());
194
195         User u = new User();
196         u.setName("Hello");
197         u.setDomainid(d.getDomainid());
198         UserStore us = new UserStore();
199         u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
200
201         Assert.assertEquals("Hello@Hello.com", u.getEmail());
202
203         String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
204         Assert.assertEquals(true, hash.equals(u.getPassword()));
205     }*/
206 }