@Test public void shouldSendEventWhenChannelWasUpdated() { Channel channel = new Channel("displayName", BUNDLE_SYMBOLIC_NAME, VERSION); EventParameter eventParameter = new EventParameter("displayName", "eventKey"); TriggerEvent triggerEvent = new TriggerEvent("displayName", "subject", null, Arrays.asList(eventParameter), ""); channel.getTriggerTaskEvents().add(triggerEvent); when(channelsDataService.findByModuleName(channel.getModuleName())).thenReturn(channel); when(bundleContext.getBundles()).thenReturn(new Bundle[]{bundle}); when(bundle.getSymbolicName()).thenReturn(BUNDLE_SYMBOLIC_NAME); ArgumentCaptor<MotechEvent> captor = ArgumentCaptor.forClass(MotechEvent.class); Channel updatedChannel = new Channel("displayName2", BUNDLE_SYMBOLIC_NAME, VERSION); updatedChannel.getTriggerTaskEvents().add(triggerEvent); channelService.addOrUpdate(updatedChannel); verify(channelsDataService).update(channel); verify(eventRelay).sendEventMessage(captor.capture()); MotechEvent event = captor.getValue(); assertEquals(CHANNEL_UPDATE_SUBJECT, event.getSubject()); assertEquals(BUNDLE_SYMBOLIC_NAME, event.getParameters().get(CHANNEL_MODULE_NAME)); }
@Override @Transactional public synchronized void addOrUpdate(final Channel channel) { Set<TaskError> errors = ChannelValidator.validate(channel); if (!isEmpty(errors)) { throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors)); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader()); final Channel existingChannel = getChannel(channel.getModuleName()); if (existingChannel != null && !existingChannel.equals(channel)) { LOGGER.debug("Updating channel {}", channel.getDisplayName()); existingChannel.setActionTaskEvents(channel.getActionTaskEvents()); existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents()); existingChannel.setDescription(channel.getDescription()); existingChannel.setDisplayName(channel.getDisplayName()); existingChannel.setModuleName(channel.getModuleName()); existingChannel.setModuleVersion(channel.getModuleVersion()); channelsDataService.update(existingChannel); sendChannelUpdatedEvent(channel); } else if (existingChannel == null) { LOGGER.debug("Creating channel {}", channel.getDisplayName()); channelsDataService.create(channel); } } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName())); }
ChannelServiceImpl implements ChannelService { @Override @Transactional public synchronized void addOrUpdate(final Channel channel) { Set<TaskError> errors = ChannelValidator.validate(channel); if (!isEmpty(errors)) { throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors)); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader()); final Channel existingChannel = getChannel(channel.getModuleName()); if (existingChannel != null && !existingChannel.equals(channel)) { LOGGER.debug("Updating channel {}", channel.getDisplayName()); existingChannel.setActionTaskEvents(channel.getActionTaskEvents()); existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents()); existingChannel.setDescription(channel.getDescription()); existingChannel.setDisplayName(channel.getDisplayName()); existingChannel.setModuleName(channel.getModuleName()); existingChannel.setModuleVersion(channel.getModuleVersion()); channelsDataService.update(existingChannel); sendChannelUpdatedEvent(channel); } else if (existingChannel == null) { LOGGER.debug("Creating channel {}", channel.getDisplayName()); channelsDataService.create(channel); } } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName())); } }
ChannelServiceImpl implements ChannelService { @Override @Transactional public synchronized void addOrUpdate(final Channel channel) { Set<TaskError> errors = ChannelValidator.validate(channel); if (!isEmpty(errors)) { throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors)); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader()); final Channel existingChannel = getChannel(channel.getModuleName()); if (existingChannel != null && !existingChannel.equals(channel)) { LOGGER.debug("Updating channel {}", channel.getDisplayName()); existingChannel.setActionTaskEvents(channel.getActionTaskEvents()); existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents()); existingChannel.setDescription(channel.getDescription()); existingChannel.setDisplayName(channel.getDisplayName()); existingChannel.setModuleName(channel.getModuleName()); existingChannel.setModuleVersion(channel.getModuleVersion()); channelsDataService.update(existingChannel); sendChannelUpdatedEvent(channel); } else if (existingChannel == null) { LOGGER.debug("Creating channel {}", channel.getDisplayName()); channelsDataService.create(channel); } } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName())); } @Autowired  ChannelServiceImpl(TriggerEventService triggerEventService, ChannelsDataService channelsDataService, EventRelay eventRelay); }
ChannelServiceImpl implements ChannelService { @Override @Transactional public synchronized void addOrUpdate(final Channel channel) { Set<TaskError> errors = ChannelValidator.validate(channel); if (!isEmpty(errors)) { throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors)); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader()); final Channel existingChannel = getChannel(channel.getModuleName()); if (existingChannel != null && !existingChannel.equals(channel)) { LOGGER.debug("Updating channel {}", channel.getDisplayName()); existingChannel.setActionTaskEvents(channel.getActionTaskEvents()); existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents()); existingChannel.setDescription(channel.getDescription()); existingChannel.setDisplayName(channel.getDisplayName()); existingChannel.setModuleName(channel.getModuleName()); existingChannel.setModuleVersion(channel.getModuleVersion()); channelsDataService.update(existingChannel); sendChannelUpdatedEvent(channel); } else if (existingChannel == null) { LOGGER.debug("Creating channel {}", channel.getDisplayName()); channelsDataService.create(channel); } } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName())); } @Autowired  ChannelServiceImpl(TriggerEventService triggerEventService, ChannelsDataService channelsDataService, EventRelay eventRelay); @Override @Transactional void registerChannel(ChannelRequest channelRequest); @Override @Transactional void unregisterChannel(String moduleName); @Override @Transactional void registerChannel(final InputStream stream, String moduleName, String moduleVersion); @Override @Transactional synchronized void addOrUpdate(final Channel channel); @Override @Transactional synchronized void delete(final String moduleName); @Override @Transactional List<Channel> getAllChannels(); @Override @Transactional Channel getChannel(final String moduleName); @Override @Transactional boolean channelExists(String moduleName); @Autowired(required = false) void setBundleContext(BundleContext bundleContext); }
ChannelServiceImpl implements ChannelService { @Override @Transactional public synchronized void addOrUpdate(final Channel channel) { Set<TaskError> errors = ChannelValidator.validate(channel); if (!isEmpty(errors)) { throw new ValidationException(ChannelValidator.CHANNEL, TaskError.toDtos(errors)); } ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); try { Thread.currentThread().setContextClassLoader(channel.getClass().getClassLoader()); final Channel existingChannel = getChannel(channel.getModuleName()); if (existingChannel != null && !existingChannel.equals(channel)) { LOGGER.debug("Updating channel {}", channel.getDisplayName()); existingChannel.setActionTaskEvents(channel.getActionTaskEvents()); existingChannel.setTriggerTaskEvents(channel.getTriggerTaskEvents()); existingChannel.setDescription(channel.getDescription()); existingChannel.setDisplayName(channel.getDisplayName()); existingChannel.setModuleName(channel.getModuleName()); existingChannel.setModuleVersion(channel.getModuleVersion()); channelsDataService.update(existingChannel); sendChannelUpdatedEvent(channel); } else if (existingChannel == null) { LOGGER.debug("Creating channel {}", channel.getDisplayName()); channelsDataService.create(channel); } } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } LOGGER.info(String.format("Saved channel: %s", channel.getDisplayName())); } @Autowired  ChannelServiceImpl(TriggerEventService triggerEventService, ChannelsDataService channelsDataService, EventRelay eventRelay); @Override @Transactional void registerChannel(ChannelRequest channelRequest); @Override @Transactional void unregisterChannel(String moduleName); @Override @Transactional void registerChannel(final InputStream stream, String moduleName, String moduleVersion); @Override @Transactional synchronized void addOrUpdate(final Channel channel); @Override @Transactional synchronized void delete(final String moduleName); @Override @Transactional List<Channel> getAllChannels(); @Override @Transactional Channel getChannel(final String moduleName); @Override @Transactional boolean channelExists(String moduleName); @Autowired(required = false) void setBundleContext(BundleContext bundleContext);  }