-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Test cases for suppressing null value in cosmos repository (#14273)
* Test cases for suppressing null value in cosmos repository * fix code smell
- Loading branch information
Showing
5 changed files
with
299 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
...re-spring-data-cosmos-test/src/test/java/com/azure/spring/data/cosmos/domain/Teacher.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
+ '\'' | ||
+ '}'; | ||
} | ||
} |
89 changes: 89 additions & 0 deletions
89
...java/com/azure/spring/data/cosmos/repository/integration/ReactiveTeacherRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Teacher, String> 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<Teacher> savedFlux = repository.saveAll(Arrays.asList(TEACHER_1)); | ||
StepVerifier.create(savedFlux).thenConsumeWhile(Teacher -> true).expectComplete().verify(); | ||
isSetupDone = true; | ||
} | ||
|
||
@After | ||
public void cleanup() { | ||
final Mono<Void> deletedMono = repository.deleteAll(); | ||
StepVerifier.create(deletedMono).thenAwait().verifyComplete(); | ||
} | ||
|
||
@AfterClass | ||
public static void afterClassCleanup() { | ||
staticTemplate.deleteContainer(entityInformation.getContainerName()); | ||
} | ||
|
||
@Test | ||
public void testSaveWithSuppressedNullValue() { | ||
final Mono<Void> 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<Teacher> saveSecond = repository.save(teacher); | ||
StepVerifier.create(saveSecond).expectNext(teacher).verifyComplete(); | ||
|
||
final Mono<Teacher> idMono = repository.findById(teacherId); | ||
StepVerifier.create(idMono).expectNext(teacher).expectComplete().verify(); | ||
|
||
final Mono<Boolean> existFirstNameMono = repository.existsByFirstNameIsNotNull(); | ||
StepVerifier.create(existFirstNameMono).expectNext(true).expectComplete().verify(); | ||
|
||
final Mono<Boolean> existLastNameMono = repository.existsByLastNameIsNull(); | ||
StepVerifier.create(existLastNameMono).expectNext(false).expectComplete().verify(); | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
...rc/test/java/com/azure/spring/data/cosmos/repository/integration/TeacherRepositoryIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Teacher, String> 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<Teacher> 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()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...st/java/com/azure/spring/data/cosmos/repository/repository/ReactiveTeacherRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.Teacher; | ||
import com.azure.spring.data.cosmos.repository.ReactiveCosmosRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
|
||
public interface ReactiveTeacherRepository extends ReactiveCosmosRepository<Teacher, String> { | ||
|
||
Mono<Boolean> existsByFirstNameIsNotNull(); | ||
|
||
Mono<Boolean> existsByLastNameIsNull(); | ||
} |
17 changes: 17 additions & 0 deletions
17
...t/src/test/java/com/azure/spring/data/cosmos/repository/repository/TeacherRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Teacher, String> { | ||
|
||
boolean existsByFirstNameIsNotNull(); | ||
|
||
boolean existsByLastNameIsNull(); | ||
} |