Search in sources :

Example 1 with PropertyName

use of org.opencastproject.assetmanager.api.PropertyName 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)

Aggregations

Fn (com.entwinemedia.fn.Fn)1 Random (java.util.Random)1 Parameters (junitparams.Parameters)1 Test (org.junit.Test)1 Property (org.opencastproject.assetmanager.api.Property)1 PropertyName (org.opencastproject.assetmanager.api.PropertyName)1 AResult (org.opencastproject.assetmanager.api.query.AResult)1 ASelectQuery (org.opencastproject.assetmanager.api.query.ASelectQuery)1