Skip to content

Commit 97df0fa

Browse files
wimdeblauwechristophstrobl
authored andcommitted
DATACMNS-1762 - Add tests for projections returning Optional.
Original Pull Request: #459
1 parent bd3992d commit 97df0fa

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

src/main/java/org/springframework/data/repository/util/ReactiveWrapperConverters.java

+1
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ public static boolean supports(Class<?> type) {
155155
*
156156
* @param type must not be {@literal null}.
157157
* @return will never be {@literal null}.
158+
* @since 2.4
158159
*/
159160
public static TypeInformation<?> unwrapWrapperTypes(TypeInformation<?> type) {
160161

src/test/java/org/springframework/data/projection/ProxyProjectionFactoryUnitTests.java

+75
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.HashMap;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Optional;
2627

2728
import org.junit.jupiter.api.Test;
2829
import org.springframework.aop.Advisor;
@@ -34,6 +35,8 @@
3435
* Unit tests for {@link ProxyProjectionFactory}.
3536
*
3637
* @author Oliver Gierke
38+
* @author Wim Deblauwe
39+
* @author Mark Paluch
3740
*/
3841
class ProxyProjectionFactoryUnitTests {
3942

@@ -218,6 +221,61 @@ void doesNotCreateWrappingProxyIfTargetImplementsProjectionInterface() {
218221
assertThat(factory.createProjection(Contact.class, customer)).isSameAs(customer);
219222
}
220223

224+
@Test // DATACMNS-1762
225+
void supportsOptionalAsReturnTypeIfEmpty() {
226+
227+
Customer customer = new Customer();
228+
customer.picture = null;
229+
230+
CustomerWithOptional excerpt = factory.createProjection(CustomerWithOptional.class, customer);
231+
232+
assertThat(excerpt.getPicture()).isEmpty();
233+
}
234+
235+
@Test // DATACMNS-1762
236+
void supportsOptionalAsReturnTypeIfPresent() {
237+
238+
Customer customer = new Customer();
239+
customer.picture = new byte[] { 1, 2, 3 };
240+
241+
CustomerWithOptional excerpt = factory.createProjection(CustomerWithOptional.class, customer);
242+
243+
assertThat(excerpt.getPicture()).hasValueSatisfying(bytes -> {
244+
assertThat(bytes).isEqualTo(new byte[] { 1, 2, 3 });
245+
});
246+
}
247+
248+
@Test // DATACMNS-1762
249+
void supportsOptionalBackedByOptional() {
250+
251+
Customer customer = new Customer();
252+
customer.optional = Optional.of("foo");
253+
254+
CustomerWithOptional excerpt = factory.createProjection(CustomerWithOptional.class, customer);
255+
256+
assertThat(excerpt.getOptional()).hasValue("foo");
257+
}
258+
259+
@Test // DATACMNS-1762
260+
void supportsOptionalWithProjectionAsReturnTypeIfPresent() {
261+
262+
Customer customer = new Customer();
263+
customer.firstname = "Dave";
264+
customer.lastname = "Matthews";
265+
266+
customer.address = new Address();
267+
customer.address.city = "New York";
268+
customer.address.zipCode = "ZIP";
269+
270+
CustomerWithOptionalHavingProjection excerpt = factory.createProjection(CustomerWithOptionalHavingProjection.class,
271+
customer);
272+
273+
assertThat(excerpt.getFirstname()).isEqualTo("Dave");
274+
assertThat(excerpt.getAddress()).hasValueSatisfying(addressExcerpt -> {
275+
assertThat(addressExcerpt.getZipCode()).isEqualTo("ZIP");
276+
});
277+
}
278+
221279
interface Contact {}
222280

223281
static class Customer implements Contact {
@@ -228,6 +286,7 @@ static class Customer implements Contact {
228286
byte[] picture;
229287
Address[] shippingAddresses;
230288
Map<String, Object> data;
289+
Optional<String> optional;
231290
}
232291

233292
static class Address {
@@ -261,4 +320,20 @@ interface CustomerProxy {
261320

262321
void setFirstname(String firstname);
263322
}
323+
324+
interface CustomerWithOptional {
325+
326+
String getFirstname();
327+
328+
Optional<byte[]> getPicture();
329+
330+
Optional<String> getOptional();
331+
}
332+
333+
interface CustomerWithOptionalHavingProjection {
334+
335+
String getFirstname();
336+
337+
Optional<AddressExcerpt> getAddress();
338+
}
264339
}

0 commit comments

Comments
 (0)