Search in sources :

Example 31 with AResult

use of org.opencastproject.assetmanager.api.query.AResult in project opencast by opencast.

the class AbstractAssetManagerBasicTest method testSetAndUpdateProperty.

@Test
public void testSetAndUpdateProperty() throws Exception {
    final MediaPackage mp = mkMediaPackage(mkCatalog());
    final String mpId = mp.getIdentifier().toString();
    am.takeSnapshot(OWNER, mp);
    final Date d1 = new Date(0);
    final Date d2 = new Date(1);
    final Date d3 = new Date(2);
    logger.info("Add a property to the media package");
    assertTrue("The property should be set", am.setProperty(p.start.mk(mpId, d1)));
    logger.info("Select all properties of the media package");
    {
        AResult r = q.select(p.allProperties()).where(q.mediaPackageId(mpId)).run();
        assertEquals("One record should be found", 1, r.getSize());
        assertEquals("One property should be found", 1, r.getRecords().bind(getProperties).toList().size());
        assertEquals("Value check", d1, r.getRecords().bind(getProperties).head2().getValue().get(Value.DATE));
        assertEquals("One property should be found", 1, q.select(q.properties()).run().getRecords().toList().size());
    }
    logger.info("Update the property");
    assertTrue("The property should be updated", am.setProperty(p.start.mk(mpId, d2)));
    {
        AResult r = q.select(p.allProperties()).where(q.mediaPackageId(mpId)).run();
        assertEquals("One record should be found", 1, r.getSize());
        assertEquals("One property should be found", 1, r.getRecords().bind(getProperties).toList().size());
        assertEquals("Value check", d2, r.getRecords().bind(getProperties).head2().getValue().get(Value.DATE));
        assertEquals("One record should be found", 1, q.select(q.properties()).run().getRecords().toList().size());
    }
    logger.info("The existence of multiple versions of a media package should not affect property storage");
    logger.info("Add a new version of the media package");
    am.takeSnapshot(OWNER, mp);
    logger.info("Update the property again");
    assertTrue("The property should be updated", am.setProperty(p.start.mk(mpId, d3)));
    {
        AResult r = q.select(p.allProperties()).where(q.mediaPackageId(mpId)).run();
        assertEquals("Two records should be found since there are now two versions of the media package and no version restriction has been applied", 2, r.getSize());
        assertEquals("Two properties should be found, one per found record", 2, r.getRecords().bind(getProperties).toList().size());
        assertEquals("There should be one distinct property in all of the found records", 1, r.getRecords().bind(getProperties).toSet().size());
        assertEquals("Value check", d3, r.getRecords().bind(getProperties).head2().getValue().get(Value.DATE));
        assertEquals("Two record should be found", 2, q.select(q.properties()).run().getRecords().toList().size());
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) AResult(org.opencastproject.assetmanager.api.query.AResult) Date(java.util.Date) Test(org.junit.Test)

Example 32 with AResult

use of org.opencastproject.assetmanager.api.query.AResult in project opencast by opencast.

the class AbstractAssetManagerPropertyRetrievalTest method testPropertyRetrieval.

/**
 * Create some media packages and associate some random properties to each of them.
 * Then iterate all created properties and create a query for each of them.
 */
@Test
@Parameters
public void testPropertyRetrieval(final Params params) {
    // create a set of media packages and add them to the asset manager
    final String[] mps = createAndAddMediaPackagesSimple(params.mpCount, 1, 1);
    final Random random = new Random(System.nanoTime());
    // create a set of random property names
    final PropertyName[] propertyNames = createRandomPropertyNames(params.propertyNameSetSize);
    // create a random amount of random properties for each media package
    final Stream<Property> props = $(mps).bind(new Fn<String, Stream<Property>>() {

        @Override
        public Stream<Property> apply(final String mp) {
            // create a random amount of random properties
            return Stream.cont(inc()).take(random.nextInt(params.maxProps - params.minProps + 1) + params.minProps).map(new Fn<Integer, Property>() {

                @Override
                public Property apply(Integer ignore) {
                    // try to pick a free property a 100 times
                    for (int i = 0; i < 100; i++) {
                        // randomly select a property name
                        final PropertyName pName = propertyNames[random.nextInt(propertyNames.length)];
                        // check if the selected property is already associated with the current media package
                        final ASelectQuery doesPropertyExist = q.select(q.properties(pName)).where(q.mediaPackageId(mp));
                        if (sizeOf(doesPropertyExist.run().getRecords().bind(ARecords.getProperties)) == 0) {
                            // create a property with a randomly picked value
                            final Property p = Property.mk(PropertyId.mk(mp, pName), params.values[random.nextInt(params.values.length)]);
                            if (am.setProperty(p))
                                return p;
                        }
                    }
                    fail("Cannot pick another random property that has not been inserted yet");
                    return null;
                }
            });
        }
    }).eval();
    assertThat("Number of generated properties", sizeOf(props), allOf(greaterThanOrEqualTo(params.mpCount * params.minProps), lessThanOrEqualTo(params.mpCount * params.maxProps)));
    // iterate all properties and try to retrieve them from the AssetManager
    for (final Property prop : props) {
        final AResult r = q.select(params.mkTarget.apply(prop)).where(params.mkWhere.apply(prop)).run();
        // get all properties of the result records
        assertThat("Number of records", r.getSize(), params.expectRecords);
        final Stream<Property> allProps = r.getRecords().bind(ARecords.getProperties);
        assertThat("Total number of properties: " + allProps.mkString(", "), sizeOf(allProps), params.expectPropertiesTotal);
        assertThat("Total number of snapshots", sizeOf(r.getRecords().bind(ARecords.getSnapshot)), params.expectSnapshotsTotal);
        final Stream<Property> findSavedProperty = r.getRecords().bind(ARecords.getProperties).filter(Booleans.eq(prop));
        if (params.expectContainsSavedProperty) {
            assertThat("Contains saved property", findSavedProperty, hasItem(prop));
        }
    }
}
Also used : PropertyName(org.opencastproject.assetmanager.api.PropertyName) Fn(com.entwinemedia.fn.Fn) Random(java.util.Random) AResult(org.opencastproject.assetmanager.api.query.AResult) ASelectQuery(org.opencastproject.assetmanager.api.query.ASelectQuery) Property(org.opencastproject.assetmanager.api.Property) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 33 with AResult

use of org.opencastproject.assetmanager.api.query.AResult in project opencast by opencast.

the class AbstractAssetManagerSelectTest method testSelectProperties.

@Test
public void testSelectProperties() throws Exception {
    final MediaPackage mp1 = mkMediaPackage();
    final MediaPackageElement mpe = mkCatalog();
    mp1.add(mpe);
    am.takeSnapshot(OWNER, mp1);
    // 
    assertEquals("No records should be found", 0, q.select(q.snapshot()).where(q.hasPropertiesOf("org.opencastproject.service")).run().getSize());
    // 
    logger.info("Set property on first episode");
    am.setProperty(Property.mk(PropertyId.mk(mp1.getIdentifier().toString(), "org.opencastproject.service", "count"), Value.mk(10L)));
    assertEquals("One record should be found", 1, q.select(q.snapshot()).where(q.hasPropertiesOf("org.opencastproject.service")).run().getSize());
    // 
    logger.info("Add another media package with some properties of the same namespace");
    final MediaPackage mp2 = mkMediaPackage(mkCatalog());
    am.takeSnapshot(OWNER, mp2);
    am.setProperty(p.count.mk(mp2.getIdentifier().toString(), 20L));
    am.setProperty(p.approved.mk(mp2.getIdentifier().toString(), true));
    am.setProperty(p.start.mk(mp2.getIdentifier().toString(), new Date()));
    // 
    logger.info("Add a 3rd media package without any properties");
    am.takeSnapshot(OWNER, mkMediaPackage(mkCatalog()));
    // 
    {
        final AResult r = q.select(q.snapshot(), q.nothing()).where(p.hasPropertiesOfNamespace()).run();
        assertEquals("Two records should be found", 2, r.getSize());
        logger.info(r.getSearchTime() + "ms");
    }
    {
        final AResult r = q.select(q.snapshot()).where(p.hasPropertiesOfNamespace()).run();
        assertEquals("Two snapshots should be found", 2, r.getRecords().bind(getSnapshot).toList().size());
        logger.info(r.getSearchTime() + "ms");
    }
    {
        final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.hasPropertiesOfNamespace()).run();
        assertEquals("Two snapshots should be found", 2, r.getRecords().bind(getSnapshot).toList().size());
        logger.info(r.getSearchTime() + "ms");
    }
    {
        final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.hasPropertiesOfNamespace()).run();
        assertEquals("Two records with properties of the defined namespace should be found", 2, r.getSize());
        logger.info(r.getSearchTime() + "ms");
    }
    {
        final AResult r = q.select(q.snapshot(), p.allProperties()).run();
        assertEquals("Three records should be found in total", 3, r.getSize());
        logger.info(r.getSearchTime() + "ms");
    }
    {
        final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.count.le(5L)).run();
        assertEquals("No records should be found in total", 0, r.getSize());
    }
    {
        final AResult r = q.select(q.snapshot(), p.allProperties()).where(p.count.gt(5L)).run();
        assertEquals("No records should be found in total", 2, r.getSize());
    }
}
Also used : MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) MediaPackage(org.opencastproject.mediapackage.MediaPackage) AResult(org.opencastproject.assetmanager.api.query.AResult) RichAResult(org.opencastproject.assetmanager.api.query.RichAResult) Date(java.util.Date) Test(org.junit.Test)

Example 34 with AResult

use of org.opencastproject.assetmanager.api.query.AResult in project opencast by opencast.

the class AbstractAssetManagerSelectTest method testSelectByEndAndStartProperty.

@Test
public void testSelectByEndAndStartProperty() throws Exception {
    final MediaPackage mp = mkMediaPackage(mkCatalog());
    final String mpId = mp.getIdentifier().toString();
    am.takeSnapshot(OWNER, mp);
    final Date d0 = new Date(0);
    final Date d1 = new Date(1);
    final Date d3 = new Date(3);
    assertTrue("The property should be set", am.setProperty(p.start.mk(mpId, d1)));
    assertTrue("The property should be set", am.setProperty(p.end.mk(mpId, d3)));
    logger.info("Select all media package's end from d0 and start to d3");
    {
        AResult r = q.select(q.snapshot()).where(q.mediaPackageId(mpId).and(p.start.le(d3)).and(p.end.ge(d0))).run();
        assertEquals("One record should be found", 1, r.getSize());
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) AResult(org.opencastproject.assetmanager.api.query.AResult) RichAResult(org.opencastproject.assetmanager.api.query.RichAResult) Date(java.util.Date) Test(org.junit.Test)

Example 35 with AResult

use of org.opencastproject.assetmanager.api.query.AResult in project opencast by opencast.

the class AbstractAssetManagerSelectTest method testPredicateZero.

@Test
public void testPredicateZero() throws Exception {
    final MediaPackage mp1 = mkMediaPackage(mkCatalog());
    am.takeSnapshot(OWNER, mp1);
    {
        final AResult r = q.select(q.snapshot()).run();
        assertEquals("One record should be found", 1, r.getSize());
        assertEquals("One snapshot should be found", 1, r.getRecords().bind(getSnapshot).toList().size());
        assertEquals("No properties should be found", 0, r.getRecords().bind(getProperties).toList().size());
    }
    {
        final AResult r = q.select(q.snapshot()).where(q.always()).run();
        assertEquals("One record should be found", 1, r.getSize());
        assertEquals("One snapshot should be found", 1, r.getRecords().bind(getSnapshot).toList().size());
        assertEquals("No properties should be found", 0, r.getRecords().bind(getProperties).toList().size());
    }
    {
        final AResult r = q.select(q.snapshot()).where(q.always().not()).run();
        assertEquals("No record should be found", 0, r.getSize());
    }
    {
        logger.info("Show an example use case of predicate's zero element");
        final boolean selectAll = true;
        final AResult r = q.select(q.snapshot()).where(selectAll ? q.always() : q.mediaPackageId("bla")).run();
        assertEquals("One record should be found", 1, r.getSize());
        assertEquals("One snapshot should be found", 1, r.getRecords().bind(getSnapshot).toList().size());
        assertEquals("No properties should be found", 0, r.getRecords().bind(getProperties).toList().size());
    }
}
Also used : MediaPackage(org.opencastproject.mediapackage.MediaPackage) AResult(org.opencastproject.assetmanager.api.query.AResult) RichAResult(org.opencastproject.assetmanager.api.query.RichAResult) Test(org.junit.Test)

Aggregations

AResult (org.opencastproject.assetmanager.api.query.AResult)46 AQueryBuilder (org.opencastproject.assetmanager.api.query.AQueryBuilder)37 ARecord (org.opencastproject.assetmanager.api.query.ARecord)29 NotFoundException (org.opencastproject.util.NotFoundException)27 SchedulerTransactionLockException (org.opencastproject.scheduler.api.SchedulerTransactionLockException)21 MediaPackage (org.opencastproject.mediapackage.MediaPackage)20 SchedulerConflictException (org.opencastproject.scheduler.api.SchedulerConflictException)20 SchedulerException (org.opencastproject.scheduler.api.SchedulerException)20 UnauthorizedException (org.opencastproject.security.api.UnauthorizedException)20 IOException (java.io.IOException)19 MediaPackageException (org.opencastproject.mediapackage.MediaPackageException)19 ValidationException (net.fortuna.ical4j.model.ValidationException)18 Test (org.junit.Test)18 SeriesException (org.opencastproject.series.api.SeriesException)18 ServiceException (org.osgi.framework.ServiceException)18 ConfigurationException (org.osgi.service.cm.ConfigurationException)18 Date (java.util.Date)16 RichAResult (org.opencastproject.assetmanager.api.query.RichAResult)14 DublinCoreCatalog (org.opencastproject.metadata.dublincore.DublinCoreCatalog)11 Log.getHumanReadableTimeString (org.opencastproject.util.Log.getHumanReadableTimeString)10