PushNotificationSmsMessageProcessor extends AbstractProtocolRequestMessageProcessor { @Override public void processMessage(final ObjectMessage message) throws JMSException { final MessageMetadata metadata = MessageMetadata.fromMessage(message); LOGGER.info("Received message of messageType: {} organisationIdentification: {} deviceIdentification: {}", messageType, metadata.getOrganisationIdentification(), metadata.getDeviceIdentification()); final RequestMessage requestMessage = (RequestMessage) message.getObject(); final Object dataObject = requestMessage.getRequest(); try { final Device device = getDevice(metadata.getDeviceIdentification()); final PushNotificationSmsDto pushNotificationSms = (PushNotificationSmsDto) dataObject; this.storeSmsAsEvent(pushNotificationSms); if (pushNotificationSms.getIpAddress() != null && !"".equals(pushNotificationSms.getIpAddress())) { LOGGER.info("Updating device {} IP address from {} to {}", metadata.getDeviceIdentification(), requestMessage.getIpAddress(), pushNotificationSms.getIpAddress()); final InetAddress address = InetAddress.getByName(pushNotificationSms.getIpAddress()); device.updateRegistrationData(address, device.getDeviceType()); device.updateConnectionDetailsToSuccess(); this.deviceRepository.save(device); } else { LOGGER.warn("Sms notification request for device = {} has no new IP address. Discard request.", metadata.getDeviceIdentification()); } } catch (final UnknownHostException | FunctionalException e) { String errorMessage = String.format("%s occurred, reason: %s", e.getClass().getName(), e.getMessage()); LOGGER.error(errorMessage, e); throw new JMSException(errorMessage); } } protected  PushNotificationSmsMessageProcessor(); @Override void processMessage(final ObjectMessage message);  }
@Test public void testProcessMessageSuccess() throws JMSException, UnknownEntityException { when(this.pushNotificationSms.getIpAddress()).thenReturn(IP_ADDRESS); doNothing().when(this.eventNotificationMessageService) .handleEvent(any(String.class), any(Date.class), any(EventType.class), any(String.class), any(Integer.class)); final Device device = new Device(DEVICE_IDENTIFICATION); when(this.deviceRepository.findByDeviceIdentification(DEVICE_IDENTIFICATION)).thenReturn(device); assertThat(device.getLastSuccessfulConnectionTimestamp()).isNull(); this.pushNotificationSmsMessageProcessor.processMessage(this.message); assertThat(device.getLastSuccessfulConnectionTimestamp()).isNotNull(); verify(this.deviceRepository).save(device); }
@Test public void testUnknownDevice() { when(this.deviceRepository.findByDeviceIdentification(DEVICE_IDENTIFICATION)).thenReturn(null); assertThatExceptionOfType(JMSException.class).isThrownBy(() -> { this.pushNotificationSmsMessageProcessor.processMessage(this.message); }); }