/*
* Hibernate, Relational Persistence for Idiomatic Java
*
* JBoss, Home of Professional Open Source
* Copyright 2012 Red Hat Inc. and/or its affiliates and other contributors
* as indicated by the @authors tag. All rights reserved.
* See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This copyrighted material is made available to anyone wishing to use,
* modify, copy, or redistribute it subject to the terms and conditions
* of the GNU Lesser General Public License, v. 2.1.
* This program is distributed in the hope that it will be useful, but WITHOUT A
* 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,
* v.2.1 along with this distribution; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
package org.hibernate.search.test.integration.jbossas7;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.inject.Inject;
import org.hibernate.search.test.integration.jbossas7.controller.MemberRegistration;
import org.hibernate.search.test.integration.jbossas7.model.Member;
import org.hibernate.search.test.integration.jbossas7.util.Resources;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.Asset;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.descriptor.api.Descriptors;
import org.jboss.shrinkwrap.descriptor.api.persistence20.PersistenceDescriptor;
import org.jboss.shrinkwrap.descriptor.api.spec.se.manifest.ManifestDescriptor;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Test the hibernate search module in JBoss AS.
*
* @author Davide D'Alto
* @author Sanne Grinovero
*/
@RunWith(Arquillian.class)
public class ModuleMemberRegistrationIT {
@Deployment
public static Archive<?> createTestArchive() {
WebArchive archive = ShrinkWrap
.create( WebArchive.class, ModuleMemberRegistrationIT.class.getSimpleName() + ".war" )
.addClasses( Member.class, MemberRegistration.class, Resources.class )
.addAsResource( persistenceXml(), "META-INF/persistence.xml" )
.add( manifest(), "META-INF/MANIFEST.MF" )
.addAsWebInfResource( EmptyAsset.INSTANCE, "beans.xml" );
return archive;
}
private static Asset manifest() {
String manifest = Descriptors.create( ManifestDescriptor.class )
.attribute( "Dependencies", "org.hibernate.search.orm services" )
.exportAsString();
return new StringAsset( manifest );
}
private static Asset persistenceXml() {
String persistenceXml = Descriptors.create( PersistenceDescriptor.class )
.version( "2.0" )
.createPersistenceUnit()
.name( "primary" )
.jtaDataSource( "java:jboss/datasources/ExampleDS" )
.getOrCreateProperties()
.createProperty().name( "hibernate.hbm2ddl.auto" ).value( "create-drop" ).up()
.createProperty().name( "hibernate.search.default.lucene_version" ).value( "LUCENE_CURRENT" ).up()
.createProperty().name( "hibernate.search.default.directory_provider" ).value( "ram" ).up()
.createProperty().name( "hibernate.search.autoregister_listeners" ).value( "true" ).up()
.up().up()
.exportAsString();
return new StringAsset( persistenceXml );
}
@Inject
MemberRegistration memberRegistration;
@Test
public void testRegister() throws Exception {
Member newMember = memberRegistration.getNewMember();
newMember.setName( "Davide D'Alto" );
newMember.setEmail( "davide@mailinator.com" );
newMember.setPhoneNumber( "2125551234" );
memberRegistration.register();
assertNotNull( newMember.getId() );
}
@Test
public void testNewMemberSearch() throws Exception {
Member newMember = memberRegistration.getNewMember();
newMember.setName( "Peter O'Tall" );
newMember.setEmail( "peter@mailinator.com" );
newMember.setPhoneNumber( "4643646643" );
memberRegistration.register();
List<Member> search = memberRegistration.search( "Peter" );
assertFalse( "Expected at least one result after the indexing", search.isEmpty() );
assertEquals( "Search hasn't found a new member", newMember.getName(), search.get( 0 ).getName() );
}
@Test
public void testUnexistingMember() throws Exception {
List<Member> search = memberRegistration.search( "TotallyInventedName" );
assertNotNull( "Search should never return null", search );
assertTrue( "Search results should be empty", search.isEmpty() );
}
}