@Test void testPopulate() { PluginController pluginController0 = when(mock(PluginController.class).getId()).thenReturn(NEW_PLUGIN).getMock(); PluginController pluginController1 = when(mock(PluginController.class).getId()).thenReturn(CHANGED_PLUGIN).getMock(); ApplicationContext ctx = mock(ApplicationContext.class); when(ctx.getBeansOfType(PluginController.class)) .thenReturn( ImmutableMap.of(NEW_PLUGIN, pluginController0, CHANGED_PLUGIN, pluginController1)); Plugin newPlugin = when(mock(Plugin.class).getId()).thenReturn(NEW_PLUGIN).getMock(); when(newPlugin.setLabel(NEW_PLUGIN)).thenReturn(newPlugin); when(newPlugin.setPath(NEW_PLUGIN)).thenReturn(newPlugin); Plugin changedPlugin = when(mock(Plugin.class).getId()).thenReturn(CHANGED_PLUGIN).getMock(); when(changedPlugin.setLabel(CHANGED_PLUGIN)).thenReturn(changedPlugin); when(changedPlugin.setPath(CHANGED_PLUGIN)).thenReturn(changedPlugin); Plugin existingChangedPlugin = when(mock(Plugin.class).getId()).thenReturn(CHANGED_PLUGIN).getMock(); Plugin deletedPlugin = when(mock(Plugin.class).getId()).thenReturn(DELETED_PLUGIN).getMock(); doReturn(newPlugin).when(pluginFactory).create(NEW_PLUGIN); doReturn(changedPlugin).when(pluginFactory).create(CHANGED_PLUGIN); when(dataService.getRepository(PluginMetadata.PLUGIN, Plugin.class)) .thenReturn(pluginRepository); when(dataService.findAll(PluginMetadata.PLUGIN, Plugin.class)) .thenReturn(Stream.of(existingChangedPlugin, deletedPlugin)); pluginPopulator.populate(ctx); verify(pluginRepository).upsertBatch(updateStreamArgumentCaptor.capture()); assertEquals( newHashSet(newPlugin, changedPlugin), newHashSet(updateStreamArgumentCaptor.getValue())); verify(dataService).delete(eq(PluginMetadata.PLUGIN), deleteStreamArgumentCaptor.capture()); assertEquals( singletonList(deletedPlugin), deleteStreamArgumentCaptor.getValue().collect(toList())); }
public void populate(ApplicationContext ctx) { Map<String, Plugin> newOrChangedPluginMap = getPlugins(ctx); List<Plugin> deletedPlugins = new ArrayList<>(); Map<String, Plugin> existingPluginMap = dataService .findAll(PLUGIN, Plugin.class) .collect(toMap(Plugin::getId, Function.identity())); existingPluginMap.forEach( (pluginId, plugin) -> { if (newOrChangedPluginMap.get(pluginId) == null && !pluginId.startsWith(APP_PREFIX)) { deletedPlugins.add(plugin); } }); if (!newOrChangedPluginMap.isEmpty()) { dataService .getRepository(PLUGIN, Plugin.class) .upsertBatch(newArrayList(newOrChangedPluginMap.values())); } if (!deletedPlugins.isEmpty()) { dataService.delete(PLUGIN, deletedPlugins.stream()); } }
PluginPopulator { public void populate(ApplicationContext ctx) { Map<String, Plugin> newOrChangedPluginMap = getPlugins(ctx); List<Plugin> deletedPlugins = new ArrayList<>(); Map<String, Plugin> existingPluginMap = dataService .findAll(PLUGIN, Plugin.class) .collect(toMap(Plugin::getId, Function.identity())); existingPluginMap.forEach( (pluginId, plugin) -> { if (newOrChangedPluginMap.get(pluginId) == null && !pluginId.startsWith(APP_PREFIX)) { deletedPlugins.add(plugin); } }); if (!newOrChangedPluginMap.isEmpty()) { dataService .getRepository(PLUGIN, Plugin.class) .upsertBatch(newArrayList(newOrChangedPluginMap.values())); } if (!deletedPlugins.isEmpty()) { dataService.delete(PLUGIN, deletedPlugins.stream()); } } }
PluginPopulator { public void populate(ApplicationContext ctx) { Map<String, Plugin> newOrChangedPluginMap = getPlugins(ctx); List<Plugin> deletedPlugins = new ArrayList<>(); Map<String, Plugin> existingPluginMap = dataService .findAll(PLUGIN, Plugin.class) .collect(toMap(Plugin::getId, Function.identity())); existingPluginMap.forEach( (pluginId, plugin) -> { if (newOrChangedPluginMap.get(pluginId) == null && !pluginId.startsWith(APP_PREFIX)) { deletedPlugins.add(plugin); } }); if (!newOrChangedPluginMap.isEmpty()) { dataService .getRepository(PLUGIN, Plugin.class) .upsertBatch(newArrayList(newOrChangedPluginMap.values())); } if (!deletedPlugins.isEmpty()) { dataService.delete(PLUGIN, deletedPlugins.stream()); } } PluginPopulator(DataService dataService, PluginFactory pluginFactory); }
PluginPopulator { public void populate(ApplicationContext ctx) { Map<String, Plugin> newOrChangedPluginMap = getPlugins(ctx); List<Plugin> deletedPlugins = new ArrayList<>(); Map<String, Plugin> existingPluginMap = dataService .findAll(PLUGIN, Plugin.class) .collect(toMap(Plugin::getId, Function.identity())); existingPluginMap.forEach( (pluginId, plugin) -> { if (newOrChangedPluginMap.get(pluginId) == null && !pluginId.startsWith(APP_PREFIX)) { deletedPlugins.add(plugin); } }); if (!newOrChangedPluginMap.isEmpty()) { dataService .getRepository(PLUGIN, Plugin.class) .upsertBatch(newArrayList(newOrChangedPluginMap.values())); } if (!deletedPlugins.isEmpty()) { dataService.delete(PLUGIN, deletedPlugins.stream()); } } PluginPopulator(DataService dataService, PluginFactory pluginFactory); void populate(ApplicationContext ctx); }
PluginPopulator { public void populate(ApplicationContext ctx) { Map<String, Plugin> newOrChangedPluginMap = getPlugins(ctx); List<Plugin> deletedPlugins = new ArrayList<>(); Map<String, Plugin> existingPluginMap = dataService .findAll(PLUGIN, Plugin.class) .collect(toMap(Plugin::getId, Function.identity())); existingPluginMap.forEach( (pluginId, plugin) -> { if (newOrChangedPluginMap.get(pluginId) == null && !pluginId.startsWith(APP_PREFIX)) { deletedPlugins.add(plugin); } }); if (!newOrChangedPluginMap.isEmpty()) { dataService .getRepository(PLUGIN, Plugin.class) .upsertBatch(newArrayList(newOrChangedPluginMap.values())); } if (!deletedPlugins.isEmpty()) { dataService.delete(PLUGIN, deletedPlugins.stream()); } } PluginPopulator(DataService dataService, PluginFactory pluginFactory); void populate(ApplicationContext ctx); static final String APP_PREFIX; }