diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Teacher.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Teacher.java new file mode 100644 index 0000000000000..7acf7225a56ed --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Teacher.java @@ -0,0 +1,89 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.spring.data.cosmos.domain; + +import com.azure.cosmos.models.IndexingMode; +import com.azure.spring.data.cosmos.core.mapping.Container; +import com.azure.spring.data.cosmos.core.mapping.CosmosIndexingPolicy; +import com.fasterxml.jackson.annotation.JsonInclude; + +import java.util.Objects; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@Container +@CosmosIndexingPolicy(mode = IndexingMode.CONSISTENT) +public class Teacher { + private String id; + private String firstName; + private String lastName; + + public Teacher() { + } + + public Teacher(String id, String firstName, String lastName) { + this.id = id; + this.firstName = firstName; + this.lastName = lastName; + } + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + if (null != lastName) { + this.lastName = lastName + "-changed"; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Teacher teacher = (Teacher) o; + return Objects.equals(id, teacher.id) + && Objects.equals(firstName, teacher.firstName) + && Objects.equals(lastName, teacher.lastName); + } + + @Override + public int hashCode() { + return Objects.hash(id, firstName, lastName); + } + + @Override + public String toString() { + return "Teacher{" + + "id='" + + id + + '\'' + + ", firstName='" + + firstName + + '\'' + + ", lastName='" + + lastName + + '\'' + + '}'; + } +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java new file mode 100644 index 0000000000000..52df72ef726ea --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java @@ -0,0 +1,89 @@ +// 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.core.ReactiveCosmosTemplate; +import com.azure.spring.data.cosmos.domain.Teacher; +import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; +import com.azure.spring.data.cosmos.repository.repository.ReactiveTeacherRepository; +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 ReactiveTeacherRepositoryIT { + + private static final String TEACHER_ID_1 = "1"; + + private static final String TEACHER_FIRST_NAME_1 = "FirstName1"; + + private static final String DEPARTMENT_LAST_NAME_1 = "LastName1"; + + private static final Teacher TEACHER_1 = new Teacher(TEACHER_ID_1, TEACHER_FIRST_NAME_1, DEPARTMENT_LAST_NAME_1); + + private static final CosmosEntityInformation entityInformation = + new CosmosEntityInformation<>(Teacher.class); + + private static ReactiveCosmosTemplate staticTemplate; + private static boolean isSetupDone; + + @Autowired + private ReactiveCosmosTemplate template; + + @Autowired + private ReactiveTeacherRepository repository; + + @Before + public void setUp() { + if (!isSetupDone) { + staticTemplate = template; + template.createContainerIfNotExists(entityInformation); + } + final Flux savedFlux = repository.saveAll(Arrays.asList(TEACHER_1)); + StepVerifier.create(savedFlux).thenConsumeWhile(Teacher -> true).expectComplete().verify(); + isSetupDone = true; + } + + @After + public void cleanup() { + final Mono deletedMono = repository.deleteAll(); + StepVerifier.create(deletedMono).thenAwait().verifyComplete(); + } + + @AfterClass + public static void afterClassCleanup() { + staticTemplate.deleteContainer(entityInformation.getContainerName()); + } + + @Test + public void testSaveWithSuppressedNullValue() { + final Mono deletedMono = repository.deleteAll(); + StepVerifier.create(deletedMono).thenAwait().verifyComplete(); + String teacherId = TEACHER_ID_1 + "-Other"; + final Teacher teacher = new Teacher(teacherId, TEACHER_FIRST_NAME_1, null); + final Mono saveSecond = repository.save(teacher); + StepVerifier.create(saveSecond).expectNext(teacher).verifyComplete(); + + final Mono idMono = repository.findById(teacherId); + StepVerifier.create(idMono).expectNext(teacher).expectComplete().verify(); + + final Mono existFirstNameMono = repository.existsByFirstNameIsNotNull(); + StepVerifier.create(existFirstNameMono).expectNext(true).expectComplete().verify(); + + final Mono existLastNameMono = repository.existsByLastNameIsNull(); + StepVerifier.create(existLastNameMono).expectNext(false).expectComplete().verify(); + } +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java new file mode 100644 index 0000000000000..b9a0bf032ec6b --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java @@ -0,0 +1,88 @@ +// 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.core.CosmosTemplate; +import com.azure.spring.data.cosmos.domain.Teacher; +import com.azure.spring.data.cosmos.repository.TestRepositoryConfig; +import com.azure.spring.data.cosmos.repository.repository.TeacherRepository; +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.Optional; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; + + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = TestRepositoryConfig.class) +public class TeacherRepositoryIT { + public static final String ID_0 = "id-0"; + + public static final String FIRST_NAME_0 = "Moary"; + + public static final String LAST_NAME_0 = "Chen"; + + private static final Teacher TEACHER_0 = new Teacher(ID_0, FIRST_NAME_0, LAST_NAME_0); + + private static final CosmosEntityInformation entityInformation = + new CosmosEntityInformation<>(Teacher.class); + + private static CosmosTemplate staticTemplate; + private static boolean isSetupDone; + + @Autowired + private CosmosTemplate template; + + @Autowired + private TeacherRepository repository; + + @Before + public void setUp() { + if (!isSetupDone) { + staticTemplate = template; + template.createContainerIfNotExists(entityInformation); + } + this.repository.save(TEACHER_0); + isSetupDone = true; + } + + @After + public void cleanup() { + this.repository.deleteAll(); + } + + @AfterClass + public static void afterClassCleanup() { + staticTemplate.deleteContainer(entityInformation.getContainerName()); + } + + @Test + public void testSaveWithSuppressedNullValue() { + this.repository.deleteAll(); + String teacherId = ID_0 + "-Other"; + final Teacher teacher = new Teacher(teacherId, FIRST_NAME_0, null); + Teacher save = repository.save(teacher); + assertNotNull(save); + + final Optional optionalTeacher = repository.findById(teacherId); + assertTrue(optionalTeacher.isPresent()); + assertTrue(optionalTeacher.get().getId().equals(teacherId)); + assertTrue(optionalTeacher.get().getFirstName().equals(FIRST_NAME_0)); + assertNull(optionalTeacher.get().getLastName()); + + assertTrue(repository.existsByFirstNameIsNotNull()); + assertFalse(repository.existsByLastNameIsNull()); + } +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ReactiveTeacherRepository.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ReactiveTeacherRepository.java new file mode 100644 index 0000000000000..f0028f8f00602 --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/ReactiveTeacherRepository.java @@ -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.Teacher; +import com.azure.spring.data.cosmos.repository.ReactiveCosmosRepository; +import reactor.core.publisher.Mono; + + +public interface ReactiveTeacherRepository extends ReactiveCosmosRepository { + + Mono existsByFirstNameIsNotNull(); + + Mono existsByLastNameIsNull(); +} diff --git a/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/TeacherRepository.java b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/TeacherRepository.java new file mode 100644 index 0000000000000..3c7a566d2ad9e --- /dev/null +++ b/sdk/cosmos/azure-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/repository/repository/TeacherRepository.java @@ -0,0 +1,17 @@ +// 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.Teacher; +import com.azure.spring.data.cosmos.repository.CosmosRepository; +import org.springframework.stereotype.Repository; + + +@Repository +public interface TeacherRepository extends CosmosRepository { + + boolean existsByFirstNameIsNotNull(); + + boolean existsByLastNameIsNull(); +}