/** * Fill a builder with attribute values from the provided {@code ListResult} instance. * Regular attribute values will be replaced with those from the given instance. * Absent optional values will not replace present values. * Collection elements and entries will be added, not replaced. * @param instance The instance from which to copy values * @return {@code this} builder for use in a chained invocation */ public final ListResult.Builder<T> createFrom(ListResult<T> instance) { Objects.requireNonNull(instance, "instance"); totalCount(instance.getTotalCount()); addAllItems(instance.getItems()); return (ListResult.Builder<T>) this; }
public ListResult<String> getResult() { return ListResult.of(tags); }
@JsonIgnore @Override default void forEach(Consumer<? super T> action) { getItems().forEach(action); }
@Test public void testDeletedIntegrationsCurator() throws IOException, ParseException { String integrationId = "intId1"; Set<String> livePodIds = new HashSet<String>( Arrays.asList("pod1", "pod2")); Map<String,RawMetrics> metrics = jsondbRM.getRawMetrics(integrationId); IntegrationMetricsSummary summary = intMH.compute(integrationId, metrics, livePodIds); dataManager.create(summary); assertThat(metrics.size()).isEqualTo(3); assertThat(dataManager.fetchAll(IntegrationMetricsSummary.class).getTotalCount()).isEqualTo(1); //Now pretend to delete the integration itself and //run the curator with no active integrations jsondbRM.curate(new HashSet<String>()); intMH.curate(new HashSet<String>()); //expect all metrics to be deleted Map<String,RawMetrics> metricsAfter = jsondbRM.getRawMetrics(integrationId); assertThat(metricsAfter.size()).isEqualTo(0); assertThat(dataManager.fetchAll(IntegrationMetricsSummary.class).getTotalCount()).isEqualTo(0); } }
@JsonIgnore @Override default Iterator<T> iterator() { return getItems().iterator(); }
@Test @SuppressWarnings("unchecked") public void overviewListShouldAugmentWithConnectionUsageNoIntegrations() { ListResult<Connection> connectionResult = new ListResult.Builder<Connection>().addItem(c1, c2, c3).build(); when(dataManager.fetchAll(eq(Connection.class), any())).thenReturn(connectionResult); when(dataManager.fetchAll(Integration.class)).thenReturn(new ListResult.Builder<Integration>().build()); UriInfo uriInfo = mock(UriInfo.class); MultivaluedMap<String, String> params = mock(MultivaluedMap.class); when(uriInfo.getQueryParameters()).thenReturn(params); ListResult<ConnectionOverview> overviewResult = handler.list(uriInfo); assertNotNull(overviewResult); assertThat(overviewResult.getTotalCount()).isPositive(); List<ConnectionOverview> items = overviewResult.getItems(); for (int i = 0; i < items.size(); ++i) { ConnectionOverview overview = items.get(i); OptionalInt uses = overview.getUses(); assertThat(uses.isPresent()).isTrue(); assertThat(uses.getAsInt()).isZero(); } }
@Test public void shouldNotCountUsedExtensionsWhenIntegrationDoesntDependOnIt() { final ExtensionHandler handler = new ExtensionHandler(data, null, null, null, resource); when(data.fetchAll(Integration.class)).thenReturn(ListResult.of(integration)); when(data.fetchAll(IntegrationDeployment.class)).thenReturn(ListResult.of(publishedDeployment)); when(resource.collectDependencies(integration)).thenReturn(Collections.emptyList()); assertThat(handler.enhance(extension).getUses()).hasValue(0); } }
@Test public void noParams() { ListResult<TestPersonInterface> toSort = new ListResult.Builder<TestPersonInterface>().items(getTestData()).totalCount(getTestData().size()).build(); Function<ListResult<TestPersonInterface>, ListResult<TestPersonInterface>> operator = new ReflectiveSorter<>(TestPersonInterface.class, getOptions(null, null)); ListResult<TestPersonInterface> sorted = operator.apply(toSort); String[] expectedNames = { "Schrödinger", "Heisenberg", "Feynman", "Maxwell", }; for (int i = 0; i < expectedNames.length; i++) { assertEquals(sorted.getItems().get(i).getLastName(), expectedNames[i]); } assertEquals(getTestData().size(), sorted.getTotalCount()); }
/** * Fetch the Connectors */ protected static Map<String, Connector> fetchConnectorsMap(DataManager dataManager) { return dataManager.fetchAll(Connector.class).getItems() .stream() .collect(Collectors.toMap(o -> o.getId().get(), Function.identity())); } }
@Test public void shouldFilterEmptyResults() { assertThat(filter.apply(ListResult.of(emptyList()))).isEmpty(); }
@Test public void apply() throws Exception { try { ListResult<Integer> filtered = new PaginationFilter<Integer>(new PaginationOptions() { @Override public int getPage() { return PaginationFilterTest.this.parameter.page; } @Override public int getPerPage() { return PaginationFilterTest.this.parameter.perPage; } }).apply(new ListResult.Builder<Integer>().items(parameter.inputList).totalCount(parameter.inputList.size()).build()); assertEquals(parameter.outputList, filtered.getItems()); assertEquals(parameter.inputList.size(), filtered.getTotalCount()); } catch (Exception e) { if (parameter.expectedException == null) { throw e; } assertEquals(parameter.expectedException, e.getClass()); return; } if (parameter.expectedException != null) { fail("Expected exception " + parameter.expectedException); } }
static <T> Collector<T, Builder<T>, Builder<T>> collector() { return Collector.of(Builder::new, Builder::addItem, (b1, b2) -> b1.addAllItems(b2.build().getItems())); }
@Test public void shouldFilterEmptyResults() { assertThat(filter.apply(ListResult.of(emptyList()))).isEmpty(); }
/** * Counts active integrations (in DB) of the owner of the specified integration. * * @param deployment The specified IntegrationDeployment. * @return The number of integrations (excluding the current). */ private int countActiveIntegrationsOfSameUserAs(IntegrationDeployment deployment) { Integration integration = deployment.getSpec(); String integrationId = integration.getId().orElseThrow(() -> new IllegalStateException("Couldn't find the id of the integration.")); String username = deployment.getUserId().orElseThrow(() -> new IllegalStateException("Couldn't find the user of the integration")); return (int) dataManager.fetchAll(IntegrationDeployment.class).getItems() .stream() .filter(i -> !i.getIntegrationId().get().equals(integrationId)) //The "current" integration will already be in the database. .filter(i -> IntegrationDeploymentState.Published == i.getCurrentState()) .filter(i -> i.getUserId().map(username::equals).orElse(Boolean.FALSE)) .count(); }
@Test public void shouldFilterOutOnlyDeletedIntegrations() { assertThat(filter.apply(ListResult.of(integration, deleted, integration))).containsExactly(integration, integration); } }
@Override protected List<ConnectionBulletinBoard> compute(ChangeEvent event) { final List<ConnectionBulletinBoard> boards = new ArrayList<>(); final DataManager dataManager = getDataManager(); final List<Connector> connectors = dataManager.fetchAll(Connector.class).getItems(); for (int i = 0; i < connectors.size(); i++) { final Connector connector = connectors.get(i); final String id = connector.getId().get(); dataManager.fetchAllByPropertyValue(Connection.class, "connectorId", id) .filter(connection -> connection.getConnector().isPresent()) .map(connection -> computeBoard(connection, connection.getConnector().get(), connector)) .filter(Objects::nonNull) .forEach(boards::add); } return boards; }
@Test public void shouldFilterOutTrivialWantedDeployments() { assertThat(filter.apply(ListResult.of(wanted))).containsOnly(wanted); }
private ImmutableListResult(ImmutableListResult.Builder<T> builder) { this.totalCount = builder.totalCount; this.items = builder.itemsIsSet() ? createUnmodifiableList(true, builder.items) : createUnmodifiableList(false, createSafeList(ListResult.super.getItems(), true, false)); }
@Test public void shouldFilterOutWantedDeployments() { assertThat(filter.apply(ListResult.of(unwanted, wanted, unwanted))).containsOnly(wanted); }