gnodet-bot commented on code in PR #26746:
URL: https://github.com/apache/camel/pull/26746#discussion_r4071728416
##########
components/camel-aws/camel-aws2-s3/src/test/java/org/apache/camel/component/aws2/s3/utils/AWS2S3UtilsTest.java:
##########
@@ -84,4 +86,74 @@ void suffixNullOrEmptyReturnsEmptyString() {
assertEquals("", AWS2S3Utils.evaluateDestinationBucketSuffix(exchange,
config));
}
+
+ // ---- determineKey ----
+
+ @Test
+ void keyFromHeaderIsUsedLiterallyAndNotEvaluated() {
+ Exchange exchange = createExchangeWithBody("body");
+ // a key coming from the header (e.g. inherited from a consumed object
name) must be used as-is
+ exchange.getIn().setHeader(AWS2S3Constants.KEY,
"${sys.user.name}.txt");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+
+ assertEquals("${sys.user.name}.txt",
AWS2S3Utils.determineKey(exchange, config));
+ }
+
+ @Test
+ void keyFromConfigurationIsEvaluatedAsSimpleExpression() {
+ Exchange exchange = createExchangeWithBody("body");
+ exchange.getIn().setHeader("name", "report");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+ config.setKeyName("${header.name}.txt");
+
+ assertEquals("report.txt", AWS2S3Utils.determineKey(exchange, config));
+ }
+
+ @Test
+ void keyFromHeaderTakesPrecedenceOverConfiguration() {
+ Exchange exchange = createExchangeWithBody("body");
+ exchange.getIn().setHeader(AWS2S3Constants.KEY, "literal-key");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+ config.setKeyName("${header.name}.txt");
+
+ assertEquals("literal-key", AWS2S3Utils.determineKey(exchange,
config));
+ }
+
+ @Test
+ void keyMissingFromHeaderAndConfigurationThrows() {
+ Exchange exchange = createExchangeWithBody("body");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+
+ assertThrows(IllegalArgumentException.class, () ->
AWS2S3Utils.determineKey(exchange, config));
+ }
+
+ // ---- determineBucketName ----
+
+ @Test
+ void bucketFromOverrideHeaderIsUsedLiterallyAndNotEvaluated() {
+ Exchange exchange = createExchangeWithBody("body");
+ exchange.getIn().setHeader(AWS2S3Constants.OVERRIDE_BUCKET_NAME,
"${sys.user.name}-bucket");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+ config.setBucketName("configured-bucket");
+
+ assertEquals("${sys.user.name}-bucket",
AWS2S3Utils.determineBucketName(exchange, config));
+ }
+
+ @Test
+ void bucketFromConfigurationIsEvaluatedAsSimpleExpression() {
+ Exchange exchange = createExchangeWithBody("body");
+ exchange.getIn().setHeader("env", "prod");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+ config.setBucketName("bucket-${header.env}");
+
+ assertEquals("bucket-prod", AWS2S3Utils.determineBucketName(exchange,
config));
+ }
+
+ @Test
+ void bucketMissingFromHeaderAndConfigurationThrows() {
+ Exchange exchange = createExchangeWithBody("body");
+ AWS2S3Configuration config = new AWS2S3Configuration();
+
+ assertThrows(IllegalArgumentException.class, () ->
AWS2S3Utils.determineBucketName(exchange, config));
Review Comment:
💡 **Missing test: `bucketOverrideHeaderTakesPrecedenceOverConfiguration`**
The key section adds `keyFromHeaderTakesPrecedenceOverConfiguration` to
confirm the header wins over a configured `keyName`. The bucket section has no
equivalent. The bug this PR fixes (unwanted expression evaluation of
header-supplied values) does not affect the header-wins logic, but the
precedence test is still worth having for symmetry and to prevent a future
regression.
Add alongside the existing bucket tests:
```java
@Test
void bucketOverrideHeaderTakesPrecedenceOverConfiguration() {
Exchange exchange = createExchangeWithBody("body");
exchange.getIn().setHeader(AWS2S3Constants.OVERRIDE_BUCKET_NAME,
"header-bucket");
AWS2S3Configuration config = new AWS2S3Configuration();
config.setBucketName("configured-bucket");
assertEquals("header-bucket",
AWS2S3Utils.determineBucketName(exchange, config));
}
```
--
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]