@Test public void testWrongNotifyFeatureUpdate() { initService(true); ExperimentalFeatureImpl feature = new ExperimentalFeatureImpl(FEATURE_1, true); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); feature = new ExperimentalFeatureImpl(WRONG_FEATURE_ID, true); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); }
@Override public void store(final ExperimentalFeatureImpl feature) { List<ExperimentalFeatureImpl> registeredFeatures = (List<ExperimentalFeatureImpl>) getFeatures(); Optional<ExperimentalFeatureImpl> optional = registeredFeatures.stream() .filter(registeredFeature -> registeredFeature.getFeatureId().equals(feature.getFeatureId())) .findAny(); if (optional.isPresent()) { ExperimentalFeatureImpl registeredFeature = optional.get(); registeredFeature.setEnabled(feature.isEnabled()); doStoreFeatures(registeredFeatures, () -> this.maybeNotifyFeatureUpdate(feature)); } }
private boolean syncLoadedFeatures(final List<ExperimentalFeatureImpl> registeredFeatures) { final Collection<ExperimentalFeatureDefinition> expectedDefinitions = getSupportedDefinitions(); List<String> registryFeatureIds = registeredFeatures.stream().map(ExperimentalFeatureImpl::getFeatureId).collect(Collectors.toList()); List<String> expectedFeatureIds = expectedDefinitions.stream().map(ExperimentalFeatureDefinition::getId).collect(Collectors.toList()); List<String> missingFeatures = expectedFeatureIds.stream() .filter(expectedFeatureId -> !registryFeatureIds.contains(expectedFeatureId)) .collect(Collectors.toList()); boolean requiresSync = false; if (!missingFeatures.isEmpty()) { requiresSync = true; missingFeatures.stream() .forEach(expectedFeatureId -> registeredFeatures.add(new ExperimentalFeatureImpl(expectedFeatureId, false))); } List<ExperimentalFeatureImpl> extraFeatures = registryFeatureIds.stream() .filter(registeredFeatureId -> !expectedFeatureIds.contains(registeredFeatureId)) .map(registryFeatureId -> registeredFeatures.stream().filter(experimentalFeature -> experimentalFeature.getFeatureId().equals(registryFeatureId)).findAny().orElse(null)) .filter(Objects::nonNull) .collect(Collectors.toList()); if (!extraFeatures.isEmpty()) { requiresSync = true; registeredFeatures.removeAll(extraFeatures); } return requiresSync; }
@Override public void updateExperimentalFeature(String featureId, boolean enabled) { if (isExperimentalEnabled()) { Optional<ExperimentalFeature> optional = session.getFeaturesRegistry().getFeature(featureId); if (optional.isPresent()) { ExperimentalFeatureImpl feature = (ExperimentalFeatureImpl) optional.get(); if (feature.isEnabled() != enabled) { feature.setEnabled(enabled); event.fire(new NonPortableExperimentalFeatureModifiedEvent(feature)); } } } }
protected List<ExperimentalFeatureImpl> readFeatures() { final Path fsPath = fileSystem.getPath(getStoragePath()); final List<ExperimentalFeatureImpl> registeredFeatures = new ArrayList<>(); boolean existsOnVFS = true; if (ioService.exists(fsPath)) { try (InputStream in = ioService.newInputStream(fsPath)) { Properties properties = new Properties(); properties.load(in); properties.entrySet().stream() .map(entry -> new ExperimentalFeatureImpl((String) entry.getKey(), Boolean.valueOf((String) entry.getValue()))) .forEach(registeredFeatures::add); } catch (Exception ex) { log().warn("Impossible to load registry", ex); } } else { existsOnVFS = false; } boolean requiresVFSSync = syncLoadedFeatures(registeredFeatures); if (!existsOnVFS || requiresVFSSync) { storeFeatures(registeredFeatures); } return registeredFeatures; }
public void doStoreFeatures(Collection<ExperimentalFeatureImpl> features, Runnable callback) { final String path = getStoragePath(); final Path fsPath = fileSystem.getPath(path); Properties properties = new Properties(); features.stream() .filter(experimentalFeature -> defRegistry.getFeatureById(experimentalFeature.getFeatureId()) != null) .forEach(feature -> properties.put(feature.getFeatureId(), String.valueOf(feature.isEnabled()))); try (OutputStream out = ioService.newOutputStream(fsPath)) { ioService.startBatch(fileSystem); properties.store(out, COMMENTS); if (callback != null) { callback.run(); } } catch (Exception ex) { log().warn("Impossible to write experimental features registry on '{}': {}", path, ex); } finally { ioService.endBatch(); } }
private void testLoad(boolean enableAll) { if (!enableAll) { features.forEach(feature -> feature.setEnabled(true)); } group.init(GROUP_KEY, new ArrayList<>(features), callback); checkClear(); verify(view).setLabel(GROUP_KEY); checkSetEnableAllLabel(enableAll, 1); }
@Override public Optional<ExperimentalFeature> getFeature(String featureId) { return features.stream() .filter(feature -> feature.getFeatureId().equals(featureId)) .map(feature -> (ExperimentalFeature) feature) .findAny(); }
protected List<ExperimentalFeatureImpl> readFeatures() { final Path fsPath = fileSystem.getPath(getStoragePath()); final List<ExperimentalFeatureImpl> registeredFeatures = new ArrayList<>(); boolean existsOnVFS = true; if (ioService.exists(fsPath)) { try (InputStream in = ioService.newInputStream(fsPath)) { Properties properties = new Properties(); properties.load(in); properties.entrySet().stream() .map(entry -> new ExperimentalFeatureImpl((String) entry.getKey(), Boolean.valueOf((String) entry.getValue()))) .forEach(registeredFeatures::add); } catch (Exception ex) { log().warn("Impossible to load registry", ex); } } else { existsOnVFS = false; } boolean requiresVFSSync = syncLoadedFeatures(registeredFeatures); if (!existsOnVFS || requiresVFSSync) { storeFeatures(registeredFeatures); } return registeredFeatures; }
@Override public void updateExperimentalFeature(String featureId, boolean enabled) { if (isExperimentalEnabled()) { Optional<ExperimentalFeature> optional = session.getFeaturesRegistry().getFeature(featureId); if (optional.isPresent()) { ExperimentalFeatureImpl feature = (ExperimentalFeatureImpl) optional.get(); if (feature.isEnabled() != enabled) { feature.setEnabled(enabled); event.fire(new NonPortableExperimentalFeatureModifiedEvent(feature)); } } } }
public void doStoreFeatures(Collection<ExperimentalFeatureImpl> features, Runnable callback) { final String path = getStoragePath(); final Path fsPath = fileSystem.getPath(path); Properties properties = new Properties(); features.stream() .filter(experimentalFeature -> defRegistry.getFeatureById(experimentalFeature.getFeatureId()) != null) .forEach(feature -> properties.put(feature.getFeatureId(), String.valueOf(feature.isEnabled()))); try (OutputStream out = ioService.newOutputStream(fsPath)) { ioService.startBatch(fileSystem); properties.store(out, COMMENTS); if (callback != null) { callback.run(); } } catch (Exception ex) { log().warn("Impossible to write experimental features registry on '{}': {}", path, ex); } finally { ioService.endBatch(); } }
private boolean syncLoadedFeatures(final List<ExperimentalFeatureImpl> registeredFeatures) { final Collection<ExperimentalFeatureDefinition> expectedDefinitions = getSupportedDefinitions(); List<String> registryFeatureIds = registeredFeatures.stream().map(ExperimentalFeatureImpl::getFeatureId).collect(Collectors.toList()); List<String> expectedFeatureIds = expectedDefinitions.stream().map(ExperimentalFeatureDefinition::getId).collect(Collectors.toList()); List<String> missingFeatures = expectedFeatureIds.stream() .filter(expectedFeatureId -> !registryFeatureIds.contains(expectedFeatureId)) .collect(Collectors.toList()); boolean requiresSync = false; if (!missingFeatures.isEmpty()) { requiresSync = true; missingFeatures.stream() .forEach(expectedFeatureId -> registeredFeatures.add(new ExperimentalFeatureImpl(expectedFeatureId, false))); } List<ExperimentalFeatureImpl> extraFeatures = registryFeatureIds.stream() .filter(registeredFeatureId -> !expectedFeatureIds.contains(registeredFeatureId)) .map(registryFeatureId -> registeredFeatures.stream().filter(experimentalFeature -> experimentalFeature.getFeatureId().equals(registryFeatureId)).findAny().orElse(null)) .filter(Objects::nonNull) .collect(Collectors.toList()); if (!extraFeatures.isEmpty()) { requiresSync = true; registeredFeatures.removeAll(extraFeatures); } return requiresSync; }
private void testLoad(boolean enableAll) { if (!enableAll) { features.forEach(feature -> feature.setEnabled(true)); } group.init(GROUP_KEY, new ArrayList<>(features), callback); checkClear(); verify(view).setLabel(GROUP_KEY); checkSetEnableAllLabel(enableAll, 1); }
@Override public Optional<ExperimentalFeature> getFeature(String featureId) { return features.stream() .filter(feature -> feature.getFeatureId().equals(featureId)) .map(feature -> (ExperimentalFeature) feature) .findAny(); }
@Test public void testWrongNotifyFeatureUpdate() { initService(true); ExperimentalFeatureImpl feature = new ExperimentalFeatureImpl(FEATURE_1, true); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); feature = new ExperimentalFeatureImpl(WRONG_FEATURE_ID, true); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); }
private void initService(boolean experimentalEnabled) { List<ExperimentalFeatureImpl> features = new ArrayList<>(); features.add(new ExperimentalFeatureImpl(FEATURE_1, true)); features.add(new ExperimentalFeatureImpl(FEATURE_2, true)); features.add(new ExperimentalFeatureImpl(FEATURE_3, false)); registry = spy(new ExperimentalFeaturesRegistryImpl(features)); when(backendService.getExperimentalFeaturesSession()).thenReturn(new ExperimentalFeaturesSessionImpl(experimentalEnabled, registry)); service.loadRegistry(); } }
@Override public void store(final ExperimentalFeatureImpl feature) { List<ExperimentalFeatureImpl> registeredFeatures = (List<ExperimentalFeatureImpl>) getFeatures(); Optional<ExperimentalFeatureImpl> optional = registeredFeatures.stream() .filter(registeredFeature -> registeredFeature.getFeatureId().equals(feature.getFeatureId())) .findAny(); if (optional.isPresent()) { ExperimentalFeatureImpl registeredFeature = optional.get(); registeredFeature.setEnabled(feature.isEnabled()); doStoreFeatures(registeredFeatures, () -> this.maybeNotifyFeatureUpdate(feature)); } }
@Test public void testNotifyFeatureUpdateWhenExperimentalDisabled() { initService(false); ExperimentalFeatureImpl feature = new ExperimentalFeatureImpl(FEATURE_1, false); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); }
private void initService(boolean experimentalEnabled) { List<ExperimentalFeatureImpl> features = new ArrayList<>(); features.add(new ExperimentalFeatureImpl(FEATURE_1, true)); features.add(new ExperimentalFeatureImpl(FEATURE_2, true)); features.add(new ExperimentalFeatureImpl(FEATURE_3, false)); registry = spy(new ExperimentalFeaturesRegistryImpl(features)); when(backendService.getExperimentalFeaturesSession()).thenReturn(new ExperimentalFeaturesSessionImpl(experimentalEnabled, registry)); service.loadRegistry(); } }
@Test public void testNotifyFeatureUpdateWhenExperimentalDisabled() { initService(false); ExperimentalFeatureImpl feature = new ExperimentalFeatureImpl(FEATURE_1, false); service.updateExperimentalFeature(feature.getFeatureId(), feature.isEnabled()); verify(event, never()).fire(any()); }