View Javadoc

1   /*
2    * $Id: EndpointTestCase.java 19191 2010-08-25 21:05:23Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.endpoint;
12  
13  import static org.mockito.Matchers.any;
14  import static org.mockito.Mockito.doAnswer;
15  import static org.mockito.Mockito.mock;
16  
17  import org.mule.MessageExchangePattern;
18  import org.mule.api.MuleContext;
19  import org.mule.api.MuleException;
20  import org.mule.api.construct.FlowConstruct;
21  import org.mule.api.endpoint.EndpointMessageProcessorChainFactory;
22  import org.mule.api.endpoint.EndpointURI;
23  import org.mule.api.endpoint.ImmutableEndpoint;
24  import org.mule.api.processor.MessageProcessor;
25  import org.mule.api.retry.RetryPolicyTemplate;
26  import org.mule.api.security.EndpointSecurityFilter;
27  import org.mule.api.transaction.TransactionConfig;
28  import org.mule.api.transformer.Transformer;
29  import org.mule.api.transport.Connector;
30  import org.mule.tck.AbstractMuleTestCase;
31  
32  import java.util.ArrayList;
33  import java.util.HashMap;
34  import java.util.List;
35  import java.util.Map;
36  
37  import org.mockito.invocation.InvocationOnMock;
38  import org.mockito.stubbing.Answer;
39  
40  public class EndpointTestCase extends AbstractMuleTestCase
41  {
42      /**
43       * Tests that endpoint configuration is done before setting the endpoint in the
44       * passed transformers to avoid a race condition when the transformer asks for
45       * endpoint's information that has not ben set yet. Related to these issues:
46       * EE-1937, MULE-3983
47       */
48      @SuppressWarnings("serial")
49      public void testTransformersAreSetupAfterCompleteEndpointConfig()
50      {
51          // Defines all the values required in order to create a full configured
52          // endpoint
53          final Connector mockConnector = mock(Connector.class);
54          final EndpointURI uri = mock(EndpointURI.class);
55          final List<Transformer> inputTransformers = new ArrayList<Transformer>();
56          final List<Transformer> outputTransformers = new ArrayList<Transformer>();
57          final String name = "testEndpoint";
58          
59          final Map<String, String> properties = new HashMap<String, String>();
60          final String property1 = "property1";
61          final String value1 = "value1";
62          properties.put(property1, value1);
63          
64          final TransactionConfig mockTransactionConfig = mock(TransactionConfig.class);
65          final boolean deleteUnacceptedMessages = true;
66          final EndpointSecurityFilter mockEndpointSecurityFilter = mock(EndpointSecurityFilter.class);
67          final MessageExchangePattern messageExchangePattern = MessageExchangePattern.REQUEST_RESPONSE;
68          final int responseTimeout = 5;
69          final String initialState = "state1";
70          final String endpointEncoding = "enconding1";
71          final String endpointBuilderName = "builderName1";
72          final MuleContext muleContext = mock(MuleContext.class);
73          final RetryPolicyTemplate retryPolicyTemplate = mock(RetryPolicyTemplate.class);
74          final EndpointMessageProcessorChainFactory messageProcessorsFactory = mock(EndpointMessageProcessorChainFactory.class);
75          final List<MessageProcessor> messageProcessors = new ArrayList<MessageProcessor>();
76          final List<MessageProcessor> responseMessageProcessors = new ArrayList<MessageProcessor>();
77          final String mimeType = "text/plain";
78          final boolean disableTransportTransformer = true;
79  
80          // Creates a mock Transformer that will check that the endpoint is completely
81          // configured when setEndpoint method is called.
82          Transformer mockTransformer = mock(Transformer.class);
83          doAnswer(new Answer<Object>()
84          {
85              public Object answer(InvocationOnMock invocation) throws Throwable
86              {
87                  AbstractEndpoint endpoint = (AbstractEndpoint) invocation.getArguments()[0];
88                  assertEquals(mockConnector, endpoint.getConnector());
89                  assertEquals(uri, endpoint.getEndpointURI());
90                  assertEquals(name, endpoint.getName());
91                  assertEquals(value1, endpoint.getProperties().get(property1));
92                  assertEquals(mockTransactionConfig, endpoint.getTransactionConfig());
93                  assertEquals(deleteUnacceptedMessages, endpoint.isDeleteUnacceptedMessages());
94                  assertEquals(mockEndpointSecurityFilter, endpoint.getSecurityFilter());
95                  assertEquals(messageExchangePattern, endpoint.getExchangePattern());
96                  assertEquals(responseTimeout, endpoint.getResponseTimeout());
97                  assertEquals(initialState, endpoint.getInitialState());
98                  assertEquals(endpointEncoding, endpoint.getEncoding());
99                  assertEquals(endpointBuilderName, endpoint.getEndpointBuilderName());
100                 assertEquals(muleContext, endpoint.getMuleContext());
101                 assertEquals(retryPolicyTemplate, endpoint.getRetryPolicyTemplate());
102                 assertEquals(mimeType, endpoint.getMimeType());
103                 assertEquals(disableTransportTransformer, endpoint.isDisableTransportTransformer());
104 
105                 return null;
106             }
107         }).when(mockTransformer).setEndpoint(any(ImmutableEndpoint.class));
108 
109         inputTransformers.add(mockTransformer);
110         outputTransformers.add(mockTransformer);
111 
112         // Creates the endpoint using the transformers which will validate the
113         // configuration
114         new AbstractEndpoint(mockConnector, uri, name, properties,
115             mockTransactionConfig, deleteUnacceptedMessages, 
116             messageExchangePattern, responseTimeout, initialState, endpointEncoding, 
117             endpointBuilderName, muleContext, retryPolicyTemplate, messageProcessorsFactory, 
118             messageProcessors, responseMessageProcessors, disableTransportTransformer,
119             mimeType)
120         {
121             @Override
122             protected MessageProcessor createMessageProcessorChain(FlowConstruct flowConstruct) throws MuleException
123             {
124                 return null;
125             }
126         };
127     }
128 }