Java Code Examples for org.eclipse.microprofile.rest.client.RestClientDefinitionException

The following examples show how to use org.eclipse.microprofile.rest.client.RestClientDefinitionException. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: microprofile1.4-samples   Source File: ClientServerAsyncTest.java    License: MIT License 6 votes vote down vote up
@Test
@RunAsClient
public void testClientServerAsync() throws IOException, IllegalStateException, RestClientDefinitionException, URISyntaxException, InterruptedException, ExecutionException {

    HelloService remoteApi = RestClientBuilder.newBuilder()
            .baseUrl(base)
            .build(HelloService.class);


    String response =
        remoteApi.helloAsync("Programmer (Async)")
                 .thenApply(String::toUpperCase)
                 .toCompletableFuture()
                 .get();

    System.out.println("-------------------------------------------------------------------------");
    System.out.println("Response: " + response);
    System.out.println("-------------------------------------------------------------------------");

    assertTrue(
        response.contains("HELLO PROGRAMMER (ASYNC)!")
    );
}
 
Example 2
Source Project: microprofile1.4-samples   Source File: ClientServerTest.java    License: MIT License 6 votes vote down vote up
@Test
@RunAsClient
public void testClientServer() throws IOException, IllegalStateException, RestClientDefinitionException, URISyntaxException {

    HelloService remoteApi = RestClientBuilder.newBuilder()
            .baseUrl(base)
            .build(HelloService.class);

    String response  =  remoteApi.hello("Programmer");



    System.out.println("-------------------------------------------------------------------------");
    System.out.println("Response: " + response);
    System.out.println("-------------------------------------------------------------------------");

    assertTrue(
        response.contains("Hello Programmer!")
    );
}
 
Example 3
Source Project: thorntail   Source File: RestClientProxyTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testClose() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    counter.reset(1);

    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    RestClientProxy clientProxy = (RestClientProxy) helloClient;
    assertEquals("C:OK1:C", helloClient.hello());
    clientProxy.close();
    assertTrue(CharlieService.DESTROYED.get());
    // Further calls are no-op
    clientProxy.close();
    // Invoking any method on the client proxy should result in ISE
    try {
        helloClient.hello();
        fail();
    } catch (IllegalStateException expected) {
    }
}
 
Example 4
Source Project: thorntail   Source File: FaultToleranceTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testTimeout() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    counter.reset(1);
    timer.reset(400);
    latch.reset(1);

    try {
        helloClient.helloTimeout();
        fail();
    } catch (TimeoutException expected) {
    }

    latch.await();
}
 
Example 5
Source Project: cxf   Source File: Validator.java    License: Apache License 2.0 6 votes vote down vote up
private static void checkMethodsForMultipleHTTPMethodAnnotations(Method[] clientMethods)
    throws RestClientDefinitionException {

    Map<String, Class<? extends Annotation>> httpMethods = new HashMap<>();
    for (Method method : clientMethods) {

        for (Annotation anno : method.getAnnotations()) {
            Class<? extends Annotation> annoClass = anno.annotationType();
            HttpMethod verb = annoClass.getAnnotation(HttpMethod.class);
            if (verb != null) {
                httpMethods.put(verb.value(), annoClass);
            }
        }
        if (httpMethods.size() > 1) {
            throwException("VALIDATION_METHOD_WITH_MULTIPLE_VERBS", method, httpMethods.values());
        }
        httpMethods.clear();
    }

}
 
Example 6
Source Project: thorntail   Source File: InterceptorTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInterception() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    counter.reset(1);

    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    // Interceptor ordering should be: Charlie, Alpha, Bravo
    assertEquals("C:A:B:OK1:B:A:C", helloClient.hello());
}
 
Example 7
Source Project: thorntail   Source File: MetricsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testTimed() throws IllegalStateException, RestClientDefinitionException {
    counter.reset(1);
    timer.reset(0);

    HelloMetricsClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloMetricsClient.class);
    assertEquals("OK1", helloClient.helloTimed());

    org.eclipse.microprofile.metrics.Timer metricsTimer = registry.getTimers().get(new MetricID(HelloMetricsClient.TIMED_NAME));
    assertNotNull(metricsTimer);
    assertEquals(1, metricsTimer.getCount());
}
 
Example 8
Source Project: thorntail   Source File: MetricsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCounted() throws IllegalStateException, RestClientDefinitionException {
    counter.reset(1);
    timer.reset(0);

    HelloMetricsClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloMetricsClient.class);
    assertEquals("OK1", helloClient.helloCounted());
    assertEquals("OK2", helloClient.helloCounted());
    assertEquals("OK3", helloClient.helloCounted());

    org.eclipse.microprofile.metrics.Counter metricsCounter = registry.getCounters().get(new MetricID(HelloMetricsClient.COUNTED_NAME));
    assertNotNull(metricsCounter);
    assertEquals(3, metricsCounter.getCount());
}
 
Example 9
Source Project: thorntail   Source File: MetricsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testClassLevelCounted() throws IllegalStateException, RestClientDefinitionException {
    counter.reset(1);
    timer.reset(0);

    HelloMetricsClassLevelClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloMetricsClassLevelClient.class);
    assertEquals("OK1", helloClient.hello());
    assertEquals("OK2", helloClient.hello());

    org.eclipse.microprofile.metrics.Counter metricsCounter = registry.getCounters().get(new MetricID(HelloMetricsClassLevelClient.class.getName() + "." + "hello"));
    assertNotNull(metricsCounter);
    assertEquals(2, metricsCounter.getCount());
}
 
Example 10
Source Project: thorntail   Source File: RestClientProxyTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetClient() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    counter.reset(1);

    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    Client client = ((RestClientProxy) helloClient).getClient();
    assertNotNull(client);
    assertEquals("C:OK1:C", helloClient.hello());
    client.close();
}
 
Example 11
Source Project: thorntail   Source File: HeaderPassingTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPassHeadersFromClientAutomatically() throws IllegalStateException, RestClientDefinitionException {
    String baseUrl = url.toString() + "/v1";
    String headerValue = "some header value";

    HeaderConsumingClient client =
            RestClientBuilder.newBuilder()
                    .baseUri(URI.create(baseUrl))
                    .build(HeaderConsumingClient.class);

    Map<String, String> headers = client.postWithHeader(headerValue, baseUrl);
    assertNotNull(headers);
    assertEquals(headerValue, headers.get(HEADER_NAME));
    assertEquals(headerValue, headers.get("ClientHeaderParam"));
}
 
Example 12
Source Project: thorntail   Source File: FaultToleranceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRetry() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    counter.reset(3);
    timer.reset(0);

    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    assertEquals("OK3", helloClient.helloRetry());
}
 
Example 13
Source Project: thorntail   Source File: FaultToleranceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testFallback() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException {
    HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).build(HelloClient.class);

    timer.reset(0);
    counter.reset(3);

    assertEquals("fallback", helloClient.helloFallback());
    assertEquals("defaultFallback", helloClient.helloFallbackDefaultMethod());
}
 
Example 14
Source Project: thorntail   Source File: FaultToleranceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBulkhead() throws InterruptedException, IllegalStateException, RestClientDefinitionException, MalformedURLException, ExecutionException {

    int pool = BULKHEAD;
    // Start latch
    latch.reset(pool);
    // End latch
    latch.add("end", 1);

    ExecutorService executor = Executors.newFixedThreadPool(pool);

    try {
        HelloClient helloClient = RestClientBuilder.newBuilder().baseUrl(url).property("resteasy.connectionPoolSize", pool * 2).build(HelloClient.class);

        List<Future<String>>  results = new ArrayList<>();
        for (int i = 0; i < pool; i++) {
            results.add(executor.submit(() -> helloClient.helloBulkhead(true)));
        }
        // Wait until all requests are being processed
        if(!latch.await()) {
            fail();
        }

        // Next invocation should return fallback due to BulkheadException
        assertEquals("bulkheadFallback", helloClient.helloBulkhead(false));

        latch.countDown("end");
        for (Future<String> future : results) {
            assertEquals("OK", future.get());
        }

    } finally {
        executor.shutdown();
    }
}
 
Example 15
Source Project: cxf   Source File: Validator.java    License: Apache License 2.0 5 votes vote down vote up
public static void checkValid(Class<?> userType) throws RestClientDefinitionException {
    if (!userType.isInterface()) {
        throwException("VALIDATION_NOT_AN_INTERFACE", userType);
    }
    Method[] methods = userType.getMethods();
    checkMethodsForMultipleHTTPMethodAnnotations(methods);
    checkMethodsForInvalidURITemplates(userType, methods);
    checkForInvalidClientHeaderParams(userType, methods);
}
 
Example 16
Source Project: cxf   Source File: ValidatorTest.java    License: Apache License 2.0 5 votes vote down vote up
private void test(Class<?> clientInterface, String...expectedMessageTexts) {
    try {
        newBuilder().build(clientInterface);
        fail("Expected RestClientDefinitionException");
    } catch (RestClientDefinitionException ex) {
        String msgText = ex.getMessage();
        assertNotNull("No message text in RestClientDefinitionException", msgText);
        for (String expectedMessageText : expectedMessageTexts) {
            assertTrue("Exception text does not contain expected message: " + expectedMessageText,
                       msgText.contains(expectedMessageText));
        }
    }
}
 
Example 17
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenInterfaceHasMethodWithMultipleHTTPMethodAnnotations() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MultipleHTTPMethodAnnotations.class);
}
 
Example 18
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenInterfaceHasMethodWithMissingPathParamAnnotation_templateDeclaredAtTypeLevel() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MissingPathParam.class);
}
 
Example 19
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenInterfaceHasMethodWithMissingPathParamAnnotation_templateDeclaredAtMethodLevel() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MissingPathParamSub.class);
}
 
Example 20
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenInterfaceHasMethodWithPathParamAnnotationButNoURITemplate() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MissingUriTemplate.class);
}
 
Example 21
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenInterfaceHasMethodWithMismatchedPathParameter() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(TemplateMismatch.class);
}
 
Example 22
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenClientHeaderParamComputeValueSpecifiesMissingMethod() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MissingHeaderComputeMethod.class);
}
 
Example 23
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenClientHeaderParamComputeValueSpecifiesMethodWithInvalidSignature() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(InvalidComputeMethodSignature.class);
}
 
Example 24
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenMultipleClientHeaderParamsSpecifySameHeaderOnMethod() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MultipleHeadersOnSameMethod.class);
}
 
Example 25
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenMultipleClientHeaderParamsSpecifySameHeaderOnInterface() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MultipleHeadersOnSameInterface.class);
}
 
Example 26
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenMultipleHeaderValuesSpecifiedIncludeComputeMethodOnInterface() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MultiValueClientHeaderWithComputeMethodOnInterface.class);
}
 
Example 27
@Test(expectedExceptions={RestClientDefinitionException.class})
public void testExceptionThrownWhenMultipleHeaderValuesSpecifiedIncludeComputeMethodOnMethod() throws Exception {
    RestClientBuilder.newBuilder().baseUri(new URI("http://localhost:8080/")).build(MultiValueClientHeaderWithComputeMethodOnMethod.class);
}
 
Example 28
Source Project: cxf   Source File: Validator.java    License: Apache License 2.0 4 votes vote down vote up
private static void checkMethodsForInvalidURITemplates(Class<?> userType, Method[] methods)
    throws RestClientDefinitionException {

    Path classPathAnno = userType.getAnnotation(Path.class);

    URITemplate classTemplate = null;
    if (classPathAnno != null) {
        classTemplate = new URITemplate(classPathAnno.value());
    }
    URITemplate template;
    for (Method method : methods) {

        Path methodPathAnno = method.getAnnotation(Path.class);
        if (methodPathAnno != null) {
            template = classPathAnno == null ? new URITemplate(methodPathAnno.value())
                : new URITemplate(classPathAnno.value() + "/" + methodPathAnno.value());
        } else {
            template = classTemplate;
        }
        if (template == null) {
            continue;
        }

        List<String> foundParams = new ArrayList<>();
        for (Parameter p : method.getParameters()) {
            PathParam pathParam = p.getAnnotation(PathParam.class);
            if (pathParam != null) {
                foundParams.add(pathParam.value());
            }
        }

        Set<String> allVariables = new HashSet<>(template.getVariables());
        if (!allVariables.isEmpty()) {
            for (String variable : template.getVariables()) {
                if (!foundParams.contains(variable)) {
                    throwException("VALIDATION_UNRESOLVED_PATH_PARAMS", userType, method);
                }
            }
        } else if (!foundParams.isEmpty()) {
            throwException("VALIDATION_EXTRA_PATH_PARAMS", userType, method);
        }
    }
}
 
Example 29
Source Project: cxf   Source File: Validator.java    License: Apache License 2.0 4 votes vote down vote up
private static void throwException(String msgKey, Object...msgParams) throws RestClientDefinitionException {
    Message msg = new Message(msgKey, BUNDLE, msgParams);
    throw new RestClientDefinitionException(msg.toString());
}