|
| 1 | +package com.baeldung.solrjava; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | + |
| 5 | +import java.io.IOException; |
| 6 | + |
| 7 | +import org.apache.solr.client.solrj.SolrQuery; |
| 8 | +import org.apache.solr.client.solrj.SolrServerException; |
| 9 | +import org.apache.solr.client.solrj.impl.HttpSolrClient; |
| 10 | +import org.apache.solr.client.solrj.impl.XMLResponseParser; |
| 11 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 12 | +import org.apache.solr.common.SolrDocument; |
| 13 | +import org.apache.solr.common.SolrDocumentList; |
| 14 | +import org.apache.solr.common.SolrInputDocument; |
| 15 | +import org.junit.Before; |
| 16 | +import org.junit.Test; |
| 17 | + |
| 18 | +public class SolrJavaIntegrationTest { |
| 19 | + |
| 20 | + private HttpSolrClient solr; |
| 21 | + |
| 22 | + @Before |
| 23 | + public void setUp() throws Exception { |
| 24 | + |
| 25 | + solr = new HttpSolrClient("http://localhost:8983/solr/bigboxstore"); |
| 26 | + solr.setParser(new XMLResponseParser()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void givenAdd_thenVerifyAdded() throws SolrServerException, IOException { |
| 31 | + |
| 32 | + SolrInputDocument document = new SolrInputDocument(); |
| 33 | + document.addField("id", "123456"); |
| 34 | + document.addField("name", "Kenmore Dishwasher"); |
| 35 | + document.addField("price", "599.99"); |
| 36 | + |
| 37 | + solr.add(document); |
| 38 | + solr.commit(); |
| 39 | + |
| 40 | + SolrQuery query = new SolrQuery(); |
| 41 | + query.set("q", "id:123456"); |
| 42 | + QueryResponse response = null; |
| 43 | + |
| 44 | + response = solr.query(query); |
| 45 | + |
| 46 | + SolrDocumentList docList = response.getResults(); |
| 47 | + assertEquals(docList.getNumFound(), 1); |
| 48 | + |
| 49 | + for (SolrDocument doc : docList) { |
| 50 | + assertEquals((String) doc.getFieldValue("id"), "123456"); |
| 51 | + assertEquals((Double) doc.getFieldValue("price"), (Double) 599.99); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void givenDelete_thenVerifyDeleted() throws SolrServerException, IOException { |
| 57 | + |
| 58 | + solr.deleteById("123456"); |
| 59 | + solr.commit(); |
| 60 | + |
| 61 | + SolrQuery query = new SolrQuery(); |
| 62 | + query.set("q", "id:123456"); |
| 63 | + QueryResponse response = null; |
| 64 | + |
| 65 | + response = solr.query(query); |
| 66 | + |
| 67 | + SolrDocumentList docList = response.getResults(); |
| 68 | + assertEquals(docList.getNumFound(), 0); |
| 69 | + } |
| 70 | +} |
0 commit comments