@Test
  public void shouldRegisterChannel() {
    String triggerEvent =
        "{ displayName: 'displayName', subject: 'subject', eventParameters: [{ displayName: 'displayName', eventKey: 'eventKey' }] }";
    String channel =
        String.format(
            "{displayName: %s, triggerTaskEvents: [%s]}", BUNDLE_SYMBOLIC_NAME, triggerEvent);
    InputStream stream = new ByteArrayInputStream(channel.getBytes(Charset.forName("UTF-8")));

    channelService.registerChannel(stream, BUNDLE_SYMBOLIC_NAME, VERSION);

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

    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    verify(channelsDataService).create(captor.capture());

    Channel c = captor.getValue();

    assertEquals(BUNDLE_SYMBOLIC_NAME, c.getDisplayName());
    assertEquals(BUNDLE_SYMBOLIC_NAME, c.getModuleName());
    assertEquals(VERSION, c.getModuleVersion());
    assertEquals(1, c.getTriggerTaskEvents().size());
    assertEquals(
        new TriggerEvent(
            "displayName",
            "subject",
            null,
            asList(new EventParameter("displayName", "eventKey")),
            ""),
        c.getTriggerTaskEvents().get(0));
  }
  @Test
  public void shouldUnregisterChannel() {
    Channel channel = new Channel("Channel to delete", 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);

    channelService.unregisterChannel(channel.getModuleName());

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

    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    verify(channelsDataService).delete(captor.capture());

    Channel deletedChannel = captor.getValue();

    assertEquals("Channel to delete", deletedChannel.getDisplayName());
    assertEquals(BUNDLE_SYMBOLIC_NAME, deletedChannel.getModuleName());
    assertEquals(VERSION, deletedChannel.getModuleVersion());
  }
  @Test
  public void shouldRegisterChannelFromChannelRequest() {
    List<ActionEventRequest> actionEventRequests =
        asList(
            new TestActionEventRequestBuilder()
                .setDisplayName("actionName")
                .setSubject("subject.foo")
                .setDescription("action description")
                .setServiceInterface("some.interface")
                .setServiceMethod("method")
                .setActionParameters(new TreeSet<ActionParameterRequest>())
                .createActionEventRequest());
    List<TriggerEventRequest> triggerEventsRequest =
        asList(
            new TriggerEventRequest(
                "displayName",
                "subject.foo",
                "description",
                asList(new EventParameterRequest("displayName", "eventKey"))));
    ChannelRequest channelRequest =
        new ChannelRequest(
            BUNDLE_SYMBOLIC_NAME,
            BUNDLE_SYMBOLIC_NAME,
            VERSION,
            "",
            triggerEventsRequest,
            actionEventRequests);

    channelService.registerChannel(channelRequest);

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

    ArgumentCaptor<Channel> captor = ArgumentCaptor.forClass(Channel.class);
    verify(channelsDataService).create(captor.capture());

    Channel channelToBeCreated = captor.getValue();

    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getDisplayName());
    assertEquals(BUNDLE_SYMBOLIC_NAME, channelToBeCreated.getModuleName());
    assertEquals(VERSION, channelToBeCreated.getModuleVersion());

    assertEquals(1, channelToBeCreated.getTriggerTaskEvents().size());
    TriggerEvent expectedTrigger =
        new TriggerEvent(
            "displayName",
            "subject.foo",
            "description",
            asList(new EventParameter("displayName", "eventKey")),
            "");
    TriggerEvent actualTrigger = channelToBeCreated.getTriggerTaskEvents().get(0);
    assertEquals(expectedTrigger, actualTrigger);

    assertEquals(1, channelToBeCreated.getActionTaskEvents().size());
    ActionEvent expectedAction =
        new ActionEventBuilder()
            .setDisplayName("actionName")
            .setSubject("subject.foo")
            .setDescription("action description")
            .setServiceInterface("some.interface")
            .setServiceMethod("method")
            .setActionParameters(new TreeSet<>())
            .build();
    ActionEvent actualAction = channelToBeCreated.getActionTaskEvents().get(0);
    assertEquals(expectedAction, actualAction);
  }