MessageSender extends ch.squaredesk.nova.comm.sending.MessageSender<Destination, String, OutgoingMessageMetaData> { @Override public Single<OutgoingMessageMetaData> send(String message, OutgoingMessageMetaData meta) { requireNonNull(message, "message must not be null"); try { TextMessage textMessage = jmsObjectRepository.createTextMessage(); if (meta.details != null) { textMessage.setJMSCorrelationID(meta.details.correlationId); textMessage.setJMSReplyTo(meta.details.replyDestination); if (meta.details.customHeaders != null) { for (String key : meta.details.customHeaders.keySet()) { textMessage.setObjectProperty(key, meta.details.customHeaders.get(key)); } } } textMessage.setText(message); MessageProducer producer = jmsObjectRepository.createMessageProducer(meta.destination); if (meta.details == null) { producer.send(textMessage); } else { producer.send(textMessage, meta.details.deliveryMode, meta.details.priority, meta.details.timeToLive); } return Single.just(meta.setJmsMessage(textMessage)) .doOnSuccess(m -> metricsCollector.messageSent(m.destination)); } catch (Exception e) { return Single.error(e); } } MessageSender(String identifier,
                            JmsObjectRepository jmsObjectRepository,
                            Metrics metrics); @Override Single<OutgoingMessageMetaData> send(String message, OutgoingMessageMetaData meta);  }
@Test void sendNullMessageReturnsError() throws Exception { setup(); OutgoingMessageMetaData meta = new OutgoingMessageMetaData(jmsHelper.createQueue("not used")); assertThrows(NullPointerException.class, () -> sut.send(null, meta)); }
@Test void sendMessage() throws Exception { setup(); Destination queue = jmsHelper.createQueue("sendTest"); OutgoingMessageMetaData meta = new OutgoingMessageMetaData(queue); MessageConsumer consumer = jmsHelper.createMessageConsumer(queue); TestObserver<OutgoingMessageMetaData> observer = sut.send("Hallo", meta).test(); observer.await(10, SECONDS); observer.assertComplete(); Message message = consumer.receive(1000L); assertNotNull(message); assertTrue(message instanceof TextMessage); MatcherAssert.assertThat(((TextMessage) message).getText(), is("Hallo")); }
@Test void sendMessageWithException() throws Exception { Destination queue = jmsHelper.createQueue("sendTest"); OutgoingMessageMetaData meta = new OutgoingMessageMetaData(queue); TestObserver<OutgoingMessageMetaData> observer = sut.send("Hallo", meta, message -> { throw new RuntimeException("4 test"); }).test(); observer.await(1, SECONDS); observer.assertError(RuntimeException.class); observer.assertErrorMessage("4 test"); }