Project

General

Profile

« Previous | Next » 

Revision 10260

Added by Jing Tao over 7 years ago

Add a integration test.

View differences:

test/edu/ucsb/nceas/metacat/dataone/MNodeReplicationTest.java
1
/**
2
 *  '$RCSfile$'
3
 *  Copyright: 2010 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:$'
8
 *     '$Date:$'
9
 * '$Revision:$'
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.dataone;
27

  
28

  
29

  
30

  
31
import edu.ucsb.nceas.MCTestCase;
32
import edu.ucsb.nceas.metacat.dataone.CNodeService;
33
import edu.ucsb.nceas.metacat.dataone.MNodeService;
34
import edu.ucsb.nceas.metacat.properties.PropertyService;
35
import edu.ucsb.nceas.metacat.properties.SkinPropertyService;
36
import edu.ucsb.nceas.metacat.service.ServiceService;
37
import edu.ucsb.nceas.utilities.IOUtil;
38
import gov.loc.repository.bagit.Bag;
39
import gov.loc.repository.bagit.BagFactory;
40
import gov.loc.repository.bagit.BagFile;
41
import gov.loc.repository.bagit.Manifest;
42

  
43
import java.io.ByteArrayInputStream;
44
import java.io.ByteArrayOutputStream;
45
import java.io.File;
46
import java.io.FileInputStream;
47
import java.io.FileOutputStream;
48
import java.io.IOException;
49
import java.io.InputStream;
50
import java.io.UnsupportedEncodingException;
51
import java.math.BigInteger;
52
import java.net.URL;
53
import java.text.SimpleDateFormat;
54
import java.util.ArrayList;
55
import java.util.Calendar;
56
import java.util.Date;
57
import java.util.HashMap;
58
import java.util.Iterator;
59
import java.util.List;
60
import java.util.Map;
61
import java.util.Vector;
62

  
63
import junit.framework.Test;
64
import junit.framework.TestSuite;
65

  
66
import org.apache.commons.io.FileUtils;
67
import org.apache.commons.io.IOUtils;
68
import org.dataone.client.v2.MNode;
69
import org.dataone.client.v2.itk.D1Client;
70
import org.dataone.client.v2.formats.ObjectFormatCache;
71
import org.dataone.exceptions.MarshallingException;
72
import org.dataone.configuration.Settings;
73
import org.dataone.ore.ResourceMapFactory;
74
import org.dataone.service.util.Constants;
75
import org.dataone.service.util.TypeMarshaller;
76
import org.dataone.service.exceptions.IdentifierNotUnique;
77
import org.dataone.service.exceptions.InsufficientResources;
78
import org.dataone.service.exceptions.InvalidRequest;
79
import org.dataone.service.exceptions.InvalidSystemMetadata;
80
import org.dataone.service.exceptions.InvalidToken;
81
import org.dataone.service.exceptions.NotAuthorized;
82
import org.dataone.service.exceptions.NotFound;
83
import org.dataone.service.exceptions.NotImplemented;
84
import org.dataone.service.exceptions.ServiceFailure;
85
import org.dataone.service.exceptions.SynchronizationFailed;
86
import org.dataone.service.exceptions.UnsupportedType;
87
import org.dataone.service.types.v1.AccessPolicy;
88
import org.dataone.service.types.v1.AccessRule;
89
import org.dataone.service.types.v1.Checksum;
90
import org.dataone.service.types.v1.DescribeResponse;
91
import org.dataone.service.types.v1.Event;
92
import org.dataone.service.types.v1.ObjectFormatIdentifier;
93
import org.dataone.service.types.v1.Identifier;
94
import org.dataone.service.types.v2.Log;
95
import org.dataone.service.types.v2.Node;
96
import org.dataone.service.types.v2.OptionList;
97
import org.dataone.service.types.v1.NodeReference;
98
import org.dataone.service.types.v1.ObjectFormatIdentifier;
99
import org.dataone.service.types.v1.ObjectList;
100
import org.dataone.service.types.v1.Permission;
101
import org.dataone.service.types.v1.Person;
102
import org.dataone.service.types.v1.ReplicationPolicy;
103
import org.dataone.service.types.v1.Session;
104
import org.dataone.service.types.v1.Subject;
105
import org.dataone.service.types.v1.SubjectInfo;
106
import org.dataone.service.types.v1.util.ChecksumUtil;
107
import org.dataone.service.types.v2.SystemMetadata;
108
import org.dspace.foresite.ResourceMap;
109
import org.junit.After;
110
import org.junit.Before;
111

  
112
/**
113
 *This really is a integration test. It should be run in a configured DataONE environment. 
114
 *It will upload a metadata object to a MN with the replication perference of the running node (the local host). 
115
 *Couple minutes later, it will check if the running node has the replica.
116
 * 
117
 * @author Jing Tao
118
 *
119
 */
120
public class MNodeReplicationTest extends D1NodeServiceTest {
121
    public static final String replicationSourceFile = "./test/eml-sample.xml";
122
    protected static String sourceMNodeId = null;
123
    protected static int waitTime = 0;
124

  
125
  /**
126
   * Set up the test fixtures
127
   * 
128
   * @throws Exception
129
   */
130
  @Before
131
  public void setUp() throws Exception {
132
     sourceMNodeId = PropertyService.getProperty("test.dataone.replication.sourceNodeId");
133
     System.out.println("The sourceNodeId is ++++++++++++++++++ "+sourceMNodeId);
134
     waitTime = (new Integer(PropertyService.getProperty("test.dataone.replication.waitingTime"))).intValue();
135
     System.out.println("The waiting time is ++++++++++++++++++ "+waitTime+ " seconds");
136
     System.out.println("The cn base url is "+Settings.getConfiguration().getString("D1Client.CN_URL"));
137
  }
138

  
139
  /**
140
   * Remove the test fixtures
141
   */
142
  @After
143
  public void tearDown() {
144
  }
145
  
146
  /**
147
   * Build the test suite
148
   * @return
149
   */
150
  public static Test suite() {
151
    
152
    TestSuite suite = new TestSuite();
153
    suite.addTest(new MNodeReplicationTest("initialize"));
154
    suite.addTest(new MNodeReplicationTest("testReplicate"));
155
    return suite;
156
    
157
  }
158
  
159
  /**
160
   * Constructor for the tests
161
   * 
162
   * @param name - the name of the test
163
   */
164
  public MNodeReplicationTest(String name) {
165
    super(name);
166
    
167
  }
168

  
169
  /**
170
   * Initial blank test
171
   */
172
  public void initialize() {
173
    assertTrue(1 == 1);
174
    
175
  }
176
  
177
  /**
178
   *
179
   * 
180
   */
181
  public void testReplicate() {
182
      printTestHeader("testReplicate");
183
      try {
184
          
185
        //insert an object to the source node
186
        Session session = null;
187
        Identifier guid = new Identifier();
188
        guid.setValue("testReplicate." + System.currentTimeMillis());
189
        InputStream object = new FileInputStream(new File(replicationSourceFile));
190
        Subject subject = MNodeService.getInstance(request).getCapabilities().getSubject(0);
191
        SystemMetadata sysmeta = createSystemMetadata(guid, subject, object);
192
        ObjectFormatIdentifier formatId = new ObjectFormatIdentifier();
193
        formatId.setValue("eml://ecoinformatics.org/eml-2.0.1");
194
        sysmeta.setFormatId(formatId);
195
        //create a replication policy
196
        Node localNode = MNodeService.getInstance(request).getCapabilities();
197
        if(!localNode.isReplicate()) {
198
            throw new Exception("The local node "+localNode.getIdentifier().getValue()+" is configured to not to accept replicas!");
199
        }
200
        ReplicationPolicy rePolicy = new ReplicationPolicy();
201
        rePolicy.setReplicationAllowed(true);
202
        rePolicy.setNumberReplicas(new Integer(3));
203
        rePolicy.addPreferredMemberNode(localNode.getIdentifier());
204
        sysmeta.setReplicationPolicy(rePolicy);
205
        
206
        NodeReference sourceNode = new NodeReference();
207
        sourceNode.setValue(sourceMNodeId);
208
        MNode sourceMN = D1Client.getMN(sourceNode);
209
        Node source = sourceMN.getCapabilities();
210
        if(!source.isSynchronize()) {
211
            throw new Exception("The source node "+source.getIdentifier().getValue()+" is configured to not to be synchronized to the cn!");
212
        }
213
        object = new FileInputStream(new File(replicationSourceFile));
214
        sysmeta.setAuthoritativeMemberNode(sourceNode);
215
        System.out.println("------------------------before creating the object into the source node "+sourceMNodeId+" with id "+guid.getValue());
216
        sourceMN.create(session, guid, object, sysmeta);
217
        System.out.println("scucessfully created the object into the source node "+sourceMNodeId+" with id "+guid.getValue());
218
        Thread.sleep(waitTime);
219
        MNode local = D1Client.getMN(localNode.getIdentifier());
220
        SystemMetadata sys = local.getSystemMetadata(session, guid);
221
        System.out.println("--------------The pid from the replica on the localhost is  "+sys.getIdentifier().getValue());
222
        assertTrue(sys.getIdentifier().equals(guid));
223
      } catch (Exception e) {
224
        e.printStackTrace();
225
        System.out.println("Failed to test the replicate method : " + e.getMessage());
226
        fail("Failed to test the replicate method : " + e.getMessage());
227
      }
228
  }
229
 
230
}

Also available in: Unified diff