1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.cdm.persistence.hibenate.permission;
|
11
|
|
12
|
import static org.junit.Assert.assertEquals;
|
13
|
|
14
|
import java.util.EnumSet;
|
15
|
import java.util.UUID;
|
16
|
|
17
|
import org.junit.Before;
|
18
|
import org.junit.Test;
|
19
|
|
20
|
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
|
21
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
|
22
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
|
23
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthorityParsingException;
|
24
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionClass;
|
25
|
import eu.etaxonomy.cdm.persistence.hibernate.permission.Operation;
|
26
|
|
27
|
/**
|
28
|
* @author c.mathew
|
29
|
* @created Mar 26, 2013
|
30
|
*/
|
31
|
public class CdmAuthorityTest {
|
32
|
|
33
|
private GrantedAuthorityImpl grantedAuthority;
|
34
|
private String authority;
|
35
|
|
36
|
/**
|
37
|
* @throws java.lang.Exception
|
38
|
*/
|
39
|
@Before
|
40
|
public void setUp() throws Exception {
|
41
|
grantedAuthority = GrantedAuthorityImpl.NewInstance(null);
|
42
|
authority = "";
|
43
|
}
|
44
|
|
45
|
/**
|
46
|
* Test method for {@link eu.etaxonomy.cdm.persistence.hibernate.permission#getAuthority()}.
|
47
|
* @throws CdmAuthorityParsingException
|
48
|
*/
|
49
|
@Test
|
50
|
public final void testGetAuthority() throws CdmAuthorityParsingException {
|
51
|
// create CdmAuthority object manually
|
52
|
CdmPermissionClass tnClass = CdmPermissionClass.TAXONBASE;
|
53
|
String property = "Taxon";
|
54
|
EnumSet<CRUD> operation = EnumSet.noneOf(CRUD.class);
|
55
|
operation.add(CRUD.READ);
|
56
|
operation.add(CRUD.UPDATE);
|
57
|
UUID uuid = UUID.fromString("e0358c98-4222-4d17-811c-7ce18bd565ee");
|
58
|
CdmAuthority cdmAuth = new CdmAuthority(tnClass, property, operation, uuid);
|
59
|
String expectedAuthority = "TAXONBASE(Taxon).[READ,UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}";
|
60
|
// System.out.println(cdmAuth.getAuthority());
|
61
|
// System.out.println(expectedAuthority);
|
62
|
// check object getAuthority with expectedAuthority
|
63
|
assertEquals(expectedAuthority, cdmAuth.getAuthority());
|
64
|
//check programmatic generation of CdmAuthority by parsing authority string
|
65
|
cdmAuth = CdmAuthority.fromGrantedAuthority(cdmAuth);
|
66
|
assertEquals(expectedAuthority, cdmAuth.getAuthority());
|
67
|
}
|
68
|
|
69
|
@Test
|
70
|
public final void testparse_1() throws CdmAuthorityParsingException {
|
71
|
TestCdmAuthority auth = new TestCdmAuthority();
|
72
|
String[] tokens = auth.parseForTest("TAXONBASE(Taxon).[READ,UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}");
|
73
|
assertEquals("TAXONBASE", tokens[0]);
|
74
|
assertEquals("Taxon", tokens[1]);
|
75
|
assertEquals("READ,UPDATE", tokens[2]);
|
76
|
assertEquals("e0358c98-4222-4d17-811c-7ce18bd565ee", tokens[3]);
|
77
|
}
|
78
|
|
79
|
@Test
|
80
|
public final void testparse_2() throws CdmAuthorityParsingException {
|
81
|
TestCdmAuthority auth = new TestCdmAuthority();
|
82
|
String[] tokens = auth.parseForTest("TAXONBASE(Foo,Bar).[READ,UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}");
|
83
|
assertEquals("TAXONBASE", tokens[0]);
|
84
|
assertEquals("Foo,Bar", tokens[1]);
|
85
|
assertEquals("READ,UPDATE", tokens[2]);
|
86
|
assertEquals("e0358c98-4222-4d17-811c-7ce18bd565ee", tokens[3]);
|
87
|
}
|
88
|
|
89
|
@Test
|
90
|
public final void testparse_3() throws CdmAuthorityParsingException {
|
91
|
TestCdmAuthority auth = new TestCdmAuthority();
|
92
|
String[] tokens = auth.parseForTest("REGISTRATION(PREPARATION,READY).[UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}");
|
93
|
assertEquals("REGISTRATION", tokens[0]);
|
94
|
assertEquals("PREPARATION,READY", tokens[1]);
|
95
|
assertEquals("UPDATE", tokens[2]);
|
96
|
assertEquals("e0358c98-4222-4d17-811c-7ce18bd565ee", tokens[3]);
|
97
|
}
|
98
|
|
99
|
/**
|
100
|
* Without whitespace in "[UPDATE,DELETE]"
|
101
|
*
|
102
|
* see https://dev.e-taxonomy.eu/redmine/issues/7027
|
103
|
*
|
104
|
* @throws CdmAuthorityParsingException
|
105
|
*/
|
106
|
@Test
|
107
|
public final void testFromString_issue7027_A() throws CdmAuthorityParsingException {
|
108
|
TestCdmAuthority auth = new TestCdmAuthority("REGISTRATION(PREPARATION,READY).[UPDATE, DELETE]{e0358c98-4222-4d17-811c-7ce18bd565ee}");
|
109
|
assertEquals(CdmPermissionClass.REGISTRATION, auth.getPermissionClass());
|
110
|
assertEquals("PREPARATION,READY", auth.getProperty());
|
111
|
assertEquals(EnumSet.of(CRUD.UPDATE, CRUD.DELETE), auth.getOperation());
|
112
|
assertEquals("e0358c98-4222-4d17-811c-7ce18bd565ee", auth.getTargetUUID().toString());
|
113
|
}
|
114
|
|
115
|
/**
|
116
|
* With whitespace in "[UPDATE, DELETE]"
|
117
|
*
|
118
|
* see https://dev.e-taxonomy.eu/redmine/issues/7027
|
119
|
*
|
120
|
* @throws CdmAuthorityParsingException
|
121
|
*/
|
122
|
@Test
|
123
|
public final void testFromString_issue7027_B() throws CdmAuthorityParsingException {
|
124
|
TestCdmAuthority auth = new TestCdmAuthority("REGISTRATION(PREPARATION,READY).[UPDATE,DELETE]{e0358c98-4222-4d17-811c-7ce18bd565ee}");
|
125
|
assertEquals(CdmPermissionClass.REGISTRATION, auth.getPermissionClass());
|
126
|
assertEquals("PREPARATION,READY", auth.getProperty());
|
127
|
assertEquals(EnumSet.of(CRUD.UPDATE, CRUD.DELETE), auth.getOperation());
|
128
|
assertEquals("e0358c98-4222-4d17-811c-7ce18bd565ee", auth.getTargetUUID().toString());
|
129
|
}
|
130
|
|
131
|
|
132
|
@SuppressWarnings("serial")
|
133
|
class TestCdmAuthority extends CdmAuthority {
|
134
|
|
135
|
public TestCdmAuthority() {
|
136
|
// just create a dummy instance
|
137
|
super(CdmPermissionClass.REGISTRATION, Operation.UPDATE);
|
138
|
}
|
139
|
|
140
|
public TestCdmAuthority(String string) throws CdmAuthorityParsingException {
|
141
|
super(string);
|
142
|
}
|
143
|
|
144
|
public String[] parseForTest(String authority) throws CdmAuthorityParsingException {
|
145
|
return super.parse(authority);
|
146
|
}
|
147
|
|
148
|
}
|
149
|
|
150
|
|
151
|
|
152
|
}
|
153
|
|