|
| 1 | +/* |
| 2 | + * Copyright 2012-2014 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.data.solr.core; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | + |
| 20 | +import org.apache.solr.client.solrj.SolrClient; |
| 21 | +import org.apache.solr.client.solrj.SolrQuery; |
| 22 | +import org.apache.solr.client.solrj.SolrRequest; |
| 23 | +import org.apache.solr.client.solrj.SolrServerException; |
| 24 | +import org.apache.solr.client.solrj.response.QueryResponse; |
| 25 | +import org.apache.solr.client.solrj.response.SolrPingResponse; |
| 26 | +import org.apache.solr.common.SolrDocumentList; |
| 27 | +import org.junit.Assert; |
| 28 | +import org.junit.Before; |
| 29 | +import org.junit.Test; |
| 30 | +import org.junit.runner.RunWith; |
| 31 | +import org.mockito.ArgumentCaptor; |
| 32 | +import org.mockito.Mock; |
| 33 | +import org.mockito.Mockito; |
| 34 | +import org.mockito.runners.MockitoJUnitRunner; |
| 35 | +import org.springframework.data.solr.core.query.Criteria; |
| 36 | +import org.springframework.data.solr.core.query.SimpleQuery; |
| 37 | +import org.springframework.data.solr.server.SolrClientFactory; |
| 38 | + |
| 39 | +/** |
| 40 | + * |
| 41 | + * @author Venil Noronha |
| 42 | + */ |
| 43 | +@RunWith(MockitoJUnitRunner.class) |
| 44 | +public class SolrTemplateMulticoreTests { |
| 45 | + |
| 46 | + private SolrTemplate solrTemplate; |
| 47 | + private @Mock SolrClient defaultSolrClient; |
| 48 | + private @Mock SolrClient core1Client; |
| 49 | + private @Mock SolrClient core2Client; |
| 50 | + private @Mock SolrClientFactory solrClientFactory; |
| 51 | + |
| 52 | + @Before |
| 53 | + public void setUp() { |
| 54 | + Mockito.when(solrClientFactory.getSolrClient()).thenReturn(defaultSolrClient); |
| 55 | + Mockito.when(solrClientFactory.getSolrClient("core1")).thenReturn(core1Client); |
| 56 | + Mockito.when(solrClientFactory.getSolrClient("core2")).thenReturn(core2Client); |
| 57 | + solrTemplate = new SolrTemplate(solrClientFactory); |
| 58 | + solrTemplate.afterPropertiesSet(); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testGetSolrClients() throws SolrServerException, IOException { |
| 63 | + SolrClient client1 = solrClientFactory.getSolrClient("core1"); |
| 64 | + SolrClient client2 = solrClientFactory.getSolrClient("core2"); |
| 65 | + Assert.assertNotNull(client1); |
| 66 | + Assert.assertNotNull(client2); |
| 67 | + Assert.assertEquals(core1Client, client1); |
| 68 | + Assert.assertEquals(core2Client, client2); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testPingSpecificCores() throws SolrServerException, IOException { |
| 73 | + Mockito.when(core1Client.ping()).thenReturn(new SolrPingResponse()); |
| 74 | + Mockito.when(core2Client.ping()).thenReturn(new SolrPingResponse()); |
| 75 | + SolrPingResponse pingResult1 = solrTemplate.ping("core1"); |
| 76 | + SolrPingResponse pingResult2 = solrTemplate.ping("core2"); |
| 77 | + Assert.assertNotNull(pingResult1); |
| 78 | + Assert.assertNotNull(pingResult2); |
| 79 | + Mockito.verify(core1Client, Mockito.times(1)).ping(); |
| 80 | + Mockito.verify(core2Client, Mockito.times(1)).ping(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testCountQueries() throws SolrServerException, IOException { |
| 85 | + ArgumentCaptor<SolrQuery> captor1 = ArgumentCaptor.forClass(SolrQuery.class); |
| 86 | + ArgumentCaptor<SolrQuery> captor2 = ArgumentCaptor.forClass(SolrQuery.class); |
| 87 | + |
| 88 | + QueryResponse response1Mock = Mockito.mock(QueryResponse.class); |
| 89 | + SolrDocumentList resultList1 = new SolrDocumentList(); |
| 90 | + resultList1.setNumFound(10); |
| 91 | + Mockito.when(response1Mock.getResults()).thenReturn(resultList1); |
| 92 | + QueryResponse response2Mock = Mockito.mock(QueryResponse.class); |
| 93 | + SolrDocumentList resultList2 = new SolrDocumentList(); |
| 94 | + resultList2.setNumFound(10); |
| 95 | + Mockito.when(response2Mock.getResults()).thenReturn(resultList2); |
| 96 | + |
| 97 | + Mockito.when(core1Client.query(Mockito.any(SolrQuery.class), Mockito.eq(SolrRequest.METHOD.GET))).thenReturn(response1Mock); |
| 98 | + Mockito.when(core2Client.query(Mockito.any(SolrQuery.class), Mockito.eq(SolrRequest.METHOD.GET))).thenReturn(response2Mock); |
| 99 | + |
| 100 | + long result1 = solrTemplate.count("core1", new SimpleQuery(new Criteria("field_1").is("value1"))); |
| 101 | + long result2 = solrTemplate.count("core2", new SimpleQuery(new Criteria("field_2").is("value2"))); |
| 102 | + Assert.assertEquals(resultList1.getNumFound(), result1); |
| 103 | + Assert.assertEquals(resultList2.getNumFound(), result2); |
| 104 | + |
| 105 | + Mockito.verify(core1Client, Mockito.times(1)).query(captor1.capture(), Mockito.eq(SolrRequest.METHOD.GET)); |
| 106 | + Mockito.verify(core2Client, Mockito.times(1)).query(captor2.capture(), Mockito.eq(SolrRequest.METHOD.GET)); |
| 107 | + } |
| 108 | + |
| 109 | +} |
0 commit comments