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