DatastoreStateRepository implements StateRepository { @Override public FeatureState getFeatureState(final Feature feature) { try { final Key key = KeyFactory.createKey(kind(), feature.name()); return createFeatureState(feature, getInsideTransaction(key)); } catch (final EntityNotFoundException ignored) { return null; } } DatastoreStateRepository(final DatastoreService datastoreService);  DatastoreStateRepository(final String kind, final DatastoreService datastoreService); @Override FeatureState getFeatureState(final Feature feature); @Override void setFeatureState(final FeatureState featureState); static final String STRATEGY_PARAMS_VALUES; static final String STRATEGY_PARAMS_NAMES; static final String STRATEGY_ID; static final String ENABLED; }
@Test public void shouldNotAddNewEntityGroupToCurrentCrossGroupTransaction() { update("F", false, null, null, null); final Transaction txn = datastoreService.beginTransaction( TransactionOptions.Builder.withXG(true)); for (int i = 0; i < MAX_ENTITY_GROUPS - 1; i++) { update("F" + i, false, null, null, txn); } update("F", false, null, null, txn); repository.getFeatureState(TestFeature.F1); txn.commit(); }
@Test public void shouldNotStartNewTransaction() { update("F1", false, null, null, null); DatastoreService spyDatastoreService = Mockito.spy(DelegateDatastoreService.getInstance(datastoreService)); repository = new DatastoreStateRepository(spyDatastoreService); repository.getFeatureState(TestFeature.F1); Mockito.verify(spyDatastoreService, Mockito.never()).beginTransaction(); }
@Test public void shouldWorkInsideRunningTransaction() { update("F1", false, null, null, null); final Transaction txn = datastoreService.beginTransaction(); update("F3", false, null, null, txn); repository.getFeatureState(TestFeature.F1); txn.commit(); }
@Test public void shouldReturnNullWhenStateDoesntExist() { final FeatureState state = repository.getFeatureState(TestFeature.F1); assertNull(state); }
@Test public void testShouldReadStateWithoutStrategyAndParameters() { update("F1", false, null, null, null); final FeatureState state = repository.getFeatureState(TestFeature.F1); assertNotNull(state); assertEquals(TestFeature.F1, state.getFeature()); assertFalse(state.isEnabled()); assertNull(state.getStrategyId()); assertEquals(0, state.getParameterNames().size()); }
@SuppressWarnings("serial") @Test public void testShouldReadStateWithStrategyAndParameters() { final Map<String, String> map = new HashMap<String, String>() { { put("param23", "foobar"); } }; update("F1", true, "myStrategy", map, null); final FeatureState state = repository.getFeatureState(TestFeature.F1); assertNotNull(state); assertEquals(TestFeature.F1, state.getFeature()); assertTrue(state.isEnabled()); assertEquals("myStrategy", state.getStrategyId()); assertEquals(1, state.getParameterNames().size()); assertEquals("foobar", state.getParameter("param23")); }