Search in sources :

Example 1 with RestOperations

use of org.springframework.web.client.RestOperations in project spring-framework by spring-projects.

the class RestTemplateXhrTransportTests method connectFailure.

@Test
public void connectFailure() throws Exception {
    final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR);
    RestOperations restTemplate = mock(RestOperations.class);
    given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected);
    final CountDownLatch latch = new CountDownLatch(1);
    connect(restTemplate).addCallback(new ListenableFutureCallback<WebSocketSession>() {

        @Override
        public void onSuccess(WebSocketSession result) {
        }

        @Override
        public void onFailure(Throwable ex) {
            if (ex == expected) {
                latch.countDown();
            }
        }
    });
    verifyNoMoreInteractions(this.webSocketHandler);
}
Also used : RestOperations(org.springframework.web.client.RestOperations) CountDownLatch(java.util.concurrent.CountDownLatch) HttpServerErrorException(org.springframework.web.client.HttpServerErrorException) WebSocketSession(org.springframework.web.socket.WebSocketSession) Test(org.junit.Test)

Example 2 with RestOperations

use of org.springframework.web.client.RestOperations in project spring-security-oauth by spring-projects.

the class OAuth2ContextSetup method initializeIfNecessary.

private void initializeIfNecessary(FrameworkMethod method, final Object target) {
    final TestClass testClass = new TestClass(target.getClass());
    OAuth2ContextConfiguration contextConfiguration = findOAuthContextConfiguration(method, testClass);
    if (contextConfiguration == null) {
        // Nothing to do
        return;
    }
    this.initializeAccessToken = contextConfiguration.initialize();
    this.resource = creatResource(target, contextConfiguration);
    final List<FrameworkMethod> befores = testClass.getAnnotatedMethods(BeforeOAuth2Context.class);
    if (!befores.isEmpty()) {
        logger.debug("Running @BeforeOAuth2Context methods");
        for (FrameworkMethod before : befores) {
            RestOperations savedServerClient = clientHolder.getRestTemplate();
            OAuth2ContextConfiguration beforeConfiguration = findOAuthContextConfiguration(before, testClass);
            if (beforeConfiguration != null) {
                OAuth2ProtectedResourceDetails resource = creatResource(target, beforeConfiguration);
                AccessTokenRequest beforeRequest = new DefaultAccessTokenRequest();
                beforeRequest.setAll(parameters);
                OAuth2RestTemplate client = createRestTemplate(resource, beforeRequest);
                clientHolder.setRestTemplate(client);
            }
            AccessTokenRequest request = new DefaultAccessTokenRequest();
            request.setAll(parameters);
            this.client = createRestTemplate(this.resource, request);
            List<FrameworkMethod> list = Arrays.asList(before);
            try {
                new RunBefores(new Statement() {

                    public void evaluate() {
                    }
                }, list, target).evaluate();
            } catch (AssumptionViolatedException e) {
                throw e;
            } catch (RuntimeException e) {
                throw e;
            } catch (AssertionError e) {
                throw e;
            } catch (Throwable e) {
                logger.debug("Exception in befores", e);
                Assert.assertThat(e, CoreMatchers.not(CoreMatchers.anything()));
            } finally {
                clientHolder.setRestTemplate(savedServerClient);
            }
        }
    }
}
Also used : AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) Statement(org.junit.runners.model.Statement) OAuth2ProtectedResourceDetails(org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails) TestClass(org.junit.runners.model.TestClass) OAuth2RestTemplate(org.springframework.security.oauth2.client.OAuth2RestTemplate) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest) AccessTokenRequest(org.springframework.security.oauth2.client.token.AccessTokenRequest) RestOperations(org.springframework.web.client.RestOperations) RunBefores(org.junit.internal.runners.statements.RunBefores) FrameworkMethod(org.junit.runners.model.FrameworkMethod) DefaultAccessTokenRequest(org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)

Example 3 with RestOperations

use of org.springframework.web.client.RestOperations in project spring-security-oauth by spring-projects.

the class ServerRunning method apply.

public Statement apply(final Statement base, FrameworkMethod method, Object target) {
    // Check at the beginning, so this can be used as a static field
    if (assumeOnline) {
        Assume.assumeTrue(serverOnline.get(port));
    } else {
        Assume.assumeTrue(serverOffline.get(port));
    }
    RestTemplate client = new RestTemplate();
    boolean followRedirects = HttpURLConnection.getFollowRedirects();
    HttpURLConnection.setFollowRedirects(false);
    boolean online = false;
    try {
        client.getForEntity(new UriTemplate(getUrl("/sparklr2/login.jsp")).toString(), String.class);
        online = true;
        logger.info("Basic connectivity test passed");
    } catch (RestClientException e) {
        logger.warn(String.format("Not executing tests because basic connectivity test failed for hostName=%s, port=%d", hostName, port), e);
        if (assumeOnline) {
            Assume.assumeNoException(e);
        }
    } finally {
        HttpURLConnection.setFollowRedirects(followRedirects);
        if (online) {
            serverOffline.put(port, false);
            if (!assumeOnline) {
                Assume.assumeTrue(serverOffline.get(port));
            }
        } else {
            serverOnline.put(port, false);
        }
    }
    final RestOperations savedClient = getRestTemplate();
    postForStatus(savedClient, "/sparklr2/oauth/uncache_approvals", new LinkedMultiValueMap<String, String>());
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            try {
                base.evaluate();
            } finally {
                postForStatus(savedClient, "/sparklr2/oauth/cache_approvals", new LinkedMultiValueMap<String, String>());
            }
        }
    };
}
Also used : Statement(org.junit.runners.model.Statement) RestTemplate(org.springframework.web.client.RestTemplate) RestClientException(org.springframework.web.client.RestClientException) UriTemplate(org.springframework.web.util.UriTemplate) RestOperations(org.springframework.web.client.RestOperations)

Aggregations

RestOperations (org.springframework.web.client.RestOperations)3 Statement (org.junit.runners.model.Statement)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)1 RunBefores (org.junit.internal.runners.statements.RunBefores)1 FrameworkMethod (org.junit.runners.model.FrameworkMethod)1 TestClass (org.junit.runners.model.TestClass)1 OAuth2RestTemplate (org.springframework.security.oauth2.client.OAuth2RestTemplate)1 OAuth2ProtectedResourceDetails (org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails)1 AccessTokenRequest (org.springframework.security.oauth2.client.token.AccessTokenRequest)1 DefaultAccessTokenRequest (org.springframework.security.oauth2.client.token.DefaultAccessTokenRequest)1 HttpServerErrorException (org.springframework.web.client.HttpServerErrorException)1 RestClientException (org.springframework.web.client.RestClientException)1 RestTemplate (org.springframework.web.client.RestTemplate)1 WebSocketSession (org.springframework.web.socket.WebSocketSession)1 UriTemplate (org.springframework.web.util.UriTemplate)1