Bump versions to 0.14.2-SNAPSHOT
[aaa.git] / aaa-shiro / impl / src / test / java / org / opendaylight / aaa / datastore / h2 / H2StoreTest.java
1 /*
2  * Copyright (c) 2016, 2017 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 package org.opendaylight.aaa.datastore.h2;
9
10 import java.io.File;
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;
25
26 public class H2StoreTest {
27
28     @BeforeClass
29     public static void start() {
30         File file = new File("idmlight.db.mv.db");
31         if (file.exists()) {
32             file.delete();
33         }
34         file = new File("idmlight.db.trace.db");
35         if (file.exists()) {
36             file.delete();
37         }
38     }
39
40     @AfterClass
41     public static void end() {
42         File file = new File("idmlight.db.mv.db");
43         if (file.exists()) {
44             file.delete();
45         }
46         file = new File("idmlight.db.trace.db");
47         if (file.exists()) {
48             file.delete();
49         }
50     }
51
52     private H2Store h2Store;
53     private PasswordHashService passwordService = new DefaultPasswordHashService();
54
55     @Before
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);
60         us.dbClean();
61         DomainStore ds = new DomainStore(dbConnectionFactory);
62         ds.dbClean();
63         RoleStore rs = new RoleStore(dbConnectionFactory);
64         rs.dbClean();
65         GrantStore gs = new GrantStore(dbConnectionFactory);
66         gs.dbClean();
67
68         h2Store = new H2Store("foo", "bar", passwordService);
69     }
70
71     @Test
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);
81     }
82
83     @Test
84     public void testCreateTempRole() throws StoreException {
85         Role role = h2Store.createRole("temp", "temp domain", "Temp Testing role");
86         Assert.assertEquals(true, role != null);
87     }
88
89     @Test
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);
94     }
95
96     @Test
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);
104     }
105
106     @Test
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");
114
115         user.setName("test");
116         user = us.putUser(user);
117         Assert.assertEquals(true, user != null);
118
119         user.setEmail("Test@Test.com");
120         user = us.putUser(user);
121
122         user = new User();
123         user.setName("test");
124         user.setDomainid(domain.getDomainid());
125         user = us.getUser(IDMStoreUtil.createUserid(user.getName(), user.getDomainid()));
126
127         Assert.assertEquals("Test@Test.com", user.getEmail());
128     }
129
130     /*
131      * @Test public void testCreateUserViaAPI() throws StoreException { Domain d
132      * = StoreBuilder.createDomain("sdn",true);
133      *
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);
137      *
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()));
141      *
142      * Assert.assertEquals(true, u != null); }
143      *
144      * @Test public void testUpdateUserViaAPI() throws StoreException { Domain d
145      * = StoreBuilder.createDomain("sdn",true);
146      *
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);
150      *
151      * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
152      * h.putUser(null, user, "" + user.getUserid());
153      *
154      * UserStore us = new UserStore();
155      *
156      * User u = new User(); u.setName("Hello"); u.setDomainid(d.getDomainid());
157      * u = us.getUser(IDMStoreUtil.createUserid(u.getName(),u.getDomainid()));
158      *
159      * Assert.assertEquals("Hello@Hello.com", u.getEmail());
160      *
161      * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
162      * Assert.assertEquals(u.getPassword(), hash); }
163      *
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");
167      * Role role2 =
168      * StoreBuilder.createRole("temp2",d.getDomainid(),"Temp Testing role");
169      *
170      * User user = new User(); user.setName("Hello"); user.setPassword("Hello");
171      * user.setDomainid(d.getDomainid());
172      *
173      * UserHandler h = new UserHandler(); h.createUser(null, user);
174      *
175      * user.setEmail("Hello@Hello.com"); user.setPassword("Test123");
176      * h.putUser(null, user, user.getUserid());
177      *
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);
181      *
182      * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
183      * role1.getRoleid());
184      *
185      * g = gs.deleteGrant(IDMStoreUtil.createGrantid(user.getUserid(),
186      * d.getDomainid(), role1.getRoleid())); g.setRoleid(role2.getRoleid()); g =
187      * gs.createGrant(g);
188      *
189      * Assert.assertEquals(true, g != null); Assert.assertEquals(g.getRoleid(),
190      * role2.getRoleid());
191      *
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()));
195      *
196      * Assert.assertEquals("Hello@Hello.com", u.getEmail());
197      *
198      * String hash = SHA256Calculator.getSHA256("Test123", u.getSalt());
199      * Assert.assertEquals(true, hash.equals(u.getPassword())); }
200      */
201 }