1 | /* |
---|
2 | * Copyright (C) 2013 DASISH |
---|
3 | * |
---|
4 | * This program is free software; you can redistribute it and/or |
---|
5 | * modify it under the terms of the GNU General Public License |
---|
6 | * as published by the Free Software Foundation; either version 2 |
---|
7 | * of the License, or (at your option) any later version. |
---|
8 | * |
---|
9 | * This program is distributed in the hope that it will be useful, |
---|
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
12 | * GNU General Public License for more details. |
---|
13 | * |
---|
14 | * You should have received a copy of the GNU General Public License |
---|
15 | * along with this program; if not, write to the Free Software |
---|
16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
---|
17 | */ |
---|
18 | package eu.dasish.annotation.backend.dao.impl; |
---|
19 | |
---|
20 | |
---|
21 | import eu.dasish.annotation.backend.Helpers; |
---|
22 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
23 | import eu.dasish.annotation.backend.PrincipalCannotBeDeleted; |
---|
24 | import eu.dasish.annotation.schema.Access; |
---|
25 | import eu.dasish.annotation.schema.Principal; |
---|
26 | import java.util.ArrayList; |
---|
27 | import java.util.List; |
---|
28 | import java.util.UUID; |
---|
29 | import org.junit.Test; |
---|
30 | import static org.junit.Assert.*; |
---|
31 | import org.junit.runner.RunWith; |
---|
32 | import org.springframework.beans.factory.annotation.Autowired; |
---|
33 | import org.springframework.test.context.ContextConfiguration; |
---|
34 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
---|
35 | |
---|
36 | /** |
---|
37 | * |
---|
38 | * @author olhsha |
---|
39 | */ |
---|
40 | @RunWith(SpringJUnit4ClassRunner.class) |
---|
41 | @ContextConfiguration({"/spring-test-config/dataSource.xml", "/spring-config/principalDao.xml"}) |
---|
42 | public class JdbcPrincipalDaoTest extends JdbcResourceDaoTest { |
---|
43 | |
---|
44 | @Autowired |
---|
45 | JdbcPrincipalDao jdbcPrincipalDao; |
---|
46 | |
---|
47 | /** |
---|
48 | * Test of stringURItoExternalID method |
---|
49 | * public String stringURItoExternalID(String uri); |
---|
50 | */ |
---|
51 | @Test |
---|
52 | public void testHrefToExternalID() { |
---|
53 | System.out.println("test hrefToExternalID"); |
---|
54 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
55 | String randomUUID = Helpers.generateUUID().toString(); |
---|
56 | String uri = "/api/principals/" + randomUUID; |
---|
57 | String externalID = jdbcPrincipalDao.hrefToExternalID(uri).toString(); |
---|
58 | assertEquals(randomUUID, externalID); |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Test of externalIDtoURI method |
---|
63 | * public String externalIDtoURI(String externalID); |
---|
64 | */ |
---|
65 | @Test |
---|
66 | public void testExternalIDtoURI() { |
---|
67 | System.out.println("test stringURItoExternalID"); |
---|
68 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
69 | String randomUUID = Helpers.generateUUID().toString(); |
---|
70 | String uri = "/api/principals/"+randomUUID; |
---|
71 | String uriResult = jdbcPrincipalDao.externalIDtoHref(randomUUID); |
---|
72 | assertEquals(uri, uriResult); |
---|
73 | } |
---|
74 | |
---|
75 | /** |
---|
76 | * Test of getInternalID method, of class JdbcPrincipalDao. Number |
---|
77 | * getInternalID(UUID UUID); |
---|
78 | */ |
---|
79 | @Test |
---|
80 | public void testGetInternalID() throws NotInDataBaseException{ |
---|
81 | Number testOne = jdbcPrincipalDao.getInternalID(UUID.fromString("00000000-0000-0000-0000-000000000113")); |
---|
82 | assertEquals(3, testOne.intValue()); |
---|
83 | |
---|
84 | try { |
---|
85 | Number testTwo = jdbcPrincipalDao.getInternalID(UUID.fromString("00000000-0000-0000-0000-000000000ccc")); |
---|
86 | assertEquals(null, testTwo); |
---|
87 | } catch (NotInDataBaseException e){ |
---|
88 | System.out.println(e); |
---|
89 | } |
---|
90 | |
---|
91 | } |
---|
92 | |
---|
93 | /** |
---|
94 | * public UUID getExternalID(Number internalId) |
---|
95 | */ |
---|
96 | @Test |
---|
97 | public void testGetExternalID() { |
---|
98 | assertEquals("00000000-0000-0000-0000-000000000113", jdbcPrincipalDao.getExternalID(3).toString()); |
---|
99 | |
---|
100 | } |
---|
101 | |
---|
102 | @Test |
---|
103 | public void testGetPrincipal() { |
---|
104 | System.out.println("test getPrincipal"); |
---|
105 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
106 | Principal result = jdbcPrincipalDao.getPrincipal(1); |
---|
107 | assertEquals("Twan", result.getDisplayName()); |
---|
108 | assertEquals("Twan.Goosen@mpi.nl", result.getEMail()); |
---|
109 | assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", result.getHref()); |
---|
110 | assertEquals("00000000-0000-0000-0000-000000000111", result.getId()); |
---|
111 | } |
---|
112 | |
---|
113 | @Test |
---|
114 | public void testAddPrincipal() throws NotInDataBaseException{ |
---|
115 | System.out.println("test addPrincipal"); |
---|
116 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
117 | String freshPrincipalName = "Guilherme"; |
---|
118 | String freshPrincipalEmail = "guisil@mpi.nl"; |
---|
119 | |
---|
120 | Principal freshPrincipal = new Principal(); |
---|
121 | freshPrincipal.setDisplayName(freshPrincipalName); |
---|
122 | freshPrincipal.setEMail(freshPrincipalEmail); |
---|
123 | |
---|
124 | Number result = jdbcPrincipalDao.addPrincipal(freshPrincipal, "secret X"); |
---|
125 | assertEquals(12, result.intValue()); |
---|
126 | Principal addedPrincipal = jdbcPrincipalDao.getPrincipal(result); |
---|
127 | assertEquals(freshPrincipalName, addedPrincipal.getDisplayName()); |
---|
128 | assertEquals(freshPrincipalEmail, addedPrincipal.getEMail()); |
---|
129 | assertEquals(addedPrincipal.getHref(), "/api/principals/"+addedPrincipal.getId()); |
---|
130 | |
---|
131 | } |
---|
132 | |
---|
133 | @Test |
---|
134 | public void testDeletePrincipal() throws PrincipalCannotBeDeleted{ |
---|
135 | System.out.println("test deletePrincipal"); |
---|
136 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
137 | |
---|
138 | int result = jdbcPrincipalDao.deletePrincipal(10); |
---|
139 | assertEquals(1, result); |
---|
140 | assertEquals(0, jdbcPrincipalDao.deletePrincipal(10)); |
---|
141 | } |
---|
142 | |
---|
143 | |
---|
144 | @Test |
---|
145 | public void testPrincipalIsInUse(){ |
---|
146 | assertTrue(jdbcPrincipalDao.principalIsInUse(1)); |
---|
147 | assertTrue(jdbcPrincipalDao.principalIsInUse(3)); |
---|
148 | assertFalse(jdbcPrincipalDao.principalIsInUse(10)); |
---|
149 | } |
---|
150 | |
---|
151 | @Test |
---|
152 | public void tesPrincipalExists() { |
---|
153 | System.out.println("test principalExists"); |
---|
154 | assertEquals(false,jdbcPrincipalDao.principalExists("guisil@mpi.nl")); |
---|
155 | assertTrue(jdbcPrincipalDao.principalExists("olhsha@mpi.nl")); |
---|
156 | } |
---|
157 | |
---|
158 | /** |
---|
159 | * Test of getPrincipalIDsWithAccess method, of class JdbcNotebookDao. |
---|
160 | */ |
---|
161 | @Test |
---|
162 | public void testGetPrincipalIDsWithAccess() { |
---|
163 | System.out.println("test getPrincipalIDsWithAccess"); |
---|
164 | List<Number> expResult = new ArrayList<Number>(); |
---|
165 | expResult.add(2); |
---|
166 | expResult.add(4); |
---|
167 | List result = jdbcPrincipalDao.getPrincipalIDsWithAccessForNotebook(1, Access.WRITE); |
---|
168 | assertEquals(expResult, result); |
---|
169 | } |
---|
170 | |
---|
171 | @Test |
---|
172 | public void testUpdateIdentifier() throws NotInDataBaseException{ |
---|
173 | System.out.println("test updateResourceIdentifier "); |
---|
174 | |
---|
175 | boolean result = jdbcPrincipalDao.updateResourceIdentifier(UUID.fromString("00000000-0000-0000-0000-000000000111"), UUID.fromString("a0000000-0000-0000-0000-000000000111")); |
---|
176 | assertEquals(true, result); |
---|
177 | Number id = jdbcPrincipalDao.getInternalID(UUID.fromString("a0000000-0000-0000-0000-000000000111")); |
---|
178 | assertEquals(1, id); |
---|
179 | } |
---|
180 | |
---|
181 | // |
---|
182 | // @Test |
---|
183 | // public void generateHashes() { |
---|
184 | // System.out.println("*****"); |
---|
185 | // System.out.println("generate hashes"); |
---|
186 | // System.out.println(Helpers.hashPswd("1234", 512, "olhsha@mpi.nl")); |
---|
187 | // System.out.println(Helpers.hashPswd("5678", 512, "olasei@mpi.nl")); |
---|
188 | // System.out.println("*****"); |
---|
189 | // |
---|
190 | // } |
---|
191 | |
---|
192 | } |
---|