public void handle(MotechEvent event) {
   if (event.getSubject().equals(EventKeys.CREATED_NEW_PATIENT_SUBJECT)) {
     created = true;
   } else if (event.getSubject().equals(EventKeys.UPDATED_PATIENT_SUBJECT)) {
     updated = true;
   } else if (event.getSubject().equals(EventKeys.PATIENT_DECEASED_SUBJECT)) {
     deceased = true;
   } else if (event.getSubject().equals(EventKeys.DELETED_PATIENT_SUBJECT)) {
     deleted = true;
   }
   eventParameters = event.getParameters();
   synchronized (lock) {
     lock.notify();
   }
 }
  @Test
  public void testShouldRaiseExceptionEventWhenCaseNameIsMissing()
      throws FileNotFoundException, CaseParserException {

    CaseTask task = new CaseTask();

    CreateTask createTask = new CreateTask();

    createTask.setOwnerId("OWNER_ID");

    createTask.setCaseType("CASE_TYPE");

    task.setCreateTask(createTask);

    ArgumentCaptor<MotechEvent> motechEventCaptor = ArgumentCaptor.forClass(MotechEvent.class);

    String xml = caseConverter.convertToCaseXml(task);

    verify(eventRelay).sendEventMessage(motechEventCaptor.capture());

    MotechEvent motechEvent = motechEventCaptor.getValue();

    Assert.assertEquals(motechEvent.getSubject(), EventSubjects.MALFORMED_CASE_EXCEPTION);

    Assert.assertNull(xml);
  }
  @Test
  public void testShouldRaiseExceptionEventWhenIndexIsMalformed()
      throws FileNotFoundException, CaseParserException {

    CaseTask task = new CaseTask();

    List<IndexSubElement> subElements = new ArrayList<IndexSubElement>();

    subElements.add(new IndexSubElement(null, null, null));

    IndexTask indexTask = new IndexTask(subElements);

    task.setIndexTask(indexTask);

    ArgumentCaptor<MotechEvent> motechEventCaptor = ArgumentCaptor.forClass(MotechEvent.class);

    String xml = caseConverter.convertToCaseXml(task);

    verify(eventRelay).sendEventMessage(motechEventCaptor.capture());

    MotechEvent motechEvent = motechEventCaptor.getValue();

    Assert.assertEquals(motechEvent.getSubject(), EventSubjects.MALFORMED_CASE_EXCEPTION);

    Assert.assertNull(xml);
  }
  @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);

    ArgumentCaptor<TransactionCallback> transactionCaptor =
        ArgumentCaptor.forClass(TransactionCallback.class);
    verify(channelsDataService).doInTransaction(transactionCaptor.capture());
    transactionCaptor.getValue().doInTransaction(null);
    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));
  }
  @Test
  public void shouldSendEventWhenChannelWasDeleted() {
    Channel channel = new Channel("displayName", BUNDLE_SYMBOLIC_NAME, VERSION);

    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 deletedChannel = new Channel("displayName2", BUNDLE_SYMBOLIC_NAME, VERSION);
    channelService.delete(deletedChannel.getModuleName());

    ArgumentCaptor<TransactionCallback> transactionCaptor =
        ArgumentCaptor.forClass(TransactionCallback.class);
    verify(channelsDataService).doInTransaction(transactionCaptor.capture());
    transactionCaptor.getValue().doInTransaction(null);
    verify(channelsDataService).delete(channel);

    verify(eventRelay).sendEventMessage(captor.capture());

    MotechEvent event = captor.getValue();

    assertEquals(CHANNEL_DEREGISTER_SUBJECT, event.getSubject());
    assertEquals(BUNDLE_SYMBOLIC_NAME, event.getParameters().get(CHANNEL_MODULE_NAME));
  }
  /**
   * Closes or marks the alert as read based on the received event.
   *
   * @param event the event - id of the alert to update is the only expected parameter
   */
  @MotechListener(subjects = {CLOSE_ALERT_SUBJECT, MARK_ALERT_READ_SUBJECT})
  public void updateStatus(MotechEvent event) {
    Long id = Long.valueOf(getValueAsString(event, ALERT_ID));
    UpdateCriteria criteria = new UpdateCriteria();

    if (event.getSubject().equals(CLOSE_ALERT_SUBJECT)) {
      criteria.status(AlertStatus.CLOSED);
    } else {
      criteria.status(AlertStatus.READ);
    }

    alertService.update(id, criteria);
  }