Skip to content

Commit a39a742

Browse files
committed
Some refactoring and added Unit Test for Struct
1 parent a1bbf04 commit a39a742

File tree

4 files changed

+74
-11
lines changed

4 files changed

+74
-11
lines changed

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
import java.sql.SQLException;
1010
import java.sql.Struct;
11+
import java.util.Arrays;
1112
import java.util.Map;
1213

1314
public class StructImpl implements Struct{
@@ -30,4 +31,24 @@ public Object[] getAttributes() throws SQLException {
3031
public Object[] getAttributes(Map<String,Class<?>> map) throws SQLException {
3132
throw new java.lang.UnsupportedOperationException("Not supported yet.");
3233
}
34+
35+
public boolean equals(Object obj) {
36+
if (!(obj instanceof Struct)) {
37+
return false;
38+
}
39+
if (obj == this) {
40+
return true;
41+
}
42+
Struct other = (Struct) obj;
43+
try {
44+
if (!typeName.equals(other.getSQLTypeName())) {
45+
return false;
46+
}
47+
Object[] otherAttributes = other.getAttributes();
48+
return Arrays.equals(attributes, otherAttributes);
49+
}
50+
catch (Exception e) {
51+
return false;
52+
}
53+
}
3354
}

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,7 @@ public String getTypeName() {
3030

3131
@Override
3232
public Struct fromValue(Object value, Map<String, Object> conversionParams) {
33-
int i = 0;
3433
Map<String, Object> structKeyValues = (Map<String, Object>) value;
35-
Object[] attributes = new Map.Entry[structKeyValues.size()];
36-
37-
for(Map.Entry<String, Object> entry: structKeyValues.entrySet()) {
38-
attributes[i++] = entry;
39-
}
40-
return new StructImpl(getTypeName(), attributes);
34+
return new StructImpl(getTypeName(), structKeyValues.entrySet().toArray());
4135
}
4236
}

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ public static TypeConverter getInstance(JDBCType jdbcType) {
6868

6969
public static class StructTypeConverter extends BaseTypeConverter {
7070

71-
private static final Set<Class> supportedJavaClasses = Collections.unmodifiableSet(
72-
new HashSet<>(Arrays.asList(
73-
Struct.class
74-
)));
71+
private static final Set<Class> supportedJavaClasses = Collections.singleton(Struct.class);
7572

7673
StructTypeConverter() {
7774

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
7+
package org.opensearch.jdbc.types;
8+
9+
import static org.junit.jupiter.api.Assertions.*;
10+
import org.junit.jupiter.api.Test;
11+
import org.opensearch.jdbc.types.StructType;
12+
import org.opensearch.jdbc.StructImpl;
13+
14+
import java.sql.Struct;
15+
import java.sql.SQLException;
16+
import java.util.Arrays;
17+
import java.util.Map;
18+
import java.util.HashMap;
19+
20+
public class StructTypeTests {
21+
22+
@Test
23+
public void testStructTypeFromValue() throws SQLException {
24+
Map<String, Object> attributes = new HashMap<String, Object>() {{
25+
put("attribute1", "value1");
26+
put("attribute2", 2);
27+
put("attribute3", 15.0);
28+
}};
29+
30+
Struct actualStruct = StructType.INSTANCE.fromValue(attributes, null);
31+
assertTrue(Arrays.equals(actualStruct.getAttributes(), attributes.entrySet().toArray()));
32+
assertEquals(actualStruct.getAttributes().length, 3);
33+
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);
41+
42+
Map<String, Object> nestedAttributes = new HashMap<>();
43+
nestedAttributes.put("struct", attributes);
44+
nestedAttributes.put("string", "hello");
45+
nestedAttributes.put("int", 1);
46+
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()));
50+
}
51+
}

0 commit comments

Comments
 (0)