Search in sources :

Example 6 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class WorkspaceImplTest method testGetNoFilename.

@Test
public void testGetNoFilename() throws Exception {
    final File expectedFile = testFolder.newFile("test.txt");
    FileUtils.write(expectedFile, "asdf");
    expectedFile.deleteOnExit();
    WorkingFileRepository repo = EasyMock.createNiceMock(WorkingFileRepository.class);
    EasyMock.expect(repo.getBaseUri()).andReturn(new URI("http://localhost:8080/files")).anyTimes();
    EasyMock.replay(repo);
    workspace.setRepository(repo);
    RequestRunner<Either<String, Option<File>>> requestRunner = new TrustedHttpClient.RequestRunner<Either<String, Option<File>>>() {

        @Override
        public Either<Exception, Either<String, Option<File>>> run(Function<HttpResponse, Either<String, Option<File>>> f) {
            Either<String, Option<File>> right = Either.right(Option.some(expectedFile));
            return Either.right(right);
        }
    };
    TrustedHttpClient trustedHttpClient = EasyMock.createNiceMock(TrustedHttpClient.class);
    EasyMock.expect(trustedHttpClient.<Either<String, Option<File>>>runner(EasyMock.anyObject(HttpUriRequest.class))).andReturn(requestRunner).anyTimes();
    EasyMock.replay(trustedHttpClient);
    workspace.setTrustedHttpClient(trustedHttpClient);
    File resultingFile = workspace.get(URI.create("http://foo.com/myaccount/videos/"));
    Assert.assertEquals(expectedFile, resultingFile);
}
Also used : TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) Function(org.opencastproject.util.data.Function) RequestRunner(org.opencastproject.security.api.TrustedHttpClient.RequestRunner) Either(org.opencastproject.util.data.Either) Option(org.opencastproject.util.data.Option) File(java.io.File) URI(java.net.URI) WorkingFileRepository(org.opencastproject.workingfilerepository.api.WorkingFileRepository) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Example 7 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class Functions method addDay.

public static Function<Date, Date> addDay(final int days) {
    return new Function<Date, Date>() {

        @Override
        public Date apply(Date date) {
            final Calendar c = Calendar.getInstance();
            c.setTimeZone(TimeZone.getTimeZone("UTC"));
            c.setTime(date);
            c.add(Calendar.DAY_OF_MONTH, days);
            return c.getTime();
        }
    };
}
Also used : Function(org.opencastproject.util.data.Function) Calendar(java.util.Calendar) Date(java.util.Date)

Example 8 with Function

use of org.opencastproject.util.data.Function in project opencast by opencast.

the class PersistenceUtil method newPersistenceEnvironment.

/**
 * Create a new, concurrently usable persistence environment which uses JPA local transactions.
 * <p>
 * Please note that calling {@link PersistenceEnv#tx(Function)} always creates a <em>new</em> transaction. Transaction
 * propagation is <em>not</em> supported.
 */
public static PersistenceEnv newPersistenceEnvironment(final EntityManagerFactory emf) {
    return new PersistenceEnv() {

        @Override
        public <A> A tx(Function<EntityManager, A> transactional) {
            EntityManager em = null;
            EntityTransaction tx = null;
            try {
                em = emf.createEntityManager();
                tx = em.getTransaction();
                tx.begin();
                A ret = transactional.apply(em);
                tx.commit();
                return ret;
            } catch (RuntimeException e) {
                if (tx.isActive()) {
                    tx.rollback();
                }
                // propagate exception
                throw (e);
            } finally {
                if (em != null)
                    em.close();
            }
        }

        @Override
        public void close() {
            emf.close();
        }
    };
}
Also used : Function(org.opencastproject.util.data.Function) EntityTransaction(javax.persistence.EntityTransaction) EntityManager(javax.persistence.EntityManager)

Aggregations

Function (org.opencastproject.util.data.Function)8 EntityManager (javax.persistence.EntityManager)3 File (java.io.File)2 URI (java.net.URI)2 EntityTransaction (javax.persistence.EntityTransaction)2 TrustedHttpClient (org.opencastproject.security.api.TrustedHttpClient)2 Either (org.opencastproject.util.data.Either)2 Option (org.opencastproject.util.data.Option)2 Calendar (java.util.Calendar)1 Date (java.util.Date)1 Hashtable (java.util.Hashtable)1 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)1 Predicate (javax.persistence.criteria.Predicate)1 HttpResponse (org.apache.http.HttpResponse)1 StatusLine (org.apache.http.StatusLine)1 HttpUriRequest (org.apache.http.client.methods.HttpUriRequest)1 IAnswer (org.easymock.IAnswer)1 Before (org.junit.Before)1 Test (org.junit.Test)1 SearchResultItem (org.opencastproject.oaipmh.persistence.SearchResultItem)1