|
| 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.response.QueryResponse; |
| 10 | +import org.apache.solr.common.SolrDocument; |
| 11 | +import org.apache.solr.common.SolrDocumentList; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | + |
| 15 | +public class SolrJavaIntegrationTest { |
| 16 | + |
| 17 | + private SolrJavaIntegration solrJavaIntegration; |
| 18 | + |
| 19 | + @Before |
| 20 | + public void setUp() throws Exception { |
| 21 | + |
| 22 | + solrJavaIntegration = new SolrJavaIntegration("http://localhost:8983/solr/bigboxstore"); |
| 23 | + solrJavaIntegration.addSolrDocument("123456", "Kenmore Dishwasher", "599.99"); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenAdd_thenVerifyAddedByQueryOnId() throws SolrServerException, IOException { |
| 28 | + |
| 29 | + SolrQuery query = new SolrQuery(); |
| 30 | + query.set("q", "id:123456"); |
| 31 | + QueryResponse response = null; |
| 32 | + |
| 33 | + response = solrJavaIntegration.getSolrClient().query(query); |
| 34 | + |
| 35 | + SolrDocumentList docList = response.getResults(); |
| 36 | + assertEquals(docList.getNumFound(), 1); |
| 37 | + |
| 38 | + for (SolrDocument doc : docList) { |
| 39 | + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); |
| 40 | + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void whenAdd_thenVerifyAddedByQueryOnPrice() throws SolrServerException, IOException { |
| 46 | + |
| 47 | + SolrQuery query = new SolrQuery(); |
| 48 | + query.set("q", "price:599.99"); |
| 49 | + QueryResponse response = null; |
| 50 | + |
| 51 | + response = solrJavaIntegration.getSolrClient().query(query); |
| 52 | + |
| 53 | + SolrDocumentList docList = response.getResults(); |
| 54 | + assertEquals(1, docList.getNumFound()); |
| 55 | + |
| 56 | + for (SolrDocument doc : docList) { |
| 57 | + assertEquals("123456", (String) doc.getFieldValue("id")); |
| 58 | + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void whenAdd_thenVerifyAddedByQuery() throws SolrServerException, IOException { |
| 64 | + |
| 65 | + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("123456"); |
| 66 | + assertEquals("Kenmore Dishwasher", (String) doc.getFieldValue("name")); |
| 67 | + assertEquals((Double) 599.99, (Double) doc.getFieldValue("price")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + public void whenAddBean_thenVerifyAddedByQuery() throws SolrServerException, IOException { |
| 72 | + |
| 73 | + ProductBean pBean = new ProductBean("888", "Apple iPhone 6s", "299.99"); |
| 74 | + solrJavaIntegration.addProductBean(pBean); |
| 75 | + |
| 76 | + SolrDocument doc = solrJavaIntegration.getSolrClient().getById("888"); |
| 77 | + assertEquals("Apple iPhone 6s", (String) doc.getFieldValue("name")); |
| 78 | + assertEquals((Double) 299.99, (Double) doc.getFieldValue("price")); |
| 79 | + } |
| 80 | + |
| 81 | + @Test |
| 82 | + public void whenDeleteById_thenVerifyDeleted() throws SolrServerException, IOException { |
| 83 | + |
| 84 | + solrJavaIntegration.deleteSolrDocumentById("123456"); |
| 85 | + |
| 86 | + SolrQuery query = new SolrQuery(); |
| 87 | + query.set("q", "id:123456"); |
| 88 | + QueryResponse response = solrJavaIntegration.getSolrClient().query(query); |
| 89 | + |
| 90 | + SolrDocumentList docList = response.getResults(); |
| 91 | + assertEquals(0, docList.getNumFound()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void whenDeleteByQuery_thenVerifyDeleted() throws SolrServerException, IOException { |
| 96 | + |
| 97 | + solrJavaIntegration.deleteSolrDocumentByQuery("name:Kenmore Dishwasher"); |
| 98 | + |
| 99 | + SolrQuery query = new SolrQuery(); |
| 100 | + query.set("q", "id:123456"); |
| 101 | + QueryResponse response = null; |
| 102 | + |
| 103 | + response = solrJavaIntegration.getSolrClient().query(query); |
| 104 | + |
| 105 | + SolrDocumentList docList = response.getResults(); |
| 106 | + assertEquals(0, docList.getNumFound()); |
| 107 | + } |
| 108 | +} |
0 commit comments