gnodet-bot commented on code in PR #26588: URL: https://github.com/apache/camel/pull/26588#discussion_r4045799738
########## components/camel-elasticsearch/src/test/java/org/apache/camel/component/es/aggregation/ElasticsearchBulkRequestAggregationStrategyTest.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.es.aggregation; + +import java.util.List; +import java.util.Map; + +import co.elastic.clients.elasticsearch.core.BulkRequest; +import co.elastic.clients.elasticsearch.core.bulk.BulkOperation; +import co.elastic.clients.elasticsearch.core.bulk.IndexOperation; +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +public class ElasticsearchBulkRequestAggregationStrategyTest { + + private final ElasticsearchBulkRequestAggregationStrategy strategy = new ElasticsearchBulkRequestAggregationStrategy(); + private CamelContext context; + + @BeforeEach + void setUp() { + context = new DefaultCamelContext(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + private Exchange exchangeWith(String id) { + Exchange exchange = new DefaultExchange(context); + BulkOperation operation = new BulkOperation.Builder() + .index(new IndexOperation.Builder<>().index("idx").id(id).document(Map.of("id", id)).build()) + .build(); + exchange.getIn().setBody(new BulkOperation[] { operation }); + return exchange; + } + + private static List<String> ids(BulkRequest request) { + return request.operations().stream().map(op -> op.index().id()).toList(); + } + + @Test + void firstAggregationMustReturnNewExchangeCarryingTheRequest() { + Exchange newExchange = exchangeWith("1"); + + Exchange result = strategy.aggregate(null, newExchange); + + // On the first call oldExchange is null; returning it (the previous bug) would make the + // AggregationStrategy return null, which AggregateProcessor rejects. + assertSame(newExchange, result); + BulkRequest request = result.getIn().getBody(BulkRequest.class); + assertNotNull(request); + assertEquals(List.of("1"), ids(request)); + } + + @Test + void subsequentAggregationMergesAllOperationsInInsertionOrder() { + Exchange aggregated = strategy.aggregate(null, exchangeWith("1")); + Exchange newExchange = exchangeWith("2"); + + Exchange result = strategy.aggregate(aggregated, newExchange); + + assertSame(newExchange, result); + BulkRequest request = result.getIn().getBody(BulkRequest.class); + assertEquals(List.of("1", "2"), ids(request)); Review Comment: 💡 **Suggestion:** The test only exercises 2-element aggregation. A 3-step chain would prove insertion order is preserved cumulatively (not just for the second call) and guard against any off-by-one in future refactors: ```suggestion void subsequentAggregationMergesAllOperationsInInsertionOrder() { Exchange first = strategy.aggregate(null, exchangeWith("1")); Exchange second = strategy.aggregate(first, exchangeWith("2")); Exchange third = strategy.aggregate(second, exchangeWith("3")); assertSame(second, second); BulkRequest r2 = second.getIn().getBody(BulkRequest.class); assertEquals(List.of("1", "2"), ids(r2), "2-step order"); assertSame(third, third); BulkRequest r3 = third.getIn().getBody(BulkRequest.class); assertEquals(List.of("1", "2", "3"), ids(r3), "3-step order"); } ``` ########## components/camel-opensearch/src/test/java/org/apache/camel/component/opensearch/aggregation/OpensearchBulkRequestAggregationStrategyTest.java: ########## @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.component.opensearch.aggregation; + +import java.util.List; +import java.util.Map; + +import org.apache.camel.CamelContext; +import org.apache.camel.Exchange; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.support.DefaultExchange; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.opensearch.client.opensearch.core.BulkRequest; +import org.opensearch.client.opensearch.core.bulk.BulkOperation; +import org.opensearch.client.opensearch.core.bulk.IndexOperation; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; + +public class OpensearchBulkRequestAggregationStrategyTest { + + private final OpensearchBulkRequestAggregationStrategy strategy = new OpensearchBulkRequestAggregationStrategy(); + private CamelContext context; + + @BeforeEach + void setUp() { + context = new DefaultCamelContext(); + } + + @AfterEach + void tearDown() { + context.stop(); + } + + private Exchange exchangeWith(String id) { + Exchange exchange = new DefaultExchange(context); + BulkOperation operation = new BulkOperation.Builder() + .index(new IndexOperation.Builder<>().index("idx").id(id).document(Map.of("id", id)).build()) + .build(); + exchange.getIn().setBody(new BulkOperation[] { operation }); + return exchange; + } + + private static List<String> ids(BulkRequest request) { + return request.operations().stream().map(op -> op.index().id()).toList(); + } + + @Test + void firstAggregationMustReturnNewExchangeCarryingTheRequest() { + Exchange newExchange = exchangeWith("1"); + + Exchange result = strategy.aggregate(null, newExchange); + + // On the first call oldExchange is null; returning it (the previous bug) would make the + // AggregationStrategy return null, which AggregateProcessor rejects. + assertSame(newExchange, result); + BulkRequest request = result.getIn().getBody(BulkRequest.class); + assertNotNull(request); + assertEquals(List.of("1"), ids(request)); + } + + @Test + void subsequentAggregationMergesAllOperationsInInsertionOrder() { + Exchange aggregated = strategy.aggregate(null, exchangeWith("1")); + Exchange newExchange = exchangeWith("2"); + + Exchange result = strategy.aggregate(aggregated, newExchange); + + assertSame(newExchange, result); + BulkRequest request = result.getIn().getBody(BulkRequest.class); + assertEquals(List.of("1", "2"), ids(request)); Review Comment: Same suggestion as for the ES test: extend to a 3-step chain to verify cumulative insertion order beyond the first merge. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
