Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed mapping cosmos converter to handle value nodes #22073

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.azure.spring.data.cosmos.repository.TestRepositoryConfig;
import com.azure.spring.data.cosmos.repository.repository.AddressRepository;
import com.azure.spring.data.cosmos.repository.repository.AuditableRepository;
import com.fasterxml.jackson.databind.JsonNode;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
Expand All @@ -25,6 +26,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

import static com.azure.spring.data.cosmos.common.PageTestUtils.validateLastPage;
import static com.azure.spring.data.cosmos.common.PageTestUtils.validateNonLastPage;
Expand Down Expand Up @@ -111,6 +113,50 @@ public void testAnnotatedQueryWithSort() {
assertAddressOrder(resultsDesc, Address.TEST_ADDRESS2_PARTITION1, Address.TEST_ADDRESS1_PARTITION1);
}

@Test
public void testAnnotatedQueryWithValueAsPage() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
final Page<String> postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY,
cosmosPageRequest);

assertAddressPostalCodes(postalCodes.getContent(), addresses);
}

@Test
public void testAnnotatedQueryWithValueAsList() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final List<String> postalCodes = addressRepository.annotatedFindPostalCodeValuesByCity(TestConstants.CITY);

assertAddressPostalCodes(postalCodes, addresses);
}

@Test
public void testAnnotatedQueryWithJsonNodeAsPage() {
final List<Address> addresses = Arrays.asList(Address.TEST_ADDRESS1_PARTITION1, Address.TEST_ADDRESS2_PARTITION1);
addressRepository.saveAll(addresses);

final PageRequest cosmosPageRequest = CosmosPageRequest.of(0, 10);
final Page<JsonNode> postalCodes = addressRepository.annotatedFindPostalCodesByCity(TestConstants.CITY,
cosmosPageRequest);
final List<String> actualPostalCodes = postalCodes.getContent()
.stream()
.map(jsonNode -> jsonNode.get("postalCode").asText())
.collect(Collectors.toList());
assertAddressPostalCodes(actualPostalCodes, addresses);
}

private void assertAddressPostalCodes(List<String> postalCodes, List<Address> expectedResults) {
List<String> expectedPostalCodes = expectedResults.stream()
.map(Address::getPostalCode)
.collect(Collectors.toList());
assertThat(postalCodes).isEqualTo(expectedPostalCodes);
}

private void assertAddressOrder(List<Address> actualResults, Address ... expectedResults) {
assertThat(actualResults.size()).isEqualTo(expectedResults.length);
for (int i = 0; i < expectedResults.length; i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,14 @@
@ContextConfiguration(classes = TestRepositoryConfig.class)
public class ContactRepositoryIT {

private static final Contact TEST_CONTACT1 = new Contact("testId", "faketitle", 25, true);
private static final Contact TEST_CONTACT2 = new Contact("testId2", "faketitle2", 32, false);
private static final Contact TEST_CONTACT3 = new Contact("testId3", "faketitle3", 25, false);
private static final Contact TEST_CONTACT4 = new Contact("testId4", "faketitle4", 43, true);
private static final Contact TEST_CONTACT5 = new Contact("testId5", "faketitle3", 43, true);
private static final Integer INT_VALUE_1 = 25;
private static final Integer INT_VALUE_2 = 32;
private static final Integer INT_VALUE_3 = 43;
private static final Contact TEST_CONTACT1 = new Contact("testId", "faketitle", INT_VALUE_1, true);
private static final Contact TEST_CONTACT2 = new Contact("testId2", "faketitle2", INT_VALUE_2, false);
private static final Contact TEST_CONTACT3 = new Contact("testId3", "faketitle3", INT_VALUE_1, false);
private static final Contact TEST_CONTACT4 = new Contact("testId4", "faketitle4", INT_VALUE_3, true);
private static final Contact TEST_CONTACT5 = new Contact("testId5", "faketitle3", INT_VALUE_3, true);

@ClassRule
public static final IntegrationTestCollectionManager collectionManager = new IntegrationTestCollectionManager();
Expand Down Expand Up @@ -245,4 +248,16 @@ public void testAnnotatedQueries() {
List<ObjectNode> groupByContacts = repository.selectGroupBy();
Assert.assertEquals(3, groupByContacts.size());
}

@Test
public void testAnnotatedQueriesDistinctIntValue() {
List<Integer> valueContacts = repository.findDistinctIntValueValues();
assertThat(valueContacts).isEqualTo(Arrays.asList(INT_VALUE_1, INT_VALUE_2, INT_VALUE_3));
}

@Test
public void testAnnotatedQueriesDistinctStatus() {
List<Boolean> statusContacts = repository.findDistinctStatusValues();
assertThat(statusContacts).isEqualTo(Arrays.asList(Boolean.TRUE, Boolean.FALSE));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.azure.spring.data.cosmos.domain.Address;
import com.azure.spring.data.cosmos.repository.CosmosRepository;
import com.azure.spring.data.cosmos.repository.Query;
import com.fasterxml.jackson.databind.JsonNode;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -36,4 +37,12 @@ public interface AddressRepository extends CosmosRepository<Address, String> {
@Query("select * from a where a.city = @city")
List<Address> annotatedFindByCity(@Param("city") String city, Sort pageable);

@Query("select DISTINCT value a.postalCode from a where a.city = @city")
Page<String> annotatedFindPostalCodeValuesByCity(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT a.postalCode from a where a.city = @city")
Page<JsonNode> annotatedFindPostalCodesByCity(@Param("city") String city, Pageable pageable);

@Query("select DISTINCT value a.postalCode from a where a.city = @city")
List<String> annotatedFindPostalCodeValuesByCity(@Param("city") String city);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,10 @@ public interface ContactRepository extends CosmosRepository<Contact, String> {
@Query(value = "SELECT count(c.id) as id_count, c.intValue FROM c group by c.intValue")
List<ObjectNode> selectGroupBy();

@Query(value = "Select DISTINCT value c.intValue from c")
List<Integer> findDistinctIntValueValues();

@Query(value = "Select DISTINCT value c.active from c")
List<Boolean> findDistinctStatusValues();

}
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ public MappingCosmosConverter(
public <R> R read(Class<R> type, JsonNode jsonNode) {

final CosmosPersistentEntity<?> entity = mappingContext.getPersistentEntity(type);
Assert.notNull(entity, "Entity is null.");

return readInternal(entity, type, jsonNode);
}
Expand All @@ -74,8 +73,13 @@ public void write(Object source, JsonNode sink) {

private <R> R readInternal(final CosmosPersistentEntity<?> entity, Class<R> type,
final JsonNode jsonNode) {
final ObjectNode objectNode = jsonNode.deepCopy();
try {
if (jsonNode.isValueNode()) {
return objectMapper.treeToValue(jsonNode, type);
}

Assert.notNull(entity, "Entity is null.");
final ObjectNode objectNode = jsonNode.deepCopy();
final CosmosPersistentProperty idProperty = entity.getIdProperty();
final JsonNode idValue = jsonNode.get("id");
if (idProperty != null) {
Expand All @@ -90,7 +94,7 @@ private <R> R readInternal(final CosmosPersistentEntity<?> entity, Class<R> type
return objectMapper.treeToValue(objectNode, type);
} catch (JsonProcessingException e) {
throw new IllegalStateException("Failed to read the source document "
+ objectNode.toPrettyString()
+ jsonNode.toPrettyString()
+ " to target type "
+ type, e);
}
Expand Down