Project

General

Profile

« Previous | Next » 

Revision 10452

Added by Jing Tao over 6 years ago

Add an IT test.

View differences:

test/edu/ucsb/nceas/metacat/dataone/MultiThreadClientIT.java
1
package edu.ucsb.nceas.metacat.dataone;
2
import java.io.File;
3
import java.io.FileInputStream;
4
import java.io.IOException;
5
import java.util.concurrent.ExecutorService;
6
import java.util.concurrent.Executors;
7
import java.util.concurrent.Future;
8

  
9
import org.apache.commons.io.FileUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.dataone.client.auth.AuthTokenSession;
13
import org.dataone.client.exception.ClientSideException;
14
import org.dataone.client.v2.MNode;
15
import org.dataone.client.v2.impl.MultipartMNode;
16
import org.dataone.service.types.v1.Identifier;
17
import org.dataone.service.types.v2.SystemMetadata;
18
import org.dataone.service.util.TypeMarshaller;
19
import org.junit.Test;
20
import edu.ucsb.nceas.MCTestCase;
21
import edu.ucsb.nceas.metacat.properties.PropertyService;
22

  
23
public class MultiThreadClientIT extends MCTestCase {
24
    //need to configure this.
25
    private static String tokenPath = "/Users/tao/curl-files/token";
26
    
27
    protected static Log log = LogFactory.getLog(MultiThreadClientIT.class);
28
    private static ExecutorService executor = Executors.newFixedThreadPool(5);
29
    private static int number_of_objects = 30;
30
    private static String mnURL = null;
31
    static {
32
        try {
33
            mnURL = PropertyService.getProperty("test.contextUrl")+"/d1/mn";
34
        } catch (Exception e) {
35
            log.error("MultiThreadClientIT - can't find the test.contexturl from the metacat.porperties file.", e);
36
        }
37
    }
38
    
39
    @Test 
40
    public void testConstructUsingRegisteredCertificate() throws IOException, ClientSideException, Exception {
41
        String token = FileUtils.readFileToString(new File(tokenPath), "UTF-8");
42
        final AuthTokenSession tokenSession = new AuthTokenSession(token);
43
        final MNode node = new MultipartMNode(tokenSession.getMultipartRestClient(), mnURL, tokenSession);
44
        Runnable runner = new Runnable() {
45
            @Override
46
            public void run() {
47
                try {
48
                    double random = Math.random()*10000;
49
                    long round = Math.round(random);
50
                    long time = System.nanoTime();
51
                    String identifier ="test"+time+round;
52
                    Identifier pid = new Identifier();
53
                    pid.setValue(identifier);
54
                    FileInputStream sysFile = new FileInputStream("test/sysmeta-noaa.xml");
55
                    SystemMetadata sysmeta = TypeMarshaller.unmarshalTypeFromStream(SystemMetadata.class, sysFile);
56
                    sysmeta.setIdentifier(pid);
57
                    FileInputStream object = new FileInputStream("test/sciencemetadata-noaa.xml");
58
                    node.create(tokenSession, pid, object, sysmeta);
59
                    System.out.println("after sending -------"+identifier);
60
                    object.close();
61
                    sysFile.close();
62
                } catch (Exception e) {
63
                    log.error("Error running: " + e.getMessage(), e);
64
                    //throw new RuntimeException(e.getMessage(), e);
65
                }
66
            }
67
        };
68
        // submit the task, and that's it
69
        Future future = null;
70
        for(int i=0; i<number_of_objects; i++) {
71
            future = executor.submit(runner);
72
        }
73
        while (!future.isDone()) {
74
            Thread.sleep(5000);
75
        }
76
        Thread.sleep(20000);
77
    }
78

  
79
}

Also available in: Unified diff