@Test public void testGetExperimentWithLabel() throws IOException { Application.Name appName = Application.Name.valueOf("testApp"); Experiment.Label label = Experiment.Label.valueOf("testLabel"); Experiment.ID id = Experiment.ID.newInstance(); List queryResult = mock(List.class); when(transaction.select(anyString(), eq(appName.toString()), eq(label.toString()), eq(Experiment.State.DELETED.toString()))).thenReturn(queryResult); when(queryResult.isEmpty()).thenReturn(true); Experiment result = repository.getExperiment(appName, label); assertThat(result, is(nullValue())); when(queryResult.isEmpty()).thenReturn(false); when(queryResult.size()).thenReturn(2); given(transaction.select(anyString(), eq(appName.toString()), eq(label.toString()), eq(Experiment.State.DELETED.toString()))).willReturn(queryResult); BDDCatchException.when(repository).getExperiment(appName, label); BDDCatchException.then(caughtException()) .isInstanceOf(RepositoryException.class) .hasMessage("Could not retrieve experiment \"" + label + "\"") .hasRootCauseExactlyInstanceOf(IllegalStateException.class); when(queryResult.size()).thenReturn(1); Map map = mock(Map.class); when(queryResult.get(0)).thenReturn(map); Date date = new Date(); ByteArrayOutputStream ba = new ByteArrayOutputStream(16); DataOutputStream da = new DataOutputStream(ba); da.writeLong(id.getRawID().getMostSignificantBits()); da.writeLong(id.getRawID().getLeastSignificantBits()); when(map.get("id")).thenReturn(ba.toByteArray()); when(map.get("label")).thenReturn("test"); when(map.get("end_time")).thenReturn(date); when(map.get("state")).thenReturn("DRAFT"); when(map.get("modification_time")).thenReturn(date); when(map.get("start_time")).thenReturn(date); when(map.get("creation_time")).thenReturn(date); when(map.get("app_name")).thenReturn("app"); when(map.get("sampling_percent")).thenReturn(1.0); result = repository.getExperiment(appName, label); assertThat(result.getID(), is(id)); assertThat(result.getLabel().toString(), is("test")); assertThat(result.getApplicationName().toString(), is("app")); assertThat(result.getSamplingPercent(), is(1.0)); assertThat(result.getState(), is(Experiment.State.DRAFT)); assertThat(result.getStartTime(), is(date)); assertThat(result.getEndTime(), is(date)); assertThat(result.getCreationTime(), is(date)); assertThat(result.getModificationTime(), is(date)); }
@Override public Experiment getExperiment(Experiment.ID experimentID) throws RepositoryException { final String SQL = "select * from experiment " + "where id = ? and state != ?"; try { List results = newTransaction().select( SQL, experimentID, State.DELETED.toString()); if (results.isEmpty()) { return null; } if (results.size() > 1) { throw new IllegalStateException( "More than one experiment for ID \"" + experimentID + "\" was found (found " + results.size() + ")"); } Map experimentMap = (Map) results.get(0); return Experiment.withID(experimentID) .withLabel(Experiment.Label.valueOf((String) experimentMap.get("label"))) .withEndTime((Date) experimentMap.get("end_time")) .withDescription((String) experimentMap.get("description")) .withState(State.toExperimentState((String) experimentMap.get("state"))) .withModificationTime((Date) experimentMap.get("modification_time")) .withStartTime((Date) experimentMap.get("start_time")) .withCreationTime((Date) experimentMap.get("creation_time")) .withApplicationName(Application.Name.valueOf((String) experimentMap.get("app_name"))) .withSamplingPercent((Double) experimentMap.get("sampling_percent")) .build(); } catch (WasabiException e) { throw e; } catch (Exception e) { throw new RepositoryException("Could not retrieve experiment \"" + experimentID + "\"", e); } }
DatabaseExperimentRepository implements ExperimentRepository { @Override public Experiment getExperiment(Experiment.ID experimentID) throws RepositoryException { final String SQL = "select * from experiment " + "where id = ? and state != ?"; try { List results = newTransaction().select( SQL, experimentID, State.DELETED.toString()); if (results.isEmpty()) { return null; } if (results.size() > 1) { throw new IllegalStateException( "More than one experiment for ID \"" + experimentID + "\" was found (found " + results.size() + ")"); } Map experimentMap = (Map) results.get(0); return Experiment.withID(experimentID) .withLabel(Experiment.Label.valueOf((String) experimentMap.get("label"))) .withEndTime((Date) experimentMap.get("end_time")) .withDescription((String) experimentMap.get("description")) .withState(State.toExperimentState((String) experimentMap.get("state"))) .withModificationTime((Date) experimentMap.get("modification_time")) .withStartTime((Date) experimentMap.get("start_time")) .withCreationTime((Date) experimentMap.get("creation_time")) .withApplicationName(Application.Name.valueOf((String) experimentMap.get("app_name"))) .withSamplingPercent((Double) experimentMap.get("sampling_percent")) .build(); } catch (WasabiException e) { throw e; } catch (Exception e) { throw new RepositoryException("Could not retrieve experiment \"" + experimentID + "\"", e); } } }
DatabaseExperimentRepository implements ExperimentRepository { @Override public Experiment getExperiment(Experiment.ID experimentID) throws RepositoryException { final String SQL = "select * from experiment " + "where id = ? and state != ?"; try { List results = newTransaction().select( SQL, experimentID, State.DELETED.toString()); if (results.isEmpty()) { return null; } if (results.size() > 1) { throw new IllegalStateException( "More than one experiment for ID \"" + experimentID + "\" was found (found " + results.size() + ")"); } Map experimentMap = (Map) results.get(0); return Experiment.withID(experimentID) .withLabel(Experiment.Label.valueOf((String) experimentMap.get("label"))) .withEndTime((Date) experimentMap.get("end_time")) .withDescription((String) experimentMap.get("description")) .withState(State.toExperimentState((String) experimentMap.get("state"))) .withModificationTime((Date) experimentMap.get("modification_time")) .withStartTime((Date) experimentMap.get("start_time")) .withCreationTime((Date) experimentMap.get("creation_time")) .withApplicationName(Application.Name.valueOf((String) experimentMap.get("app_name"))) .withSamplingPercent((Double) experimentMap.get("sampling_percent")) .build(); } catch (WasabiException e) { throw e; } catch (Exception e) { throw new RepositoryException("Could not retrieve experiment \"" + experimentID + "\"", e); } } @Inject  DatabaseExperimentRepository(TransactionFactory transactionFactory, ExperimentValidator validator,
                                        Flyway flyway); }
DatabaseExperimentRepository implements ExperimentRepository { @Override public Experiment getExperiment(Experiment.ID experimentID) throws RepositoryException { final String SQL = "select * from experiment " + "where id = ? and state != ?"; try { List results = newTransaction().select( SQL, experimentID, State.DELETED.toString()); if (results.isEmpty()) { return null; } if (results.size() > 1) { throw new IllegalStateException( "More than one experiment for ID \"" + experimentID + "\" was found (found " + results.size() + ")"); } Map experimentMap = (Map) results.get(0); return Experiment.withID(experimentID) .withLabel(Experiment.Label.valueOf((String) experimentMap.get("label"))) .withEndTime((Date) experimentMap.get("end_time")) .withDescription((String) experimentMap.get("description")) .withState(State.toExperimentState((String) experimentMap.get("state"))) .withModificationTime((Date) experimentMap.get("modification_time")) .withStartTime((Date) experimentMap.get("start_time")) .withCreationTime((Date) experimentMap.get("creation_time")) .withApplicationName(Application.Name.valueOf((String) experimentMap.get("app_name"))) .withSamplingPercent((Double) experimentMap.get("sampling_percent")) .build(); } catch (WasabiException e) { throw e; } catch (Exception e) { throw new RepositoryException("Could not retrieve experiment \"" + experimentID + "\"", e); } } @Inject  DatabaseExperimentRepository(TransactionFactory transactionFactory, ExperimentValidator validator,
                                        Flyway flyway); @Override Experiment getExperiment(Experiment.ID experimentID); @Override Map<Application.Name, List<Experiment>> getExperimentsForApps(Collection<Application.Name> appNames); @Override Experiment getExperiment(Application.Name appName,
                                    Experiment.Label experimentLabel); @Override List<Experiment.ID> getExperiments(); @Override ExperimentList getExperiments(Collection<Experiment.ID> experimentIDs); @Override Map<Experiment.ID, Experiment> getExperimentsMap(Collection<Experiment.ID> experimentIDs); @Override Table<Experiment.ID, Experiment.Label, Experiment> getExperimentList(Application.Name appName); @Override List<Application.Name> getApplicationsList(); @Override Experiment.ID createExperiment(NewExperiment newExperiment); @Override Experiment updateExperiment(Experiment experiment); @Override Experiment updateExperimentState(Experiment experiment, State state); @Override Bucket getBucket(Experiment.ID experimentID,
                            Bucket.Label bucketLabel); @Override void createBucket(Bucket newBucket); @Override Bucket updateBucket(Bucket bucket); @Override Bucket updateBucketAllocationPercentage(Bucket bucket, Double desiredAllocationPercentage); @Override Bucket updateBucketState(Bucket bucket, Bucket.State desiredState); @Override BucketList updateBucketBatch(Experiment.ID experimentID, BucketList bucketList); @Override void deleteBucket(Experiment.ID experimentID,
                             Bucket.Label bucketLabel); @Override void logBucketChanges(Experiment.ID experimentID, Bucket.Label bucketLabel,
                                 List<Bucket.BucketAuditInfo> changeList); @Override void logExperimentChanges(Experiment.ID experimentID, List<Experiment.ExperimentAuditInfo> changeList); @Override List<Experiment> getExperiments(Application.Name appName); @Override void deleteExperiment(NewExperiment newExperiment); @Override void createIndicesForNewExperiment(NewExperiment newExperiment); @Override HashMap<Experiment.ID, BucketList> getBucketList(Collection<Experiment.ID> experimentIDCollection); @Override BucketList getBucketList(Experiment.ID experimentID); @Override BucketList getBuckets(Experiment.ID experimentID, boolean checkExperiment); @Override Statement createApplication(Application.Name applicationName); @Override Map<Application.Name, Set<String>> getTagListForApplications(Collection<Application.Name> applicationNames); @Override void updateStateIndex(Experiment experiment); }
DatabaseExperimentRepository implements ExperimentRepository { @Override public Experiment getExperiment(Experiment.ID experimentID) throws RepositoryException { final String SQL = "select * from experiment " + "where id = ? and state != ?"; try { List results = newTransaction().select( SQL, experimentID, State.DELETED.toString()); if (results.isEmpty()) { return null; } if (results.size() > 1) { throw new IllegalStateException( "More than one experiment for ID \"" + experimentID + "\" was found (found " + results.size() + ")"); } Map experimentMap = (Map) results.get(0); return Experiment.withID(experimentID) .withLabel(Experiment.Label.valueOf((String) experimentMap.get("label"))) .withEndTime((Date) experimentMap.get("end_time")) .withDescription((String) experimentMap.get("description")) .withState(State.toExperimentState((String) experimentMap.get("state"))) .withModificationTime((Date) experimentMap.get("modification_time")) .withStartTime((Date) experimentMap.get("start_time")) .withCreationTime((Date) experimentMap.get("creation_time")) .withApplicationName(Application.Name.valueOf((String) experimentMap.get("app_name"))) .withSamplingPercent((Double) experimentMap.get("sampling_percent")) .build(); } catch (WasabiException e) { throw e; } catch (Exception e) { throw new RepositoryException("Could not retrieve experiment \"" + experimentID + "\"", e); } } @Inject  DatabaseExperimentRepository(TransactionFactory transactionFactory, ExperimentValidator validator,
                                        Flyway flyway); @Override Experiment getExperiment(Experiment.ID experimentID); @Override Map<Application.Name, List<Experiment>> getExperimentsForApps(Collection<Application.Name> appNames); @Override Experiment getExperiment(Application.Name appName,
                                    Experiment.Label experimentLabel); @Override List<Experiment.ID> getExperiments(); @Override ExperimentList getExperiments(Collection<Experiment.ID> experimentIDs); @Override Map<Experiment.ID, Experiment> getExperimentsMap(Collection<Experiment.ID> experimentIDs); @Override Table<Experiment.ID, Experiment.Label, Experiment> getExperimentList(Application.Name appName); @Override List<Application.Name> getApplicationsList(); @Override Experiment.ID createExperiment(NewExperiment newExperiment); @Override Experiment updateExperiment(Experiment experiment); @Override Experiment updateExperimentState(Experiment experiment, State state); @Override Bucket getBucket(Experiment.ID experimentID,
                            Bucket.Label bucketLabel); @Override void createBucket(Bucket newBucket); @Override Bucket updateBucket(Bucket bucket); @Override Bucket updateBucketAllocationPercentage(Bucket bucket, Double desiredAllocationPercentage); @Override Bucket updateBucketState(Bucket bucket, Bucket.State desiredState); @Override BucketList updateBucketBatch(Experiment.ID experimentID, BucketList bucketList); @Override void deleteBucket(Experiment.ID experimentID,
                             Bucket.Label bucketLabel); @Override void logBucketChanges(Experiment.ID experimentID, Bucket.Label bucketLabel,
                                 List<Bucket.BucketAuditInfo> changeList); @Override void logExperimentChanges(Experiment.ID experimentID, List<Experiment.ExperimentAuditInfo> changeList); @Override List<Experiment> getExperiments(Application.Name appName); @Override void deleteExperiment(NewExperiment newExperiment); @Override void createIndicesForNewExperiment(NewExperiment newExperiment); @Override HashMap<Experiment.ID, BucketList> getBucketList(Collection<Experiment.ID> experimentIDCollection); @Override BucketList getBucketList(Experiment.ID experimentID); @Override BucketList getBuckets(Experiment.ID experimentID, boolean checkExperiment); @Override Statement createApplication(Application.Name applicationName); @Override Map<Application.Name, Set<String>> getTagListForApplications(Collection<Application.Name> applicationNames); @Override void updateStateIndex(Experiment experiment);  }