Skip to content

Commit 78eba86

Browse files
committed
Polishing.
Introduce assertions for OutboundRow. Closes #593
1 parent 2e116df commit 78eba86

File tree

4 files changed

+424
-79
lines changed

4 files changed

+424
-79
lines changed

src/test/java/org/springframework/data/r2dbc/core/PostgresReactiveDataAccessStrategyTests.java

Lines changed: 24 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616
package org.springframework.data.r2dbc.core;
1717

18-
import static org.assertj.core.api.Assertions.*;
18+
import static org.springframework.data.r2dbc.testing.Assertions.*;
1919

2020
import lombok.RequiredArgsConstructor;
2121

@@ -33,7 +33,6 @@
3333
import org.springframework.data.r2dbc.dialect.PostgresDialect;
3434
import org.springframework.data.r2dbc.mapping.OutboundRow;
3535
import org.springframework.data.relational.core.sql.SqlIdentifier;
36-
import org.springframework.r2dbc.core.Parameter;
3736

3837
/**
3938
* {@link PostgresDialect} specific tests for {@link ReactiveDataAccessStrategy}.
@@ -54,26 +53,23 @@ void shouldConvertPrimitiveMultidimensionArrayToWrapper() {
5453

5554
OutboundRow row = strategy.getOutboundRow(new WithMultidimensionalArray(new int[][] { { 1, 2, 3 }, { 4, 5 } }));
5655

57-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).hasValue()).isTrue();
58-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).getValue()).isInstanceOf(Integer[][].class);
56+
assertThat(row).withColumn("myarray").hasValueInstanceOf(Integer[][].class);
5957
}
6058

6159
@Test // gh-161
6260
void shouldConvertNullArrayToDriverArrayType() {
6361

6462
OutboundRow row = strategy.getOutboundRow(new WithMultidimensionalArray(null));
6563

66-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).hasValue()).isFalse();
67-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).getType()).isEqualTo(Integer[].class);
64+
assertThat(row).withColumn("myarray").isEmpty().hasType(Integer[].class);
6865
}
6966

7067
@Test // gh-161
7168
void shouldConvertCollectionToArray() {
7269

7370
OutboundRow row = strategy.getOutboundRow(new WithIntegerCollection(Arrays.asList(1, 2, 3)));
7471

75-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).hasValue()).isTrue();
76-
assertThat(row.get(SqlIdentifier.unquoted("myarray")).getValue()).isInstanceOf(Integer[].class);
72+
assertThat(row).withColumn("myarray").hasValueInstanceOf(Integer[].class);
7773
assertThat((Integer[]) row.get(SqlIdentifier.unquoted("myarray")).getValue()).contains(1, 2, 3);
7874
}
7975

@@ -88,9 +84,8 @@ void shouldConvertToArray() {
8884

8985
OutboundRow outboundRow = strategy.getOutboundRow(withArray);
9086

91-
assertThat(outboundRow) //
92-
.containsEntry(SqlIdentifier.unquoted("string_array"), Parameter.from(new String[] { "hello", "world" }))
93-
.containsEntry(SqlIdentifier.unquoted("string_list"), Parameter.from(new String[] { "hello", "world" }));
87+
assertThat(outboundRow).containsColumnWithValue("string_array", new String[] { "hello", "world" })
88+
.containsColumnWithValue("string_list", new String[] { "hello", "world" });
9489
}
9590

9691
@Test // gh-139
@@ -104,8 +99,7 @@ void shouldApplyCustomConversion() {
10499

105100
OutboundRow outboundRow = strategy.getOutboundRow(withConversion);
106101

107-
assertThat(outboundRow) //
108-
.containsEntry(SqlIdentifier.unquoted("my_objects"), Parameter.from("[one, two]"));
102+
assertThat(outboundRow).containsColumnWithValue("my_objects", "[one, two]");
109103
}
110104

111105
@Test // gh-139
@@ -119,12 +113,7 @@ void shouldApplyCustomConversionForNull() {
119113

120114
OutboundRow outboundRow = strategy.getOutboundRow(withConversion);
121115

122-
assertThat(outboundRow) //
123-
.containsKey(SqlIdentifier.unquoted("my_objects"));
124-
125-
Parameter value = outboundRow.get("my_objects");
126-
assertThat(value.isEmpty()).isTrue();
127-
assertThat(value.getType()).isEqualTo(String.class);
116+
assertThat(outboundRow).containsColumn("my_objects").withColumn("my_objects").isEmpty().hasType(String.class);
128117
}
129118

130119
@Test // gh-252, gh-593
@@ -139,16 +128,10 @@ void shouldConvertCollectionOfEnumToString() {
139128

140129
OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
141130

142-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_set"));
143-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_set")).getValue()).isEqualTo(new String[] { "ONE", "TWO" });
144-
145-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_array"));
146-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_array")).getValue())
147-
.isEqualTo(new String[] { "ONE", "TWO" });
148-
149-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_list"));
150-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_list")).getValue())
151-
.isEqualTo(new String[] { "ONE", "TWO" });
131+
assertThat(outboundRow).containsColumns("enum_set", "enum_array", "enum_list");
132+
assertThat(outboundRow).withColumn("enum_set").hasValue(new String[] { "ONE", "TWO" }).hasType(String[].class);
133+
assertThat(outboundRow).withColumn("enum_array").hasValue(new String[] { "ONE", "TWO" }).hasType(String[].class);
134+
assertThat(outboundRow).withColumn("enum_list").hasValue(new String[] { "ONE", "TWO" }).hasType(String[].class);
152135
}
153136

154137
@Test // gh-593
@@ -160,14 +143,10 @@ void shouldCorrectlyWriteConvertedEnumNullValues() {
160143

161144
OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
162145

163-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_set"));
164-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_set")).getType()).isEqualTo(String[].class);
165-
166-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_array"));
167-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_array")).getType()).isEqualTo(String[].class);
168-
169-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_list"));
170-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_list")).getType()).isEqualTo(String[].class);
146+
assertThat(outboundRow).containsColumns("enum_set", "enum_array", "enum_list");
147+
assertThat(outboundRow).withColumn("enum_set").isEmpty().hasType(String[].class);
148+
assertThat(outboundRow).withColumn("enum_array").isEmpty().hasType(String[].class);
149+
assertThat(outboundRow).withColumn("enum_list").isEmpty().hasType(String[].class);
171150
}
172151

173152
@Test // gh-593
@@ -183,14 +162,10 @@ void shouldConvertCollectionOfEnumNatively() {
183162

184163
OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
185164

186-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_set"));
187-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_set")).getValue()).isInstanceOf(MyEnum[].class);
188-
189-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_array"));
190-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_array")).getValue()).isInstanceOf(MyEnum[].class);
191-
192-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_list"));
193-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_list")).getValue()).isInstanceOf(MyEnum[].class);
165+
assertThat(outboundRow).containsColumns("enum_set", "enum_array", "enum_list");
166+
assertThat(outboundRow).withColumn("enum_set").hasValue().hasType(MyEnum[].class);
167+
assertThat(outboundRow).withColumn("enum_array").hasValue().hasType(MyEnum[].class);
168+
assertThat(outboundRow).withColumn("enum_list").hasValue().hasType(MyEnum[].class);
194169
}
195170

196171
@Test // gh-593
@@ -203,14 +178,10 @@ void shouldCorrectlyWriteNativeEnumNullValues() {
203178

204179
OutboundRow outboundRow = strategy.getOutboundRow(withEnums);
205180

206-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_set"));
207-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_set")).getType()).isEqualTo(MyEnum[].class);
208-
209-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_array"));
210-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_array")).getType()).isEqualTo(MyEnum[].class);
211-
212-
assertThat(outboundRow).containsKey(SqlIdentifier.unquoted("enum_list"));
213-
assertThat(outboundRow.get(SqlIdentifier.unquoted("enum_list")).getType()).isEqualTo(MyEnum[].class);
181+
assertThat(outboundRow).containsColumns("enum_set", "enum_array", "enum_list");
182+
assertThat(outboundRow).withColumn("enum_set").isEmpty().hasType(MyEnum[].class);
183+
assertThat(outboundRow).withColumn("enum_array").isEmpty().hasType(MyEnum[].class);
184+
assertThat(outboundRow).withColumn("enum_list").isEmpty().hasType(MyEnum[].class);
214185
}
215186

216187
@RequiredArgsConstructor

src/test/java/org/springframework/data/r2dbc/core/ReactiveDataAccessStrategyTestSupport.java

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -52,135 +52,135 @@ public abstract class ReactiveDataAccessStrategyTestSupport {
5252
protected abstract ReactiveDataAccessStrategy getStrategy();
5353

5454
@Test // gh-85
55-
public void shouldReadAndWriteString() {
55+
void shouldReadAndWriteString() {
5656
testType(PrimitiveTypes::setString, PrimitiveTypes::getString, "foo", "string");
5757
}
5858

5959
@Test // gh-85
60-
public void shouldReadAndWriteCharacter() {
60+
void shouldReadAndWriteCharacter() {
6161
testType(PrimitiveTypes::setCharacter, PrimitiveTypes::getCharacter, 'f', "character");
6262
}
6363

6464
@Test // gh-85
65-
public void shouldReadAndWriteBoolean() {
65+
void shouldReadAndWriteBoolean() {
6666
testType(PrimitiveTypes::setBooleanValue, PrimitiveTypes::isBooleanValue, true, "boolean_value");
6767
}
6868

6969
@Test // gh-85
70-
public void shouldReadAndWriteBoxedBoolean() {
70+
void shouldReadAndWriteBoxedBoolean() {
7171
testType(PrimitiveTypes::setBoxedBooleanValue, PrimitiveTypes::getBoxedBooleanValue, true, "boxed_boolean_value");
7272
}
7373

7474
@Test // gh-85
75-
public void shouldReadAndWriteByte() {
75+
void shouldReadAndWriteByte() {
7676
testType(PrimitiveTypes::setByteValue, PrimitiveTypes::getByteValue, (byte) 123, "byte_value");
7777
}
7878

7979
@Test // gh-85
80-
public void shouldReadAndWriteBoxedByte() {
80+
void shouldReadAndWriteBoxedByte() {
8181
testType(PrimitiveTypes::setBoxedByteValue, PrimitiveTypes::getBoxedByteValue, (byte) 123, "boxed_byte_value");
8282
}
8383

8484
@Test // gh-85
85-
public void shouldReadAndWriteShort() {
85+
void shouldReadAndWriteShort() {
8686
testType(PrimitiveTypes::setShortValue, PrimitiveTypes::getShortValue, (short) 123, "short_value");
8787
}
8888

8989
@Test // gh-85
90-
public void shouldReadAndWriteBoxedShort() {
90+
void shouldReadAndWriteBoxedShort() {
9191
testType(PrimitiveTypes::setBoxedShortValue, PrimitiveTypes::getBoxedShortValue, (short) 123, "boxed_short_value");
9292
}
9393

9494
@Test // gh-85
95-
public void shouldReadAndWriteInteger() {
95+
void shouldReadAndWriteInteger() {
9696
testType(PrimitiveTypes::setIntValue, PrimitiveTypes::getIntValue, 123, "int_value");
9797
}
9898

9999
@Test // gh-85
100-
public void shouldReadAndWriteBoxedInteger() {
100+
void shouldReadAndWriteBoxedInteger() {
101101
testType(PrimitiveTypes::setBoxedIntegerValue, PrimitiveTypes::getBoxedIntegerValue, 123, "boxed_integer_value");
102102
}
103103

104104
@Test // gh-85
105-
public void shouldReadAndWriteLong() {
105+
void shouldReadAndWriteLong() {
106106
testType(PrimitiveTypes::setLongValue, PrimitiveTypes::getLongValue, 123L, "long_value");
107107
}
108108

109109
@Test // gh-85
110-
public void shouldReadAndWriteBoxedLong() {
110+
void shouldReadAndWriteBoxedLong() {
111111
testType(PrimitiveTypes::setBoxedLongValue, PrimitiveTypes::getBoxedLongValue, 123L, "boxed_long_value");
112112
}
113113

114114
@Test // gh-85
115-
public void shouldReadAndWriteFloat() {
115+
void shouldReadAndWriteFloat() {
116116
testType(PrimitiveTypes::setFloatValue, PrimitiveTypes::getFloatValue, 0.1f, "float_value");
117117
}
118118

119119
@Test // gh-85
120-
public void shouldReadAndWriteBoxedFloat() {
120+
void shouldReadAndWriteBoxedFloat() {
121121
testType(PrimitiveTypes::setBoxedFloatValue, PrimitiveTypes::getBoxedFloatValue, 0.1f, "boxed_float_value");
122122
}
123123

124124
@Test // gh-85
125-
public void shouldReadAndWriteDouble() {
125+
void shouldReadAndWriteDouble() {
126126
testType(PrimitiveTypes::setDoubleValue, PrimitiveTypes::getDoubleValue, 0.1, "double_value");
127127
}
128128

129129
@Test // gh-85
130-
public void shouldReadAndWriteBoxedDouble() {
130+
void shouldReadAndWriteBoxedDouble() {
131131
testType(PrimitiveTypes::setBoxedDoubleValue, PrimitiveTypes::getBoxedDoubleValue, 0.1, "boxed_double_value");
132132
}
133133

134134
@Test // gh-85
135-
public void shouldReadAndWriteBigInteger() {
135+
void shouldReadAndWriteBigInteger() {
136136
testType(PrimitiveTypes::setBigInteger, PrimitiveTypes::getBigInteger, BigInteger.TEN, "big_integer");
137137
}
138138

139139
@Test // gh-85
140-
public void shouldReadAndWriteBigDecimal() {
140+
void shouldReadAndWriteBigDecimal() {
141141
testType(PrimitiveTypes::setBigDecimal, PrimitiveTypes::getBigDecimal, new BigDecimal("100.123"), "big_decimal");
142142
}
143143

144144
@Test // gh-85
145-
public void shouldReadAndWriteLocalDate() {
145+
void shouldReadAndWriteLocalDate() {
146146
testType(PrimitiveTypes::setLocalDate, PrimitiveTypes::getLocalDate, LocalDate.now(), "local_date");
147147
}
148148

149149
@Test // gh-85
150-
public void shouldReadAndWriteLocalTime() {
150+
void shouldReadAndWriteLocalTime() {
151151
testType(PrimitiveTypes::setLocalTime, PrimitiveTypes::getLocalTime, LocalTime.now(), "local_time");
152152
}
153153

154154
@Test // gh-85
155-
public void shouldReadAndWriteLocalDateTime() {
155+
void shouldReadAndWriteLocalDateTime() {
156156
testType(PrimitiveTypes::setLocalDateTime, PrimitiveTypes::getLocalDateTime, LocalDateTime.now(),
157157
"local_date_time");
158158
}
159159

160160
@Test // gh-85
161-
public void shouldReadAndWriteZonedDateTime() {
161+
void shouldReadAndWriteZonedDateTime() {
162162
testType(PrimitiveTypes::setZonedDateTime, PrimitiveTypes::getZonedDateTime, ZonedDateTime.now(),
163163
"zoned_date_time");
164164
}
165165

166166
@Test // gh-85
167-
public void shouldReadAndWriteOffsetDateTime() {
167+
void shouldReadAndWriteOffsetDateTime() {
168168
testType(PrimitiveTypes::setOffsetDateTime, PrimitiveTypes::getOffsetDateTime, OffsetDateTime.now(),
169169
"offset_date_time");
170170
}
171171

172172
@Test // gh-85
173-
public void shouldReadAndWriteUuid() {
173+
void shouldReadAndWriteUuid() {
174174
testType(PrimitiveTypes::setUuid, PrimitiveTypes::getUuid, UUID.randomUUID(), "uuid");
175175
}
176176

177177
@Test // gh-186
178-
public void shouldReadAndWriteBinary() {
178+
void shouldReadAndWriteBinary() {
179179
testType(PrimitiveTypes::setBinary, PrimitiveTypes::getBinary, "hello".getBytes(), "binary");
180180
}
181181

182182
@Test // gh-354
183-
public void shouldNotWriteReadOnlyFields() {
183+
void shouldNotWriteReadOnlyFields() {
184184

185185
TypeWithReadOnlyFields toSave = new TypeWithReadOnlyFields();
186186

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2021 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.r2dbc.testing;
17+
18+
import org.springframework.data.r2dbc.mapping.OutboundRow;
19+
20+
/**
21+
* @author Mark Paluch
22+
*/
23+
public abstract class Assertions extends org.assertj.core.api.Assertions {
24+
25+
private Assertions() {}
26+
27+
/**
28+
* Create assertion for {@link OutboundRow}.
29+
*
30+
* @param actual the actual value.
31+
* @param <T> the type of the value contained in the {@link OutboundRow}.
32+
* @return the created assertion object.
33+
*/
34+
public static OutboundRowAssert assertThat(OutboundRow actual) {
35+
return OutboundRowAssert.assertThat(actual);
36+
}
37+
}

0 commit comments

Comments
 (0)