Project

General

Profile

« Previous | Next » 

Revision 7582

use integration-test ("mvn verify") to test things that require Metacat to be running. These classes should end be named "*IT.java" and unit tests that do not require metacat to be running can be named "*Test.java" - https://projects.ecoinformatics.org/ecoinfo/issues/5918

View differences:

metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/ApplicationControllerTest.java
1
package edu.ucsb.nceas.metacat.index;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5

  
6
import java.util.List;
7

  
8
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
9
import org.dataone.cn.indexer.parser.ScienceMetadataDocumentSubprocessor;
10
import org.dataone.configuration.Settings;
11
import org.junit.Test;
12

  
13
public class ApplicationControllerTest {
14
    public static final String pathToHazelcastFile = "../lib/hazelcast.xml";
15
    
16
    /**
17
     * Test lookup from default properties file
18
     */
19
    @Test
20
    public void testConstructor() {
21
        Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", pathToHazelcastFile);
22
        ApplicationController controller = new ApplicationController();
23
        List<SolrIndex> list = controller.getSolrIndexes();
24
        assertTrue(list.size() == 1);
25
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
26
        SolrIndex index = solrIndexesarray[0];
27
        List<IDocumentSubprocessor> subprocessors = index.getSubprocessors();
28
        IDocumentSubprocessor[] subprocessorArray = subprocessors.toArray(new IDocumentSubprocessor[subprocessors.size()]);
29
        assertTrue(subprocessorArray[0] instanceof ScienceMetadataDocumentSubprocessor);
30
    }
31
    
32
}
metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexTest.java
1
package edu.ucsb.nceas.metacat.index;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5

  
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.util.List;
11

  
12
import javax.xml.parsers.ParserConfigurationException;
13

  
14

  
15

  
16
import org.apache.commons.io.FileUtils;
17
import org.apache.solr.client.solrj.SolrServer;
18
import org.apache.solr.client.solrj.SolrServerException;
19
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
20
import org.apache.solr.client.solrj.response.QueryResponse;
21
import org.apache.solr.common.SolrDocument;
22
import org.apache.solr.common.SolrDocumentList;
23
import org.apache.solr.common.params.SolrParams;
24
import org.apache.solr.core.CoreContainer;
25
import org.apache.solr.servlet.SolrRequestParsers;
26
import org.dataone.configuration.Settings;
27
import org.dataone.service.types.v1.SystemMetadata;
28
import org.dataone.service.util.TypeMarshaller;
29
import org.junit.Test;
30
import org.xml.sax.SAXException;
31

  
32
public class SolrIndexTest  {
33
    
34
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
35
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
36
    private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
37
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
38
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
39
    private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
40
    private static final String SOLRHOME = "solr-home";
41
    private static final String SOLRTESTHOMEPATH = "target/"+SOLRHOME;
42
    
43
    
44
    /**
45
     * Test building index for an insert.
46
     */
47
    @Test
48
    public void testInsert() throws Exception {
49
        SolrIndex solrIndex = generateSolrIndex();
50
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
51
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
52
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));    
53
       solrIndex.insert(id, systemMetadata, emlInputStream);
54
       String result = doQuery(solrIndex.getSolrServer());
55
       assertTrue(result.contains("version1"));
56
    }
57
    
58
    /**
59
     * Test building index for an insert.
60
     */
61
    @Test
62
    public void testUpdate() throws Exception {
63
        SolrIndex solrIndex = generateSolrIndex();
64
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
65
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
66
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
67
       solrIndex.update(newId, systemMetadata, emlInputStream);
68
       String result = doQuery(solrIndex.getSolrServer());
69
       assertTrue(result.contains("version2"));
70
    }
71
    
72
    /**
73
     * Test building index for an insert.
74
     */
75
    //@Test
76
    /*public void testDelete() throws Exception {
77
       SolrIndex solrIndex = generateSolrIndex();
78
       //solrIndex.remove(newId);
79
       //String result = doQuery(solrIndex.getSolrServer());
80
       //assertTrue(!result.contains("version1"));
81
       //assertTrue(!result.contains("version2"));
82
    }*/
83
    
84
   
85
    
86
    /*
87
     * Do query
88
     */
89
    private String doQuery(SolrServer server)
90
                    throws SolrServerException {
91
                StringBuffer request = new StringBuffer();
92
                request.append("q=" + "*:*");
93
                SolrParams solrParams = SolrRequestParsers.parseQueryString(request
94
                        .toString());
95
                QueryResponse reponse = server.query(solrParams);
96
                System.out.println("**************************************************************************");
97
                System.out.println("The query result:\n");
98
                System.out.println(reponse.toString());
99
                System.out.println("**************************************************************************");
100
                return reponse.toString();
101
    }
102

  
103
    
104
    /*
105
     * Print out the QueryResponse
106
     */
107
    /*private void print(QueryResponse response) {
108
        SolrDocumentList docs = response.getResults();
109
        if (docs != null) {
110
            System.out.println(docs.getNumFound() + " documents found, "
111
                    + docs.size() + " returned : ");
112
            for (int i = 0; i < docs.size(); i++) {
113
                SolrDocument doc = docs.get(i);
114
                System.out.println("\t" + doc.toString());
115
            }
116
        }
117
    }*/
118
    
119
    private SolrIndex generateSolrIndex() throws Exception {
120
        Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", ApplicationControllerTest.pathToHazelcastFile);
121
        ApplicationController controller = new ApplicationController();
122
        List<SolrIndex> list = controller.getSolrIndexes();
123
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
124
        SolrIndex index = solrIndexesarray[0];
125
        index.setSolrServer(generateSolrServer());
126
        return index;
127
    }
128
    
129
    /*
130
     * Generate a test solr server
131
     */
132
    private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException {
133
        createSolrHome();
134
        System.setProperty(SolrIndex.SOLRHOME, SOLRTESTHOMEPATH);
135
        //System.out.println("====="+"/Users/tao/projects/metacat-index/"+SOLRTESTHOMEPATH);
136
        CoreContainer.Initializer init = new CoreContainer.Initializer();
137
        CoreContainer c = init.initialize();
138
        SolrServer solrServer = new EmbeddedSolrServer(c, "collection1");
139
        return solrServer;
140
    }
141
    
142
    /*
143
     * Create a solr home directory
144
     */
145
    private void createSolrHome() throws IOException {
146
        File solrHomeTestDir =  new File(SOLRTESTHOMEPATH);
147
        solrHomeTestDir.mkdir();
148
        File solrData = new File(SOLRTESTHOMEPATH+"/data");
149
        solrData.mkdir();
150
        File solr_home_source_dir = new File("src/main/resources/"+SOLRHOME);
151
        FileUtils.copyDirectory(solr_home_source_dir, solrHomeTestDir);
152
    }
153
}
154

  
metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/ApplicationControllerIT.java
1
package edu.ucsb.nceas.metacat.index;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5

  
6
import java.util.List;
7

  
8
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
9
import org.dataone.cn.indexer.parser.ScienceMetadataDocumentSubprocessor;
10
import org.dataone.configuration.Settings;
11
import org.junit.Test;
12

  
13
public class ApplicationControllerIT {
14
    public static final String pathToHazelcastFile = "../lib/hazelcast.xml";
15
    
16
    /**
17
     * Test lookup from default properties file
18
     */
19
    @Test
20
    public void testConstructor() {
21
        Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", pathToHazelcastFile);
22
        ApplicationController controller = new ApplicationController();
23
        List<SolrIndex> list = controller.getSolrIndexes();
24
        assertTrue(list.size() == 1);
25
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
26
        SolrIndex index = solrIndexesarray[0];
27
        List<IDocumentSubprocessor> subprocessors = index.getSubprocessors();
28
        IDocumentSubprocessor[] subprocessorArray = subprocessors.toArray(new IDocumentSubprocessor[subprocessors.size()]);
29
        assertTrue(subprocessorArray[0] instanceof ScienceMetadataDocumentSubprocessor);
30
    }
31
    
32
}
metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexIT.java
1
package edu.ucsb.nceas.metacat.index;
2

  
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertTrue;
5

  
6
import java.io.File;
7
import java.io.FileInputStream;
8
import java.io.IOException;
9
import java.io.InputStream;
10
import java.util.List;
11

  
12
import javax.xml.parsers.ParserConfigurationException;
13

  
14

  
15

  
16
import org.apache.commons.io.FileUtils;
17
import org.apache.solr.client.solrj.SolrServer;
18
import org.apache.solr.client.solrj.SolrServerException;
19
import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer;
20
import org.apache.solr.client.solrj.response.QueryResponse;
21
import org.apache.solr.common.SolrDocument;
22
import org.apache.solr.common.SolrDocumentList;
23
import org.apache.solr.common.params.SolrParams;
24
import org.apache.solr.core.CoreContainer;
25
import org.apache.solr.servlet.SolrRequestParsers;
26
import org.dataone.configuration.Settings;
27
import org.dataone.service.types.v1.SystemMetadata;
28
import org.dataone.service.util.TypeMarshaller;
29
import org.junit.Ignore;
30
import org.junit.Test;
31
import org.xml.sax.SAXException;
32

  
33
public class SolrIndexIT  {
34
    
35
    private static final String SYSTEMMETAFILEPATH = "src/test/resources/eml-system-meta-example.xml";
36
    private static final String EMLFILEPATH = "src/test/resources/eml-example.xml";
37
    private static final String SYSTEMMETAUPDATEFILEPATH = "src/test/resources/eml-updating-system-meta-example.xml";
38
    private static final String EMLUPDATEFILEPATH = "src/test/resources/eml-updating-example.xml";
39
    private static final String id = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39098";
40
    private static final String newId = "urn:uuid:606a19dd-b531-4bf4-b5a5-6d06c3d39099";
41
    private static final String SOLRHOME = "solr-home";
42
    private static final String SOLRTESTHOMEPATH = "target/"+SOLRHOME;
43
    
44
    
45
    /**
46
     * Test building index for an insert.
47
     */
48
    @Test
49
    public void testInsert() throws Exception {
50
        SolrIndex solrIndex = generateSolrIndex();
51
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
52
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAFILEPATH);
53
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));    
54
       solrIndex.insert(id, systemMetadata, emlInputStream);
55
       String result = doQuery(solrIndex.getSolrServer());
56
       assertTrue(result.contains("version1"));
57
    }
58
    
59
    /**
60
     * Test building index for an insert.
61
     */
62
    @Test
63
    public void testUpdate() throws Exception {
64
        SolrIndex solrIndex = generateSolrIndex();
65
       //InputStream systemInputStream = new FileInputStream(new File(SYSTEMMETAFILEPATH));
66
       SystemMetadata systemMetadata = TypeMarshaller.unmarshalTypeFromFile(SystemMetadata.class, SYSTEMMETAUPDATEFILEPATH);
67
       InputStream emlInputStream = new FileInputStream(new File(EMLUPDATEFILEPATH));    
68
       solrIndex.update(newId, systemMetadata, emlInputStream);
69
       String result = doQuery(solrIndex.getSolrServer());
70
       assertTrue(result.contains("version2"));
71
    }
72
    
73
    /**
74
     * Test building index for an insert.
75
     */
76
    //@Test
77
    /*public void testDelete() throws Exception {
78
       SolrIndex solrIndex = generateSolrIndex();
79
       //solrIndex.remove(newId);
80
       //String result = doQuery(solrIndex.getSolrServer());
81
       //assertTrue(!result.contains("version1"));
82
       //assertTrue(!result.contains("version2"));
83
    }*/
84
    
85
   
86
    
87
    /*
88
     * Do query
89
     */
90
    private String doQuery(SolrServer server)
91
                    throws SolrServerException {
92
                StringBuffer request = new StringBuffer();
93
                request.append("q=" + "*:*");
94
                SolrParams solrParams = SolrRequestParsers.parseQueryString(request
95
                        .toString());
96
                QueryResponse reponse = server.query(solrParams);
97
                System.out.println("**************************************************************************");
98
                System.out.println("The query result:\n");
99
                System.out.println(reponse.toString());
100
                System.out.println("**************************************************************************");
101
                return reponse.toString();
102
    }
103

  
104
    
105
    /*
106
     * Print out the QueryResponse
107
     */
108
    /*private void print(QueryResponse response) {
109
        SolrDocumentList docs = response.getResults();
110
        if (docs != null) {
111
            System.out.println(docs.getNumFound() + " documents found, "
112
                    + docs.size() + " returned : ");
113
            for (int i = 0; i < docs.size(); i++) {
114
                SolrDocument doc = docs.get(i);
115
                System.out.println("\t" + doc.toString());
116
            }
117
        }
118
    }*/
119
    
120
    private SolrIndex generateSolrIndex() throws Exception {
121
        Settings.getConfiguration().setProperty("dataone.hazelcast.configFilePath", ApplicationControllerIT.pathToHazelcastFile);
122
        ApplicationController controller = new ApplicationController();
123
        List<SolrIndex> list = controller.getSolrIndexes();
124
        SolrIndex[] solrIndexesarray = list.toArray(new SolrIndex[list.size()]);
125
        SolrIndex index = solrIndexesarray[0];
126
        index.setSolrServer(generateSolrServer());
127
        return index;
128
    }
129
    
130
    /*
131
     * Generate a test solr server
132
     */
133
    private SolrServer generateSolrServer() throws IOException, ParserConfigurationException, SAXException {
134
        createSolrHome();
135
        System.setProperty(SolrIndex.SOLRHOME, SOLRTESTHOMEPATH);
136
        //System.out.println("====="+"/Users/tao/projects/metacat-index/"+SOLRTESTHOMEPATH);
137
        CoreContainer.Initializer init = new CoreContainer.Initializer();
138
        CoreContainer c = init.initialize();
139
        SolrServer solrServer = new EmbeddedSolrServer(c, "collection1");
140
        return solrServer;
141
    }
142
    
143
    /*
144
     * Create a solr home directory
145
     */
146
    private void createSolrHome() throws IOException {
147
        File solrHomeTestDir =  new File(SOLRTESTHOMEPATH);
148
        solrHomeTestDir.mkdir();
149
        File solrData = new File(SOLRTESTHOMEPATH+"/data");
150
        solrData.mkdir();
151
        File solr_home_source_dir = new File("src/main/resources/"+SOLRHOME);
152
        FileUtils.copyDirectory(solr_home_source_dir, solrHomeTestDir);
153
    }
154
}
155

  
metacat-index/pom.xml
65 65
			<scope>provided</scope>
66 66
		</dependency>
67 67
	</dependencies>
68

  
69
	<build>
70
		<plugins>
71
		
72
			<plugin>
73
				<artifactId>maven-failsafe-plugin</artifactId>
74
				<version>2.6</version>
75
				<executions>
76
					<execution>
77
						<goals>
78
							<goal>integration-test</goal>
79
							<goal>verify</goal>
80
						</goals>
81
					</execution>
82
				</executions>
83
			</plugin>
84

  
85
		</plugins>
86
	</build>
87

  
68 88
</project>

Also available in: Unified diff