1
2
3
4
5
6
7
8
9
10
11 package org.mule.agent;
12
13 import static org.junit.Assert.assertNotNull;
14 import static org.mockito.Matchers.any;
15 import static org.mockito.Matchers.anyString;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.times;
18 import static org.mockito.Mockito.verify;
19 import static org.mockito.Mockito.when;
20
21 import org.mule.api.MuleContext;
22 import org.mule.api.MuleMessage;
23 import org.mule.api.context.notification.ServerNotification;
24 import org.mule.api.endpoint.OutboundEndpoint;
25 import org.mule.api.lifecycle.InitialisationException;
26 import org.mule.api.routing.filter.Filter;
27 import org.mule.api.transport.Connector;
28 import org.mule.context.notification.ConnectionNotification;
29 import org.mule.context.notification.MuleContextNotification;
30
31 import java.lang.reflect.Field;
32
33 import org.apache.commons.logging.Log;
34 import org.junit.Before;
35 import org.junit.Test;
36
37 public class EndpointNotificationLoggerAgentTestCase
38 {
39
40 private EndpointNotificationLoggerAgent agent;
41 private OutboundEndpoint outboundEndpoint;
42 private Connector connector;
43 private ServerNotification serverNotification;
44 private Filter filterMock;
45
46 @Before
47 public void setUp() throws InitialisationException
48 {
49 agent = new EndpointNotificationLoggerAgent();
50 outboundEndpoint = mock(OutboundEndpoint.class);
51 agent.logger = mock(Log.class);
52 agent.setEndpoint(outboundEndpoint);
53 connector = mock(Connector.class);
54 serverNotification = mock(ServerNotification.class);
55 filterMock = mock(Filter.class);
56 agent.setMuleContext(mock(MuleContext.class));
57 agent.doInitialise();
58 when(outboundEndpoint.getConnector()).thenReturn(connector);
59 when(serverNotification.getSource()).thenReturn(connector);
60 }
61
62 @Test(expected = InitialisationException.class)
63 public void initializationFailsWhenNoEndpoint() throws Exception
64 {
65 agent.setEndpoint(null);
66 agent.doInitialise();
67 }
68
69 @Test
70 public void initializationCreatesTheSession() throws Exception
71 {
72 assertNotNull(getAttribute(agent, "session"));
73 }
74
75 @Test
76 public void notLoggedBecauseNotificationIsIgnored() throws InitialisationException
77 {
78 when(serverNotification.getAction()).thenReturn(MuleContextNotification.CONTEXT_STOPPED);
79 agent.logEvent(serverNotification);
80 verify(serverNotification, times(1)).getAction();
81 }
82
83 @Test
84 public void logEndpointNotStarted() throws InitialisationException
85 {
86 when(serverNotification.getAction()).thenReturn(MuleContextNotification.CONTEXT_STARTING);
87 when(connector.isStarted()).thenReturn(false);
88 agent.logEvent(serverNotification);
89 verify(agent.logger, times(1)).warn(anyString());
90 }
91
92 @Test
93 public void ignoreConnectionFailedWhenSameConnector() throws InitialisationException
94 {
95 when(serverNotification.getAction()).thenReturn(ConnectionNotification.CONNECTION_FAILED);
96 when(connector.isStarted()).thenReturn(true);
97 agent.logEvent(serverNotification);
98 verify(outboundEndpoint, times(0)).getFilter();
99 }
100
101 @Test
102 public void ignoreConnectionDisconnectedWhenSameConnector() throws InitialisationException
103 {
104 when(serverNotification.getAction()).thenReturn(ConnectionNotification.CONNECTION_DISCONNECTED);
105 when(connector.isStarted()).thenReturn(true);
106 agent.logEvent(serverNotification);
107 verify(outboundEndpoint, times(0)).getFilter();
108 }
109
110 @Test
111 public void eventIsLoggedBecauseMessageNotFiltered() throws Exception
112 {
113 when(agent.logger.isInfoEnabled()).thenReturn(true);
114 when(connector.isStarted()).thenReturn(true);
115 when(outboundEndpoint.getFilter()).thenReturn(filterMock);
116 when(filterMock.accept(any(MuleMessage.class))).thenReturn(false);
117 when(serverNotification.getAction()).thenReturn(ConnectionNotification.CONNECTION_CONNECTED);
118 agent.logEvent(serverNotification);
119 verify(agent.logger, times(1)).info(anyString());
120 }
121
122
123
124
125 private Object getAttribute(Object object, String fieldName) throws Exception
126 {
127 Field privateStringMethod = object.getClass().getDeclaredField(fieldName);
128 privateStringMethod.setAccessible(true);
129 return privateStringMethod.get(object);
130 }
131 }