View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.solrserver;
35  
36  import fr.paris.lutece.portal.service.filter.FilterService;
37  import fr.paris.lutece.portal.service.filter.LuteceFilter;
38  import fr.paris.lutece.portal.service.filter.LuteceFilterChain;
39  import fr.paris.lutece.portal.service.init.AppInit;
40  import fr.paris.lutece.portal.service.plugin.PluginService;
41  import fr.paris.lutece.portal.service.util.AppPathService;
42  import fr.paris.lutece.test.LuteceTestCase;
43  
44  import org.apache.commons.io.IOUtils;
45  import org.apache.solr.client.solrj.impl.HttpSolrClient;
46  import org.apache.solr.common.SolrInputDocument;
47  import org.springframework.mock.web.DelegatingServletInputStream;
48  import org.springframework.mock.web.MockHttpServletRequest;
49  import org.springframework.mock.web.MockHttpServletResponse;
50  import org.springframework.mock.web.MockServletConfig;
51  import org.springframework.mock.web.MockServletContext;
52  import org.springframework.util.StreamUtils;
53  
54  import com.fasterxml.jackson.databind.JsonNode;
55  import com.fasterxml.jackson.databind.ObjectMapper;
56  
57  import java.io.ByteArrayInputStream;
58  import java.io.File;
59  import java.io.FileOutputStream;
60  import java.io.InputStream;
61  import java.io.OutputStream;
62  import java.util.ArrayList;
63  import java.util.Collection;
64  
65  import javax.servlet.ServletInputStream;
66  import javax.servlet.http.HttpServletRequest;
67  
68  
69  /**
70   *
71   * @url http://wiki.apache.org/solr/Solrj
72   */
73  public class SolrServerTest extends LuteceTestCase
74  {
75  
76      public void setUp( ) throws Exception
77      {
78          //Because the LuteceTestCase initializes lutece without a servletcontext
79          //it needs to be done manually here
80          if( _bInit )
81          {
82              throw new Exception( "SolrServerTest must be the one to initialize LUTECE" );
83          }
84          else
85          {
86  
87              String _strResourcesDir = getClass( ).getResource( "/" ).toString( ).replaceFirst( "file:", "" ).replaceFirst( "target/.*", "target/lutece/" );
88              System.out.println( "-------------resourcesDir------------" + _strResourcesDir );
89              AppPathService.init( _strResourcesDir );
90  
91              try ( InputStream in = this.getClass( ).getResourceAsStream( "plugins.dat" ) )
92              {
93                  try ( OutputStream out = new FileOutputStream(
94                              new File( _strResourcesDir + "WEB-INF/plugins/", "plugins.dat" ) ) )
95                  {
96                      IOUtils.copy( in, out );
97                  }
98              }
99  
100             MockServletContext context = new MockServletContext( ) {
101                 @Override
102                 public String getRealPath(String path) {
103                     return _strResourcesDir + path;
104                 }
105             };
106             AppInit.initServices( context, "/WEB-INF/conf/", null );
107 
108             _bInit = true;
109             System.out.println( "Lutece services initialized" );
110             PluginService.getPlugin( "solrserver" ).install( );
111             System.out.println( "SolrServer installed" );
112         }
113 
114         super.setUp( );
115     }
116 
117     public MockHttpServletRequest newSolrRequest() {
118         return new MockHttpServletRequest( ) {
119             @Override
120             public ServletInputStream getInputStream() {
121                 return new DelegatingServletInputStream(StreamUtils.emptyInput()) {
122                     @Override public boolean isFinished() {
123                         return true;
124                     }
125                 };
126             }
127         };
128     }
129     /**
130      * @throws Exception
131      */
132     public void testPushDoc(  ) throws Exception
133     {
134         //Apparently solr needs time to start
135         long nWait = 3000;
136         System.out.println("Waiting " + nWait/1000.0 + " seconds for the solrserver to settle");
137         Thread.sleep( nWait );
138 
139         LuteceFilter filter = FilterService.getInstance( ).getFilters( ).stream( ).filter( f ->
140                 "solrserver".equals( f.getName( ) )
141         ).findFirst( ).get( );
142 
143         MockHttpServletResponse response;
144         MockHttpServletRequest request;
145         LuteceFilterChain lfc;
146 
147         response = new MockHttpServletResponse( );
148         lfc = new LuteceFilterChain( );
149         request = newSolrRequest( );
150         request.setRequestURI( "/lutece/solrserver/solr/update" );
151         request.setQueryString( "stream.body=<delete><query>*:*</query></delete>&commit=true");
152         request.setServletPath( SolrServerFilter.SOLR_URI + "/update" );
153         System.out.println( request.getRequestURI( ) + "?" + request.getQueryString( ) );
154         filter.getFilter( ).doFilter( request, response, lfc );
155         Thread.sleep( 100 );
156 
157         response = new MockHttpServletResponse( );
158         lfc = new LuteceFilterChain( );
159         request = newSolrRequest( );
160         request.setRequestURI( "/lutece/solrserver/solr/update" );
161         request.setServletPath( SolrServerFilter.SOLR_URI + "/update" );
162         request.setQueryString("stream.body=<add><doc><field name=\"uid\">junit1</field><field name=\"content\">junitcontent1</field></doc></add>&commit=true");
163         System.out.println( request.getRequestURI( ) + "?" + request.getQueryString( ) );
164         filter.getFilter( ).doFilter( request, response, lfc );
165         Thread.sleep( 100 );
166 
167         response = new MockHttpServletResponse( );
168         lfc = new LuteceFilterChain( );
169         request = newSolrRequest( );
170         request.setRequestURI( "/lutece/solrserver/solr/select" );
171         request.setServletPath( SolrServerFilter.SOLR_URI + "/select" );
172         request.setQueryString("q=*:*&wt=json");
173         System.out.println( request.getRequestURI( ) + "?" + request.getQueryString( ) );
174         filter.getFilter( ).doFilter( request, response, lfc );
175         String strResponse = response.getContentAsString( );
176         System.out.println( strResponse );
177         JsonNode res = new ObjectMapper().readTree( strResponse );
178         JsonNode responseJson = res.get( "response" );
179         assertEquals( 1, responseJson.get("numFound").asInt( ) );
180         JsonNode doc = res.get( "response" ).get("docs").get( 0 );
181         assertEquals( "junit1", doc.get("uid").asText( ) );
182         assertEquals( "junitcontent1", doc.get( "content" ).asText( ) );
183     }
184 }