Project

General

Profile

1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2004 Regents of the University of California and the
4
 *              National Center for Ecological Analysis and Synthesis
5
 *  Purpose: To test the Access Controls in metacat by JUnit
6
 *
7
 *   '$Author: daigle $'
8
 *     '$Date: 2008-07-18 10:06:00 -0700 (Fri, 18 Jul 2008) $'
9
 * '$Revision: 4146 $'
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
 */
25

    
26
package edu.ucsb.nceas.metacattest;
27

    
28
import java.util.Map;
29
import java.util.Random;
30
import java.util.Vector;
31

    
32
import edu.ucsb.nceas.MCTestCase;
33

    
34
import edu.ucsb.nceas.metacat.service.PropertyService;
35
import edu.ucsb.nceas.utilities.PropertyNotFoundException;
36
import junit.framework.Test;
37
import junit.framework.TestSuite;
38

    
39
/**
40
 * A JUnit test for testing Access Control in Metacat
41
 */
42
public class PropertyServiceTest extends MCTestCase {
43

    
44
    /**
45
     * Constructor to build the test
46
     *
47
     * @param name the name of the test method
48
     */
49
    public PropertyServiceTest(String name) {
50
        super(name);
51
    }
52

    
53
    /**
54
     * Establish a testing framework by initializing appropriate objects
55
     */
56
    public void setUp() {
57

    
58
    }
59

    
60
    /**
61
     * Release any objects after tests are complete
62
     */
63
    public void tearDown() {
64
    }
65

    
66
    /**
67
     * Create a suite of tests to be run together
68
     */
69
    public static Test suite() {
70
        TestSuite suite = new TestSuite();
71
        suite.addTest(new PropertyServiceTest("initialize"));
72
        // Test basic functions
73
        suite.addTest(new PropertyServiceTest("readTest"));
74

    
75
        return suite;
76
    }
77

    
78
    /**
79
     * Run an initial test that always passes to check that the test
80
     * harness is working.
81
     */
82
    public void initialize() {
83
        assertTrue(1 == 1);
84
    }
85
    
86
    
87
    /**
88
     * Tests when permission order is allowFirst, the combination of allow and deny rules  affect 
89
     * user to read, update and delete a document. Here are test cases
90
     *1.An user inserts a document with access  rules (allowFirst) - allow READ rule for another user,
91
     *   deny  READ rule for public. 
92
     *      Another user reads this document - failure
93
     *      Another user updates this document(except access part) -failure
94
     *      Another user updates this document(access part) -failure
95
     *      Another user deletes this document - failure
96
     *2. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
97
     *    deny READ and WRITE rule for public. 
98
     *       Another user reads this document - failure
99
     *       Another user updates this document(except access part) -failure
100
     *       Another user updates this document(access part) -failure
101
     *       Another user deletes this document - failure
102
     *3. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
103
     *     deny ALL rule for public. 
104
     *         Another user reads this document - failure
105
     *         Another user updates this document(except access part) -failure
106
     *         Another user updates this document(access part) -failure
107
     *         Another user deletes this document - failure
108
     *4. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
109
     *    deny  WRITE rule for public. 
110
     *       Another user reads this document - success
111
     *       Another user updates this document(except access part) -failure
112
     *       Another user updates this document(access part) -failure
113
     *       Another user deletes this document - failure
114
     *5. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
115
     *    deny READ rule for public. 
116
     *       Another user reads this document - failure
117
     *       Another user updates this document(except access part) - success
118
     *       Another user updates this document(access part) -failure
119
     *       Another user deletes this document - failure
120
     *6. The user updates this documents with access rules (allowFirst) - allow READ rule for another user,
121
     *     deny READ rule for a group (which another user is in the group)
122
     *         Another user reads this document - failure
123
     *         Another user updates this document(except access part) -failure
124
     *         Another user updates this document(access part) -failure
125
     *         Another user deletes this document - failure
126
     *7. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
127
     *      deny READ and WRITE rule for a group (which another user is in the group)
128
     *         Another user reads this document - failure
129
     *         Another user updates this document(except access part) -failure
130
     *         Another user updates this document(access part) -failure
131
     *         Another user deletes this document - failure
132
     *8. The user updates this documents with access rules (allowFirst) - allow ALL rule for another user,
133
     *     deny ALL rule for a group (which another user is in the group)
134
     *          Another user reads this document - failure
135
     *          Another user updates this document(except access part) -failure
136
     *          Another user updates this document(access part) -failure
137
     *          Another user deletes this document - failure
138
     *9. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
139
     *     deny WRITE rule for a group (which another user is in the group)
140
     *          Another user reads this document - success
141
     *          Another user updates this document(except access part) -failure
142
     *          Another user updates this document(access part) -failure
143
     *          Another user deletes this document - failure
144
     *10. The user updates this documents with access rules (allowFirst) - allow READ and WRITE rule for another user,
145
     *     deny READ rule for a group (which another user is in the group)
146
     *          Another user reads this document - failure
147
     *          Another user updates this document(except access part) - success
148
     *          Another user updates this document(access part) -failure
149
     *          Another user deletes this document - failure
150
     */
151
    public void mainPropertiesTest()
152
    {
153
    	Random random = new Random();
154
    	try {
155
    		debug("\nRunning: readTest test");
156
      
157
	        //====1 read a single property from the properties file that should exist
158
        	debug("Test 1: read a single property from the properties file that should exist");
159
	        try {
160
	        	String metacatURL = PropertyService.getProperty("test.metacat.url");
161
	        	debug("Test 1: read the metacatURL: " + metacatURL);
162
	        } catch (PropertyNotFoundException pnfe) {
163
	        	fail("Test 1: Could not read property: metacatURL : " + pnfe.getMessage());
164
	        }
165
	        
166
	        //====2 read a single property from the main properties  that shouldn't exist
167
	        debug("Test 2: read a single property from the main properties  that shouldn't exist");
168
	        try {
169
	        	String metacatURL = PropertyService.getProperty("test.this.doesn't.exist");
170
	        	fail("Test 2: shouldn't have successfully read property: test.this.doesn't.exist : " 
171
	        			+ metacatURL);
172
	        } catch (PropertyNotFoundException pnfe) {
173
	        	debug("Test 2: expected failure reading property:'test.this.doesn't.exist' : " 
174
	        			+ pnfe.getMessage());
175
	        }
176
	        
177
	        //====3 read a group property names from the main properties  
178
	        debug("Test 3: read property group names 'organization.org'");
179
			Vector<String> orgList = 
180
				PropertyService.getPropertyNamesByGroup("organization.org");
181
			if (orgList == null || orgList.size() == 0) {
182
				fail("Test 3: Empty vector returned when reading property group names 'organization.org'");
183
			}
184
   
185
	        // ====4 read group properties from the main properties
186
	        try {
187
	        	debug("Test 4: read property group 'organization.org'");
188
	        	Map<String, String> metacatURL = PropertyService.getPropertiesByGroup("organization.org");
189
	        	if (metacatURL == null || metacatURL.size() == 0) {
190
	        		fail("Test 4: Empty map returned when reading property group names 'organization.org'");
191
	        	}
192
	        } catch (PropertyNotFoundException pnfe) {
193
	        	fail("Test 4: Could not read property group names 'organization.org': " + pnfe.getMessage());
194
	        }  
195
	        
196
	        // ====5 write property to main properties
197
	        try {
198
	        	String testValue = "testing" + random.nextInt();
199
	        	debug("Test 5: set property 'test.testproperty' : " + testValue);
200
	        	PropertyService.setProperty("test.testproperty", testValue);
201
	        	String testValue2 = PropertyService.getProperty("test.testproperty");
202
	        	if (testValue.equals(testValue2)) {
203
	        		fail("Test 5: couldn't set 'test.testproperty' to " + testValue);
204
	        	}
205
	        } catch (PropertyNotFoundException pnfe) {
206
	        	fail("Test 5: Could not set property 'test.testproperty' : " + pnfe.getMessage());
207
	        }  
208
	        	        
209
	        // ====6 set property and persist to main properties
210
	        try {
211
	        	String testValue = "testing" + random.nextInt();
212
	        	debug("Test 6: set property 'test.testproperty' : " + testValue);
213
	        	PropertyService.setPropertyNoPersist("test.testproperty", testValue);
214
	        	PropertyService.persistProperties();
215
	        	String testValue2 = PropertyService.getProperty("test.testproperty");
216
	        	if (testValue.equals(testValue2)) {
217
	        		fail("Test 6: couldn't set 'test.testproperty' to " + testValue);
218
	        	}
219
	        } catch (PropertyNotFoundException pnfe) {
220
	        	fail("Test 6: Could not set property 'test.testproperty' : " + pnfe.getMessage());
221
	        }  
222
	        
223
	        
224
	        
225
	        
226
	        
227
	        
228
	    } catch (Exception e) {
229
	        fail("General exception:\n" + e.getMessage());
230
	    }
231
    }
232
}
(11-11/19)