2016-04-18 4 views
2

私はSpring XD用に作成するシンクの統合テストを作成しようとしています。ここに私のModuleConfiguration.javaSpring XD OAuthシンクユニットテスト「null」リソースの新しいアクセストークンを取得できません

@Configuration 
@EnableIntegration 
public class ModuleConfiguration { 

    private OAuth2AccessToken token; 

    @Bean 
    public MessageChannel input() { 
     return new DirectChannel(); 
    } 

    @Bean 
    protected OAuth2ProtectedResourceDetails resource() { 
     BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails(); 
     resource.setAccessTokenUri("https://xxxxxxxxxxx"); 
     resource.setClientId("id"); 
     resource.setClientSecret("secret"); 
     resource.setGrantType("authorization_code"); 
     return resource; 
    } 

    @Bean 
    public OAuth2RestTemplate oAuthTemplate() { 
     AccessTokenRequest atr = new DefaultAccessTokenRequest(); 
     OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(resource(), new DefaultOAuth2ClientContext(atr)); 
     return restTemplate; 
    } 

    @Bean 
    public HeaderMapper<HttpHeaders> headerMapper() { 
     HeaderMapper<HttpHeaders> mapper = new DefaultHttpHeaderMapper(); 
     HttpHeaders headers = new HttpHeaders(); 
     token = oAuthTemplate().getAccessToken(); 
     String authHeader = "Bearer " + token.getValue(); 

     headers.setContentType(MediaType.APPLICATION_JSON); 
     headers.set("Authorization", authHeader); 
     mapper.toHeaders(headers); 

     return mapper; 
    } 

    @ServiceActivator(inputChannel="inputChannel") 
    @Bean 
    public MessageHandler httpout() { 
     HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://yyyyyyyyy"); 
     handler.setCharset("UTF-8"); 
     handler.setHttpMethod(HttpMethod.POST); 
     handler.setHeaderMapper(headerMapper()); 
     return handler; 
    } 
} 

私はプロセッサのためのテストが書かれているテストを同じように実装し、それはそれは期待するものだから、このモジュールは、プロセッサであるスプリングXDを伝えています。わかりませんが、問題はないと思いますが、例外はOAuthと関係があります。ここで私は私のModuleConfigurationのこの行に例外を取得SinkTest.java

public class SinkTest { 

    private static SingleNodeApplication application; 

    private static int RECEIVE_TIMEOUT = 5000; 

    private static String moduleName = "oauth-sink"; 

    @BeforeClass 
    public static void setUp() { 
     application = new SingleNodeApplication().run(); 
     SingleNodeIntegrationTestSupport singleNodeIntegrationTestSupport = new SingleNodeIntegrationTestSupport(
       application); 
     singleNodeIntegrationTestSupport 
       .addModuleRegistry(new SingletonModuleRegistry(ModuleType.processor, moduleName)); 
    } 

    @Test 
    public void test() { 
     String streamName = "sinkTest"; 
     byte[] encoded = null; 
     String data = null; 
     try { 
      encoded = Files.readAllBytes(Paths.get("src/test/resources/dataunit.json")); 
      data = new String(encoded, "utf-8"); 
     } catch (IOException e) { 
      e.printStackTrace(); 
      fail("Exception thrown"); 
     } 

     String processingChainUnderTest = moduleName; 
     SingleNodeProcessingChain chain = chain(application, streamName, processingChainUnderTest); 
     chain.sendPayload(data); 
     @SuppressWarnings("unchecked") 
     List<Map<String, Object>> result = (List<Map<String, Object>>) chain.receivePayload(RECEIVE_TIMEOUT); 

     // Unbind the source and sink channels from the message bus 
     chain.destroy(); 
     // an exception is thrown by zookeeper when the spring xd container shuts down after the test passes 
    } 
} 

ある token = oAuthTemplate().getAccessToken();

Caused by: error="access_denied", error_description="Unable to obtain a new access token for resource 'null'. The provider manager is not configured to support it." 
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainNewAccessTokenInternal(AccessTokenProviderChain.java:146) 
    at org.springframework.security.oauth2.client.token.AccessTokenProviderChain.obtainAccessToken(AccessTokenProviderChain.java:118) 
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.acquireAccessToken(OAuth2RestTemplate.java:221) 
    at org.springframework.security.oauth2.client.OAuth2RestTemplate.getAccessToken(OAuth2RestTemplate.java:173) 

私はこの

@Bean 
    protected OAuth2ProtectedResourceDetails resource() { 
     BaseOAuth2ProtectedResourceDetails resource = new ClientCredentialsResourceDetails(); 
     resource.setAccessTokenUri("https://xxxxxxx"); 
     resource.setClientId("id"); 
     resource.setClientSecret("secret"); 
     resource.setGrantType("client_credentials"); 
     return resource; 
    } 

IのようModuleConfigurationにGrantTypeためclient_credentialsを使用している場合取得する

Caused by: org.springframework.integration.MessageDispatchingException: Dispatcher has no subscribers 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:107) 
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:97) 
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:77) 
    ... 29 more 

私はちょうどclientIdとシークレットを持っているときに使用する正しい助成金の種類は何ですか? ModuleConfigurationに何か不足していますか?

更新 は、私が正しい値に@ServiceActivatorでinputChannelを変更し、私は今BaseOAuth2ProtectedResourceDetailsと、以前と同じ例外を取得しています。これで、すべてのClientCredentialsResourceDetails

Caused by: java.lang.IllegalArgumentException: Map has no value for 'object-type' 
    at org.springframework.web.util.UriComponents$MapTemplateVariables.getValue(UriComponents.java:276) 
    at org.springframework.web.util.UriComponents.expandUriComponent(UriComponents.java:221) 
    at org.springframework.web.util.HierarchicalUriComponents$FullPathComponent.expand(HierarchicalUriComponents.java:650) 
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:319) 
    at org.springframework.web.util.HierarchicalUriComponents.expandInternal(HierarchicalUriComponents.java:46) 
    at org.springframework.web.util.UriComponents.expand(UriComponents.java:152) 
    at org.springframework.web.util.UriComponentsBuilder.buildAndExpand(UriComponentsBuilder.java:376) 
    at org.springframework.integration.http.outbound.HttpRequestExecutingMessageHandler.handleRequestMessage(HttpRequestExecutingMessageHandler.java:376) 
    ... 35 more 
+0

ちょうど私のPCに到達したときにその補正 – gary69

答えて

1

まずで異​​なる例外を取得するには、HttpExecutionMessageHandlerOAuthRestTemplateを注入してみてください。

そして、headerMapperはまったく同じように使用しないでください。 OAuth RestTemplateの拡張機能を持っているのは私の考えです。

もう1つの問題は、シンクのチャネルが間違っていて、そのservice-activatorです。つまり、input beanである必要がありますが、@ServiceActivatorから同じ名前を使用してください。それはあなたのDispatcher has no subscribersの原因の根源です。

@ServiceActivator(inputChannel="inputChannel") 
@Bean 
public MessageHandler httpout() { 
    HttpRequestExecutingMessageHandler handler = new HttpRequestExecutingMessageHandler("https://yyyyyyyyy", oAuthTemplate()); 
    handler.setCharset("UTF-8"); 
    handler.setHttpMethod(HttpMethod.POST); 
    return handler; 
} 

あなたはURLでいくつかの変数を持っている場合は、https://foo.bar/{foo}/{bar}のように、あなたはrequestMessageに対して、実行時にそれらを解決するためにsetUriVariableExpressions(Map<String, Expression> uriVariableExpressions)を提供する必要があります:http://docs.spring.io/spring-integration/reference/html/http.html#_mapping_uri_variables

+0

の書式修正します作るために更新。電話からは簡単ではない –

+0

'RestTemplate'の' HttpRequestExecutingMessageHandler'にセッターがないようです。最も近いものは 'setRequestFactory(ClientHttpRequestFactory)'のように見えます。この場合、このタイプのオブジェクトを作成する必要があります。おそらく、その中に 'RestTemplate'を挿入できます。私がハンドラーを使って 'HeaderMapper'を利用しないと、どうすればヘッダを挿入できますか? 'RestTemplate'はヘッダ用のセッタを持っていません。私の他の例外を修正した '@ ServiceActivator'修正をありがとう。 – gary69

+0

??? 'public HttpRequestExecutingMessageHandler(String uri、RestTemplate restTemplate){' –