View Javadoc

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