package no.dusken.aranea.control;
import no.dusken.aranea.AbstractTest;
import no.dusken.aranea.model.Image;
import no.dusken.aranea.model.Tag;
import no.dusken.aranea.service.ImageService;
import no.dusken.aranea.service.SearchService;
import no.dusken.aranea.service.StoreImageService;
import no.dusken.aranea.service.TagService;
import org.apache.commons.io.FileUtils;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchResults;
import org.compass.gps.CompassGps;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import java.io.File;
import java.util.List;
import java.util.Map;
import static junit.framework.Assert.*;
/**
* @author Marvin B. Lillehaug <lillehau@underdusken.no>
*/
public class TestImageSearchController extends AbstractTest {
private BasicSearchController searchController;
private SearchService searchService;
private StoreImageService storeImageService;
private ImageService imageService;
private TagService tagService;
/**
* Save it here to be able to delete it in onTearDown()
*/
File tempDir;
File file;
File file2;
/**
* Has to be before the transaction. I guess this is because also
* compass uses transactions
*
* @throws Exception if...
*/
@BeforeTransaction
public void onSetUpBeforeTransaction() throws Exception {
// create a temp dir
tempDir = File.createTempFile("araneaSearch", "tmpDir");
// This is needed, I don't know why:
tempDir.delete();
tempDir.mkdirs();
assertTrue("Could not create temp dir", tempDir.isDirectory() && tempDir.canWrite());
File orgfile = new File("src/test/resources/images/scarlett-johansson-blog.jpg");
assertTrue("Could not read test image " + orgfile.getAbsolutePath(), orgfile.exists() && orgfile.canRead());
file = File.createTempFile("tempImageSearch2", "", tempDir);
FileUtils.copyFile(orgfile, file);
Image image = storeImageService.createImage(file);
image.setDescription("scarlett johansson");
imageService.saveOrUpdate(image);
File orgfile2 = new File("src/test/resources/images/testImage.jpg");
assertTrue("Could not read test image " + orgfile2.getAbsolutePath(), orgfile2.exists() && orgfile2.canRead());
file2 = File.createTempFile("tempImageSearch", "", tempDir);
FileUtils.copyFile(orgfile, file2);
Image image2 = storeImageService.createImage(file2);
image.setDescription("a glass");
Tag tag = new Tag();
tag.setName("whisky");
image2.getTags().add(tag);
imageService.saveOrUpdate(image2);
searchController.setType("image");
CompassGps gps = (CompassGps) getApplicationContext().getBean("compassGps");
// create the index:
gps.index();
}
@Test
public void testDescriptionSearch() throws Exception {
CompassSearchResults results = getResults("scarlett");
assertTrue("Did not get hit for search", results.getHits().length == 1);
}
@Test
public void testTagSearch() throws Exception {
CompassSearchResults results = getResults("whisky");
assertTrue("Did not get hit for search " + results.getHits().length, results.getHits().length == 1);
}
@Test
public void testNoResultsSearch() throws Exception {
CompassSearchResults results = getResults("Ostepåp");
assertTrue("Did not get hit for search", results.getHits().length == 0);
}
@Test
public void testDescripotionSearch2() throws Exception {
List results = searchService.search("scarlett", 0);
assertEquals("Did not get hit for search", 1, results.size());
}
@Test
public void testTagSearch2() throws Exception {
List results = searchService.search("whisky", 0);
assertEquals("Did not get hit for search " + results.size(), 1, results.size());
}
@Test
public void testNoResultsSearch2() throws Exception {
List results = searchService.search("Ostepåp", 0);
assertTrue("Did not get hit for search", results.size() == 0);
}
private CompassSearchResults getResults(String query) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
CompassSearchCommand command = new CompassSearchCommand(query);
@SuppressWarnings("unchecked")
Map<String, Object> map = searchController
.handle(request, new MockHttpServletResponse(), command, null).getModel();
return (CompassSearchResults) map.get("searchResults");
}
@AfterTransaction
public void tearDown(){
for (Image i : imageService.getImages()){
imageService.remove(i);
}
for(Tag t : tagService.getTags()){
tagService.remove(t);
}
}
@After
public void onTearDown() throws Exception {
FileUtils.deleteDirectory(tempDir);
assertFalse("Could not delete tempDir", tempDir.exists());
assertFalse("Could not delete files", file.exists());
}
@Autowired
public void setSearchController(BasicSearchController searchController) {
this.searchController = searchController;
}
@Autowired
public void setImageService(ImageService imageService) {
this.imageService = imageService;
}
@Autowired
public void setStoreImageService(StoreImageService storeImageService) {
this.storeImageService = storeImageService;
}
@Autowired
public void setSearchService(SearchService searchService) {
this.searchService = searchService;
}
@Autowired
public void setTagService(TagService tagService) {
this.tagService = tagService;
}
}