software.amazon.awssdk.services.dynamodb.model.QueryRequest Java Examples

The following examples show how to use software.amazon.awssdk.services.dynamodb.model.QueryRequest. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AWSDynamoUtils.java    From para with Apache License 2.0 6 votes vote down vote up
private static QueryResponse queryGSI(String appid, Pager p) {
	Pager pager = (p != null) ? p : new Pager();
	GlobalSecondaryIndexDescription index = getSharedGlobalIndex();

	QueryRequest.Builder query = QueryRequest.builder().
			limit(pager.getLimit()).
			keyConditionExpression(Config._APPID + " = :aid").
			expressionAttributeValues(Collections.singletonMap(":aid", AttributeValue.builder().s(appid).build()));

	if (!StringUtils.isBlank(pager.getLastKey())) {
		// See https://stackoverflow.com/questions/40988397/42735813#42735813
		Map<String, AttributeValue> startKey = new HashMap<>(3);
		// HASH/PARTITION KEY
		startKey.put(Config._APPID, AttributeValue.builder().s(appid).build());
		// RANGE/SORT KEY
		startKey.put(Config._ID, AttributeValue.builder().s(pager.getLastKey()).build());
		// TABLE PRIMARY KEY
		startKey.put(Config._KEY, AttributeValue.builder().s(getKeyForAppid(pager.getLastKey(), appid)).build());
		query.exclusiveStartKey(startKey);
	}

	return index != null ? getClient().query(query.indexName(index.indexName()).
			tableName(getTableNameForAppid(SHARED_TABLE)).build()) : null;
}
 
Example #2
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void generateRequest_nonDefault_usesQueryConditional() {
    Map<String, AttributeValue> keyItemMap = getAttributeValueMap(keyItem);
    Expression expression = Expression.builder().expression("test-expression").expressionValues(keyItemMap).build();
    when(mockQueryConditional.expression(any(), anyString())).thenReturn(expression);

    QueryOperation<FakeItem> query = QueryOperation.create(QueryEnhancedRequest.builder()
                                                                               .queryConditional(mockQueryConditional)
                                                                               .build());
    QueryRequest queryRequest = query.generateRequest(FakeItem.getTableSchema(), PRIMARY_CONTEXT, null);

    QueryRequest expectedQueryRequest = QueryRequest.builder()
                                                    .tableName(TABLE_NAME)
                                                    .keyConditionExpression("test-expression")
                                                    .expressionAttributeValues(keyItemMap)
                                                    .build();
    assertThat(queryRequest, is(expectedQueryRequest));
    verify(mockQueryConditional).expression(FakeItem.getTableSchema(), TableMetadata.primaryIndexName());
}
 
Example #3
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void generateRequest_filterExpression_withValues() {
    Map<String, AttributeValue> expressionValues = singletonMap(":test-key", stringValue("test-value"));
    Expression filterExpression = Expression.builder()
                                            .expression("test-expression")
                                            .expressionValues(expressionValues)
                                            .build();

    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .filterExpression(filterExpression)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.filterExpression(), is("test-expression"));
    assertThat(queryRequest.expressionAttributeValues(), hasEntry(":test-key", stringValue("test-value")));
}
 
Example #4
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void generateRequest_projectionExpression() {
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .attributesToProject("id")
                                                  .addAttributeToProject("version")
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.projectionExpression(), is("#AMZN_MAPPED_id,#AMZN_MAPPED_version"));
    assertThat(queryRequest.expressionAttributeNames().get("#AMZN_MAPPED_id"), is ("id"));
    assertThat(queryRequest.expressionAttributeNames().get("#AMZN_MAPPED_version"), is ("version"));
}
 
Example #5
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void generateRequest_hashKeyOnly_withExclusiveStartKey() {
    FakeItem exclusiveStartKey = createUniqueFakeItem();
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .exclusiveStartKey(FakeItem.getTableSchema()
                                                                             .itemToMap(exclusiveStartKey,
                                                                                        FakeItem.getTableMetadata()
                                                                                                .primaryKeys()))
                                                  .build());

    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("id", AttributeValue.builder().s(exclusiveStartKey.getId()).build()));
}
 
Example #6
Source File: SecondaryIndexesIntegrationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
private void assertQueryResponseCount(Integer expected, QueryRequest request)
        throws InterruptedException {

    int retries = 0;
    QueryResponse result = null;
    do {
        result = dynamo.query(request);

        if (expected == result.count()) {
            return;
        }
        // Handling eventual consistency.
        Thread.sleep(SLEEP_TIME);
        retries++;
    } while (retries <= MAX_RETRIES);

    Assert.fail("Failed to assert query count. Expected : " + expected
                + " actual : " + result.count());
}
 
Example #7
Source File: DynamoDBPositionsStorage.java    From liiklus with MIT License 6 votes vote down vote up
@Override
public CompletionStage<Map<Integer, Map<Integer, Long>>> findAllVersionsByGroup(String topic, String groupName) {
    var request = QueryRequest.builder()
            .tableName(tableName)
            .keyConditions(ImmutableMap.of(
                    HASH_KEY_FIELD, condition(EQ, attribute(topic)),
                    RANGE_KEY_FIELD, condition(BEGINS_WITH, attribute(groupName))
            ))
            .build();

    return Flux
            .from(dynamoDB.queryPaginator(request))
            .flatMapIterable(QueryResponse::items)
            .map(item -> new AbstractMap.SimpleEntry<>(
                    GroupId.ofString(item.get("groupId").s()),
                    toPositions(item)
            ))
            .filter(it -> groupName.equals(it.getKey().getName()))
            .collectMap(
                    it -> it.getKey().getVersion().orElse(0),
                    Map.Entry::getValue
            )
            .toFuture();
}
 
Example #8
Source File: DynamoDBTest.java    From dynein with Apache License 2.0 6 votes vote down vote up
private QueryRequest getQueryRequest(String partition, JobStatus jobStatus, Instant instant) {
  Map<String, AttributeValue> values = new HashMap<>();
  Map<String, String> names = new HashMap<>();

  String keyCondition = "#shardId = :shardId and #dateToken < :dateToken";
  String filter = "#jobStatus = :jobStatus";
  String now = Long.toString(instant.toEpochMilli());
  values.put(":shardId", AttributeValue.builder().s(partition).build());
  values.put(":dateToken", AttributeValue.builder().s(now).build());
  values.put(":jobStatus", AttributeValue.builder().s(jobStatus.toString()).build());
  names.put("#shardId", DynamoDBUtils.Attribute.SHARD_ID.columnName);
  names.put("#dateToken", DynamoDBUtils.Attribute.DATE_TOKEN.columnName);
  names.put("#jobStatus", DynamoDBUtils.Attribute.JOB_STATUS.columnName);

  return QueryRequest.builder()
      .tableName(tableName)
      .keyConditionExpression(keyCondition)
      .filterExpression(filter)
      .expressionAttributeValues(values)
      .expressionAttributeNames(names)
      .limit(ddbConfig.getQueryLimit())
      .build();
}
 
Example #9
Source File: DynamoDBTest.java    From dynein with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetOverdueJobs_failure() {
  String partition = "test-partition";
  QueryRequest queryRequest = getQueryRequest(partition, JobStatus.SCHEDULED, Instant.now(clock));
  Exception exception = new Exception();

  CompletableFuture<QueryResponse> fut = new CompletableFuture<>();
  fut.completeExceptionally(exception);

  when(ddbClient.query(queryRequest)).thenReturn(fut);

  CompletableFuture<SchedulesQueryResponse> response = scheduleManager.getOverdueJobs(partition);
  assertSame(getException(response), exception);

  verify(ddbClient, times(1)).query(queryRequest);
  verifyNoMoreInteractions(ddbClient);
}
 
Example #10
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
@Test
public void generateRequest_hashAndSortKey_withExclusiveStartKey() {
    FakeItemWithSort exclusiveStartKey = createUniqueFakeItemWithSort();
    QueryOperation<FakeItemWithSort> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .exclusiveStartKey(
                                                      FakeItemWithSort.getTableSchema()
                                                                      .itemToMap(
                                                                          exclusiveStartKey,
                                                                          FakeItemWithSort.getTableSchema()
                                                                                          .tableMetadata()
                                                                                          .primaryKeys()))
                      .build());

    QueryRequest queryRequest = queryToTest.generateRequest(FakeItemWithSort.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("id", AttributeValue.builder().s(exclusiveStartKey.getId()).build()));
    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("sort", AttributeValue.builder().s(exclusiveStartKey.getSort()).build()));
}
 
Example #11
Source File: DynamoDBScheduleManager.java    From dynein with Apache License 2.0 6 votes vote down vote up
private QueryRequest makeQueryRequestForOverdueJobs(
    String partition, Instant instant, JobStatus jobStatus) {
  String keyCondition =
      Condition.of(Attribute.SHARD_ID, "=", Value.SHARD_ID)
          + " and "
          + Condition.of(Attribute.DATE_TOKEN, "<", Value.DATE_TOKEN);
  String filter = Condition.of(Attribute.JOB_STATUS, "=", Value.JOB_STATUS).toString();

  String now = Long.toString(instant.toEpochMilli());
  Map<String, AttributeValue> attributeValues =
      DynamoDBUtils.attributeValuesMap(
          ImmutableMap.of(
              Value.SHARD_ID, partition,
              Value.DATE_TOKEN, now,
              Value.JOB_STATUS, jobStatus.toString()));
  return QueryRequest.builder()
      .tableName(ddbConfig.getSchedulesTableName())
      .keyConditionExpression(keyCondition)
      .filterExpression(filter)
      .expressionAttributeValues(attributeValues)
      .expressionAttributeNames(DynamoDBUtils.getGetOverdueJobsAttributeMap())
      .limit(ddbConfig.getQueryLimit())
      .build();
}
 
Example #12
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_secondaryIndex_exclusiveStartKeyUsesPrimaryAndSecondaryIndex() {
    FakeItemWithIndices exclusiveStartKey = createUniqueFakeItemWithIndices();
    Set<String> keyFields = new HashSet<>(FakeItemWithIndices.getTableSchema().tableMetadata().primaryKeys());
    keyFields.addAll(FakeItemWithIndices.getTableSchema().tableMetadata().indexKeys("gsi_1"));

    QueryOperation<FakeItemWithIndices> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .exclusiveStartKey(FakeItemWithIndices.getTableSchema()
                                                                                        .itemToMap(exclusiveStartKey,
                                                                                                   keyFields))
                                                  .build());

    QueryRequest queryRequest = queryToTest.generateRequest(FakeItemWithIndices.getTableSchema(),
                                                            GSI_1_CONTEXT,
                                                            null);

    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("id", AttributeValue.builder().s(exclusiveStartKey.getId()).build()));
    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("sort", AttributeValue.builder().s(exclusiveStartKey.getSort()).build()));
    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("gsi_id", AttributeValue.builder().s(exclusiveStartKey.getGsiId()).build()));
    assertThat(queryRequest.exclusiveStartKey(),
               hasEntry("gsi_sort", AttributeValue.builder().s(exclusiveStartKey.getGsiSort()).build()));
}
 
Example #13
Source File: Query.java    From aws-doc-sdk-examples with Apache License 2.0 5 votes vote down vote up
public static int queryTable(DynamoDbClient ddb,
                             String tableName,
                             String partitionKeyName,
                             String partitionKeyVal,
                             String partitionAlias) {

    // Set up an alias for the partition key name in case it's a reserved word
    HashMap<String,String> attrNameAlias = new HashMap<String,String>();

    attrNameAlias.put(partitionAlias, partitionKeyName);

    // Set up mapping of the partition name with the value
    HashMap<String, AttributeValue> attrValues =
            new HashMap<String,AttributeValue>();
    attrValues.put(":"+partitionKeyName, AttributeValue.builder().s(partitionKeyVal).build());

    // Cretae a QueryRequest object
    QueryRequest queryReq = QueryRequest.builder()
            .tableName(tableName)
            .keyConditionExpression(partitionAlias + " = :" + partitionKeyName)
            .expressionAttributeNames(attrNameAlias)
            .expressionAttributeValues(attrValues)
            .build();

    try {
        QueryResponse response = ddb.query(queryReq);
        return response.count();
    } catch (DynamoDbException e) {
        System.err.println(e.getMessage());
        System.exit(1);
    }
   return -1;
}
 
Example #14
Source File: MetaStore.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@Override
public long getMaxVersion(final String materialName) {

    final List<Map<String, AttributeValue>> items =
        ddb.query(
            QueryRequest.builder()
                        .tableName(tableName)
                        .consistentRead(Boolean.TRUE)
                        .keyConditions(
                            Collections.singletonMap(
                                DEFAULT_HASH_KEY,
                                Condition.builder()
                                         .comparisonOperator(ComparisonOperator.EQ)
                                         .attributeValueList(AttributeValue.builder().s(materialName).build())
                                         .build()))
                        .limit(1)
                        .scanIndexForward(false)
                        .attributesToGet(DEFAULT_RANGE_KEY)
                        .build())
           .items();

    if (items.isEmpty()) {
        return -1L;
    } else {
        return Long.parseLong(items.get(0).get(DEFAULT_RANGE_KEY).n());
    }
}
 
Example #15
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_consistentRead() {
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .consistentRead(true)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.consistentRead(), is(true));
}
 
Example #16
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_filterExpression_withoutValues() {
    Expression filterExpression = Expression.builder().expression("test-expression").build();

    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .filterExpression(filterExpression)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.filterExpression(), is("test-expression"));
}
 
Example #17
Source File: MostRecentProviderTests.java    From aws-dynamodb-encryption-java with Apache License 2.0 5 votes vote down vote up
@Test
public void singleVersion() throws InterruptedException {
    final MostRecentProvider prov = new MostRecentProvider(store, MATERIAL_NAME, 500);
    verify(client, never()).putItem(any(PutItemRequest.class));
    final EncryptionMaterials eMat1 = prov.getEncryptionMaterials(ctx);
    // It's a new provider, so we see a single putItem
    verify(client).putItem(any(PutItemRequest.class));
    reset(client);
    // Ensure the cache is working
    final EncryptionMaterials eMat2 = prov.getEncryptionMaterials(ctx);
    verifyNoMoreInteractions(client);
    assertEquals(0, store.getVersionFromMaterialDescription(eMat1.getMaterialDescription()));
    assertEquals(0, store.getVersionFromMaterialDescription(eMat2.getMaterialDescription()));
    // Let the TTL be exceeded
    Thread.sleep(500);
    final EncryptionMaterials eMat3 = prov.getEncryptionMaterials(ctx);
    verify(client).query(any(QueryRequest.class));
    verifyNoMoreInteractions(client);
    assertEquals(0, store.getVersionFromMaterialDescription(eMat3.getMaterialDescription()));

    assertEquals(eMat1.getSigningKey(), eMat2.getSigningKey());
    assertEquals(eMat1.getSigningKey(), eMat3.getSigningKey());
    // Check algorithms. Right now we only support AES and HmacSHA256
    assertEquals("AES", eMat1.getEncryptionKey().getAlgorithm());
    assertEquals("HmacSHA256", eMat1.getSigningKey().getAlgorithm());

    // Ensure we can decrypt all of them without hitting ddb more than the minimum
    final MostRecentProvider prov2 = new MostRecentProvider(store, MATERIAL_NAME, 500);
    final DecryptionMaterials dMat1 = prov2.getDecryptionMaterials(ctx(eMat1));
    reset(client);
    assertEquals(eMat1.getEncryptionKey(), dMat1.getDecryptionKey());
    assertEquals(eMat1.getSigningKey(), dMat1.getVerificationKey());
    final DecryptionMaterials dMat2 = prov2.getDecryptionMaterials(ctx(eMat2));
    assertEquals(eMat2.getEncryptionKey(), dMat2.getDecryptionKey());
    assertEquals(eMat2.getSigningKey(), dMat2.getVerificationKey());
    final DecryptionMaterials dMat3 = prov2.getDecryptionMaterials(ctx(eMat3));
    assertEquals(eMat3.getEncryptionKey(), dMat3.getDecryptionKey());
    assertEquals(eMat3.getSigningKey(), dMat3.getVerificationKey());
    verifyNoMoreInteractions(client);
}
 
Example #18
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_descending() {
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .scanIndexForward(false)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.scanIndexForward(), is(false));
}
 
Example #19
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_limit() {
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .limit(123)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.limit(), is(123));
}
 
Example #20
Source File: ApplicationsService.java    From realworld-serverless-application with Apache License 2.0 5 votes vote down vote up
@Override
public ApplicationList listApplications(final String nextToken, final Integer maxItems) {
  log.info("Listing applications with nextToken {} and maxItems {}", nextToken, maxItems);
  Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
  expressionAttributeValues.put(":u", AttributeValue.builder()
        .s(securityContext.getUserPrincipal().getName())
        .build());

  QueryRequest.Builder requestBuilder = QueryRequest.builder()
        .consistentRead(true)
        .tableName(tableName)
        .keyConditionExpression(String.format("%s = :u",
              ApplicationRecord.USER_ID_ATTRIBUTE_NAME))
        .expressionAttributeValues(expressionAttributeValues)
        .limit(maxItems == null ? DEFAULT_LIST_APPLICATIONS_LIMIT : maxItems);
  if (nextToken != null) {
    try {
      requestBuilder.exclusiveStartKey(paginationTokenSerializer.deserialize(nextToken));
    } catch (InvalidTokenException e) {
      throw new BadRequestApiException(new BadRequestException()
            .errorCode("InvalidRequest")
            .message(String.format("NextToken %s is invalid.", nextToken)));
    }
  }
  QueryResponse queryResponse = dynamodb.query(requestBuilder.build());

  List<ApplicationSummary> applicationSummaries = queryResponse.items()
        .stream()
        .map(ApplicationRecord::new)
        .map(record -> modelMapper.map(record, ApplicationSummary.class))
        .collect(Collectors.toList());

  ApplicationList result = new ApplicationList()
        .applications(applicationSummaries);
  Map<String, AttributeValue> lastEvaluatedKey = queryResponse.lastEvaluatedKey();
  if (lastEvaluatedKey != null && !lastEvaluatedKey.isEmpty()) {
    result.nextToken(paginationTokenSerializer.serialize(lastEvaluatedKey));
  }
  return result;
}
 
Example #21
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_knowsHowToUseAnIndex() {
    FakeItemWithIndices fakeItem = createUniqueFakeItemWithIndices();
    QueryOperation<FakeItemWithIndices> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(fakeItem.getGsiId())))
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItemWithIndices.getTableSchema(), GSI_1_CONTEXT, null);

    assertThat(queryRequest.indexName(), is("gsi_1"));
}
 
Example #22
Source File: ApplicationsServiceTest.java    From realworld-serverless-application with Apache License 2.0 5 votes vote down vote up
@Test
public void listApplications() {
  String userId = UUID.randomUUID().toString();
  String applicationId = UUID.randomUUID().toString();

  Map<String, AttributeValue> recordMap = keyMap(userId, applicationId);

  Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
  expressionAttributeValues.put(":u", AttributeValue.builder()
        .s(userId)
        .build());
  QueryRequest expectedQueryRequest = QueryRequest.builder()
        .consistentRead(true)
        .tableName(TABLE_NAME)
        .keyConditionExpression(String.format("%s = :u",
              ApplicationRecord.USER_ID_ATTRIBUTE_NAME))
        .expressionAttributeValues(expressionAttributeValues)
        .limit(ApplicationsService.DEFAULT_LIST_APPLICATIONS_LIMIT)
        .build();

  QueryResponse queryResponse = QueryResponse.builder()
        .items(Collections.singletonList(recordMap))
        .build();
  when(principal.getName()).thenReturn(userId);
  when(dynamodb.query(any(QueryRequest.class))).thenReturn(queryResponse);

  ApplicationList applicationList = service.listApplications(null, null);
  List<ApplicationSummary> applicationSummaries = applicationList.getApplications();

  ArgumentCaptor<QueryRequest> queryRequestArgumentCaptor = ArgumentCaptor.forClass(QueryRequest.class);
  verify(dynamodb).query(queryRequestArgumentCaptor.capture());
  assertThat(queryRequestArgumentCaptor.getValue()).isEqualTo(expectedQueryRequest);
  assertThat(applicationSummaries.get(0).getApplicationId()).isEqualTo(applicationId);
  assertThat(applicationList.getNextToken()).isNull();
}
 
Example #23
Source File: ApplicationsServiceTest.java    From realworld-serverless-application with Apache License 2.0 5 votes vote down vote up
@Test
public void listApplications_maxItems() {
  String userId = UUID.randomUUID().toString();
  String applicationId = UUID.randomUUID().toString();
  Integer maxItems = ApplicationsService.DEFAULT_LIST_APPLICATIONS_LIMIT * 2;

  Map<String, AttributeValue> recordMap = keyMap(userId, applicationId);

  Map<String, AttributeValue> expressionAttributeValues = new HashMap<>();
  expressionAttributeValues.put(":u", AttributeValue.builder()
        .s(userId)
        .build());
  QueryRequest expectedQueryRequest = QueryRequest.builder()
        .consistentRead(true)
        .tableName(TABLE_NAME)
        .keyConditionExpression(String.format("%s = :u",
              ApplicationRecord.USER_ID_ATTRIBUTE_NAME))
        .expressionAttributeValues(expressionAttributeValues)
        .limit(maxItems)
        .build();

  QueryResponse queryResponse = QueryResponse.builder()
        .items(Collections.singletonList(recordMap))
        .build();
  when(principal.getName()).thenReturn(userId);
  when(dynamodb.query(any(QueryRequest.class))).thenReturn(queryResponse);

  ApplicationList applicationList = service.listApplications(null, maxItems);
  List<ApplicationSummary> applicationSummaries = applicationList.getApplications();

  ArgumentCaptor<QueryRequest> queryRequestArgumentCaptor = ArgumentCaptor.forClass(QueryRequest.class);
  verify(dynamodb).query(queryRequestArgumentCaptor.capture());
  assertThat(queryRequestArgumentCaptor.getValue()).isEqualTo(expectedQueryRequest);
  assertThat(applicationSummaries.get(0).getApplicationId()).isEqualTo(applicationId);
  assertThat(applicationList.getNextToken()).isNull();
}
 
Example #24
Source File: DynamoDBTest.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetOverdueJobs() throws Exception {
  String partition = "test-partition";
  QueryRequest queryRequest = getQueryRequest(partition, JobStatus.SCHEDULED, Instant.now(clock));
  DyneinJobSpec jobSpec1 = getTestJobSpec(validToken, "test1");
  Schedule schedule1 = jobSpecToSchedule(jobSpec1);
  DyneinJobSpec jobSpec2 = getTestJobSpec(getToken(4), "test2");
  Schedule schedule2 = jobSpecToSchedule(jobSpec2);

  QueryResponse queryResponse =
      QueryResponse.builder()
          .items(
              asList(
                  DynamoDBUtils.toAttributeMap(schedule1),
                  DynamoDBUtils.toAttributeMap(schedule2)))
          .count(2)
          .lastEvaluatedKey(ImmutableMap.of())
          .build();

  when(ddbClient.query(queryRequest))
      .thenReturn(CompletableFuture.completedFuture(queryResponse));

  CompletableFuture<SchedulesQueryResponse> response = scheduleManager.getOverdueJobs(partition);
  Assert.assertEquals(
      response.get(1, TimeUnit.SECONDS),
      SchedulesQueryResponse.of(asList(schedule1, schedule2), false));

  verify(ddbClient, times(1)).query(queryRequest);
  verifyNoMoreInteractions(ddbClient);
}
 
Example #25
Source File: DynamoDBTest.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetOverdueJobs_queryLimit() throws Exception {
  String partition = "test-partition";
  QueryRequest queryRequest = getQueryRequest(partition, JobStatus.SCHEDULED, Instant.now(clock));
  DyneinJobSpec jobSpec = getTestJobSpec(validToken, "test1");
  Schedule schedule = jobSpecToSchedule(jobSpec);

  List<Schedule> queryLimitSchedules = new ArrayList<>();

  for (int i = 0; i < ddbConfig.getQueryLimit(); i++) {
    queryLimitSchedules.add(schedule);
  }

  QueryResponse queryResponse =
      QueryResponse.builder()
          .items(
              queryLimitSchedules
                  .stream()
                  .map(DynamoDBUtils::toAttributeMap)
                  .collect(Collectors.toList()))
          .count(ddbConfig.getQueryLimit())
          .lastEvaluatedKey(ImmutableMap.of())
          .build();

  when(ddbClient.query(queryRequest))
      .thenReturn(CompletableFuture.completedFuture(queryResponse));

  CompletableFuture<SchedulesQueryResponse> response = scheduleManager.getOverdueJobs(partition);
  Assert.assertEquals(
      response.get(1, TimeUnit.SECONDS), SchedulesQueryResponse.of(queryLimitSchedules, true));

  verify(ddbClient, times(1)).query(queryRequest);
  verifyNoMoreInteractions(ddbClient);
}
 
Example #26
Source File: DynamoDBTest.java    From dynein with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetOverdueJobs_pagination() throws Exception {
  String partition = "test-partition";
  QueryRequest queryRequest = getQueryRequest(partition, JobStatus.SCHEDULED, Instant.now(clock));
  DyneinJobSpec jobSpec = getTestJobSpec(validToken, "test1");
  Schedule schedule = jobSpecToSchedule(jobSpec);

  List<Schedule> queryLimitSchedules = new ArrayList<>();

  for (int i = 0; i < ddbConfig.getQueryLimit() - 1; i++) {
    queryLimitSchedules.add(schedule);
  }

  QueryResponse queryResponse =
      QueryResponse.builder()
          .items(
              queryLimitSchedules
                  .stream()
                  .map(DynamoDBUtils::toAttributeMap)
                  .collect(Collectors.toList()))
          .count(ddbConfig.getQueryLimit() - 1)
          .lastEvaluatedKey(
              ImmutableMap.of("random", AttributeValue.builder().s("thing").build()))
          .build();

  when(ddbClient.query(queryRequest))
      .thenReturn(CompletableFuture.completedFuture(queryResponse));

  CompletableFuture<SchedulesQueryResponse> response = scheduleManager.getOverdueJobs(partition);
  Assert.assertEquals(
      response.get(1, TimeUnit.SECONDS), SchedulesQueryResponse.of(queryLimitSchedules, true));

  verify(ddbClient, times(1)).query(queryRequest);
  verifyNoMoreInteractions(ddbClient);
}
 
Example #27
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_defaultQuery_usesEqualTo() {
    QueryRequest queryRequest = queryOperation.generateRequest(FakeItem.getTableSchema(),
                                                               PRIMARY_CONTEXT,
                                                               null);

    QueryRequest expectedQueryRequest = QueryRequest.builder()
        .tableName(TABLE_NAME)
        .keyConditionExpression("#AMZN_MAPPED_id = :AMZN_MAPPED_id")
        .expressionAttributeValues(singletonMap(":AMZN_MAPPED_id",
                                                AttributeValue.builder().s(keyItem.getId()).build()))
        .expressionAttributeNames(singletonMap("#AMZN_MAPPED_id", "id"))
        .build();
    assertThat(queryRequest, is(expectedQueryRequest));
}
 
Example #28
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void generateRequest_ascending() {
    QueryOperation<FakeItem> queryToTest =
        QueryOperation.create(QueryEnhancedRequest.builder()
                                                  .queryConditional(keyEqualTo(k -> k.partitionValue(keyItem.getId())))
                                                  .scanIndexForward(true)
                                                  .build());
    QueryRequest queryRequest = queryToTest.generateRequest(FakeItem.getTableSchema(),
                                                            PRIMARY_CONTEXT,
                                                            null);

    assertThat(queryRequest.scanIndexForward(), is(true));
}
 
Example #29
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getAsyncServiceCall_makesTheRightCallAndReturnsResponse() {
    QueryRequest queryRequest = QueryRequest.builder().build();
    QueryPublisher mockQueryPublisher = mock(QueryPublisher.class);
    when(mockDynamoDbAsyncClient.queryPaginator(any(QueryRequest.class))).thenReturn(mockQueryPublisher);

    SdkPublisher<QueryResponse> response =
        queryOperation.asyncServiceCall(mockDynamoDbAsyncClient).apply(queryRequest);

    assertThat(response, is(mockQueryPublisher));
    verify(mockDynamoDbAsyncClient).queryPaginator(queryRequest);
}
 
Example #30
Source File: QueryOperationTest.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
@Test
public void getServiceCall_makesTheRightCallAndReturnsResponse() {
    QueryRequest queryRequest = QueryRequest.builder().build();
    QueryIterable mockQueryIterable = mock(QueryIterable.class);
    when(mockDynamoDbClient.queryPaginator(any(QueryRequest.class))).thenReturn(mockQueryIterable);

    SdkIterable<QueryResponse> response = queryOperation.serviceCall(mockDynamoDbClient).apply(queryRequest);

    assertThat(response, is(mockQueryIterable));
    verify(mockDynamoDbClient).queryPaginator(queryRequest);
}