Created
December 8, 2024 16:44
-
-
Save azagniotov/beeebda3fff5f3ddccc3d207b0673b0d to your computer and use it in GitHub Desktop.
Solr unit test example shows how to index documents using JettySolrRunner and SolrTestCaseJ4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (c) 2023-2024 Alexander Zagniotov | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
*/ | |
package io.github.azagniotov.solr; | |
import java.io.File; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.net.URL; | |
import java.nio.charset.StandardCharsets; | |
import java.util.HashMap; | |
import org.apache.commons.io.FileUtils; | |
import org.apache.commons.io.IOUtils; | |
import org.apache.http.HttpEntity; | |
import org.apache.http.HttpResponse; | |
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase; | |
import org.apache.http.client.methods.HttpGet; | |
import org.apache.http.client.methods.HttpPost; | |
import org.apache.http.client.methods.HttpPut; | |
import org.apache.http.client.methods.HttpUriRequest; | |
import org.apache.http.conn.ssl.NoopHostnameVerifier; | |
import org.apache.http.entity.ContentType; | |
import org.apache.http.entity.StringEntity; | |
import org.apache.http.impl.client.CloseableHttpClient; | |
import org.apache.http.impl.client.HttpClientBuilder; | |
import org.apache.solr.SolrTestCaseJ4; | |
import org.apache.solr.embedded.JettyConfig; | |
import org.apache.solr.embedded.JettySolrRunner; | |
import org.eclipse.jetty.http.HttpMethod; | |
import org.json.JSONObject; | |
import org.junit.AfterClass; | |
import org.junit.BeforeClass; | |
import org.junit.Test; | |
public class JettySolrRunnerTest extends SolrTestCaseJ4 { | |
// Do not change. First see SolrTestCaseJ4.TEST_HOME() | |
private static final String COLLECTION_NAME = "collection1"; | |
private static final CloseableHttpClient DEFAULT_HTTP_CLIENT = | |
HttpClientBuilder.create() | |
.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).build(); | |
private static JettySolrRunner jettySolrRunner; | |
private static String host; | |
private static int port; | |
@BeforeClass | |
public static void beforeClass() throws Exception { | |
final String tmpSolrHome = createTempDir().toFile().getAbsolutePath(); | |
FileUtils.copyDirectory(new File(TEST_HOME()), new File(tmpSolrHome).getAbsoluteFile()); | |
final JettyConfig config = JettyConfig.builder().setContext("/solr").build(); | |
jettySolrRunner = new JettySolrRunner(tmpSolrHome, config); | |
try { | |
jettySolrRunner.start(); | |
jettySolrRunner.getCoreContainer().create(COLLECTION_NAME, new HashMap<>()); | |
final URL url = jettySolrRunner.getBaseUrl(); | |
host = url.getHost(); | |
port = url.getPort(); | |
} finally { | |
assertTrue(jettySolrRunner.isRunning()); | |
} | |
} | |
@AfterClass | |
public static void afterClass() throws Exception { | |
try { | |
if (jettySolrRunner.isRunning()) { | |
jettySolrRunner.stop(); | |
} | |
} finally { | |
assertTrue(jettySolrRunner.isStopped()); | |
} | |
} | |
@Test | |
public void testDocumentIndexing() throws Exception { | |
final JSONObject firstDocument = | |
new JSONObject(indexDocument("{\"id\": \"1\", \"comments\": \"First document is awesome!\"}")); | |
assertEquals(0, firstDocument.getJSONObject("responseHeader").getInt("status")); | |
final JSONObject secondDocument = | |
new JSONObject(indexDocument("{\"id\": \"2\", \"comments\": \"Second document is awesome too!\"}")); | |
assertEquals(0, secondDocument.getJSONObject("responseHeader").getInt("status")); | |
final JSONObject allDocs = new JSONObject(searchAllDocsByComments()); | |
assertEquals(2, allDocs.getJSONObject("response").getInt("numFound")); | |
assertEquals( | |
"First document is awesome!", | |
allDocs | |
.getJSONObject("response") | |
.getJSONArray("docs") | |
.getJSONObject(0) | |
.getJSONArray("comments") | |
.get(0) | |
.toString()); | |
assertEquals( | |
"Second document is awesome too!", | |
allDocs | |
.getJSONObject("response") | |
.getJSONArray("docs") | |
.getJSONObject(1) | |
.getJSONArray("comments") | |
.get(0) | |
.toString()); | |
} | |
private static String indexDocument(final String payload) throws IOException { | |
return doHttpRequest(HttpMethod.POST, "/update/json/docs?commit=true", payload); | |
} | |
private static String searchAllDocsByComments() throws IOException { | |
return doHttpRequest(HttpMethod.GET, "/select?q=comments:*", null); | |
} | |
private static String doHttpRequest( | |
final HttpMethod method, final String endpoint, final String payload) throws IOException { | |
final String url = | |
String.format("http://%s:%s/solr/%s%s", host, port, COLLECTION_NAME, endpoint); | |
final HttpUriRequest request = constructRequest(method, url, payload); | |
final HttpResponse response = DEFAULT_HTTP_CLIENT.execute(request); | |
final HttpEntity httpEntity = response.getEntity(); | |
final InputStream in = httpEntity.getContent(); | |
final String responseString = IOUtils.toString(in, StandardCharsets.UTF_8); | |
System.out.println( | |
"\nResponse from " + method + " to " + endpoint + ":\n" + responseString + "\n"); | |
return responseString; | |
} | |
private static HttpUriRequest constructRequest( | |
final HttpMethod method, final String url, final String payload) { | |
if (method == HttpMethod.GET) { | |
return new HttpGet(url); | |
} | |
final HttpEntityEnclosingRequestBase requestBase = | |
method == HttpMethod.PUT ? new HttpPut(url) : new HttpPost(url); | |
final StringEntity entity = new StringEntity(payload, StandardCharsets.UTF_8); | |
entity.setContentType(ContentType.APPLICATION_JSON.getMimeType()); | |
requestBase.setEntity(entity); | |
return requestBase; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Make sure your test resources are structures as follows: | |
resources/ | |
|_solr/ | |
|_collection1/ | |
| |_conf/ | |
| |_schema.xml | |
| |_solrconfig.xml | |
|_solr.xml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment