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.cache.keygenerator;
8   
9   import org.mule.api.MuleContext;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleMessage;
12  import org.mule.api.expression.ExpressionManager;
13  import org.mule.tck.junit4.AbstractMuleTestCase;
14  
15  import java.io.NotSerializableException;
16  import java.io.Serializable;
17  
18  import org.junit.Before;
19  import org.junit.Test;
20  
21  import static org.junit.Assert.assertEquals;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.when;
24  
25  public class ExpressionKeyGeneratorTestCase extends AbstractMuleTestCase
26  {
27  
28      private static final String KEY = "KEY";
29      private static final String EXPRESSION = "muleExpression";
30  
31      private ExpressionKeyGenerator keyGenerator;
32      private MuleMessage message;
33      private MuleEvent event;
34  
35      @Before
36      public void setUp() throws Exception
37      {
38          expressionManager = mock(ExpressionManager.class);
39          MuleContext context = mock(MuleContext.class);
40          when(context.getExpressionManager()).thenReturn(expressionManager);
41  
42          message = mock(MuleMessage.class);
43  
44          event = mock(MuleEvent.class);
45          when(event.getMessage()).thenReturn(message);
46          when(event.getMuleContext()).thenReturn(context);
47  
48          keyGenerator = new ExpressionKeyGenerator();
49          keyGenerator.setExpression(EXPRESSION);
50      }
51  
52      private ExpressionManager expressionManager;
53  
54      @Test
55      public void testGeneratesSerializableKey() throws Exception
56      {
57          when(expressionManager.evaluate(EXPRESSION, message)).thenReturn(KEY);
58          Serializable key = keyGenerator.generateKey(event);
59  
60          assertEquals(KEY, key);
61      }
62  
63      @Test(expected = NotSerializableException.class)
64      public void testThrowsExceptionOnNonSerializableKey() throws Exception
65      {
66          when(expressionManager.evaluate(EXPRESSION, message)).thenReturn(null);
67          keyGenerator.generateKey(event);
68      }
69  }