[4800] | 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 | |
---|
[5385] | 20 | |
---|
[5393] | 21 | import eu.dasish.annotation.backend.Helpers; |
---|
[4834] | 22 | import eu.dasish.annotation.backend.NotInDataBaseException; |
---|
[4887] | 23 | import eu.dasish.annotation.backend.PrincipalCannotBeDeleted; |
---|
[4800] | 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 |
---|
[5385] | 52 | public void testHrefToExternalID() { |
---|
| 53 | System.out.println("test hrefToExternalID"); |
---|
| 54 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
[5393] | 55 | String randomUUID = Helpers.generateUUID().toString(); |
---|
[5385] | 56 | String uri = "/api/principals/" + randomUUID; |
---|
| 57 | String externalID = jdbcPrincipalDao.hrefToExternalID(uri).toString(); |
---|
[4800] | 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"); |
---|
[5385] | 68 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
[5393] | 69 | String randomUUID = Helpers.generateUUID().toString(); |
---|
[5385] | 70 | String uri = "/api/principals/"+randomUUID; |
---|
| 71 | String uriResult = jdbcPrincipalDao.externalIDtoHref(randomUUID); |
---|
[4800] | 72 | assertEquals(uri, uriResult); |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | /** |
---|
| 76 | * Test of getInternalID method, of class JdbcPrincipalDao. Number |
---|
| 77 | * getInternalID(UUID UUID); |
---|
| 78 | */ |
---|
| 79 | @Test |
---|
[4834] | 80 | public void testGetInternalID() throws NotInDataBaseException{ |
---|
[4800] | 81 | Number testOne = jdbcPrincipalDao.getInternalID(UUID.fromString("00000000-0000-0000-0000-000000000113")); |
---|
| 82 | assertEquals(3, testOne.intValue()); |
---|
| 83 | |
---|
[4834] | 84 | try { |
---|
[4800] | 85 | Number testTwo = jdbcPrincipalDao.getInternalID(UUID.fromString("00000000-0000-0000-0000-000000000ccc")); |
---|
| 86 | assertEquals(null, testTwo); |
---|
[4834] | 87 | } catch (NotInDataBaseException e){ |
---|
| 88 | System.out.println(e); |
---|
| 89 | } |
---|
[4800] | 90 | |
---|
| 91 | } |
---|
| 92 | |
---|
| 93 | /** |
---|
| 94 | * public UUID getExternalID(Number internalId) |
---|
| 95 | */ |
---|
| 96 | @Test |
---|
| 97 | public void testGetExternalID() { |
---|
[5385] | 98 | assertEquals("00000000-0000-0000-0000-000000000113", jdbcPrincipalDao.getExternalID(3).toString()); |
---|
[4800] | 99 | |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | @Test |
---|
| 103 | public void testGetPrincipal() { |
---|
| 104 | System.out.println("test getPrincipal"); |
---|
[5385] | 105 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
[4800] | 106 | Principal result = jdbcPrincipalDao.getPrincipal(1); |
---|
| 107 | assertEquals("Twan", result.getDisplayName()); |
---|
| 108 | assertEquals("Twan.Goosen@mpi.nl", result.getEMail()); |
---|
[5385] | 109 | assertEquals("/api/principals/00000000-0000-0000-0000-000000000111", result.getHref()); |
---|
| 110 | assertEquals("00000000-0000-0000-0000-000000000111", result.getId()); |
---|
[4800] | 111 | } |
---|
| 112 | |
---|
| 113 | @Test |
---|
[4834] | 114 | public void testAddPrincipal() throws NotInDataBaseException{ |
---|
[4800] | 115 | System.out.println("test addPrincipal"); |
---|
[5385] | 116 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
[4800] | 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()); |
---|
[5385] | 129 | assertEquals(addedPrincipal.getHref(), "/api/principals/"+addedPrincipal.getId()); |
---|
| 130 | |
---|
[4800] | 131 | } |
---|
| 132 | |
---|
| 133 | @Test |
---|
[4887] | 134 | public void testDeletePrincipal() throws PrincipalCannotBeDeleted{ |
---|
[4800] | 135 | System.out.println("test deletePrincipal"); |
---|
[5385] | 136 | jdbcPrincipalDao.setResourcePath("/api/principals/"); |
---|
[4800] | 137 | |
---|
| 138 | int result = jdbcPrincipalDao.deletePrincipal(10); |
---|
| 139 | assertEquals(1, result); |
---|
[4834] | 140 | assertEquals(0, jdbcPrincipalDao.deletePrincipal(10)); |
---|
[4800] | 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() { |
---|
[5385] | 153 | System.out.println("test principalExists"); |
---|
[4995] | 154 | assertEquals(false,jdbcPrincipalDao.principalExists("guisil@mpi.nl")); |
---|
[6063] | 155 | assertTrue(jdbcPrincipalDao.principalExists("alice@mail.domain")); |
---|
[4800] | 156 | } |
---|
| 157 | |
---|
| 158 | /** |
---|
| 159 | * Test of getPrincipalIDsWithAccess method, of class JdbcNotebookDao. |
---|
| 160 | */ |
---|
| 161 | @Test |
---|
| 162 | public void testGetPrincipalIDsWithAccess() { |
---|
[4909] | 163 | System.out.println("test getPrincipalIDsWithAccess"); |
---|
[4800] | 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 | } |
---|
[5468] | 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 | |
---|
[5385] | 181 | // |
---|
| 182 | // @Test |
---|
| 183 | // public void generateHashes() { |
---|
| 184 | // System.out.println("*****"); |
---|
| 185 | // System.out.println("generate hashes"); |
---|
[6063] | 186 | // System.out.println(Helpers.hashPswd("1234", 512, "alice@mail.domain")); |
---|
[5385] | 187 | // System.out.println(Helpers.hashPswd("5678", 512, "olasei@mpi.nl")); |
---|
| 188 | // System.out.println("*****"); |
---|
| 189 | // |
---|
| 190 | // } |
---|
[4800] | 191 | |
---|
| 192 | } |
---|