@Test
  public void testUnregisterAndRegisterWhenAddingFromMultipleSources()
      throws Node2BeanException, RepositoryException, RegistrationException {

    // GIVEN
    String appName1 = "app1";
    String appName2 = "app2";
    DefinitionProvider<AppDescriptor> appDescriptorProvider1 =
        createAppDescriptorProvider(appName1, true);

    Set<DefinitionMetadata> registeredNames =
        appDescriptorRegistry.unregisterAndRegister(
            Arrays.<DefinitionMetadata>asList(), Arrays.asList(appDescriptorProvider1));
    assertEquals(1, registeredNames.size());
    assertTrue(registeredNames.contains(appDescriptorProvider1.getMetadata()));
    eventHandler.clear();

    // WHEN
    // first app gets reregistered due to change in in active state
    appDescriptorProvider1 = createAppDescriptorProvider(appName1, false);
    // second app gets registered
    DefinitionProvider<AppDescriptor> appDescriptorProvider2 =
        createAppDescriptorProvider(appName2, true);

    Set<DefinitionMetadata> registeredNames2 =
        appDescriptorRegistry.unregisterAndRegister(
            Arrays.<DefinitionMetadata>asList(),
            Arrays.asList(appDescriptorProvider1, appDescriptorProvider2));

    // THEN
    assertNotNull(registeredNames2);
    assertEquals(2, registeredNames2.size());
    assertEquals(2, eventHandler.events.size());
    assertContainsEvent(AppRegistryEventType.REREGISTERED, appName1);
    assertContainsEvent(AppRegistryEventType.REGISTERED, appName2);
  }
  @Test
  public void testUnregisterAndRegisterInComplexCase()
      throws RepositoryException, Node2BeanException, RegistrationException {

    // GIVEN
    DefinitionProvider<AppDescriptor> appThatStays =
        createAppDescriptorProvider("appThatStays", true);
    DefinitionProvider<AppDescriptor> appThatGoesAway =
        createAppDescriptorProvider("appThatGoesAway", true);
    DefinitionProvider<AppDescriptor> appThatAppears =
        createAppDescriptorProvider("appThatAppears", true);

    Set<DefinitionMetadata> registeredMetadata =
        appDescriptorRegistry.unregisterAndRegister(
            Arrays.<DefinitionMetadata>asList(), Arrays.asList(appThatStays, appThatGoesAway));

    assertEquals(2, registeredMetadata.size());
    assertTrue(registeredMetadata.contains(appThatStays.getMetadata()));
    assertTrue(registeredMetadata.contains(appThatGoesAway.getMetadata()));

    assertEquals(2, eventHandler.events.size());
    assertContainsEvent(AppRegistryEventType.REGISTERED, appThatStays.getMetadata().getName());
    assertContainsEvent(AppRegistryEventType.REGISTERED, appThatGoesAway.getMetadata().getName());

    eventHandler.clear();

    // WHEN
    Set<DefinitionMetadata> registeredNames2 =
        appDescriptorRegistry.unregisterAndRegister(
            registeredMetadata, Arrays.asList(appThatStays, appThatAppears));

    // THEN
    assertEquals(2, registeredNames2.size());
    assertTrue(registeredNames2.contains(appThatStays.getMetadata()));
    assertTrue(registeredNames2.contains(appThatAppears.getMetadata()));
    assertEquals(2, eventHandler.events.size());
    assertContainsEvent(AppRegistryEventType.UNREGISTERED, appThatGoesAway.getMetadata().getName());
    assertContainsEvent(AppRegistryEventType.REGISTERED, appThatAppears.getMetadata().getName());
  }