RedisStateRepository implements StateRepository { @Override public FeatureState getFeatureState(final Feature feature) { try (final Jedis jedis = jedisPool.getResource()) { final Map<String, String> redisMap = jedis.hgetAll(keyPrefix + feature.name()); if (redisMap == null || redisMap.size() == 0) { return null; } final FeatureState featureState = new FeatureState(feature); featureState.setEnabled(Boolean.valueOf(redisMap.get(ENABLED_FIELD))); featureState.setStrategyId(redisMap.get(STRATEGY_FIELD)); for (final Map.Entry<String, String> entry : redisMap.entrySet()) { final String key = entry.getKey(); if (key.startsWith(PARAMETER_PREFIX)) { featureState.setParameter(key.substring(PARAMETER_PREFIX_LENGTH), entry.getValue()); } } return featureState; } } private  RedisStateRepository(final Builder builder); @Override FeatureState getFeatureState(final Feature feature); @Override void setFeatureState(final FeatureState featureState); static final String ENABLED_FIELD; static final String STRATEGY_FIELD; static final String PARAMETER_PREFIX; static final int PARAMETER_PREFIX_LENGTH; }
@Test public void testGetFeatureStateNotExisting() { final StateRepository stateRepository = aRedisStateRepository(); final Feature feature = new NamedFeature("A_FEATURE"); stateRepository.getFeatureState(feature); final FeatureState storedFeatureState = stateRepository.getFeatureState(feature); assertNull(storedFeatureState); }
@Test public void testFormatOfExistingFeatureState() { final JedisPool jedisPool = new JedisPool(); try (final Jedis jedis = jedisPool.getResource()) { final String key = "feature-toggles:A_FEATURE"; jedis.hset(key, "enabled", "true"); jedis.hset(key, "strategy", "TIT_FOR_TAT"); jedis.hset(key, "parameter:MEANING_OF_LIFE", "42"); } final Feature feature = new NamedFeature("A_FEATURE"); final FeatureState expectedFeatureState = new FeatureState(feature, true); expectedFeatureState.setStrategyId("TIT_FOR_TAT"); expectedFeatureState.setParameter("MEANING_OF_LIFE", "42"); final FeatureState storedFeatureState = aRedisStateRepository().getFeatureState(feature); assertTrue(EqualsBuilder.reflectionEquals(expectedFeatureState, storedFeatureState, true)); }