package uk.ac.ebi.fg.myequivalents.test;
import java.util.HashSet;
import java.util.Set;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import org.apache.commons.lang3.RandomUtils;
import uk.ac.ebi.fg.myequivalents.access_control.model.User;
import uk.ac.ebi.fg.myequivalents.access_control.model.User.Role;
import uk.ac.ebi.fg.myequivalents.dao.access_control.UserDao;
import uk.ac.ebi.fg.myequivalents.managers.impl.db.DbManagerFactory;
import uk.ac.ebi.fg.myequivalents.managers.interfaces.EntityMappingManager;
import uk.ac.ebi.fg.myequivalents.managers.interfaces.ServiceManager;
import uk.ac.ebi.fg.myequivalents.model.Service;
import uk.ac.ebi.fg.myequivalents.resources.Resources;
/**
* An helper to generate random mappings, to be used by tests.
*
* @author brandizi
* <dl><dt>Date:</dt><dd>12 Mar 2015</dd>
*
*/
public class MappingsGenerator
{
public static final String EDITOR_PASS = "test.password";
public static final String EDITOR_SECRET = "test.secret";
public static final User EDITOR_USER = new User (
"test.editor", "Test Editor", "User", EDITOR_PASS, "test editor notes", Role.EDITOR, EDITOR_SECRET );
public static final String ADMIN_PASS = "test.password";
public static final String ADMIN_SECRET = "test.secret";
public static final User ADMIN_USER = new User (
"test.admin", "Test Admin", "User", ADMIN_PASS, "test admin notes", Role.ADMIN, ADMIN_SECRET );
public static final String SERVICE_NAME_PREFIX = "test.generated.service";
/**
* Change these values to fix the size of random elements that are generated.
*/
public int nservices = 10, nmappingBundles = 50, bundleMaxleSize = 5, maxMappingId = 100;
/**
* <p>Generates {@link #nmappingBundles} random mappings, linked to {@link #nservices} randomly-generated services.
* Each mapping has a random size ranging from 2 to {@link #bundleMaxleSize}, generates up to
* {@link #maxMappingId} + 1 entities.</p>
*
* <p>Services are created using {@link #SERVICE_NAME_PREFIX}, mapping IDs are just numbers.</p>
*
* <p>Everything is created using {@link #EDITOR_USER}, which is created in advance.
* {@link #ADMIN_USER} is also generated, for your convenience.</p>
*
*/
public void generateMappings ()
{
DbManagerFactory mgrFact = Resources.getInstance ().getMyEqManagerFactory ();
EntityManagerFactory emf = mgrFact.getEntityManagerFactory ();
EntityManager em = emf.createEntityManager ();
UserDao userDao = new UserDao ( em );
EntityTransaction ts = em.getTransaction ();
ts.begin ();
userDao.storeUnauthorized ( EDITOR_USER );
userDao.storeUnauthorized ( ADMIN_USER );
ts.commit ();
em.close ();
// Services
ServiceManager servMgr = mgrFact.newServiceManager ( EDITOR_USER.getEmail (), EDITOR_SECRET );
for ( int i = 0; i < nservices; i++ )
{
Service s = new Service ( SERVICE_NAME_PREFIX + i, "testType" );
s.setDescription ( "Testing Service, generated by " + this.getClass ().getName () );
s.setTitle ( s.getDescription () );
servMgr.storeServices ( s );
}
// Mapping bundles
EntityMappingManager mapMgr = mgrFact.newEntityMappingManager ( EDITOR_USER.getEmail (), EDITOR_SECRET );
for ( int i = 0; i < nmappingBundles; i++ )
{
// The bundle
int bundleSize = RandomUtils.nextInt ( 2, bundleMaxleSize + 1 );
Set<String> edids = new HashSet<> ();
for ( int j = 0; j < bundleSize; j++ )
{
int serviceId = RandomUtils.nextInt ( 0, nservices );
int eid = RandomUtils.nextInt ( 0, maxMappingId + 1 );
edids.add ( SERVICE_NAME_PREFIX + serviceId + ':' + eid );
}
mapMgr.storeMappingBundle ( edids.toArray ( new String [ 0 ] ) );
}
}
/**
* Removes data that were created via {@link #generateMappings()}.
*/
public int cleanUp ()
{
DbManagerFactory mgrFact = Resources.getInstance ().getMyEqManagerFactory ();
EntityManagerFactory emf = mgrFact.getEntityManagerFactory ();
EntityManager em = emf.createEntityManager ();
EntityTransaction ts = em.getTransaction ();
ts.begin ();
int result = 0;
result += em.createNativeQuery (
"DELETE FROM entity_mapping WHERE service_name LIKE '" + SERVICE_NAME_PREFIX + "%'"
).executeUpdate ();
result += em.createNativeQuery (
"DELETE FROM service WHERE name LIKE '" + SERVICE_NAME_PREFIX + "%'"
).executeUpdate ();
ts.commit ();
em.close ();
return result;
}
}