Home
Cloud
Big Data
CI
Install
Samples
Java
Ubuntu
Maven
Archive
Testing
|
Using Solr test framework (MiniSolrCloudCluster)
The POM file (pom.xml)
Unit test example
The POM file (pom.xml)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>mtitek.solr.embed.samples</groupId> <artifactId>mtitek-solr-embed-samples</artifactId> <version>0.0.1-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.apache.solr</groupId> <artifactId>solr-test-framework</artifactId> <version>8.6.0</version> <scope>test</scope> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.28</version> <scope>test</scope> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <encoding>UTF-8</encoding> <source>1.8</source> <target>1.8</target> </configuration> </plugin> </plugins> </build> </project>
Unit test example
►
mtitek-solr-embed-samples/src/test/java/mtitek/solr/embed/samples/MiniSolrCloudClusterTest.java
package mtitek.solr.embed.samples; import java.io.File; import java.nio.file.Path; import org.apache.solr.client.solrj.embedded.JettyConfig; import org.apache.solr.client.solrj.impl.CloudSolrClient; import org.apache.solr.client.solrj.request.CollectionAdminRequest; import org.apache.solr.client.solrj.request.ConfigSetAdminRequest; import org.apache.solr.client.solrj.response.CollectionAdminResponse; import org.apache.solr.client.solrj.response.ConfigSetAdminResponse; import org.apache.solr.cloud.MiniSolrCloudCluster; import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; public class MiniSolrCloudClusterTest { @Rule public TemporaryFolder temporaryFolder = new TemporaryFolder(); @Test public void test() throws Exception { final File newFolder = temporaryFolder.newFolder("MiniSolrCloudClusterTest"); final Path baseDir = newFolder.toPath(); final int numServers = 1; // random unassigned HTTP port final int jettyPort = 0; final JettyConfig jettyConfig = JettyConfig.builder().setPort(jettyPort).build(); // create a MiniSolrCloudCluster instance final MiniSolrCloudCluster miniSolrCloudCluster = new MiniSolrCloudCluster(numServers, baseDir, jettyConfig); // Upload Solr configuration directory to ZooKeeper String solrZKConfigDir = "src/test/resources/conf"; String solrZKConfigName = "MySolrZKConfigName"; File configDir = new File(solrZKConfigDir); miniSolrCloudCluster.uploadConfigSet(configDir.toPath(), solrZKConfigName); // get Solr Client CloudSolrClient cloudSolrClient = miniSolrCloudCluster.getSolrClient(); // list ConfigSets { ConfigSetAdminResponse adminResponse = listConfigSets(cloudSolrClient); System.out.println(adminResponse); } // get cluster status { CollectionAdminResponse adminResponse = getClusterStatus(cloudSolrClient); System.out.println(adminResponse); } // shutdown MiniSolrCloudCluster miniSolrCloudCluster.shutdown(); } private ConfigSetAdminResponse listConfigSets(CloudSolrClient cloudSolrClient) throws Exception { final ConfigSetAdminRequest.List adminRequest = new ConfigSetAdminRequest.List(); ConfigSetAdminResponse adminResponse = adminRequest.process(cloudSolrClient); return adminResponse; } private CollectionAdminResponse getClusterStatus(CloudSolrClient cloudSolrClient) throws Exception { final CollectionAdminRequest.ClusterStatus adminRequest = CollectionAdminRequest.ClusterStatus.getClusterStatus(); CollectionAdminResponse adminResponse = adminRequest.process(cloudSolrClient); return adminResponse; } }
Running this JUnit test class should output the following:
{responseHeader={status=0,QTime=30},configSets=[MySolrZKConfigName]} {responseHeader={status=0,QTime=16},cluster={collections={},live_nodes=[127.0.0.1:51073_solr]}}
© 2010-2022
mti
tek