2017-02-15 15:48:53 -08:00
|
|
|
package com.baeldung.solrjava;
|
|
|
|
|
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
import org.apache.solr.client.solrj.SolrQuery;
|
|
|
|
import org.apache.solr.client.solrj.SolrServerException;
|
|
|
|
import org.apache.solr.client.solrj.response.QueryResponse;
|
|
|
|
import org.apache.solr.common.SolrDocument;
|
|
|
|
import org.apache.solr.common.SolrDocumentList;
|
|
|
|
import org.junit.Before;
|
|
|
|
import org.junit.Test;
|
|
|
|
|
2018-07-10 12:12:39 +05:30
|
|
|
public class SolrJavaLiveTest {
|
2017-02-15 15:48:53 -08:00
|
|
|
|
2017-02-20 17:28:16 -08:00
|
|
|
private SolrJavaIntegration solrJavaIntegration;
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
|
|
|
|
2017-02-20 17:28:16 -08:00
|
|
|
solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore");
|
|
|
|
solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99");
|
2017-02-16 19:59:06 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-27 20:26:30 -08:00
|
|
|
public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException {
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
query.set("q", "id:123456");
|
|
|
|
QueryResponse response = null;
|
|
|
|
|
2017-02-20 17:28:16 -08:00
|
|
|
response = solrJavaIntegration.getSolrClient().query(query);
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
SolrDocumentList docList = response.getResults();
|
2017-03-01 22:20:07 -06:00
|
|
|
assertEquals(1, docList.getNumFound());
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
for (SolrDocument doc : docList) {
|
2017-02-27 20:26:30 -08:00
|
|
|
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
|
|
|
|
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
|
2017-02-15 15:48:53 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2017-02-27 20:26:30 -08:00
|
|
|
public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException {
|
2017-02-15 15:48:53 -08:00
|
|
|
|
2017-02-27 20:26:30 -08:00
|
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
query.set("q", "price:599.99");
|
|
|
|
QueryResponse response = null;
|
|
|
|
|
|
|
|
response = solrJavaIntegration.getSolrClient().query(query);
|
|
|
|
|
|
|
|
SolrDocumentList docList = response.getResults();
|
|
|
|
assertEquals(1, docList.getNumFound());
|
|
|
|
|
|
|
|
for (SolrDocument doc : docList) {
|
|
|
|
assertEquals("123456", (String) doc.getFieldValue("id"));
|
|
|
|
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException {
|
|
|
|
|
|
|
|
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456");
|
|
|
|
assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name"));
|
|
|
|
assertEquals((Double) 599.99, (Double) doc.getFieldValue("price"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException {
|
|
|
|
|
|
|
|
ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99");
|
|
|
|
solrJavaIntegration.addProductBean(pBean);
|
|
|
|
|
|
|
|
SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888");
|
|
|
|
assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name"));
|
|
|
|
assertEquals((Double) 299.99, (Double) doc.getFieldValue("price"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException {
|
|
|
|
|
|
|
|
solrJavaIntegration.deleteSolrDocumentById("123456");
|
|
|
|
|
|
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
query.set("q", "id:123456");
|
|
|
|
QueryResponse response = solrJavaIntegration.getSolrClient().query(query);
|
|
|
|
|
|
|
|
SolrDocumentList docList = response.getResults();
|
|
|
|
assertEquals(0, docList.getNumFound());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException {
|
|
|
|
|
|
|
|
solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher");
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
SolrQuery query = new SolrQuery();
|
|
|
|
query.set("q", "id:123456");
|
|
|
|
QueryResponse response = null;
|
|
|
|
|
2017-02-20 17:28:16 -08:00
|
|
|
response = solrJavaIntegration.getSolrClient().query(query);
|
2017-02-15 15:48:53 -08:00
|
|
|
|
|
|
|
SolrDocumentList docList = response.getResults();
|
2017-02-27 20:26:30 -08:00
|
|
|
assertEquals(0, docList.getNumFound());
|
2017-02-15 15:48:53 -08:00
|
|
|
}
|
|
|
|
}
|