Search in sources :

Example 1 with Application

use of jakarta.ws.rs.core.Application in project jaxrs-api by eclipse-ee4j.

the class SeBootstrapIT method shouldBootInstanceUsingExternalConfiguration.

/**
 * Verifies that an instance will boot using external configuration.
 *
 * @throws ExecutionException   if the instance didn't boot correctly
 * @throws InterruptedException if the test took much longer than usually
 *                              expected
 * @throws IOException          if no IP port was free
 */
@Test
public final void shouldBootInstanceUsingExternalConfiguration() throws InterruptedException, ExecutionException, IOException {
    // given
    final int someFreeIpPort = someFreeIpPort();
    final int expectedResponse = mockInt();
    final Application application = new StaticApplication(expectedResponse);
    final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
    final SeBootstrap.Configuration requestedConfiguration = bootstrapConfigurationBuilder.from((property, type) -> {
        switch(property) {
            case SeBootstrap.Configuration.PROTOCOL:
                return Optional.of("HTTP");
            case SeBootstrap.Configuration.HOST:
                return Optional.of("localhost");
            case SeBootstrap.Configuration.PORT:
                return Optional.of(someFreeIpPort);
            case SeBootstrap.Configuration.ROOT_PATH:
                return Optional.of("/root/path");
            default:
                return Optional.empty();
        }
    }).build();
    // when
    final CompletionStage<SeBootstrap.Instance> completionStage = SeBootstrap.start(application, requestedConfiguration);
    final SeBootstrap.Instance instance = completionStage.toCompletableFuture().get();
    final SeBootstrap.Configuration actualConfiguration = instance.configuration();
    final int actualResponse = client.target(UriBuilder.newInstance().scheme(actualConfiguration.protocol()).host(actualConfiguration.host()).port(actualConfiguration.port()).path(actualConfiguration.rootPath()).path("application/resource")).request().get(int.class);
    // then
    assertThat(actualResponse, is(expectedResponse));
    assertThat(actualConfiguration.protocol(), is(requestedConfiguration.protocol()));
    assertThat(actualConfiguration.host(), is(requestedConfiguration.host()));
    assertThat(actualConfiguration.port(), is(requestedConfiguration.port()));
    assertThat(actualConfiguration.rootPath(), is(requestedConfiguration.rootPath()));
    instance.stop().toCompletableFuture().get();
}
Also used : Client(jakarta.ws.rs.client.Client) SeBootstrap(jakarta.ws.rs.SeBootstrap) Set(java.util.Set) JaxrsUtil.unprivilegedPort(ee.jakarta.tck.ws.rs.common.util.JaxrsUtil.unprivilegedPort) IOException(java.io.IOException) GET(jakarta.ws.rs.GET) Path(jakarta.ws.rs.Path) ExecutionException(java.util.concurrent.ExecutionException) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) ApplicationPath(jakarta.ws.rs.ApplicationPath) CompletionStage(java.util.concurrent.CompletionStage) BeforeAll(org.junit.jupiter.api.BeforeAll) UriBuilder(jakarta.ws.rs.core.UriBuilder) HOURS(java.util.concurrent.TimeUnit.HOURS) Optional(java.util.Optional) Application(jakarta.ws.rs.core.Application) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) SeBootstrap(jakarta.ws.rs.SeBootstrap) Application(jakarta.ws.rs.core.Application) Test(org.junit.jupiter.api.Test)

Example 2 with Application

use of jakarta.ws.rs.core.Application in project jaxrs-api by eclipse-ee4j.

the class SeBootstrapIT method shouldBootInstanceUsingImplementationsDefaultIpPort.

/**
 * Verifies that an instance will boot using the implementation's default IP
 * port.
 *
 * @throws ExecutionException   if the instance didn't boot correctly
 * @throws InterruptedException if the test took much longer than usually
 *                              expected
 */
@Test
public final void shouldBootInstanceUsingImplementationsDefaultIpPort() throws InterruptedException, ExecutionException {
    // given
    final int expectedResponse = mockInt();
    final Application application = new StaticApplication(expectedResponse);
    final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
    final SeBootstrap.Configuration requestedConfiguration = bootstrapConfigurationBuilder.protocol("HTTP").host("localhost").port(SeBootstrap.Configuration.DEFAULT_PORT).rootPath("/root/path").build();
    // when
    final CompletionStage<SeBootstrap.Instance> completionStage = SeBootstrap.start(application, requestedConfiguration);
    final SeBootstrap.Instance instance = completionStage.toCompletableFuture().get();
    final SeBootstrap.Configuration actualConfiguration = instance.configuration();
    final int actualResponse = client.target(UriBuilder.newInstance().scheme(actualConfiguration.protocol()).host(actualConfiguration.host()).port(actualConfiguration.port()).path(actualConfiguration.rootPath()).path("application/resource")).request().get(int.class);
    // then
    assertThat(actualResponse, is(expectedResponse));
    assertThat(actualConfiguration.protocol(), is(requestedConfiguration.protocol()));
    assertThat(actualConfiguration.host(), is(requestedConfiguration.host()));
    assertThat(actualConfiguration.port(), is(greaterThan(0)));
    assertThat(actualConfiguration.rootPath(), is(requestedConfiguration.rootPath()));
    instance.stop().toCompletableFuture().get();
}
Also used : SeBootstrap(jakarta.ws.rs.SeBootstrap) Application(jakarta.ws.rs.core.Application) Test(org.junit.jupiter.api.Test)

Example 3 with Application

use of jakarta.ws.rs.core.Application in project jaxrs-api by eclipse-ee4j.

the class SeBootstrapIT method shouldBootInstanceUsingDefaults.

/**
 * Verifies that an instance will boot using default configuration.
 *
 * @throws ExecutionException   if the instance didn't boot correctly
 * @throws InterruptedException if the test took much longer than usually
 *                              expected
 */
@Test
public final void shouldBootInstanceUsingDefaults() throws InterruptedException, ExecutionException {
    // given
    final int expectedResponse = mockInt();
    final Application application = new StaticApplication(expectedResponse);
    final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
    final SeBootstrap.Configuration requestedConfiguration = bootstrapConfigurationBuilder.build();
    // when
    final CompletionStage<SeBootstrap.Instance> completionStage = SeBootstrap.start(application, requestedConfiguration);
    final SeBootstrap.Instance instance = completionStage.toCompletableFuture().get();
    final SeBootstrap.Configuration actualConfiguration = instance.configuration();
    final int actualResponse = client.target(UriBuilder.newInstance().scheme(actualConfiguration.protocol()).host(actualConfiguration.host()).port(actualConfiguration.port()).path(actualConfiguration.rootPath()).path("application/resource")).request().get(int.class);
    // then
    assertThat(actualResponse, is(expectedResponse));
    assertThat(actualConfiguration.protocol(), is("HTTP"));
    assertThat(actualConfiguration.host(), is("localhost"));
    assertThat(actualConfiguration.port(), is(greaterThan(0)));
    assertThat(actualConfiguration.rootPath(), is("/"));
    instance.stop().toCompletableFuture().get();
}
Also used : SeBootstrap(jakarta.ws.rs.SeBootstrap) Application(jakarta.ws.rs.core.Application) Test(org.junit.jupiter.api.Test)

Example 4 with Application

use of jakarta.ws.rs.core.Application in project jaxrs-api by eclipse-ee4j.

the class SeBootstrapIT method shouldBootInstanceDespiteUnknownConfigurationParameters.

/**
 * Verifies that an instance will ignore unknown configuration parameters.
 *
 * @throws ExecutionException   if the instance didn't boot correctly
 * @throws InterruptedException if the test took much longer than usually
 *                              expected
 * @throws IOException          if no IP port was free
 */
@Test
public final void shouldBootInstanceDespiteUnknownConfigurationParameters() throws InterruptedException, ExecutionException, IOException {
    // given
    final int expectedResponse = mockInt();
    final Application application = new StaticApplication(expectedResponse);
    final SeBootstrap.Configuration.Builder bootstrapConfigurationBuilder = SeBootstrap.Configuration.builder();
    final SeBootstrap.Configuration requestedConfiguration = bootstrapConfigurationBuilder.protocol("HTTP").host("localhost").port(someFreeIpPort()).rootPath("/root/path").from((property, type) -> {
        switch(property) {
            case "ee.jakarta.tck.ws.rs.sebootstrap.SeBootstrapIT$Unknown_1":
                return Optional.of("Silently ignored value A");
            default:
                return Optional.empty();
        }
    }).property("ee.jakarta.tck.ws.rs.sebootstrap.SeBootstrapIT$Unknown_2", "Silently ignored value B").from(new Object()).build();
    // when
    final CompletionStage<SeBootstrap.Instance> completionStage = SeBootstrap.start(application, requestedConfiguration);
    final SeBootstrap.Instance instance = completionStage.toCompletableFuture().get();
    final SeBootstrap.Configuration actualConfiguration = instance.configuration();
    final int actualResponse = client.target(UriBuilder.newInstance().scheme(actualConfiguration.protocol()).host(actualConfiguration.host()).port(actualConfiguration.port()).path(actualConfiguration.rootPath()).path("application/resource")).request().get(int.class);
    // then
    assertThat(actualResponse, is(expectedResponse));
    assertThat(actualConfiguration.protocol(), is(requestedConfiguration.protocol()));
    assertThat(actualConfiguration.host(), is(requestedConfiguration.host()));
    assertThat(actualConfiguration.port(), is(requestedConfiguration.port()));
    assertThat(actualConfiguration.rootPath(), is(requestedConfiguration.rootPath()));
    instance.stop().toCompletableFuture().get();
}
Also used : Client(jakarta.ws.rs.client.Client) SeBootstrap(jakarta.ws.rs.SeBootstrap) Set(java.util.Set) JaxrsUtil.unprivilegedPort(ee.jakarta.tck.ws.rs.common.util.JaxrsUtil.unprivilegedPort) IOException(java.io.IOException) GET(jakarta.ws.rs.GET) Path(jakarta.ws.rs.Path) ExecutionException(java.util.concurrent.ExecutionException) AfterAll(org.junit.jupiter.api.AfterAll) Test(org.junit.jupiter.api.Test) ApplicationPath(jakarta.ws.rs.ApplicationPath) CompletionStage(java.util.concurrent.CompletionStage) BeforeAll(org.junit.jupiter.api.BeforeAll) UriBuilder(jakarta.ws.rs.core.UriBuilder) HOURS(java.util.concurrent.TimeUnit.HOURS) Optional(java.util.Optional) Application(jakarta.ws.rs.core.Application) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collections(java.util.Collections) Timeout(org.junit.jupiter.api.Timeout) ClientBuilder(jakarta.ws.rs.client.ClientBuilder) SeBootstrap(jakarta.ws.rs.SeBootstrap) Application(jakarta.ws.rs.core.Application) Test(org.junit.jupiter.api.Test)

Example 5 with Application

use of jakarta.ws.rs.core.Application in project jaxrs-api by eclipse-ee4j.

the class JAXRSClientIT method createEndpointTest.

/*
   * @testName: createEndpointTest
   * 
   * @assertion_ids: JAXRS:JAVADOC:284; JAXRS:JAVADOC:285; JAXRS:JAVADOC:286;
   * 
   * @test_Strategy: Create a configured instance of the supplied endpoint type.
   * How the returned endpoint instance is published is dependent on the type of
   * endpoint.
   * 
   * IllegalArgumentException - if application is null or the requested endpoint
   * type is not supported. UnsupportedOperationException - if the
   * implementation supports no endpoint types.
   * 
   * Note that these assertions are almost untestable, as it can either create
   * an instance or throw one exception or the other.
   */
@Test
public void createEndpointTest() throws Fault {
    Application application = new Application() {

        public java.util.Set<java.lang.Class<?>> getClasses() {
            java.util.Set<java.lang.Class<?>> set = new HashSet<Class<?>>();
            set.add(Resource.class);
            return set;
        }
    };
    RuntimeDelegate delegate = RuntimeDelegate.getInstance();
    HttpHandler handler = null;
    try {
        handler = delegate.createEndpoint(application, com.sun.net.httpserver.HttpHandler.class);
    } catch (IllegalArgumentException e) {
        logMsg("IllegalArgumentException has been thrown as expected", e);
        return;
    } catch (UnsupportedOperationException e) {
        logMsg("UnsupportedOperationException has been thrown as expected", e);
        return;
    }
    assertNotNull(handler, "HttpHandler end point should be created, or an exception thrown otherwise");
    logMsg("HttpHandler endpoint has been sucessfully created");
}
Also used : HttpHandler(com.sun.net.httpserver.HttpHandler) Application(jakarta.ws.rs.core.Application) RuntimeDelegate(jakarta.ws.rs.ext.RuntimeDelegate) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

Application (jakarta.ws.rs.core.Application)18 SeBootstrap (jakarta.ws.rs.SeBootstrap)13 Test (org.junit.jupiter.api.Test)12 URI (java.net.URI)6 Configuration (jakarta.ws.rs.SeBootstrap.Configuration)3 JaxrsUtil.unprivilegedPort (ee.jakarta.tck.ws.rs.common.util.JaxrsUtil.unprivilegedPort)2 ApplicationPath (jakarta.ws.rs.ApplicationPath)2 GET (jakarta.ws.rs.GET)2 Path (jakarta.ws.rs.Path)2 Client (jakarta.ws.rs.client.Client)2 ClientBuilder (jakarta.ws.rs.client.ClientBuilder)2 UriBuilder (jakarta.ws.rs.core.UriBuilder)2 IOException (java.io.IOException)2 Collections (java.util.Collections)2 Optional (java.util.Optional)2 Set (java.util.Set)2 CompletionStage (java.util.concurrent.CompletionStage)2 ExecutionException (java.util.concurrent.ExecutionException)2 HOURS (java.util.concurrent.TimeUnit.HOURS)2 Config (org.eclipse.microprofile.config.Config)2