Search in sources :

Example 1 with StaticFileService

use of org.opencastproject.staticfiles.api.StaticFileService in project opencast by opencast.

the class StaticFileRestServiceTest method testDeleteStaticFile.

@Test
public void testDeleteStaticFile() throws FileUploadException, Exception {
    // Setup static file service.
    StaticFileService fileService = EasyMock.createMock(StaticFileService.class);
    final String fileUuid = "12345";
    EasyMock.expect(fileService.storeFile(anyObject(String.class), anyObject(InputStream.class))).andReturn(fileUuid);
    fileService.deleteFile(fileUuid);
    EasyMock.expectLastCall();
    EasyMock.expect(fileService.getFile(fileUuid)).andThrow(new NotFoundException());
    EasyMock.replay(fileService);
    // Run the test
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.activate(getComponentContext(null, 100000000L));
    staticFileRestService.setSecurityService(getSecurityService());
    staticFileRestService.setStaticFileService(fileService);
    // Test a good store request
    Response result = staticFileRestService.postStaticFile(newMockRequest());
    assertEquals(Status.CREATED.getStatusCode(), result.getStatus());
    String location = result.getMetadata().get("location").get(0).toString();
    String uuid = location.substring(location.lastIndexOf("/") + 1);
    Response response = staticFileRestService.deleteStaticFile(uuid);
    assertEquals(Status.NO_CONTENT.getStatusCode(), response.getStatus());
    try {
        staticFileRestService.getStaticFile(uuid);
        fail("NotFoundException must be passed on");
    } catch (NotFoundException e) {
    // expected
    }
}
Also used : Response(javax.ws.rs.core.Response) StaticFileService(org.opencastproject.staticfiles.api.StaticFileService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) NotFoundException(org.opencastproject.util.NotFoundException) Test(org.junit.Test)

Example 2 with StaticFileService

use of org.opencastproject.staticfiles.api.StaticFileService in project opencast by opencast.

the class StaticFileRestServiceTest method testStoreStaticFileInputHttpServletRequest.

@Test
public void testStoreStaticFileInputHttpServletRequest() throws FileUploadException, Exception {
    // Setup static file service.
    StaticFileService fileService = EasyMock.createMock(StaticFileService.class);
    String fileUuid = "12345";
    String fileName = "other.mov";
    EasyMock.expect(fileService.storeFile(eq(fileName), anyObject(InputStream.class))).andReturn(fileUuid);
    EasyMock.expect(fileService.getFileName(fileUuid)).andReturn(fileName);
    EasyMock.replay(fileService);
    // Run the test
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.activate(getComponentContext(null, 100000000L));
    staticFileRestService.setSecurityService(getSecurityService());
    staticFileRestService.setStaticFileService(fileService);
    // Test a good store request
    Response result = staticFileRestService.postStaticFile(newMockRequest());
    assertEquals(Status.CREATED.getStatusCode(), result.getStatus());
    assertTrue(result.getMetadata().size() > 0);
    assertTrue(result.getMetadata().get("location").size() > 0);
    String location = result.getMetadata().get("location").get(0).toString();
    String uuid = location.substring(location.lastIndexOf("/") + 1);
    // assertTrue(IOUtils.contentEquals(new ByteArrayInputStream(MOCK_FILE_CONTENT.getBytes("UTF-8")),
    // staticFileServiceImpl.getFile(uuid)));
    // Test a request with too large of an input stream
    HttpServletRequest tooLargeRequest = EasyMock.createMock(HttpServletRequest.class);
    EasyMock.expect(tooLargeRequest.getContentLength()).andReturn(1000000000).anyTimes();
    EasyMock.replay(tooLargeRequest);
    result = staticFileRestService.postStaticFile(tooLargeRequest);
    assertEquals(Status.BAD_REQUEST.getStatusCode(), result.getStatus());
    staticFileRestService.activate(getComponentContext("true", 100000000L));
    URI staticFileURL = staticFileRestService.getStaticFileURL(uuid);
    assertEquals("http://localhost/staticfiles/mh_default_org/" + uuid + "/other.mov", staticFileURL.toString());
}
Also used : Response(javax.ws.rs.core.Response) MockHttpServletRequest(org.apache.commons.fileupload.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) StaticFileService(org.opencastproject.staticfiles.api.StaticFileService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) URI(java.net.URI) Test(org.junit.Test)

Example 3 with StaticFileService

use of org.opencastproject.staticfiles.api.StaticFileService in project opencast by opencast.

the class TestThemesEndpoint method setupServices.

private void setupServices() throws Exception {
    user = new JaxbUser("test", null, "Test User", "test@test.com", "test", new DefaultOrganization(), new HashSet<JaxbRole>());
    UserDirectoryService userDirectoryService = EasyMock.createNiceMock(UserDirectoryService.class);
    EasyMock.expect(userDirectoryService.loadUser((String) EasyMock.anyObject())).andReturn(user).anyTimes();
    EasyMock.replay(userDirectoryService);
    SecurityService securityService = EasyMock.createNiceMock(SecurityService.class);
    EasyMock.expect(securityService.getOrganization()).andReturn(new DefaultOrganization()).anyTimes();
    EasyMock.expect(securityService.getUser()).andReturn(user).anyTimes();
    EasyMock.replay(securityService);
    SeriesService seriesService = EasyMock.createNiceMock(SeriesService.class);
    EasyMock.replay(seriesService);
    MessageSender messageSender = EasyMock.createNiceMock(MessageSender.class);
    messageSender.sendObjectMessage(EasyMock.anyObject(String.class), EasyMock.anyObject(MessageSender.DestinationType.class), EasyMock.anyObject(Serializable.class));
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(messageSender);
    // Create AdminUI Search Index
    AdminUISearchIndex adminUISearchIndex = EasyMock.createMock(AdminUISearchIndex.class);
    final Capture<ThemeSearchQuery> themeQueryCapture = new Capture<ThemeSearchQuery>();
    EasyMock.expect(adminUISearchIndex.getByQuery(EasyMock.capture(themeQueryCapture))).andAnswer(new IAnswer<SearchResult<org.opencastproject.index.service.impl.index.theme.Theme>>() {

        @Override
        public SearchResult<org.opencastproject.index.service.impl.index.theme.Theme> answer() throws Throwable {
            return createThemeCaptureResult(themeQueryCapture);
        }
    });
    final Capture<SeriesSearchQuery> seriesQueryCapture = new Capture<SeriesSearchQuery>();
    EasyMock.expect(adminUISearchIndex.getByQuery(EasyMock.capture(seriesQueryCapture))).andAnswer(new IAnswer<SearchResult<Series>>() {

        @Override
        public SearchResult<Series> answer() throws Throwable {
            return createSeriesCaptureResult(seriesQueryCapture);
        }
    });
    EasyMock.replay(adminUISearchIndex);
    themesServiceDatabaseImpl = new ThemesServiceDatabaseImpl();
    themesServiceDatabaseImpl.setEntityManagerFactory(newTestEntityManagerFactory(ThemesServiceDatabaseImpl.PERSISTENCE_UNIT));
    themesServiceDatabaseImpl.setUserDirectoryService(userDirectoryService);
    themesServiceDatabaseImpl.setSecurityService(securityService);
    themesServiceDatabaseImpl.setMessageSender(messageSender);
    themesServiceDatabaseImpl.activate(null);
    StaticFileService staticFileService = EasyMock.createNiceMock(StaticFileService.class);
    EasyMock.expect(staticFileService.getFile(EasyMock.anyString())).andReturn(new ByteArrayInputStream("test".getBytes("utf-8"))).anyTimes();
    EasyMock.expect(staticFileService.getFileName(EasyMock.anyString())).andStubReturn("test.mp4");
    EasyMock.replay(staticFileService);
    BundleContext bundleContext = EasyMock.createNiceMock(BundleContext.class);
    EasyMock.expect(bundleContext.getProperty("org.opencastproject.server.url")).andReturn("http://localhost:8080").anyTimes();
    EasyMock.replay(bundleContext);
    ComponentContext componentContext = EasyMock.createNiceMock(ComponentContext.class);
    EasyMock.expect(componentContext.getBundleContext()).andReturn(bundleContext).anyTimes();
    EasyMock.expect(componentContext.getProperties()).andReturn(new Hashtable<String, Object>()).anyTimes();
    EasyMock.replay(componentContext);
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.setStaticFileService(staticFileService);
    staticFileRestService.activate(componentContext);
    this.setThemesServiceDatabase(themesServiceDatabaseImpl);
    this.setSecurityService(securityService);
    this.setSeriesService(seriesService);
    this.setStaticFileService(staticFileService);
    this.setStaticFileRestService(staticFileRestService);
    this.setIndex(adminUISearchIndex);
}
Also used : Serializable(java.io.Serializable) MessageSender(org.opencastproject.message.broker.api.MessageSender) AdminUISearchIndex(org.opencastproject.adminui.impl.index.AdminUISearchIndex) JaxbUser(org.opencastproject.security.api.JaxbUser) StaticFileRestService(org.opencastproject.staticfiles.endpoint.StaticFileRestService) Capture(org.easymock.Capture) ThemesServiceDatabaseImpl(org.opencastproject.themes.persistence.ThemesServiceDatabaseImpl) SecurityService(org.opencastproject.security.api.SecurityService) HashSet(java.util.HashSet) ComponentContext(org.osgi.service.component.ComponentContext) SeriesSearchQuery(org.opencastproject.index.service.impl.index.series.SeriesSearchQuery) Hashtable(java.util.Hashtable) SearchResult(org.opencastproject.matterhorn.search.SearchResult) UserDirectoryService(org.opencastproject.security.api.UserDirectoryService) StaticFileService(org.opencastproject.staticfiles.api.StaticFileService) SeriesService(org.opencastproject.series.api.SeriesService) ByteArrayInputStream(java.io.ByteArrayInputStream) ThemeSearchQuery(org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery) DefaultOrganization(org.opencastproject.security.api.DefaultOrganization) BundleContext(org.osgi.framework.BundleContext)

Example 4 with StaticFileService

use of org.opencastproject.staticfiles.api.StaticFileService in project opencast by opencast.

the class StaticFileRestServiceTest method testUploadMaxSizeReached.

@Test
public void testUploadMaxSizeReached() throws FileUploadException, Exception {
    // Setup static file service.
    StaticFileService fileService = EasyMock.createMock(StaticFileService.class);
    EasyMock.expect(fileService.storeFile(eq("other.mov"), anyObject(InputStream.class))).andReturn("12345");
    EasyMock.replay(fileService);
    // Run the test
    StaticFileRestService staticFileRestService = new StaticFileRestService();
    staticFileRestService.activate(getComponentContext(null, 10L));
    staticFileRestService.setSecurityService(getSecurityService());
    staticFileRestService.setStaticFileService(fileService);
    // Test a sized mock request
    Response result = staticFileRestService.postStaticFile(newMockRequest());
    assertEquals(Status.BAD_REQUEST.getStatusCode(), result.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) StaticFileService(org.opencastproject.staticfiles.api.StaticFileService) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)4 StaticFileService (org.opencastproject.staticfiles.api.StaticFileService)4 InputStream (java.io.InputStream)3 Response (javax.ws.rs.core.Response)3 Test (org.junit.Test)3 Serializable (java.io.Serializable)1 URI (java.net.URI)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 MockHttpServletRequest (org.apache.commons.fileupload.MockHttpServletRequest)1 Capture (org.easymock.Capture)1 AdminUISearchIndex (org.opencastproject.adminui.impl.index.AdminUISearchIndex)1 SeriesSearchQuery (org.opencastproject.index.service.impl.index.series.SeriesSearchQuery)1 ThemeSearchQuery (org.opencastproject.index.service.impl.index.theme.ThemeSearchQuery)1 SearchResult (org.opencastproject.matterhorn.search.SearchResult)1 MessageSender (org.opencastproject.message.broker.api.MessageSender)1 DefaultOrganization (org.opencastproject.security.api.DefaultOrganization)1 JaxbUser (org.opencastproject.security.api.JaxbUser)1 SecurityService (org.opencastproject.security.api.SecurityService)1