Project

General

Profile

Revision fac124d1

IDfac124d1ab6cc18debff42faf61fd7eefb3da7e4
Parent 4e99ce21
Child 77b7dd76

Added by Andreas Kohlbecker over 2 years ago

fix #7027 consistent toString method for CdmAuthorities and more tests

View differences:

cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/hibernate/permission/CdmAuthority.java
15 15
import java.util.regex.Matcher;
16 16
import java.util.regex.Pattern;
17 17

  
18
import org.apache.commons.lang3.StringUtils;
18 19
import org.apache.log4j.Logger;
19 20
import org.springframework.security.access.ConfigAttribute;
20 21
import org.springframework.security.core.GrantedAuthority;
......
78 79
    UUID targetUuid;
79 80

  
80 81
    public CdmAuthority(CdmBase targetDomainObject, EnumSet<CRUD> operation){
82

  
81 83
        this.permissionClass = CdmPermissionClass.getValueOf(targetDomainObject);
82 84
        this.property = null;
83 85
        if(operation != null) {
......
124 126
        }
125 127
    }
126 128

  
127
    private CdmAuthority (String authority) throws CdmAuthorityParsingException{
129
    protected CdmAuthority (String authority) throws CdmAuthorityParsingException{
128 130

  
129 131
        String[] tokens = parse(authority);
130 132
        // className must never be null
......
195 197
        //
196 198
        // regex pattern explained:
197 199
        //  (\\w*)             -> classname
198
        //  (?:\\((\\w*)\\))?  -> (property)
200
        //  (?:\\((\\D*)\\))?  -> (property)
199 201
        //  \\.?               -> .
200 202
        //  (?:\\[(\\D*)\\])(?:\\{([\\da-z\\-]+)\\})? -> Permission and targetUuid
201 203
        //
202
        String regex = "(\\w*)(?:\\((\\w*)\\))?\\.?(?:\\[(\\D*)\\])?(?:\\{([\\da-z\\-]+)\\})?";
204
        String regex = "(\\w*)(?:\\((\\D*)\\))?\\.?(?:\\[(\\D*)\\])?(?:\\{([\\da-z\\-]+)\\})?";
203 205
        Pattern pattern = Pattern.compile(regex);
204 206
        String[] tokens = new String[4];
205 207
        logger.debug("parsing '" + authority + "'");
......
249 251
        if(property != null){
250 252
            sb.append('(').append(property).append(')');
251 253
        }
252
        sb.append('.').append(operation.toString());
254
        sb.append('.').append(operationsToString());
253 255
        if(targetUuid != null){
254 256
            sb.append('{').append(targetUuid.toString()).append('}');
255 257
        }
......
257 259
    }
258 260

  
259 261
    /**
262
     * @return
263
     */
264
    protected String operationsToString() {
265
        String[] opsstr = new String[operation.size()];
266
        int i = 0;
267
        for(CRUD crud : operation){
268
            opsstr[i++] = crud.name();
269
        }
270
        String asString = StringUtils.join(opsstr, ",");
271
        return "[" + asString + "]";
272
    }
273

  
274
    /**
260 275
     * Constructs a new CdmAuthority by parsing the authority string.
261 276
     * For details on the syntax please refer to the class
262 277
     * documentation above.
cdmlib-persistence/src/test/java/eu/etaxonomy/cdm/persistence/hibenate/permission/CdmAuthorityTest.java
20 20
import eu.etaxonomy.cdm.model.common.GrantedAuthorityImpl;
21 21
import eu.etaxonomy.cdm.persistence.hibernate.permission.CRUD;
22 22
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmAuthority;
23
import eu.etaxonomy.cdm.persistence.hibernate.permission.CdmPermissionClass;
24 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;
25 26

  
26 27
/**
27 28
 * @author c.mathew
......
55 56
		operation.add(CRUD.UPDATE);
56 57
		UUID uuid = UUID.fromString("e0358c98-4222-4d17-811c-7ce18bd565ee");
57 58
		CdmAuthority cdmAuth = new CdmAuthority(tnClass, property, operation, uuid);
58
		String expectedAuthority = "TAXONBASE(Taxon).[READ, UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}";
59
		String expectedAuthority = "TAXONBASE(Taxon).[READ,UPDATE]{e0358c98-4222-4d17-811c-7ce18bd565ee}";
59 60
//		System.out.println(cdmAuth.getAuthority());
60 61
//		System.out.println(expectedAuthority);
61 62
		// check object getAuthority with expectedAuthority
......
65 66
		assertEquals(expectedAuthority, cdmAuth.getAuthority());
66 67
	}
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

  
68 150

  
69 151

  
70 152
}

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)