Search in sources :

Example 16 with Tuple

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

the class SecurityUtil method getUserAndOrganization.

/**
 * Get a user and an organization. Only returns something if both elements can be determined.
 */
public static Option<Tuple<User, Organization>> getUserAndOrganization(SecurityService sec, OrganizationDirectoryService orgDir, String orgId, UserDirectoryService userDir, String userId) {
    final Organization prevOrg = sec.getOrganization();
    try {
        final Organization org = orgDir.getOrganization(orgId);
        sec.setOrganization(org);
        return option(userDir.loadUser(userId)).fmap(new Function<User, Tuple<User, Organization>>() {

            @Override
            public Tuple<User, Organization> apply(User user) {
                return tuple(user, org);
            }
        });
    } catch (NotFoundException e) {
        return none();
    } finally {
        sec.setOrganization(prevOrg);
    }
}
Also used : JaxbOrganization(org.opencastproject.security.api.JaxbOrganization) Organization(org.opencastproject.security.api.Organization) User(org.opencastproject.security.api.User) JaxbUser(org.opencastproject.security.api.JaxbUser) NotFoundException(org.opencastproject.util.NotFoundException) Tuple(org.opencastproject.util.data.Tuple)

Example 17 with Tuple

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

the class IndexServiceImplTest method testCreateEventInputNormalExpectsCreatedScheduledEvent.

@Test
public void testCreateEventInputNormalExpectsCreatedScheduledEvent() throws Exception {
    String expectedTitle = "Test Event Creation";
    String username = "akm220";
    String org = "mh_default_org";
    String[] creators = new String[] {};
    Id mpId = new IdImpl("mp-id");
    String testResourceLocation = "/events/create-scheduled-event.json";
    JSONObject metadataJson = (JSONObject) parser.parse(IOUtils.toString(IndexServiceImplTest.class.getResourceAsStream(testResourceLocation)));
    Capture<Catalog> result = EasyMock.newCapture();
    Capture<String> mediapackageIdResult = EasyMock.newCapture();
    Capture<String> catalogIdResult = EasyMock.newCapture();
    Capture<String> filenameResult = EasyMock.newCapture();
    Capture<InputStream> catalogResult = EasyMock.newCapture();
    Capture<String> mediapackageTitleResult = EasyMock.newCapture();
    SecurityService securityService = setupSecurityService(username, org);
    Workspace workspace = EasyMock.createMock(Workspace.class);
    EasyMock.expect(workspace.put(EasyMock.capture(mediapackageIdResult), EasyMock.capture(catalogIdResult), EasyMock.capture(filenameResult), EasyMock.capture(catalogResult))).andReturn(new URI("catalog.xml"));
    EasyMock.replay(workspace);
    // Create Common Event Catalog UI Adapter
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = setupCommonCatalogUIAdapter(workspace).getA();
    // Setup mediapackage.
    MediaPackage mediapackage = EasyMock.createMock(MediaPackage.class);
    mediapackage.add(EasyMock.capture(result));
    EasyMock.expectLastCall();
    EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {});
    EasyMock.expect(mediapackage.getIdentifier()).andReturn(mpId).anyTimes();
    EasyMock.expect(mediapackage.getCreators()).andReturn(creators);
    mediapackage.addCreator("");
    EasyMock.expectLastCall();
    mediapackage.setTitle(EasyMock.capture(mediapackageTitleResult));
    EasyMock.expectLastCall();
    EasyMock.expect(mediapackage.getElements()).andReturn(new MediaPackageElement[] {}).anyTimes();
    EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {}).anyTimes();
    EasyMock.expect(mediapackage.getSeries()).andReturn(null).anyTimes();
    mediapackage.setSeries(EasyMock.anyString());
    EasyMock.expectLastCall();
    EasyMock.replay(mediapackage);
    IngestService ingestService = setupIngestService(mediapackage, Capture.<InputStream>newInstance());
    // Setup Authorization Service
    Tuple<MediaPackage, Attachment> returnValue = new Tuple<MediaPackage, Attachment>(mediapackage, null);
    AuthorizationService authorizationService = EasyMock.createMock(AuthorizationService.class);
    EasyMock.expect(authorizationService.setAcl(EasyMock.anyObject(MediaPackage.class), EasyMock.anyObject(AclScope.class), EasyMock.anyObject(AccessControlList.class))).andReturn(returnValue);
    EasyMock.replay(authorizationService);
    CaptureAgentStateService captureAgentStateService = setupCaptureAgentStateService();
    Capture<Date> captureStart = EasyMock.newCapture();
    Capture<Date> captureEnd = EasyMock.newCapture();
    SchedulerService schedulerService = EasyMock.createNiceMock(SchedulerService.class);
    schedulerService.addEvent(EasyMock.capture(captureStart), EasyMock.capture(captureEnd), EasyMock.anyString(), EasyMock.<Set<String>>anyObject(), EasyMock.anyObject(MediaPackage.class), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Map<String, String>>anyObject(), EasyMock.<Opt<Boolean>>anyObject(), EasyMock.<Opt<String>>anyObject(), EasyMock.anyString());
    EasyMock.expectLastCall().once();
    EasyMock.replay(schedulerService);
    // Run Test
    IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
    indexServiceImpl.setAuthorizationService(setupAuthorizationService(mediapackage));
    indexServiceImpl.setIngestService(ingestService);
    indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.addCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.setSecurityService(securityService);
    indexServiceImpl.setUserDirectoryService(noUsersUserDirectoryService);
    indexServiceImpl.setWorkspace(workspace);
    indexServiceImpl.setCaptureAgentStateService(captureAgentStateService);
    indexServiceImpl.setSchedulerService(schedulerService);
    String scheduledEvent = indexServiceImpl.createEvent(metadataJson, mediapackage);
    Assert.assertEquals(mediapackage.getIdentifier().compact(), scheduledEvent);
    assertTrue("The catalog must be added to the mediapackage", result.hasCaptured());
    assertEquals("The catalog should have been added to the correct mediapackage", mpId.toString(), mediapackageIdResult.getValue());
    assertTrue("The catalog should have a new id", catalogIdResult.hasCaptured());
    assertTrue("The catalog should have a new filename", filenameResult.hasCaptured());
    assertTrue("The catalog should have been added to the input stream", catalogResult.hasCaptured());
    assertTrue("The mediapackage should have had its title updated", catalogResult.hasCaptured());
    assertEquals("The mediapackage title should have been updated.", expectedTitle, mediapackageTitleResult.getValue());
    assertTrue("The catalog should have been created", catalogResult.hasCaptured());
    assertTrue(captureStart.hasCaptured());
    assertTrue(captureEnd.hasCaptured());
    Assert.assertEquals(new Date(DateTimeSupport.fromUTC("2008-03-16T14:00:00Z")), captureStart.getValue());
    Assert.assertEquals(new Date(DateTimeSupport.fromUTC("2008-03-16T14:01:00Z")), captureEnd.getValue());
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) SchedulerService(org.opencastproject.scheduler.api.SchedulerService) Attachment(org.opencastproject.mediapackage.Attachment) AclScope(org.opencastproject.security.api.AclScope) URI(java.net.URI) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) SecurityService(org.opencastproject.security.api.SecurityService) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) CaptureAgentStateService(org.opencastproject.capture.admin.api.CaptureAgentStateService) IngestService(org.opencastproject.ingest.api.IngestService) InputStream(java.io.InputStream) MediaPackageElementFlavor(org.opencastproject.mediapackage.MediaPackageElementFlavor) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) Date(java.util.Date) JSONObject(org.json.simple.JSONObject) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Id(org.opencastproject.mediapackage.identifier.Id) Tuple(org.opencastproject.util.data.Tuple) Workspace(org.opencastproject.workspace.api.Workspace) Test(org.junit.Test)

Example 18 with Tuple

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

the class IndexServiceImplTest method testCreateEventInputNoWorkflowConfigurationExpectsCreatedEvent.

@Test
public void testCreateEventInputNoWorkflowConfigurationExpectsCreatedEvent() throws Exception {
    String expectedTitle = "Test Event Creation";
    String username = "akm220";
    String org = "mh_default_org";
    String[] creators = new String[] {};
    Id mpId = new IdImpl("mp-id");
    String testResourceLocation = "/events/create-event-no-workflow-configuration.json";
    JSONObject metadataJson = (JSONObject) parser.parse(IOUtils.toString(IndexServiceImplTest.class.getResourceAsStream(testResourceLocation)));
    Capture<Catalog> result = EasyMock.newCapture();
    Capture<String> mediapackageIdResult = EasyMock.newCapture();
    Capture<String> catalogIdResult = EasyMock.newCapture();
    Capture<String> filenameResult = EasyMock.newCapture();
    Capture<InputStream> catalogResult = EasyMock.newCapture();
    Capture<String> mediapackageTitleResult = EasyMock.newCapture();
    SecurityService securityService = setupSecurityService(username, org);
    Workspace workspace = EasyMock.createMock(Workspace.class);
    EasyMock.expect(workspace.put(EasyMock.capture(mediapackageIdResult), EasyMock.capture(catalogIdResult), EasyMock.capture(filenameResult), EasyMock.capture(catalogResult))).andReturn(new URI("catalog.xml"));
    EasyMock.replay(workspace);
    CommonEventCatalogUIAdapter commonEventCatalogUIAdapter = setupCommonCatalogUIAdapter(workspace).getA();
    // Setup mediapackage.
    MediaPackage mediapackage = EasyMock.createMock(MediaPackage.class);
    mediapackage.add(EasyMock.capture(result));
    EasyMock.expectLastCall();
    EasyMock.expect(mediapackage.getIdentifier()).andReturn(mpId).anyTimes();
    EasyMock.expect(mediapackage.getCreators()).andReturn(creators);
    mediapackage.addCreator("");
    EasyMock.expectLastCall();
    mediapackage.setTitle(EasyMock.capture(mediapackageTitleResult));
    EasyMock.expectLastCall();
    EasyMock.expect(mediapackage.getElements()).andReturn(new MediaPackageElement[] {}).anyTimes();
    EasyMock.expect(mediapackage.getCatalogs(EasyMock.anyObject(MediaPackageElementFlavor.class))).andReturn(new Catalog[] {}).anyTimes();
    EasyMock.expect(mediapackage.getSeries()).andReturn(null).anyTimes();
    mediapackage.setSeries(EasyMock.anyString());
    EasyMock.expectLastCall();
    mediapackage.setSeriesTitle(EasyMock.anyString());
    EasyMock.expectLastCall();
    EasyMock.replay(mediapackage);
    IngestService ingestService = setupIngestService(mediapackage, Capture.<InputStream>newInstance());
    // Setup Authorization Service
    Tuple<MediaPackage, Attachment> returnValue = new Tuple<MediaPackage, Attachment>(mediapackage, null);
    AuthorizationService authorizationService = EasyMock.createMock(AuthorizationService.class);
    EasyMock.expect(authorizationService.setAcl(EasyMock.anyObject(MediaPackage.class), EasyMock.anyObject(AclScope.class), EasyMock.anyObject(AccessControlList.class))).andReturn(returnValue);
    EasyMock.replay(authorizationService);
    // Run Test
    IndexServiceImpl indexServiceImpl = new IndexServiceImpl();
    indexServiceImpl.setAuthorizationService(setupAuthorizationService(mediapackage));
    indexServiceImpl.setIngestService(ingestService);
    indexServiceImpl.setCommonEventCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.addCatalogUIAdapter(commonEventCatalogUIAdapter);
    indexServiceImpl.setUserDirectoryService(noUsersUserDirectoryService);
    indexServiceImpl.setSecurityService(securityService);
    indexServiceImpl.setWorkspace(workspace);
    indexServiceImpl.createEvent(metadataJson, mediapackage);
    assertTrue("The catalog must be added to the mediapackage", result.hasCaptured());
    assertEquals("The catalog should have been added to the correct mediapackage", mpId.toString(), mediapackageIdResult.getValue());
    assertTrue("The catalog should have a new id", catalogIdResult.hasCaptured());
    assertTrue("The catalog should have a new filename", filenameResult.hasCaptured());
    assertTrue("The catalog should have been added to the input stream", catalogResult.hasCaptured());
    assertTrue("The mediapackage should have had its title updated", catalogResult.hasCaptured());
    assertEquals("The mediapackage title should have been updated.", expectedTitle, mediapackageTitleResult.getValue());
    assertTrue("The catalog should have been created", catalogResult.hasCaptured());
}
Also used : AccessControlList(org.opencastproject.security.api.AccessControlList) Attachment(org.opencastproject.mediapackage.Attachment) AclScope(org.opencastproject.security.api.AclScope) URI(java.net.URI) IdImpl(org.opencastproject.mediapackage.identifier.IdImpl) SecurityService(org.opencastproject.security.api.SecurityService) MediaPackageElement(org.opencastproject.mediapackage.MediaPackageElement) CommonEventCatalogUIAdapter(org.opencastproject.index.service.catalog.adapter.events.CommonEventCatalogUIAdapter) IngestService(org.opencastproject.ingest.api.IngestService) InputStream(java.io.InputStream) DublinCoreCatalog(org.opencastproject.metadata.dublincore.DublinCoreCatalog) Catalog(org.opencastproject.mediapackage.Catalog) JSONObject(org.json.simple.JSONObject) AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Id(org.opencastproject.mediapackage.identifier.Id) Tuple(org.opencastproject.util.data.Tuple) Workspace(org.opencastproject.workspace.api.Workspace) Test(org.junit.Test)

Example 19 with Tuple

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

the class IndexServiceImplTest method setupAuthorizationService.

private AuthorizationService setupAuthorizationService(MediaPackage mediapackage) {
    // Setup Authorization Service
    Tuple<MediaPackage, Attachment> returnValue = new Tuple<>(mediapackage, null);
    AuthorizationService authorizationService = EasyMock.createMock(AuthorizationService.class);
    EasyMock.expect(authorizationService.setAcl(EasyMock.anyObject(MediaPackage.class), EasyMock.anyObject(AclScope.class), EasyMock.anyObject(AccessControlList.class))).andReturn(returnValue).anyTimes();
    EasyMock.replay(authorizationService);
    return authorizationService;
}
Also used : AuthorizationService(org.opencastproject.security.api.AuthorizationService) MediaPackage(org.opencastproject.mediapackage.MediaPackage) Attachment(org.opencastproject.mediapackage.Attachment) Tuple(org.opencastproject.util.data.Tuple)

Example 20 with Tuple

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

the class SeriesEndpoint method getFromAndToCreationRange.

/**
 * Parse two strings in UTC format into Date objects to represent a range of dates.
 *
 * @param createdFrom
 *          The string that represents the start date of the range.
 * @param createdTo
 *          The string that represents the end date of the range.
 * @return A Tuple with the two Dates
 * @throws IllegalArgumentException
 *           Thrown if the input strings are not valid UTC strings
 */
private Tuple<Date, Date> getFromAndToCreationRange(String createdFrom, String createdTo) {
    Date createdFromDate = null;
    Date createdToDate = null;
    if ((StringUtils.isNotBlank(createdFrom) && StringUtils.isBlank(createdTo)) || (StringUtils.isBlank(createdFrom) && StringUtils.isNotBlank(createdTo))) {
        logger.error("Both createdTo '{}' and createdFrom '{}' have to be specified or neither of them", createdTo, createdFrom);
        throw new IllegalArgumentException("Both createdTo '" + createdTo + "' and createdFrom '" + createdFrom + "' have to be specified or neither of them");
    } else {
        if (StringUtils.isNotBlank(createdFrom)) {
            try {
                createdFromDate = new Date(DateTimeSupport.fromUTC(createdFrom));
            } catch (IllegalStateException e) {
                logger.error("Unable to parse createdFrom parameter '{}':{}", createdFrom, ExceptionUtils.getStackTrace(e));
                throw new IllegalArgumentException("Unable to parse createdFrom parameter.");
            } catch (java.text.ParseException e) {
                logger.error("Unable to parse createdFrom parameter '{}':{}", createdFrom, ExceptionUtils.getStackTrace(e));
                throw new IllegalArgumentException("Unable to parse createdFrom parameter.");
            }
        }
        if (StringUtils.isNotBlank(createdTo)) {
            try {
                createdToDate = new Date(DateTimeSupport.fromUTC(createdTo));
            } catch (IllegalStateException e) {
                logger.error("Unable to parse createdTo parameter '{}':{}", createdTo, ExceptionUtils.getStackTrace(e));
                throw new IllegalArgumentException("Unable to parse createdTo parameter.");
            } catch (java.text.ParseException e) {
                logger.error("Unable to parse createdTo parameter '{}':{}", createdTo, ExceptionUtils.getStackTrace(e));
                throw new IllegalArgumentException("Unable to parse createdTo parameter.");
            }
        }
    }
    return new Tuple<>(createdFromDate, createdToDate);
}
Also used : ComponentContext(org.osgi.service.component.ComponentContext) Date(java.util.Date) Tuple(org.opencastproject.util.data.Tuple)

Aggregations

Tuple (org.opencastproject.util.data.Tuple)28 ArrayList (java.util.ArrayList)11 Date (java.util.Date)9 MediaPackage (org.opencastproject.mediapackage.MediaPackage)9 NotFoundException (org.opencastproject.util.NotFoundException)8 Test (org.junit.Test)7 IOException (java.io.IOException)6 URI (java.net.URI)6 Job (org.opencastproject.job.api.Job)5 WorkflowOperationException (org.opencastproject.workflow.api.WorkflowOperationException)5 HashMap (java.util.HashMap)4 JSONObject (org.json.simple.JSONObject)4 LaidOutElement (org.opencastproject.composer.api.LaidOutElement)4 Dimension (org.opencastproject.composer.layout.Dimension)4 Catalog (org.opencastproject.mediapackage.Catalog)4 AccessControlList (org.opencastproject.security.api.AccessControlList)4 File (java.io.File)3 InputStream (java.io.InputStream)3 LinkedList (java.util.LinkedList)3 MultiShapeLayout (org.opencastproject.composer.layout.MultiShapeLayout)3