use of com.google.common.base.Predicate in project buck by facebook.
the class AuditRulesCommand method runWithoutHelp.
@Override
public int runWithoutHelp(CommandRunnerParams params) throws IOException, InterruptedException {
ProjectFilesystem projectFilesystem = params.getCell().getFilesystem();
try (ProjectBuildFileParser parser = params.getCell().createBuildFileParser(new ConstructorArgMarshaller(new DefaultTypeCoercerFactory(params.getObjectMapper())), params.getConsole(), params.getBuckEventBus(), /* ignoreBuckAutodepsFiles */
false)) {
PrintStream out = params.getConsole().getStdOut();
for (String pathToBuildFile : getArguments()) {
if (!json) {
// Print a comment with the path to the build file.
out.printf("# %s\n\n", pathToBuildFile);
}
// Resolve the path specified by the user.
Path path = Paths.get(pathToBuildFile);
if (!path.isAbsolute()) {
Path root = projectFilesystem.getRootPath();
path = root.resolve(path);
}
// Parse the rules from the build file.
List<Map<String, Object>> rawRules;
try {
rawRules = parser.getAll(path);
} catch (BuildFileParseException e) {
throw new HumanReadableException(e);
}
// Format and print the rules from the raw data, filtered by type.
final ImmutableSet<String> types = getTypes();
Predicate<String> includeType = type -> types.isEmpty() || types.contains(type);
printRulesToStdout(params, rawRules, includeType);
}
} catch (BuildFileParseException e) {
throw new HumanReadableException("Unable to create parser");
}
return 0;
}
use of com.google.common.base.Predicate in project buck by facebook.
the class AuditRulesCommand method printRulesToStdout.
private void printRulesToStdout(CommandRunnerParams params, List<Map<String, Object>> rawRules, final Predicate<String> includeType) throws IOException {
Iterable<Map<String, Object>> filteredRules = FluentIterable.from(rawRules).filter(rawRule -> {
String type = (String) rawRule.get(BuckPyFunction.TYPE_PROPERTY_NAME);
return includeType.apply(type);
});
PrintStream stdOut = params.getConsole().getStdOut();
if (json) {
Map<String, Object> rulesKeyedByName = new HashMap<>();
for (Map<String, Object> rawRule : filteredRules) {
String name = (String) rawRule.get("name");
Preconditions.checkNotNull(name);
rulesKeyedByName.put(name, Maps.filterValues(rawRule, v -> shouldInclude(v)));
}
// We create a new JsonGenerator that does not close the stream.
ObjectMapper mapper = params.getObjectMapper();
JsonFactory factory = mapper.getFactory();
try (JsonGenerator generator = factory.createGenerator(stdOut).disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET).useDefaultPrettyPrinter()) {
mapper.writeValue(generator, rulesKeyedByName);
}
stdOut.print('\n');
} else {
for (Map<String, Object> rawRule : filteredRules) {
printRuleAsPythonToStdout(stdOut, rawRule);
}
}
}
use of com.google.common.base.Predicate in project killbill by killbill.
the class TestUsage method testRecordUsage.
@Test(groups = "slow", description = "Can record and retrieve usage data")
public void testRecordUsage() throws Exception {
final Account accountJson = createAccountWithDefaultPaymentMethod();
final Subscription base = new Subscription();
base.setAccountId(accountJson.getAccountId());
base.setProductName("Pistol");
base.setProductCategory(ProductCategory.BASE);
base.setBillingPeriod(BillingPeriod.MONTHLY);
base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Subscription addOn = new Subscription();
addOn.setAccountId(accountJson.getAccountId());
addOn.setProductName("Bullets");
addOn.setProductCategory(ProductCategory.ADD_ON);
addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Bundle bundle = killBillClient.createSubscriptionWithAddOns(ImmutableList.<Subscription>of(base, addOn), null, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {
@Override
public boolean apply(final Subscription input) {
return ProductCategory.ADD_ON.equals(input.getProductCategory());
}
}).getSubscriptionId();
clock.addDays(1);
final UsageRecord usageRecord1 = new UsageRecord();
usageRecord1.setAmount(10L);
usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
final UsageRecord usageRecord2 = new UsageRecord();
usageRecord2.setAmount(5L);
usageRecord2.setRecordDate(clock.getUTCToday());
final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
unitUsageRecord.setUnitType("bullets");
unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1, usageRecord2));
final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
usage.setSubscriptionId(addOnSubscriptionId);
usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
final RolledUpUsage retrievedUsage1 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday());
Assert.assertEquals(retrievedUsage1.getSubscriptionId(), usage.getSubscriptionId());
Assert.assertEquals(retrievedUsage1.getRolledUpUnits().size(), 1);
Assert.assertEquals(retrievedUsage1.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
// endDate is excluded
Assert.assertEquals((long) retrievedUsage1.getRolledUpUnits().get(0).getAmount(), 10);
final RolledUpUsage retrievedUsage2 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday().minusDays(1), clock.getUTCToday().plusDays(1));
Assert.assertEquals(retrievedUsage2.getSubscriptionId(), usage.getSubscriptionId());
Assert.assertEquals(retrievedUsage2.getRolledUpUnits().size(), 1);
Assert.assertEquals(retrievedUsage2.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
Assert.assertEquals((long) retrievedUsage2.getRolledUpUnits().get(0).getAmount(), 15);
final RolledUpUsage retrievedUsage3 = killBillClient.getRolledUpUsage(addOnSubscriptionId, unitUsageRecord.getUnitType(), clock.getUTCToday(), clock.getUTCToday().plusDays(1));
Assert.assertEquals(retrievedUsage3.getSubscriptionId(), usage.getSubscriptionId());
Assert.assertEquals(retrievedUsage3.getRolledUpUnits().size(), 1);
Assert.assertEquals(retrievedUsage3.getRolledUpUnits().get(0).getUnitType(), unitUsageRecord.getUnitType());
Assert.assertEquals((long) retrievedUsage3.getRolledUpUnits().get(0).getAmount(), 5);
final RolledUpUsage retrievedUsage4 = killBillClient.getRolledUpUsage(addOnSubscriptionId, null, clock.getUTCToday(), clock.getUTCToday().plusDays(1));
Assert.assertEquals(retrievedUsage4.getSubscriptionId(), usage.getSubscriptionId());
Assert.assertEquals(retrievedUsage4.getRolledUpUnits().size(), 1);
Assert.assertEquals(retrievedUsage4.getRolledUpUnits().get(0).getUnitType(), "bullets");
Assert.assertEquals((long) retrievedUsage4.getRolledUpUnits().get(0).getAmount(), 5);
}
use of com.google.common.base.Predicate in project killbill by killbill.
the class TestUsage method testRecordUsageTrackingIdExists.
@Test(groups = "slow", description = "Test tracking ID already exists")
public void testRecordUsageTrackingIdExists() throws Exception {
final Account accountJson = createAccountWithDefaultPaymentMethod();
final Subscription base = new Subscription();
base.setAccountId(accountJson.getAccountId());
base.setProductName("Pistol");
base.setProductCategory(ProductCategory.BASE);
base.setBillingPeriod(BillingPeriod.MONTHLY);
base.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Subscription addOn = new Subscription();
addOn.setAccountId(accountJson.getAccountId());
addOn.setProductName("Bullets");
addOn.setProductCategory(ProductCategory.ADD_ON);
addOn.setBillingPeriod(BillingPeriod.NO_BILLING_PERIOD);
addOn.setPriceList(PriceListSet.DEFAULT_PRICELIST_NAME);
final Bundle bundle = killBillClient.createSubscriptionWithAddOns(ImmutableList.<Subscription>of(base, addOn), null, DEFAULT_WAIT_COMPLETION_TIMEOUT_SEC, createdBy, reason, comment);
final UUID addOnSubscriptionId = Iterables.<Subscription>find(bundle.getSubscriptions(), new Predicate<Subscription>() {
@Override
public boolean apply(final Subscription input) {
return ProductCategory.ADD_ON.equals(input.getProductCategory());
}
}).getSubscriptionId();
final UsageRecord usageRecord1 = new UsageRecord();
usageRecord1.setAmount(10L);
usageRecord1.setRecordDate(clock.getUTCToday().minusDays(1));
final UnitUsageRecord unitUsageRecord = new UnitUsageRecord();
unitUsageRecord.setUnitType("bullets");
unitUsageRecord.setUsageRecords(ImmutableList.<UsageRecord>of(usageRecord1));
final SubscriptionUsageRecord usage = new SubscriptionUsageRecord();
usage.setSubscriptionId(addOnSubscriptionId);
usage.setTrackingId(UUID.randomUUID().toString());
usage.setUnitUsageRecords(ImmutableList.<UnitUsageRecord>of(unitUsageRecord));
killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
try {
killBillClient.createSubscriptionUsageRecord(usage, createdBy, reason, comment);
Assert.fail();
} catch (final KillBillClientException e) {
Assert.assertEquals(e.getBillingException().getCode(), (Integer) ErrorCode.USAGE_RECORD_TRACKING_ID_ALREADY_EXISTS.getCode());
}
}
use of com.google.common.base.Predicate in project cassandra-mesos-deprecated by mesosphere.
the class CassandraClusterTest method removeExecutor_cleansAllTasksAndExecutorInfo.
@Test
public void removeExecutor_cleansAllTasksAndExecutorInfo() throws Exception {
final InMemoryState state = new InMemoryState();
final Clock clock = new SystemClock();
final ExecutorCounter execCounter = new ExecutorCounter(state, 0);
final PersistedCassandraClusterState clusterState = new PersistedCassandraClusterState(state);
final PersistedCassandraClusterHealthCheckHistory healthCheckHistory = new PersistedCassandraClusterHealthCheckHistory(state);
final PersistedCassandraClusterJobs jobsState = new PersistedCassandraClusterJobs(state);
final PersistedCassandraFrameworkConfiguration configuration = new PersistedCassandraFrameworkConfiguration(state, "cassandra.unit-test", 15, 15, "2.1.4", 1.0, 64, 64, 32, 1, 1, "*", "./backup", ".", true, false, "rack0", "dc0", Collections.<CassandraFrameworkProtos.ExternalDc>emptyList(), "cassandra.unit-test");
final CassandraCluster cluster1 = new CassandraCluster(clock, "http://localhost:1234/", execCounter, clusterState, healthCheckHistory, jobsState, configuration, new SeedManager(configuration, new ObjectMapper(), clock));
final Offer offer1 = Offer.newBuilder().setId(OfferID.newBuilder().setValue("1")).setFrameworkId(FrameworkID.newBuilder().setValue("fw1")).setSlaveId(SlaveID.newBuilder().setValue("slave1")).setHostname("localhost").addAllResources(newArrayList(cpu(4, "*"), mem(4096, "*"), disk(20 * 1024, "*"), ports(newArrayList(7000L, 7001L, 7199L, 9042L, 9160L, 10000L), "*"))).build();
final TasksForOffer tasksForOffer = cluster1.getTasksForOffer(offer1);
assertThat(tasksForOffer).isNotNull();
//noinspection ConstantConditions
assertThat(tasksForOffer.getExecutor()).isNotNull();
final List<CassandraNode> nodes1 = clusterState.nodes();
assertThat(nodes1).hasSize(1);
final CassandraNode node1 = nodes1.get(0);
assertThat(node1.getTasksList()).hasSize(1);
assertThat(node1.hasCassandraNodeExecutor()).isTrue();
final CassandraNodeExecutor executor1 = node1.getCassandraNodeExecutor();
assertThat(executor1.getDownloadList()).hasSize(3);
final List<FileDownload> from1 = newArrayList(from(executor1.getDownloadList()).filter(new Predicate<FileDownload>() {
@Override
public boolean apply(final FileDownload input) {
return input.getDownloadUrl().startsWith("http://localhost:1234/");
}
}));
assertThat(from1).hasSize(3);
cluster1.removeExecutor("cassandra.unit-test.node.0.executor");
final List<CassandraNode> afterRemove1 = clusterState.nodes();
assertThat(afterRemove1).hasSize(1);
final CassandraNode nodeAfterRemove1 = clusterState.nodes().get(0);
assertThat(nodeAfterRemove1.getTasksList()).isEmpty();
assertThat(nodeAfterRemove1.hasCassandraNodeExecutor()).isFalse();
final CassandraCluster cluster2 = new CassandraCluster(clock, "http://localhost:4321/", execCounter, clusterState, healthCheckHistory, jobsState, configuration, new SeedManager(configuration, new ObjectMapper(), clock));
final Offer offer2 = Offer.newBuilder(offer1).setId(OfferID.newBuilder().setValue("2")).build();
final TasksForOffer tasksForOffer2 = cluster2.getTasksForOffer(offer2);
assertThat(tasksForOffer2).isNotNull();
//noinspection ConstantConditions
assertThat(tasksForOffer2.getExecutor()).isNotNull();
final List<CassandraNode> nodes2 = clusterState.nodes();
assertThat(nodes2).hasSize(1);
final CassandraNode node2 = nodes2.get(0);
assertThat(node2.getTasksList()).hasSize(1);
assertThat(node2.hasCassandraNodeExecutor()).isTrue();
final CassandraNodeExecutor executor2 = node2.getCassandraNodeExecutor();
assertThat(executor2.getDownloadList()).hasSize(3);
final List<FileDownload> from2 = newArrayList(from(executor2.getDownloadList()).filter(new Predicate<FileDownload>() {
@Override
public boolean apply(final FileDownload input) {
return input.getDownloadUrl().startsWith("http://localhost:4321/");
}
}));
assertThat(from2).hasSize(3);
}
Aggregations