Search in sources :

Example 1 with JdkHttpClient

use of com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient in project embedded-cassandra by nosan.

the class WebCassandraDirectoryProviderTests method beforeAll.

@BeforeAll
static void beforeAll() throws IOException {
    httpServer = HttpServer.create(new InetSocketAddress(0), 0);
    httpServer.createContext("/", exchange -> {
        String uri = exchange.getRequestURI().toString();
        ClassPathResource resource = new ClassPathResource(uri.substring(uri.lastIndexOf('/')));
        long size = Files.size(Paths.get(resource.toURI()));
        exchange.sendResponseHeaders(200, size);
        try (InputStream inputStream = resource.getInputStream()) {
            StreamUtils.copy(inputStream, exchange.getResponseBody());
        } finally {
            exchange.close();
        }
    });
    httpServer.setExecutor(Executors.newCachedThreadPool());
    httpServer.start();
    httpClient = new JdkHttpClient(Duration.ofSeconds(1), Duration.ofSeconds(1));
}
Also used : InetSocketAddress(java.net.InetSocketAddress) InputStream(java.io.InputStream) JdkHttpClient(com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient) ClassPathResource(com.github.nosan.embedded.cassandra.commons.ClassPathResource) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 2 with JdkHttpClient

use of com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient in project embedded-cassandra by nosan.

the class WebCassandraDirectoryProviderTests method construct2.

@Test
void construct2() {
    JdkHttpClient httpClient = new JdkHttpClient();
    WebCassandraDirectoryProvider wcdp = new WebCassandraDirectoryProvider(httpClient);
    assertThat(wcdp).hasFieldOrPropertyWithValue("httpClient", httpClient);
    assertThat(wcdp).hasFieldOrPropertyWithValue("downloadDirectory", Paths.get(System.getProperty("user.home")));
}
Also used : JdkHttpClient(com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient) RepeatedTest(org.junit.jupiter.api.RepeatedTest) Test(org.junit.jupiter.api.Test)

Example 3 with JdkHttpClient

use of com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient in project embedded-cassandra by nosan.

the class CassandraBuilder method build.

/**
 * Build a new {@link Cassandra} instance.
 *
 * @return a {@link Cassandra} instance.
 */
public Cassandra build() {
    String name = (this.name != null) ? this.name : "cassandra-" + CASSANDRA_ID.getAndIncrement();
    Version version = (this.version != null) ? this.version : DEFAULT_VERSION;
    Path workingDirectory;
    try {
        IOSupplier<? extends Path> workingDirectorySupplier = this.workingDirectorySupplier;
        if (workingDirectorySupplier != null) {
            workingDirectory = workingDirectorySupplier.get();
            Objects.requireNonNull(workingDirectory, "Working Directory must not be null");
        } else {
            workingDirectory = Files.createTempDirectory("");
        }
    } catch (IOException ex) {
        throw new UncheckedIOException("Unable to get a working directory", ex);
    }
    WorkingDirectoryInitializer workingDirectoryInitializer = this.workingDirectoryInitializer;
    if (workingDirectoryInitializer == null) {
        workingDirectoryInitializer = new DefaultWorkingDirectoryInitializer(new WebCassandraDirectoryProvider(new JdkHttpClient(Duration.ofSeconds(30), Duration.ofSeconds(30))));
    }
    WorkingDirectoryDestroyer workingDirectoryDestroyer = this.workingDirectoryDestroyer;
    if (workingDirectoryDestroyer == null) {
        workingDirectoryDestroyer = WorkingDirectoryDestroyer.deleteOnly("bin", "pylib", "lib", "tools", "doc", "javadoc", "interface");
    }
    Duration startupTimeout = this.startupTimeout;
    if (startupTimeout == null) {
        startupTimeout = Duration.ofMinutes(2);
    }
    Logger logger = this.logger;
    if (logger == null) {
        logger = Logger.get(Cassandra.class);
    }
    Map<String, Object> environmentVariables = new LinkedHashMap<>(this.environmentVariables);
    environmentVariables.values().removeIf(Objects::isNull);
    Map<String, Object> systemProperties = new LinkedHashMap<>(this.systemProperties);
    systemProperties.values().removeIf(Objects::isNull);
    Set<String> jvmOptions = new LinkedHashSet<>(this.jvmOptions);
    jvmOptions.removeIf(Objects::isNull);
    Set<WorkingDirectoryCustomizer> workingDirectoryCustomizers = new LinkedHashSet<>(this.workingDirectoryCustomizers);
    workingDirectoryCustomizers.removeIf(Objects::isNull);
    Map<String, Object> configProperties = new LinkedHashMap<>(this.configProperties);
    CassandraDatabaseFactory databaseFactory = new DefaultCassandraDatabaseFactory(name, version, environmentVariables, configProperties, systemProperties, jvmOptions);
    return new DefaultCassandra(name, version, workingDirectory.normalize().toAbsolutePath(), this.registerShutdownHook, workingDirectoryInitializer, workingDirectoryDestroyer, startupTimeout, workingDirectoryCustomizers, databaseFactory, logger);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) UncheckedIOException(java.io.UncheckedIOException) Logger(com.github.nosan.embedded.cassandra.commons.logging.Logger) LinkedHashMap(java.util.LinkedHashMap) JdkHttpClient(com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient) Path(java.nio.file.Path) Duration(java.time.Duration) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Objects(java.util.Objects)

Aggregations

JdkHttpClient (com.github.nosan.embedded.cassandra.commons.web.JdkHttpClient)3 ClassPathResource (com.github.nosan.embedded.cassandra.commons.ClassPathResource)1 Logger (com.github.nosan.embedded.cassandra.commons.logging.Logger)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 UncheckedIOException (java.io.UncheckedIOException)1 InetSocketAddress (java.net.InetSocketAddress)1 Path (java.nio.file.Path)1 Duration (java.time.Duration)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Objects (java.util.Objects)1 BeforeAll (org.junit.jupiter.api.BeforeAll)1 RepeatedTest (org.junit.jupiter.api.RepeatedTest)1 Test (org.junit.jupiter.api.Test)1