You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by cm...@apache.org on 2011/06/26 17:12:55 UTC

svn commit: r1139809 - in /camel/trunk/components/camel-smpp/src: main/java/org/apache/camel/component/smpp/ test/java/org/apache/camel/component/smpp/

Author: cmueller
Date: Sun Jun 26 15:12:55 2011
New Revision: 1139809

URL: http://svn.apache.org/viewvc?rev=1139809&view=rev
Log:
CAMEL-4136: refactored MessageReceiverListener out to be able to test the implementation

Added:
    camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/MessageReceiverListenerImpl.java
    camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/MessageReceiverListenerImplTest.java
Modified:
    camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConsumer.java

Added: camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/MessageReceiverListenerImpl.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/MessageReceiverListenerImpl.java?rev=1139809&view=auto
==============================================================================
--- camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/MessageReceiverListenerImpl.java (added)
+++ camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/MessageReceiverListenerImpl.java Sun Jun 26 15:12:55 2011
@@ -0,0 +1,100 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.smpp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.ExceptionHandler;
+import org.jsmpp.bean.AlertNotification;
+import org.jsmpp.bean.DataSm;
+import org.jsmpp.bean.DeliverSm;
+import org.jsmpp.extra.ProcessRequestException;
+import org.jsmpp.session.DataSmResult;
+import org.jsmpp.session.MessageReceiverListener;
+import org.jsmpp.session.Session;
+import org.jsmpp.util.MessageIDGenerator;
+import org.jsmpp.util.MessageId;
+import org.jsmpp.util.RandomMessageIDGenerator;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MessageReceiverListenerImpl implements MessageReceiverListener {
+    
+    private static final transient Logger LOG = LoggerFactory.getLogger(MessageReceiverListenerImpl.class);
+
+    private MessageIDGenerator messageIDGenerator = new RandomMessageIDGenerator();
+    private SmppEndpoint endpoint;
+    private Processor processor;
+    private ExceptionHandler exceptionHandler;
+    
+    public MessageReceiverListenerImpl(SmppEndpoint endpoint, Processor processor, ExceptionHandler exceptionHandler) {
+        this.endpoint = endpoint;
+        this.processor = processor;
+        this.exceptionHandler = exceptionHandler;
+    }
+
+    public void onAcceptAlertNotification(AlertNotification alertNotification) {
+        LOG.debug("Received an alertNotification {}", alertNotification);
+
+        try {
+            Exchange exchange = endpoint.createOnAcceptAlertNotificationExchange(alertNotification);
+
+            LOG.trace("Processing the new smpp exchange...");
+            processor.process(exchange);
+            LOG.trace("Processed the new smpp exchange");
+        } catch (Exception e) {
+            exceptionHandler.handleException(e);
+        }
+    }
+
+    public void onAcceptDeliverSm(DeliverSm deliverSm) {
+        LOG.debug("Received a deliverSm {}", deliverSm);
+
+        try {
+            Exchange exchange = endpoint.createOnAcceptDeliverSmExchange(deliverSm);
+
+            LOG.trace("processing the new smpp exchange...");
+            processor.process(exchange);
+            LOG.trace("processed the new smpp exchange");
+        } catch (Exception e) {
+            exceptionHandler.handleException(e);
+        }
+    }
+
+    public DataSmResult onAcceptDataSm(DataSm dataSm, Session session) throws ProcessRequestException {
+        LOG.debug("Received a dataSm {}", dataSm);
+
+        MessageId newMessageId = messageIDGenerator.newMessageId();
+
+        try {
+            Exchange exchange = endpoint.createOnAcceptDataSm(dataSm, newMessageId.getValue());
+
+            LOG.trace("processing the new smpp exchange...");
+            processor.process(exchange);
+            LOG.trace("processed the new smpp exchange");
+        } catch (Exception e) {
+            exceptionHandler.handleException(e);
+            throw new ProcessRequestException(e.getMessage(), 255, e);
+        }
+
+        return new DataSmResult(newMessageId, dataSm.getOptionalParametes());
+    }
+
+    public void setMessageIDGenerator(MessageIDGenerator messageIDGenerator) {
+        this.messageIDGenerator = messageIDGenerator;
+    }
+}
\ No newline at end of file

Modified: camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConsumer.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConsumer.java?rev=1139809&r1=1139808&r2=1139809&view=diff
==============================================================================
--- camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConsumer.java (original)
+++ camel/trunk/components/camel-smpp/src/main/java/org/apache/camel/component/smpp/SmppConsumer.java Sun Jun 26 15:12:55 2011
@@ -19,30 +19,20 @@ package org.apache.camel.component.smpp;
 import java.io.IOException;
 import java.util.concurrent.locks.ReentrantLock;
 
-import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
 import org.apache.camel.impl.DefaultConsumer;
 import org.jsmpp.DefaultPDUReader;
 import org.jsmpp.DefaultPDUSender;
 import org.jsmpp.SynchronizedPDUSender;
-import org.jsmpp.bean.AlertNotification;
 import org.jsmpp.bean.BindType;
-import org.jsmpp.bean.DataSm;
-import org.jsmpp.bean.DeliverSm;
 import org.jsmpp.bean.NumberingPlanIndicator;
 import org.jsmpp.bean.TypeOfNumber;
-import org.jsmpp.extra.ProcessRequestException;
 import org.jsmpp.extra.SessionState;
 import org.jsmpp.session.BindParameter;
-import org.jsmpp.session.DataSmResult;
 import org.jsmpp.session.MessageReceiverListener;
 import org.jsmpp.session.SMPPSession;
-import org.jsmpp.session.Session;
 import org.jsmpp.session.SessionStateListener;
 import org.jsmpp.util.DefaultComposer;
-import org.jsmpp.util.MessageIDGenerator;
-import org.jsmpp.util.MessageId;
-import org.jsmpp.util.RandomMessageIDGenerator;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -79,59 +69,7 @@ public class SmppConsumer extends Defaul
                 }
             }
         };
-        this.messageReceiverListener = new MessageReceiverListener() {
-            private final MessageIDGenerator messageIDGenerator = new RandomMessageIDGenerator();
-
-            public void onAcceptAlertNotification(AlertNotification alertNotification) {
-                LOG.debug("Received an alertNotification {}", alertNotification);
-
-                try {
-                    Exchange exchange = getEndpoint().createOnAcceptAlertNotificationExchange(
-                            alertNotification);
-
-                    LOG.trace("Processing the new smpp exchange...");
-                    getProcessor().process(exchange);
-                    LOG.trace("Processed the new smpp exchange");
-                } catch (Exception e) {
-                    getExceptionHandler().handleException(e);
-                }
-            }
-
-            public void onAcceptDeliverSm(DeliverSm deliverSm) {
-                LOG.debug("Received a deliverSm {}", deliverSm);
-
-                try {
-                    Exchange exchange = getEndpoint().createOnAcceptDeliverSmExchange(deliverSm);
-
-                    LOG.trace("processing the new smpp exchange...");
-                    getProcessor().process(exchange);
-                    LOG.trace("processed the new smpp exchange");
-                } catch (Exception e) {
-                    getExceptionHandler().handleException(e);
-                }
-            }
-
-            public DataSmResult onAcceptDataSm(DataSm dataSm, Session session)
-                throws ProcessRequestException {
-                LOG.debug("Received a dataSm {}", dataSm);
-
-                MessageId newMessageId = messageIDGenerator.newMessageId();
-
-                try {
-                    Exchange exchange = getEndpoint().createOnAcceptDataSm(dataSm,
-                            newMessageId.getValue());
-
-                    LOG.trace("processing the new smpp exchange...");
-                    getProcessor().process(exchange);
-                    LOG.trace("processed the new smpp exchange");
-                } catch (Exception e) {
-                    getExceptionHandler().handleException(e);
-                    throw new ProcessRequestException(e.getMessage(), 255, e);
-                }
-
-                return new DataSmResult(newMessageId, dataSm.getOptionalParametes());
-            }
-        };
+        this.messageReceiverListener = new MessageReceiverListenerImpl(getEndpoint(), getProcessor(), getExceptionHandler());
     }
 
     @Override

Added: camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/MessageReceiverListenerImplTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/MessageReceiverListenerImplTest.java?rev=1139809&view=auto
==============================================================================
--- camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/MessageReceiverListenerImplTest.java (added)
+++ camel/trunk/components/camel-smpp/src/test/java/org/apache/camel/component/smpp/MessageReceiverListenerImplTest.java Sun Jun 26 15:12:55 2011
@@ -0,0 +1,188 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.smpp;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.spi.ExceptionHandler;
+import org.jsmpp.PDUStringException;
+import org.jsmpp.bean.AlertNotification;
+import org.jsmpp.bean.DataSm;
+import org.jsmpp.bean.DeliverSm;
+import org.jsmpp.bean.OptionalParameter;
+import org.jsmpp.extra.ProcessRequestException;
+import org.jsmpp.session.DataSmResult;
+import org.jsmpp.session.SMPPSession;
+import org.jsmpp.util.MessageIDGenerator;
+import org.jsmpp.util.MessageId;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.expectLastCall;
+import static org.easymock.classextension.EasyMock.createMock;
+import static org.easymock.classextension.EasyMock.replay;
+import static org.easymock.classextension.EasyMock.verify;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.fail;
+
+public class MessageReceiverListenerImplTest {
+    
+    private MessageReceiverListenerImpl listener;
+    private SmppEndpoint endpoint;
+    private Processor processor;
+    private ExceptionHandler exceptionHandler;
+    
+    @Before
+    public void setUp() {
+        endpoint = createMock(SmppEndpoint.class);
+        processor = createMock(Processor.class);
+        exceptionHandler = createMock(ExceptionHandler.class);
+        
+        listener = new MessageReceiverListenerImpl(endpoint, processor, exceptionHandler);
+        listener.setMessageIDGenerator(new MessageIDGenerator() {
+            public MessageId newMessageId() {
+                try {
+                    return new MessageId("1");
+                } catch (PDUStringException e) {
+                    throw new RuntimeException(e);
+                }
+            }
+        });
+    }
+
+    @Test
+    public void onAcceptAlertNotificationSuccess() throws Exception {
+        AlertNotification alertNotification = createMock(AlertNotification.class);
+        Exchange exchange = createMock(Exchange.class);
+        Exception exception = new Exception("forced exception for test");
+        
+        expect(endpoint.createOnAcceptAlertNotificationExchange(alertNotification))
+            .andReturn(exchange);
+        processor.process(exchange);
+        expectLastCall().andThrow(exception);
+        exceptionHandler.handleException(exception);
+        
+        replay(endpoint, processor, exceptionHandler, alertNotification, exchange);
+        
+        listener.onAcceptAlertNotification(alertNotification);
+        
+        verify(endpoint, processor, exceptionHandler, alertNotification, exchange);
+    }
+    
+    @Test
+    public void onAcceptAlertNotificationFailure() throws Exception {
+        AlertNotification alertNotification = createMock(AlertNotification.class);
+        Exchange exchange = createMock(Exchange.class);
+        
+        expect(endpoint.createOnAcceptAlertNotificationExchange(alertNotification))
+            .andReturn(exchange);
+        processor.process(exchange);
+        
+        replay(endpoint, processor, exceptionHandler, alertNotification, exchange);
+        
+        listener.onAcceptAlertNotification(alertNotification);
+        
+        verify(endpoint, processor, exceptionHandler, alertNotification, exchange);
+    }
+    
+    @Test
+    public void onAcceptDeliverSmSuccess() throws Exception {
+        DeliverSm deliverSm = createMock(DeliverSm.class);
+        Exchange exchange = createMock(Exchange.class);
+        Exception exception = new Exception("forced exception for test");
+        
+        expect(endpoint.createOnAcceptDeliverSmExchange(deliverSm))
+            .andReturn(exchange);
+        processor.process(exchange);
+        expectLastCall().andThrow(exception);
+        exceptionHandler.handleException(exception);
+        
+        replay(endpoint, processor, exceptionHandler, deliverSm, exchange);
+        
+        listener.onAcceptDeliverSm(deliverSm);
+        
+        verify(endpoint, processor, exceptionHandler, deliverSm, exchange);
+    }
+    
+    @Test
+    public void onAcceptDeliverSmFailure() throws Exception {
+        DeliverSm deliverSm = createMock(DeliverSm.class);
+        Exchange exchange = createMock(Exchange.class);
+        
+        expect(endpoint.createOnAcceptDeliverSmExchange(deliverSm))
+            .andReturn(exchange);
+        processor.process(exchange);
+        
+        replay(endpoint, processor, exceptionHandler, deliverSm, exchange);
+        
+        listener.onAcceptDeliverSm(deliverSm);
+        
+        verify(endpoint, processor, exceptionHandler, deliverSm, exchange);
+    }
+    
+    @Test
+    public void onAcceptDataSmSuccess() throws Exception {
+        SMPPSession session = createMock(SMPPSession.class);
+        DataSm dataSm = createMock(DataSm.class);
+        Exchange exchange = createMock(Exchange.class);
+        OptionalParameter[] optionalParameters = new OptionalParameter[]{};
+        
+        expect(endpoint.createOnAcceptDataSm(dataSm, "1"))
+            .andReturn(exchange);
+        processor.process(exchange);
+        expect(dataSm.getOptionalParametes())
+            .andReturn(optionalParameters);
+        
+        replay(endpoint, processor, exceptionHandler, session, dataSm, exchange);
+        
+        DataSmResult result = listener.onAcceptDataSm(dataSm, session);
+        
+        verify(endpoint, processor, exceptionHandler, session, dataSm, exchange);
+        
+        assertEquals("1", result.getMessageId());
+        assertSame(optionalParameters, result.getOptionalParameters());
+    }
+    
+    @Test
+    public void onAcceptDataSmFailure() throws Exception {
+        SMPPSession session = createMock(SMPPSession.class);
+        DataSm dataSm = createMock(DataSm.class);
+        Exchange exchange = createMock(Exchange.class);
+        Exception exception = new Exception("forced exception for test");
+        
+        expect(endpoint.createOnAcceptDataSm(dataSm, "1"))
+            .andReturn(exchange);
+        processor.process(exchange);
+        expectLastCall().andThrow(exception);
+        exceptionHandler.handleException(exception);
+        
+        replay(endpoint, processor, exceptionHandler, session, dataSm, exchange);
+        
+        try {
+            listener.onAcceptDataSm(dataSm, session);
+            fail("ProcessRequestException expected");
+        } catch (ProcessRequestException e) {
+            assertEquals(255, e.getErrorCode());
+            assertEquals("forced exception for test", e.getMessage());
+            assertSame(exception, e.getCause());
+        }
+        
+        verify(endpoint, processor, exceptionHandler, session, dataSm, exchange);
+    }
+}
\ No newline at end of file