Skip to content

Commit

Permalink
Adding a fix to pick the annotated container name for annotated queri…
Browse files Browse the repository at this point in the history
…es (Azure#15924)

* Adding a fix to pick the annotated container name for the user types while running annotated queries.
Renaming RoleRepository to ReactiveRoleRepository appropriately
Adding a new RoleRepository for test
Adding tests to test the fix for both reactive and synchronous repositories.

* Reafactoring RoleRepository to BookRepository to avoid flaky IT.
  • Loading branch information
mbhaskar authored Oct 6, 2020
1 parent 61e8c15 commit 07c4003
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class TestConstants {
public static final List<Address> ADDRESSES = Arrays.asList(ADDRESS_1, ADDRESS_2);

public static final String ROLE_COLLECTION_NAME = "RoleCollectionName";
public static final String BOOK_COLLECTION_NAME = "BookCollectionName";
public static final int TIME_TO_LIVE = 5;
public static final boolean INDEXING_POLICY_AUTOMATIC = true;
public static final IndexingMode INDEXING_POLICY_MODE = IndexingMode.CONSISTENT;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.domain;

import com.azure.spring.data.cosmos.common.TestConstants;
import com.azure.spring.data.cosmos.core.mapping.Container;
import com.azure.spring.data.cosmos.core.mapping.PartitionKey;
import org.springframework.data.annotation.Id;

@Container(containerName = TestConstants.BOOK_COLLECTION_NAME,
autoCreateContainer = false)
public class Book {
@Id
String id;

@PartitionKey
String authorId;

String title;

public Book() {

}

public Book(String id, String authorId, String title) {
this.id = id;
this.authorId = authorId;
this.title = title;
}

/**
* Getter for property 'id'.
*
* @return Value for property 'id'.
*/
public String getId() {
return id;
}

/**
* Setter for property 'id'.
*
* @param id Value to set for property 'id'.
*/
public void setId(final String id) {
this.id = id;
}

/**
* Getter for property 'authorId'.
*
* @return Value for property 'authorId'.
*/
public String getAuthorId() {
return authorId;
}

/**
* Setter for property 'authorId'.
*
* @param authorId Value to set for property 'authorId'.
*/
public void setAuthorId(final String authorId) {
this.authorId = authorId;
}

/**
* Getter for property 'title'.
*
* @return Value for property 'title'.
*/
public String getTitle() {
return title;
}

/**
* Setter for property 'title'.
*
* @param title Value to set for property 'title'.
*/
public void setTitle(final String title) {
this.title = title;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.repository.integration;

import com.azure.spring.data.cosmos.common.TestConstants;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import com.azure.spring.data.cosmos.domain.Book;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.BookRepository;
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class BookRepositoryIT {

private static final Book TEST_BOOK_1 = new Book(TestConstants.ID_1, UUID.randomUUID().toString(),
"title1");
private static final Book TEST_BOOK_2 = new Book(TestConstants.ID_2, UUID.randomUUID().toString(),
"title2");

private static final CosmosEntityInformation<Book, String> entityInformation =
new CosmosEntityInformation<>(Book.class);

private static CosmosTemplate staticTemplate;
private static boolean isSetupDone;

@Autowired
private CosmosTemplate template;

@Autowired
private BookRepository repository;

@AfterClass
public static void afterClassCleanup() {
staticTemplate.deleteContainer(entityInformation.getContainerName());
}

@Before
public void setUp() {
if (!isSetupDone) {
staticTemplate = template;
template.createContainerIfNotExists(entityInformation);
}
repository.saveAll(Arrays.asList(TEST_BOOK_1, TEST_BOOK_2));
isSetupDone = true;
}

@After
public void cleanup() {
repository.deleteAll();
}

@Test
public void testAnnotatedQuery() {
final List<Book> result = repository.annotatedFindBookById(TEST_BOOK_1.getId());
assertThat(result).isNotNull();
assertThat(result.size()).isEqualTo(1);
assertThat(result.get(0).getId()).isEqualTo(TEST_BOOK_1.getId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import com.azure.spring.data.cosmos.domain.Role;
import com.azure.spring.data.cosmos.domain.TimeToLiveSample;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.RoleRepository;
import com.azure.spring.data.cosmos.repository.repository.ReactiveRoleRepository;
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.AfterClass;
Expand Down Expand Up @@ -60,7 +60,7 @@ public class CosmosAnnotationIT {
@Autowired
private CosmosClientBuilder cosmosClientBuilder;
@Autowired
private RoleRepository repository;
private ReactiveRoleRepository repository;

private static CosmosTemplate cosmosTemplate;
private static CosmosContainerProperties collectionRole;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.repository.integration;

import com.azure.spring.data.cosmos.common.TestConstants;
import com.azure.spring.data.cosmos.core.CosmosTemplate;
import com.azure.spring.data.cosmos.domain.Role;
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.ReactiveRoleRepository;
import com.azure.spring.data.cosmos.repository.support.CosmosEntityInformation;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.test.StepVerifier;

import java.util.Arrays;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ReactiveRoleRepositoryIT {

private static final Role TEST_ROLE_1 = new Role(TestConstants.ID_1, true, TestConstants.LEVEL,
TestConstants.ROLE_NAME);
private static final Role TEST_ROLE_2 = new Role(TestConstants.ID_2, false, TestConstants.LEVEL,
TestConstants.ROLE_NAME);

private static final CosmosEntityInformation<Role, String> entityInformation =
new CosmosEntityInformation<>(Role.class);
private static CosmosTemplate staticTemplate;
private static boolean isSetupDone;
@Autowired
private CosmosTemplate template;
@Autowired
private ReactiveRoleRepository repository;

@AfterClass
public static void afterClassCleanup() {
staticTemplate.deleteContainer(entityInformation.getContainerName());
}

@Before
public void setUp() {
if (!isSetupDone) {
staticTemplate = template;
template.createContainerIfNotExists(entityInformation);
}
final Flux<Role> savedFlux = repository.saveAll(Arrays.asList(TEST_ROLE_1, TEST_ROLE_2));
StepVerifier.create(savedFlux).thenConsumeWhile(role -> true).expectComplete().verify();
isSetupDone = true;
}

@After
public void cleanup() {
final Mono<Void> deletedMono = repository.deleteAll();
StepVerifier.create(deletedMono).thenAwait().verifyComplete();
}

@Test
public void testAnnotatedQuery() {
Flux<Role> roleFlux = repository.annotatedFindRoleById(TestConstants.ID_1);
StepVerifier.create(roleFlux).expectNext(TEST_ROLE_1).verifyComplete();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.spring.data.cosmos.repository.repository;

import com.azure.spring.data.cosmos.domain.Book;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.List;

public interface BookRepository extends CosmosRepository<Book, String> {

@Query(value = "select * from c where c.id = @id")
List<Book> annotatedFindBookById(@Param("id") String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
package com.azure.spring.data.cosmos.repository.repository;

import com.azure.spring.data.cosmos.domain.Role;
import com.azure.spring.data.cosmos.repository.Query;
import com.azure.spring.data.cosmos.repository.ReactiveCosmosRepository;
import org.springframework.data.repository.query.Param;
import reactor.core.publisher.Flux;

public interface RoleRepository extends ReactiveCosmosRepository<Role, String> {
public interface ReactiveRoleRepository extends ReactiveCosmosRepository<Role, String> {

Flux<Role> findByDeveloperAndId(boolean isDeveloper, String id);

@Query(value = "select * from c where c.id = @id")
Flux<Role> annotatedFindRoleById(@Param("id") String id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ public MappingCosmosConverter getConverter() {

@Override
public <T> Iterable<T> runQuery(SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType) {
return getJsonNodeFluxFromQuerySpec(domainType.getSimpleName(), querySpec, returnType)
return getJsonNodeFluxFromQuerySpec(getContainerName(domainType), querySpec, returnType)
.collectList()
.block();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ public MappingCosmosConverter getConverter() {

@Override
public <T> Flux<T> runQuery(SqlQuerySpec querySpec, Class<?> domainType, Class<T> returnType) {
String containerName = domainType.getSimpleName();
String containerName = getContainerName(domainType);
CosmosQueryRequestOptions options = new CosmosQueryRequestOptions();
return cosmosAsyncClient.getDatabase(this.databaseName)
.getContainer(containerName)
Expand Down

0 comments on commit 07c4003

Please sign in to comment.