Search in sources :

Example 1 with NamespaceBuilder

use of org.apache.distributedlog.api.namespace.NamespaceBuilder in project incubator-heron by apache.

the class DLDownloaderTest method testDownload.

@Test
public void testDownload() throws Exception {
    String logName = "test-download";
    URI uri = URI.create("distributedlog://127.0.0.1/test/distributedlog/" + logName);
    File tempFile = File.createTempFile("test", "download");
    // make sure it is deleted when the test completes
    tempFile.deleteOnExit();
    Path path = Paths.get(tempFile.toURI());
    Namespace ns = mock(Namespace.class);
    DistributedLogManager dlm = mock(DistributedLogManager.class);
    LogReader reader = mock(LogReader.class);
    when(ns.openLog(anyString())).thenReturn(dlm);
    when(dlm.getInputStream(eq(DLSN.InitialDLSN))).thenReturn(reader);
    when(reader.readNext(anyBoolean())).thenThrow(new EndOfStreamException("eos"));
    NamespaceBuilder nsBuilder = mock(NamespaceBuilder.class);
    when(nsBuilder.clientId(anyString())).thenReturn(nsBuilder);
    when(nsBuilder.conf(any(DistributedLogConfiguration.class))).thenReturn(nsBuilder);
    when(nsBuilder.uri(any(URI.class))).thenReturn(nsBuilder);
    when(nsBuilder.build()).thenReturn(ns);
    PowerMockito.mockStatic(Extractor.class);
    PowerMockito.doNothing().when(Extractor.class, "extract", any(InputStream.class), any(Path.class));
    DLDownloader downloader = new DLDownloader(() -> nsBuilder);
    downloader.download(uri, path);
    URI parentUri = URI.create("distributedlog://127.0.0.1/test/distributedlog");
    verify(nsBuilder, times(1)).clientId(eq("heron-downloader"));
    verify(nsBuilder, times(1)).conf(eq(CONF));
    verify(nsBuilder, times(1)).uri(parentUri);
    PowerMockito.verifyStatic(times(1));
    Extractor.extract(any(InputStream.class), eq(path));
    verify(ns, times(1)).openLog(eq(logName));
    verify(ns, times(1)).close();
}
Also used : Path(java.nio.file.Path) DistributedLogConfiguration(org.apache.distributedlog.DistributedLogConfiguration) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) EndOfStreamException(org.apache.distributedlog.exceptions.EndOfStreamException) DLInputStream(com.twitter.heron.dlog.DLInputStream) InputStream(java.io.InputStream) LogReader(org.apache.distributedlog.api.LogReader) Matchers.anyString(org.mockito.Matchers.anyString) URI(java.net.URI) File(java.io.File) Namespace(org.apache.distributedlog.api.namespace.Namespace) NamespaceBuilder(org.apache.distributedlog.api.namespace.NamespaceBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

DLInputStream (com.twitter.heron.dlog.DLInputStream)1 File (java.io.File)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 DistributedLogConfiguration (org.apache.distributedlog.DistributedLogConfiguration)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 LogReader (org.apache.distributedlog.api.LogReader)1 Namespace (org.apache.distributedlog.api.namespace.Namespace)1 NamespaceBuilder (org.apache.distributedlog.api.namespace.NamespaceBuilder)1 EndOfStreamException (org.apache.distributedlog.exceptions.EndOfStreamException)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1