Project

General

Profile

« Previous | Next » 

Revision 6770

simple tests for 2-way data replication
http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5520

View differences:

test/edu/ucsb/nceas/metacat/replication/ReplicationTest.java
25 25

  
26 26
package edu.ucsb.nceas.metacat.replication;
27 27

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

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

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

  
38 44
/**
......
41 47
public class ReplicationTest
42 48
    extends MCTestCase {
43 49
    
44
    private String targetReplicationServer = null;
50
    private static final long forceReplicationSleep = 10000;
51
	private String targetReplicationServer = null;
52
	private Metacat targetMetacat = null;
45 53
    
46 54
	/**
47 55
     * Constructor to build the test
......
57 65
     */
58 66
    public void setUp() {
59 67
        try {
60
            System.err.println("Test Metacat: " + metacatUrl);
68
        	// get the target ("B server")
69
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
61 70
            m = MetacatFactory.createMetacatConnection(metacatUrl);
62
            targetReplicationServer = PropertyService.getProperty("test.replication.targetServer");
71
            targetMetacat = MetacatFactory.createMetacatConnection(targetReplicationServer + "/metacat");
72

  
63 73
        }
64 74
        catch (MetacatInaccessibleException mie) {
65 75
            System.err.println("Metacat is: " + metacatUrl);
......
84 94
        suite.addTest(new ReplicationTest("initialize"));
85 95
        // Test basic functions
86 96
        suite.addTest(new ReplicationTest("testCertificate"));
87
//        suite.addTest(new ReplicationTest("replicateData"));
97
        suite.addTest(new ReplicationTest("testReplicateData_AtoB"));
98
        suite.addTest(new ReplicationTest("testReplicateData_BtoA"));
99

  
88 100
        return suite;
89 101
    }
90 102

  
......
98 110
    
99 111
    public void testCertificate() {
100 112
    	try {
101
    		URL u = new URL("https://" + targetReplicationServer  + "?server=" + MetacatUtil.getLocalReplicationServerName() + "&action=test");
113
    		URL u = new URL("https://" + targetReplicationServer  + "/replication/servlet?server=" + MetacatUtil.getLocalReplicationServerName() + "&action=test");
102 114
			String test = ReplicationService.getURLContent(u);
103 115
			assertTrue(test.contains("Test successfully"));
104 116
			
......
107 119
			fail(e.getMessage());
108 120
		}
109 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
    }
110 178

  
111 179
    
112 180
}
lib/metacat.properties
482 482
test.lterUser=uid=tmonkey,o=LTER,dc=ecoinformatics,dc=org
483 483
test.lterPassword=T3$tusr
484 484
test.testProperty=testing
485
test.replication.targetServer=demo3.test.dataone.org/knb/replication/servlet
485
test.replication.targetServer=demo3.test.dataone.org/knb
486 486

  
487 487
######## Developers Section #########################################
488 488

  

Also available in: Unified diff