/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.parsers;
import mock.SimpleEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.internna.iwebmvc.dao.DAO;
import org.internna.iwebmvc.dao.SecurityDAO;
import org.internna.iwebmvc.model.Document;
import org.internna.iwebmvc.model.MultiDocument;
import org.internna.iwebmvc.model.UUID;
import org.internna.iwebmvc.model.security.RoleImpl;
import org.internna.iwebmvc.model.security.UserImpl;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.FileCopyUtils;
import static org.junit.Assert.*;
@Transactional
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/spring.xml", "/documents.xml"})
@TransactionConfiguration(defaultRollback = true)
public class MultiDocumentParserTest {
private final Log logger = LogFactory.getLog(MultiDocumentParserTest.class);
@Autowired private DAO baseDao;
@Autowired private SecurityDAO securityDAO;
@Autowired private MultiDocumentParser multiDocumentParser;
private List<UUID> generatedDocIDs;
@BeforeTransaction
public void verifyInitialDatabaseState() {
assertTrue(CollectionUtils.isEmpty(baseDao.find(MultiDocument.class, 0, 100)));
}
@AfterTransaction
public void verifyFinalDatabaseState() {
assertNull(securityDAO.findUser("adminuser"));
assertTrue(CollectionUtils.isEmpty(baseDao.find(Document.class, 0, 100)));
assertTrue(CollectionUtils.isEmpty(baseDao.find(MultiDocument.class, 0, 100)));
}
@Before
public void init() throws Exception {
generatedDocIDs = new ArrayList<UUID>(5);
logger.debug("Creating DB data for test");
securityDAO.createAuthority("admin");
UserImpl user = new UserImpl();
user.setUsername("adminuser");
user.setPassword("adminuser");
user.setName("Admin");
user.addRole((RoleImpl) securityDAO.findAuthority("admin"));
securityDAO.createUser(user);
assertNotNull(securityDAO.findUser("adminuser"));
Document doc2 = new Document();
doc2.setTemporal(true);
doc2.setIdentifier("void image");
doc2.setWidth(1);
doc2.setHeight(1);
doc2.setCreation(new Date());
doc2.setMimeType("image/gif");
doc2.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc2.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/void.gif")));
baseDao.create(doc2);
generatedDocIDs.add(doc2.getId());
byte[] contents = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/void.gif"));
MultiDocument md = new MultiDocument();
Document doc = new Document();
doc.setTemporal(false);
doc.setIdentifier("void image");
doc.setWidth(1);
doc.setHeight(1);
doc.setCreation(new Date());
doc.setMimeType("image/gif");
doc.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc.setContents(contents);
Document doc3 = new Document();
doc3.setTemporal(false);
doc3.setIdentifier("blank image");
doc3.setWidth(1);
doc3.setHeight(1);
doc3.setCreation(new Date());
doc3.setMimeType("image/gif");
doc3.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc3.setContents(contents);
Document doc4 = new Document();
doc4.setTemporal(false);
doc4.setIdentifier("blank image");
doc4.setWidth(1);
doc4.setHeight(1);
doc4.setCreation(new Date());
doc4.setMimeType("image/gif");
doc4.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc4.setContents(contents);
Document doc5 = new Document();
doc5.setTemporal(false);
doc5.setIdentifier("blank image");
doc5.setWidth(1);
doc5.setHeight(1);
doc5.setCreation(new Date());
doc5.setMimeType("image/gif");
doc5.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc5.setContents(contents);
SimpleEntity entity = new SimpleEntity("john");
entity.setPhotos(md.addDocument(doc).addDocument(doc3).addDocument(doc4).addDocument(doc5));
baseDao.create(entity);
assertNotNull(baseDao.first(SimpleEntity.class));
assertNotNull(baseDao.first(MultiDocument.class));
}
@Test
public void testParseNulls() throws Exception {
assertNull(multiDocumentParser.parse(null));
MultiDocument md = new MultiDocument();
assertNull(multiDocumentParser.parse(md));
md.addDocument(new Document());
md.addDocument(new Document());
assertNull(multiDocumentParser.parse(md));
SimpleEntity entity = baseDao.first(SimpleEntity.class);
md = baseDao.first(MultiDocument.class);
md.removeAll();
MultiDocument parsed = multiDocumentParser.parse(md);
assertNull(parsed);
entity.setPhotos(parsed);
baseDao.update(entity);
assertNull(baseDao.first(MultiDocument.class));
}
@Test
public void testParseAdd() throws Exception {
MultiDocument md = new MultiDocument();
Document doc = baseDao.find(Document.class, generatedDocIDs.get(0));
assertNull(doc.getDocumentGroup());
md.addDocument(doc);
MultiDocument parsed = multiDocumentParser.parse(md);
baseDao.update(parsed);
assertTrue(parsed.getDocuments().size() == 1);
assertEquals(parsed, parsed.getDocuments().iterator().next().getDocumentGroup());
assertFalse(parsed.getDocuments().iterator().next().isTemporal());
assertNotNull(baseDao.find(Document.class, generatedDocIDs.get(0)).getDocumentGroup());
}
@Test
public void testParseModify() throws Exception {
MultiDocument bmd = baseDao.first(MultiDocument.class);
MultiDocument md = new MultiDocument();
md.setId(bmd.getId());
for (Document doc : bmd.getDocuments()) {
Document ndoc = new Document();
ndoc.setId(doc.getId());
ndoc.setIdentifier(doc.getIdentifier());
md.addDocument(ndoc);
}
Document mod = md.getDocuments().iterator().next();
mod.setIdentifier("newimageid");
MultiDocument parsed = multiDocumentParser.parse(md);
baseDao.update(parsed);
assertEquals(md, parsed);
for (Document d : parsed.getDocuments())
if (d.getId().equals(mod.getId()))
assertEquals("newimageid", d.getIdentifier());
}
@Test
public void testParseRemove() throws Exception {
MultiDocument bmd = baseDao.first(MultiDocument.class);
int size = bmd.getDocuments().size() - 1;
MultiDocument md = new MultiDocument();
md.setId(bmd.getId());
boolean add = false;
for (Document doc : bmd.getDocuments()) {
if (add) {
Document ndoc = new Document();
ndoc.setTemporal(true);
ndoc.setId(doc.getId());
ndoc.setIdentifier(doc.getIdentifier());
md.addDocument(ndoc);
} else add = true;
}
MultiDocument parsed = multiDocumentParser.parse(md);
baseDao.update(parsed);
assertFalse(CollectionUtils.isEmpty(parsed.getDocuments()));
assertTrue(parsed.getDocuments().size() == size);
assertTrue(parsed.getDocuments().iterator().next().getHeight() == 1);
assertFalse(parsed.getDocuments().iterator().next().isTemporal());
}
@Test
public void testParseComplete() throws Exception {
MultiDocument bmd = baseDao.first(MultiDocument.class);
MultiDocument md = new MultiDocument();
md.setId(bmd.getId());
Iterator<Document> it = bmd.getDocuments().iterator();
it.next();
Document d = it.next();
md.addDocument(d);
md.addDocument(it.next());
d.setIdentifier("newimageid");
Document ndoc = baseDao.find(Document.class, generatedDocIDs.get(0));
assertNull(ndoc.getDocumentGroup());
md.addDocument(ndoc);
MultiDocument parsed = multiDocumentParser.parse(md);
baseDao.update(parsed);
assertTrue(parsed.getDocuments().size() == 3);
for (Document pdoc : parsed.getDocuments())
if (pdoc.getId().equals(d.getId()))
assertEquals("newimageid", pdoc.getIdentifier());
}
@Test
public void testParseComplicated() throws Exception {
SimpleEntity entity = baseDao.first(SimpleEntity.class);
MultiDocument bmd = entity.getPhotos();
assertTrue(bmd.getDocuments().size() == 4);
MultiDocument md = new MultiDocument();
md.setId(bmd.getId());
md.addDocument(new Document());
md.getDocuments().iterator().next().setId(bmd.getDocuments().iterator().next().getId());
Document doc2 = new Document();
doc2.setTemporal(false);
doc2.setIdentifier("void image");
doc2.setWidth(1);
doc2.setHeight(1);
doc2.setCreation(new Date());
doc2.setMimeType("image/gif");
doc2.setAuthor((UserImpl) securityDAO.findUser("adminuser"));
doc2.setContents(FileCopyUtils.copyToByteArray(getClass().getResourceAsStream("/void.gif")));
md.addDocument(doc2);
Document doc = baseDao.find(Document.class, generatedDocIDs.get(0));
assertNull(doc.getDocumentGroup());
Document t = new Document();
t.setId(doc.getId());
md.addDocument(t);
entity.setPhotos(multiDocumentParser.parse(md));
baseDao.update(entity);
assertTrue(entity.getPhotos().getDocuments().size() == 2);
}
}