我想知道如何将空手道框架上的自动化与 DynamoDB 连接起来。它仅用于验证一行。 例如,我有一个名为 Users 的表,我想知道用户“Nick”是否已注册。
使用 AWS Java SDK 和 Karate 的 Java 互操作,这非常简单。
有一个简单的入门项目可供您用作参考:https://github.com/karatelabs/karate-examples/blob/main/aws-dynamodb/README.md
例如:
package examples;
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
import software.amazon.awssdk.services.dynamodb.model.AttributeValue;
import software.amazon.awssdk.services.dynamodb.model.GetItemRequest;
import software.amazon.awssdk.services.dynamodb.model.GetItemResponse;
import java.util.HashMap;
import java.util.Map;
public class DynamoDbUtils {
final DynamoDbClient db;
public DynamoDbUtils() {
db = DynamoDbClient.builder()
.credentialsProvider(ProfileCredentialsProvider.create("myAwsProfile"))
.region(Region.US_EAST_2)
.build();
}
public Map<String, Object> getItem(String keyValue) {
Map<String, AttributeValue> keyToGet = new HashMap<>();
keyToGet.put("myPrimaryKeyName", AttributeValue.builder().s(keyValue).build());
// keyToGet.put("mySecondaryKeyName", AttributeValue.builder().s(keyValue).build());
GetItemRequest get = GetItemRequest.builder().key(keyToGet).tableName("myTableName").build();
GetItemResponse response = db.getItem(get);
if (!response.hasItem()) {
System.out.println("item not found by key: " + keyValue);
return null;
}
Map<String, AttributeValue> row = response.item();
Map<String, Object> result = new HashMap<>();
result.put("myFieldName", row.get("myFieldName").s());
return result;
}
}
现在在空手道测试中使用它非常简单:
* def Utils = Java.type('examples.DynamoDbUtils')
* def db = new Utils()
* def result = db.getItem('myKeyValue')
* match result == { myFieldName: '#string' }