/*******************************************************************************
* Gisgraphy Project
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
*
* Copyright 2008 Gisgraphy project
* David Masclet <davidmasclet@gisgraphy.com>
*
*
*******************************************************************************/
/**
*
*/
package com.gisgraphy.fulltext;
import static com.gisgraphy.domain.valueobject.Pagination.paginate;
import java.io.File;
import static org.junit.Assert.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import com.gisgraphy.domain.geoloc.entity.AlternateName;
import com.gisgraphy.domain.geoloc.entity.City;
import com.gisgraphy.domain.geoloc.entity.GisFeature;
import com.gisgraphy.domain.repository.ICityDao;
import com.gisgraphy.domain.valueobject.AlternateNameSource;
import com.gisgraphy.domain.valueobject.Output;
import com.gisgraphy.domain.valueobject.Output.OutputStyle;
import com.gisgraphy.domain.valueobject.Pagination;
import com.gisgraphy.helper.FileHelper;
import com.gisgraphy.serializer.common.OutputFormat;
import com.gisgraphy.test.FeedChecker;
import com.gisgraphy.test.GisgraphyTestHelper;
public class OutputstreamResponseWrapperTest extends
AbstractIntegrationHttpSolrTestCase {
private ICityDao cityDao;
@Test
public void testOutputstreamResponseWrapperTestShouldSerializeFromInputStream() {
File tempDir = FileHelper.createTempDir(this.getClass()
.getSimpleName());
File file = new File(tempDir.getAbsolutePath()
+ System.getProperty("file.separator") + "serialize.txt");
Long featureId = 1001L;
GisFeature gisFeature = GisgraphyTestHelper.createCity("Saint-André",
1.5F, 2F, featureId);
AlternateName alternateName = new AlternateName();
alternateName.setName("alteré");
alternateName.setGisFeature(gisFeature);
alternateName.setSource(AlternateNameSource.ALTERNATENAMES_FILE);
gisFeature.addAlternateName(alternateName);
City paris = new City(gisFeature);
// save cities and check it is saved
this.cityDao.save(paris);
assertNotNull(this.cityDao.getByFeatureId(featureId));
// commit changes
this.solRSynchroniser.commit();
OutputStream outputStream = null;
try {
outputStream = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
fail();
}
OutputstreamResponseWrapper outputstreamResponseWrapper = new OutputstreamResponseWrapper(
outputStream, "XML");
SolrServer server;
try {
server = new CommonsHttpSolrServer(solrClient.getURL(), null,
outputstreamResponseWrapper);
Pagination pagination = paginate().from(1).to(10);
Output output = Output.withFormat(OutputFormat.XML)
.withLanguageCode("FR").withStyle(OutputStyle.SHORT)
.withIndentation();
FulltextQuery fulltextQuery = new FulltextQuery("Saint-André",
pagination, output, Constants.ONLY_CITY_PLACETYPE, "fr");
server.query(FulltextQuerySolrHelper.parameterize(fulltextQuery));
} catch (MalformedURLException e) {
fail();
} catch (SolrServerException e) {
fail();
}
// TODO test file and remove tempdir
String content = "";
try {
content = GisgraphyTestHelper.readFileAsString(file.getAbsolutePath());
} catch (IOException e) {
fail("can not get content of file " + file.getAbsolutePath());
}
FeedChecker.assertQ("The query return incorrect values", content,
"//*[@numFound='1']", "//*[@name='status'][.='0']",
"//*[@name='"+FullTextFields.NAME.getValue()+"'][.='"
+ paris.getName() + "']");
// delete temp dir
assertTrue("the tempDir has not been deleted", GisgraphyTestHelper
.DeleteNonEmptyDirectory(tempDir));
}
@Autowired
public void setCityDao(ICityDao cityDao) {
this.cityDao = cityDao;
}
}