Skip to content

Commit 255074c

Browse files
committed
Added Struct as column to ResultSetTests
Signed-off-by: Andrey Kuzin <[email protected]>
1 parent a39a742 commit 255074c

File tree

5 files changed

+80
-19
lines changed

5 files changed

+80
-19
lines changed

src/main/java/org/opensearch/jdbc/StructImpl.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.sql.Struct;
1111
import java.util.Arrays;
1212
import java.util.Map;
13+
import java.util.List;
1314

1415
public class StructImpl implements Struct{
1516
private final String typeName;
@@ -32,6 +33,7 @@ public Object[] getAttributes(Map<String,Class<?>> map) throws SQLException {
3233
throw new java.lang.UnsupportedOperationException("Not supported yet.");
3334
}
3435

36+
@Override
3537
public boolean equals(Object obj) {
3638
if (!(obj instanceof Struct)) {
3739
return false;
@@ -44,8 +46,8 @@ public boolean equals(Object obj) {
4446
if (!typeName.equals(other.getSQLTypeName())) {
4547
return false;
4648
}
47-
Object[] otherAttributes = other.getAttributes();
48-
return Arrays.equals(attributes, otherAttributes);
49+
List otherAttributes = Arrays.asList(other.getAttributes());
50+
return otherAttributes.containsAll(Arrays.asList(attributes));
4951
}
5052
catch (Exception e) {
5153
return false;

src/main/java/org/opensearch/jdbc/types/StructType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.Arrays;
1111
import java.util.List;
1212
import java.util.LinkedHashMap;
13+
import java.util.Collections;
1314

1415

1516

@@ -30,6 +31,9 @@ public String getTypeName() {
3031

3132
@Override
3233
public Struct fromValue(Object value, Map<String, Object> conversionParams) {
34+
if (value == null) {
35+
return null;
36+
}
3337
Map<String, Object> structKeyValues = (Map<String, Object>) value;
3438
return new StructImpl(getTypeName(), structKeyValues.entrySet().toArray());
3539
}

src/test/java/org/opensearch/jdbc/ResultSetTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.opensearch.jdbc.test.TestResources;
1414
import org.opensearch.jdbc.test.mocks.MockOpenSearch;
1515
import org.opensearch.jdbc.types.OpenSearchType;
16+
import org.opensearch.jdbc.types.StructType;
1617
import org.opensearch.jdbc.test.PerTestWireMockServerExtension;
1718
import org.opensearch.jdbc.test.WireMockServerHelpers;
1819
import org.opensearch.jdbc.test.mocks.MockResultSet;
@@ -32,6 +33,8 @@
3233
import java.sql.SQLException;
3334
import java.sql.Statement;
3435
import java.sql.Timestamp;
36+
import java.util.HashMap;
37+
import java.util.Map;
3538
import java.util.stream.Stream;
3639

3740
import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
@@ -176,6 +179,18 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
176179
Statement st = con.createStatement();
177180
ResultSet rs = assertDoesNotThrow(() -> st.executeQuery(queryMock.getSql()));
178181

182+
Map<String, Object> attributes = new HashMap<String, Object>() {{
183+
put("attribute1", "value1");
184+
put("attribute2", 2);
185+
put("attribute3", 15.0);
186+
}};
187+
188+
Map<String, Object> nestedAttributes = new HashMap<String, Object>() {{
189+
put("struct", attributes);
190+
put("string", "hello");
191+
put("int", 1);
192+
}};
193+
179194
assertNotNull(rs);
180195

181196
MockResultSetMetaData mockResultSetMetaData = MockResultSetMetaData.builder()
@@ -191,6 +206,7 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
191206
.column("testKeyword", OpenSearchType.KEYWORD)
192207
.column("testText", OpenSearchType.TEXT)
193208
.column("testDouble", OpenSearchType.DOUBLE)
209+
.column("testStruct", OpenSearchType.OBJECT)
194210
.build();
195211

196212
MockResultSetRows mockResultSetRows = MockResultSetRows.builder()
@@ -207,6 +223,21 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
207223
.column("Test String", false)
208224
.column("document3", false)
209225
.column((double) 0, true)
226+
.column(StructType.INSTANCE.fromValue(attributes, null), false)
227+
.row()
228+
.column(true, false)
229+
.column("1", false)
230+
.column((byte) 126, false)
231+
.column((float) 0, true)
232+
.column((long) 32000320003200030L, false)
233+
.column((short) 29000, false)
234+
.column((float) 0, true)
235+
.column(null, true)
236+
.column((double) 0, true)
237+
.column(null, true)
238+
.column(null, true)
239+
.column((double) 22.312423148903218, false)
240+
.column(null, true)
210241
.row()
211242
.column(true, false)
212243
.column("1", false)
@@ -220,6 +251,7 @@ void testNullableFieldsQuery(WireMockServer mockServer) throws SQLException, IOE
220251
.column(null, true)
221252
.column(null, true)
222253
.column((double) 22.312423148903218, false)
254+
.column(StructType.INSTANCE.fromValue(nestedAttributes, null), false)
223255
.build();
224256

225257
MockResultSet mockResultSet = new MockResultSet(mockResultSetMetaData, mockResultSetRows);

src/test/java/org/opensearch/jdbc/types/StructTypeTests.java

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,21 +31,15 @@ public void testStructTypeFromValue() throws SQLException {
3131
assertTrue(Arrays.equals(actualStruct.getAttributes(), attributes.entrySet().toArray()));
3232
assertEquals(actualStruct.getAttributes().length, 3);
3333
assertEquals(actualStruct, new StructImpl(StructType.INSTANCE.getTypeName(), attributes.entrySet().toArray()));
34-
}
35-
36-
@Test
37-
public void testNestedStructTypeFromValue() throws SQLException {
38-
Map<String, Object> attributes = new HashMap<>();
39-
attributes.put("attribute1", "value1");
40-
attributes.put("attribute2", 2);
4134

42-
Map<String, Object> nestedAttributes = new HashMap<>();
43-
nestedAttributes.put("struct", attributes);
44-
nestedAttributes.put("string", "hello");
45-
nestedAttributes.put("int", 1);
35+
Map<String, Object> nestedAttributes = new HashMap<String, Object>() {{
36+
put("struct", attributes);
37+
put("string", "hello");
38+
put("int", 1);
39+
}};
4640

47-
Struct actualStruct = StructType.INSTANCE.fromValue(nestedAttributes, null);
48-
assertTrue(Arrays.equals(actualStruct.getAttributes(), nestedAttributes.entrySet().toArray()));
49-
assertEquals(actualStruct, new StructImpl(StructType.INSTANCE.getTypeName(), nestedAttributes.entrySet().toArray()));
41+
Struct actualNestedStruct = StructType.INSTANCE.fromValue(nestedAttributes, null);
42+
assertTrue(Arrays.equals(actualNestedStruct.getAttributes(), nestedAttributes.entrySet().toArray()));
43+
assertEquals(actualNestedStruct, new StructImpl(StructType.INSTANCE.getTypeName(), nestedAttributes.entrySet().toArray()));
5044
}
51-
}
45+
}

src/test/resources/mock/protocol/json/queryresponse_nullablefields.json

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@
4747
{
4848
"name": "testDouble",
4949
"type": "double"
50+
},
51+
{
52+
"name": "testStruct",
53+
"type": "object"
5054
}
5155
],
5256
"total": 2,
@@ -63,6 +67,26 @@
6367
24.324234543532153,
6468
"Test String",
6569
"document3",
70+
null,
71+
{
72+
"attribute1": "value1",
73+
"attribute2": 2,
74+
"attribute3": 15.0
75+
}
76+
],
77+
[
78+
true,
79+
"1",
80+
126,
81+
null,
82+
32000320003200030,
83+
29000,
84+
null,
85+
null,
86+
null,
87+
null,
88+
null,
89+
22.312423148903218,
6690
null
6791
],
6892
[
@@ -77,9 +101,14 @@
77101
null,
78102
null,
79103
null,
80-
22.312423148903218
104+
22.312423148903218,
105+
{
106+
"struct": {"attribute1": "value1", "attribute2": 2, "attribute3": 15.0},
107+
"string": "hello",
108+
"int": 1
109+
}
81110
]
82111
],
83112
"size": 2,
84113
"status": 200
85-
}
114+
}

0 commit comments

Comments
 (0)