Search in sources :

Example 6 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method importCRS.

/**
 * Test importing tags and properties
 * @param context the test context
 * @throws Exception if something goes wrong
 */
@Test
public void importCRS(TestContext context) throws Exception {
    String url = "/store?fallbackCRS=test";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport(null, null, null, Optional.empty(), "test", context.asyncAssertSuccess(v -> {
        verifyPosted(url, XML, context);
        async.complete();
    }));
    w.end(Buffer.buffer(XML));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 7 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreClientImportTest method importTagsAndCRS.

/**
 * Test importing tags and properties
 * @param context the test context
 * @throws Exception if something goes wrong
 */
@Test
public void importTagsAndCRS(TestContext context) throws Exception {
    String url = "/store?tags=testTag%2CtestTag2&props=" + "hello%3Awo%5C%3Arld%2Challo2%3Aworld2&fallbackCRS=test";
    stubFor(post(urlEqualTo(url)).willReturn(aResponse().withStatus(202)));
    Async async = context.async();
    WriteStream<Buffer> w = client.getStore().startImport(null, Arrays.asList("testTag", "testTag2"), Arrays.asList("hello:wo\\:rld", "hallo2:world2"), Optional.empty(), "test", context.asyncAssertSuccess(v -> {
        verifyPosted(url, XML, context);
        async.complete();
    }));
    w.end(Buffer.buffer(XML));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) WireMock.equalTo(com.github.tomakehurst.wiremock.client.WireMock.equalTo) Arrays(java.util.Arrays) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) RunWith(org.junit.runner.RunWith) Test(org.junit.Test) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) WireMock.postRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) Optional(java.util.Optional) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) VerificationException(com.github.tomakehurst.wiremock.client.VerificationException) WireMock.post(com.github.tomakehurst.wiremock.client.WireMock.post) Async(io.vertx.ext.unit.Async) Test(org.junit.Test)

Example 8 with WriteStream

use of io.vertx.core.streams.WriteStream in project georocket by georocket.

the class StoreEndpoint method doMerge.

/**
 * Perform a search and merge all retrieved chunks using the given merger
 * @param merger the merger
 * @param data Data to merge into the response
 * @param out the response to write the merged chunks to
 * @return a single that will emit one item when all chunks have been merged
 */
private Single<Void> doMerge(Merger<ChunkMeta> merger, Single<StoreCursor> data, WriteStream<Buffer> out) {
    return data.map(RxStoreCursor::new).flatMapObservable(RxStoreCursor::toObservable).flatMap(p -> store.rxGetOne(p.getRight()).flatMapObservable(crs -> merger.merge(crs, p.getLeft(), out).map(// left: count, right: not_accepted
    v -> Pair.of(1L, 0L)).onErrorResumeNext(t -> {
        if (t instanceof IllegalStateException) {
            // ignore it, but emit a warning later
            return Observable.just(Pair.of(0L, 1L));
        }
        return Observable.error(t);
    }).doOnTerminate(() -> {
        // don't forget to close the chunk!
        crs.close();
    })), 1).defaultIfEmpty(Pair.of(0L, 0L)).reduce((p1, p2) -> Pair.of(p1.getLeft() + p2.getLeft(), p1.getRight() + p2.getRight())).flatMap(p -> {
        long count = p.getLeft();
        long notaccepted = p.getRight();
        if (notaccepted > 0) {
            log.warn("Could not merge " + notaccepted + " chunks " + "because the merger did not accept them. Most likely " + "these are new chunks that were added while the " + "merge was in progress. If this worries you, just " + "repeat the request.");
        }
        if (count > 0) {
            merger.finish(out);
            return Observable.just(null);
        } else {
            return Observable.error(new FileNotFoundException("Not Found"));
        }
    }).toSingle().map(v -> null);
}
Also used : Arrays(java.util.Arrays) Router(io.vertx.ext.web.Router) RxStoreCursor(io.georocket.storage.RxStoreCursor) RoutingContext(io.vertx.ext.web.RoutingContext) StringUtils(org.apache.commons.lang3.StringUtils) ChunkMeta(io.georocket.storage.ChunkMeta) RxStore(io.georocket.storage.RxStore) StoreCursor(io.georocket.storage.StoreCursor) Single(rx.Single) Pair(org.apache.commons.lang3.tuple.Pair) Map(java.util.Map) Pump(io.vertx.core.streams.Pump) JsonObject(io.vertx.core.json.JsonObject) Logger(io.vertx.core.logging.Logger) Splitter(com.google.common.base.Splitter) OpenOptions(io.vertx.core.file.OpenOptions) ContentType(org.apache.http.entity.ContentType) UUID(java.util.UUID) Future(io.vertx.core.Future) FileNotFoundException(java.io.FileNotFoundException) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) HttpServerResponse(io.vertx.core.http.HttpServerResponse) FileSystem(io.vertx.core.file.FileSystem) RxHelper(io.vertx.rx.java.RxHelper) MultiMerger(io.georocket.output.MultiMerger) Pattern(java.util.regex.Pattern) AddressConstants(io.georocket.constants.AddressConstants) AsyncFile(io.vertx.core.file.AsyncFile) HttpServerRequest(io.vertx.core.http.HttpServerRequest) MimeTypeUtils(io.georocket.util.MimeTypeUtils) HashMap(java.util.HashMap) LoggerFactory(io.vertx.core.logging.LoggerFactory) Observable(rx.Observable) ServerAPIException(io.georocket.ServerAPIException) WriteStream(io.vertx.core.streams.WriteStream) StoreFactory(io.georocket.storage.StoreFactory) AsyncResult(io.vertx.core.AsyncResult) HttpException(io.georocket.util.HttpException) ParseException(org.apache.http.ParseException) ObservableFuture(io.vertx.rx.java.ObservableFuture) Vertx(io.vertx.core.Vertx) IOException(java.io.IOException) StringEscapeUtils(org.apache.commons.text.StringEscapeUtils) RxAsyncCursor(io.georocket.storage.RxAsyncCursor) File(java.io.File) JsonArray(io.vertx.core.json.JsonArray) ObjectId(org.bson.types.ObjectId) Merger(io.georocket.output.Merger) Handler(io.vertx.core.Handler) ConfigConstants(io.georocket.constants.ConfigConstants) FileNotFoundException(java.io.FileNotFoundException) RxStoreCursor(io.georocket.storage.RxStoreCursor)

Example 9 with WriteStream

use of io.vertx.core.streams.WriteStream in project vert.x by eclipse.

the class FileSystemTest method testPumpFileStreams.

@Test
@SuppressWarnings("unchecked")
public void testPumpFileStreams() throws Exception {
    String fileName1 = "some-file.dat";
    String fileName2 = "some-other-file.dat";
    // Non integer multiple of buffer size
    int fileSize = (int) (AsyncFileImpl.DEFAULT_READ_BUFFER_SIZE * 1000.3);
    byte[] content = TestUtils.randomByteArray(fileSize);
    createFile(fileName1, content);
    vertx.fileSystem().open(testDir + pathSep + fileName1, new OpenOptions(), arr -> {
        if (arr.succeeded()) {
            ReadStream rs = arr.result();
            // Open file for writing
            vertx.fileSystem().open(testDir + pathSep + fileName2, new OpenOptions(), ar -> {
                if (ar.succeeded()) {
                    WriteStream ws = ar.result();
                    Pump p = Pump.pump(rs, ws);
                    p.start();
                    rs.endHandler(v -> {
                        arr.result().close(car -> {
                            if (car.failed()) {
                                fail(ar.cause().getMessage());
                            } else {
                                ar.result().close(ar2 -> {
                                    if (ar2.failed()) {
                                        fail(ar2.cause().getMessage());
                                    } else {
                                        assertTrue(fileExists(fileName2));
                                        byte[] readBytes;
                                        try {
                                            readBytes = Files.readAllBytes(Paths.get(testDir + pathSep + fileName2));
                                        } catch (IOException e) {
                                            fail(e.getMessage());
                                            return;
                                        }
                                        assertEquals(Buffer.buffer(content), Buffer.buffer(readBytes));
                                        testComplete();
                                    }
                                });
                            }
                        });
                    });
                } else {
                    fail(ar.cause().getMessage());
                }
            });
        } else {
            fail(arr.cause().getMessage());
        }
    });
    await();
}
Also used : ReadStream(io.vertx.core.streams.ReadStream) WriteStream(io.vertx.core.streams.WriteStream) IOException(java.io.IOException) Pump(io.vertx.core.streams.Pump) Test(org.junit.Test)

Example 10 with WriteStream

use of io.vertx.core.streams.WriteStream in project java-chassis by ServiceComb.

the class TestReadStreamPart method saveToWriteStream.

@Test
public void saveToWriteStream() throws InterruptedException, ExecutionException {
    Buffer buf = Buffer.buffer();
    WriteStream<Buffer> writeStream = new MockUp<WriteStream<Buffer>>() {

        @Mock
        WriteStream<Buffer> write(Buffer data) {
            buf.appendBuffer(data);
            return null;
        }
    }.getMockInstance();
    part.saveToWriteStream(writeStream).get();
    Assert.assertEquals(src, buf.toString());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) WriteStream(io.vertx.core.streams.WriteStream) Mock(mockit.Mock) Test(org.junit.Test)

Aggregations

WriteStream (io.vertx.core.streams.WriteStream)19 Buffer (io.vertx.core.buffer.Buffer)17 Test (org.junit.Test)17 Arrays (java.util.Arrays)15 Optional (java.util.Optional)9 VerificationException (com.github.tomakehurst.wiremock.client.VerificationException)8 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)8 WireMock.equalTo (com.github.tomakehurst.wiremock.client.WireMock.equalTo)8 WireMock.post (com.github.tomakehurst.wiremock.client.WireMock.post)8 WireMock.postRequestedFor (com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor)8 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)8 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)8 WireMock.verify (com.github.tomakehurst.wiremock.client.WireMock.verify)8 Handler (io.vertx.core.Handler)8 Async (io.vertx.ext.unit.Async)8 TestContext (io.vertx.ext.unit.TestContext)8 VertxUnitRunner (io.vertx.ext.unit.junit.VertxUnitRunner)8 RunWith (org.junit.runner.RunWith)8 ReadStream (io.vertx.core.streams.ReadStream)7 File (java.io.File)7