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: leinfelder $'
8
 *     '$Date: 2011-12-13 11:51:20 -0800 (Tue, 13 Dec 2011) $'
9
 * '$Revision: 6776 $'
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.metacat.replication;
27

    
28
import java.io.InputStream;
29
import java.net.URL;
30

    
31
import org.apache.commons.io.IOUtils;
32

    
33
import junit.framework.Test;
34
import junit.framework.TestSuite;
35
import edu.ucsb.nceas.MCTestCase;
36
import edu.ucsb.nceas.metacat.MetaCatServlet;
37
import edu.ucsb.nceas.metacat.client.Metacat;
38
import edu.ucsb.nceas.metacat.client.MetacatFactory;
39
import edu.ucsb.nceas.metacat.client.MetacatInaccessibleException;
40
import edu.ucsb.nceas.metacat.properties.PropertyService;
41
import edu.ucsb.nceas.metacat.util.DocumentUtil;
42
import edu.ucsb.nceas.metacat.util.MetacatUtil;
43

    
44
/**
45
 * A JUnit test for testing Metacat replication
46
 */
47
public class ReplicationTest
48
    extends MCTestCase {
49
    
50
    private static final long forceReplicationSleep = 1 * 60 * 1000;
51
	private String targetReplicationServer = null;
52
	private Metacat targetMetacat = null;
53
    
54
	/**
55
     * Constructor to build the test
56
     *
57
     * @param name the name of the test method
58
     */
59
    public ReplicationTest(String name) {
60
        super(name);
61
    }
62

    
63
    /**
64
     * Establish a testing framework by initializing appropriate objects
65
     */
66
    public void setUp() {
67
        try {
68
        	// get the target ("B server")
69
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
70
            m = MetacatFactory.createMetacatConnection(metacatUrl);
71
            targetMetacat = MetacatFactory.createMetacatConnection("https://" + targetReplicationServer + "/metacat");
72

    
73
        }
74
        catch (MetacatInaccessibleException mie) {
75
            System.err.println("Metacat is: " + metacatUrl);
76
            fail("Metacat connection failed." + mie.getMessage());
77
        } catch (Exception e) {
78
        	e.printStackTrace();
79
            fail(e.getMessage());
80
        }
81
    }
82

    
83
    /**
84
     * Release any objects after tests are complete
85
     */
86
    public void tearDown() {
87
    }
88

    
89
    /**
90
     * Create a suite of tests to be run together
91
     */
92
    public static Test suite() {
93
        TestSuite suite = new TestSuite();
94
        suite.addTest(new ReplicationTest("initialize"));
95
        // Test basic functions
96
        suite.addTest(new ReplicationTest("testCertificate"));
97
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
98
//        suite.addTest(new ReplicationTest("testReplicateData_BtoA"));
99

    
100
        return suite;
101
    }
102

    
103
    /**
104
     * Run an initial test that always passes to check that the test
105
     * harness is working.
106
     */
107
    public void initialize() {
108
        assertTrue(1 == 1);
109
    }
110
    
111
    public void testCertificate() {
112
    	try {
113
    		URL u = new URL("https://" + targetReplicationServer  + "/servlet/replication?server=" + MetacatUtil.getLocalReplicationServerName() + "&action=test");
114
			String test = ReplicationService.getURLContent(u);
115
			assertTrue(test.contains("Test successfully"));
116
			
117
    	} catch (Exception e) {
118
    		e.printStackTrace();
119
			fail(e.getMessage());
120
		}
121
    }
122
    
123
    public void testReplicateData_AtoB() {
124
    	try {
125
    		// the id
126
    		String docid = DocumentUtil.generateDocumentId("replicationTest", 1);
127
    		
128
    		// the test data
129
    		String object = "test";
130
    		
131
			// insert data locally
132
    		m.login(username, password);
133
    		m.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
134
    		
135
    		// wait for replication (forced)
136
    		Thread.sleep(forceReplicationSleep);
137
    		
138
    		// check the target for the same data
139
    		targetMetacat.login(username, password);
140
    		InputStream is = targetMetacat.read(docid);
141
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
142
    		
143
    		assertEquals(object, replicatedObject);
144
			
145
    	} catch (Exception e) {
146
    		e.printStackTrace();
147
			fail(e.getMessage());
148
		}
149
    }
150
    
151
    public void testReplicateData_BtoA() {
152
    	try {
153
    		// the id
154
    		String docid = DocumentUtil.generateDocumentId("replicationTest", 1);
155
    		
156
    		// the test data
157
    		String object = "test";
158
    		
159
			// insert data locally
160
    		targetMetacat.login(username, password);
161
    		targetMetacat.upload(docid, "testObject", IOUtils.toInputStream(object, MetaCatServlet.DEFAULT_ENCODING), object.getBytes(MetaCatServlet.DEFAULT_ENCODING).length);
162
    		
163
    		// wait for replication (forced)
164
    		Thread.sleep(forceReplicationSleep);
165
    		
166
    		// check the target for the same data
167
    		m.login(username, password);
168
    		InputStream is = m.read(docid);
169
    		String replicatedObject = IOUtils.toString(is, MetaCatServlet.DEFAULT_ENCODING);
170
    		
171
    		assertEquals(object, replicatedObject);
172
			
173
    	} catch (Exception e) {
174
    		e.printStackTrace();
175
			fail(e.getMessage());
176
		}
177
    }
178

    
179
    
180
}
181

    
    (1-1/1)