Skip to content

Commit 1293555

Browse files
committed
Add more unit tests
Signed-off-by: Binlong Gao <gbinlong@amazon.com>
1 parent ff22894 commit 1293555

File tree

5 files changed

+144
-26
lines changed

5 files changed

+144
-26
lines changed

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/response/exceptions/shardoperationfailedexception/DefaultShardOperationFailedExceptionProtoUtilsTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,4 +122,13 @@ public void testToProtoWithNullNodeId() throws IOException {
122122
assertFalse("Node should not be set", shardFailure.hasNode());
123123
assertNotNull("Reason should not be null", shardFailure.getReason());
124124
}
125+
126+
public void testToProtoWithNull() throws IOException {
127+
ShardFailure shardFailure = DefaultShardOperationFailedExceptionProtoUtils.toProto(null);
128+
assertNotNull("ShardFailure should not be null", shardFailure);
129+
assertFalse("Index should not be set", shardFailure.hasIndex());
130+
assertFalse("Status should not be set", shardFailure.hasStatus());
131+
assertFalse("Node should not be set", shardFailure.hasNode());
132+
assertFalse("Reason should not be set", shardFailure.hasReason());
133+
}
125134
}

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/proto/response/exceptions/shardoperationfailedexception/ShardOperationFailedExceptionProtoUtilsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,4 +119,14 @@ public void testToProtoWithUnsupportedShardOperationFailedException() {
119119
exception.getMessage().contains("Unsupported ShardOperationFailedException")
120120
);
121121
}
122+
123+
public void testToProtoWithNullShardOperationFailedException() {
124+
// Call the method under test with null, should throw UnsupportedOperationException
125+
UnsupportedOperationException exception = expectThrows(
126+
UnsupportedOperationException.class,
127+
() -> ShardOperationFailedExceptionProtoUtils.toProto(null)
128+
);
129+
130+
assertEquals("Unsupported ShardOperationFailedException [null] cannot be converted to proto.", exception.getMessage());
131+
}
122132
}

modules/transport-grpc/src/test/java/org/opensearch/transport/grpc/util/GrpcErrorHandlerTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,12 @@ public RestStatus status() {
279279
assertTrue(result.getMessage().contains("Root cause"));
280280
assertTrue(result.getMessage().contains("Wrapper exception"));
281281
}
282+
283+
public void testNullExceptionConversion() {
284+
StatusRuntimeException result = GrpcErrorHandler.convertToGrpcError(null);
285+
286+
// null -> INTERNAL via case null handling
287+
assertEquals(Status.INTERNAL.getCode(), result.getStatus().getCode());
288+
assertEquals("INTERNAL: Unexpected null exception", result.getMessage());
289+
}
282290
}

server/src/main/java/org/opensearch/common/inject/internal/MoreTypes.java

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,32 +143,37 @@ public static Type canonicalize(Type type) {
143143
}
144144

145145
public static Class<?> getRawType(Type type) {
146-
if (type instanceof Class<?>) {
147-
// type is a normal class.
148-
return (Class<?>) type;
149-
150-
} else if (type instanceof ParameterizedType parameterizedType) {
151-
152-
// I'm not exactly sure why getRawType() returns Type instead of Class.
153-
// Neal isn't either but suspects some pathological case related
154-
// to nested classes exists.
155-
Type rawType = parameterizedType.getRawType();
156-
if (!(rawType instanceof Class)) {
157-
throw new IllegalArgumentException("Expected a Class, but <" + type + "> is of type " + type.getClass().getName());
146+
switch (type) {
147+
case Class<?> aClass -> {
148+
// type is a normal class.
149+
return aClass;
150+
// type is a normal class.
158151
}
159-
return (Class<?>) rawType;
160-
161-
} else if (type instanceof GenericArrayType) {
162-
// TODO: Is this sufficient?
163-
return Object[].class;
164-
165-
} else if (type instanceof TypeVariable) {
166-
// we could use the variable's bounds, but that'll won't work if there are multiple.
167-
// having a raw type that's more general than necessary is okay
168-
return Object.class;
152+
case ParameterizedType parameterizedType -> {
169153

170-
} else {
171-
throw new IllegalArgumentException(
154+
// I'm not exactly sure why getRawType() returns Type instead of Class.
155+
// Neal isn't either but suspects some pathological case related
156+
// to nested classes exists.
157+
Type rawType = parameterizedType.getRawType();
158+
if (!(rawType instanceof Class)) {
159+
throw new IllegalArgumentException("Expected a Class, but <" + type + "> is of type " + type.getClass().getName());
160+
}
161+
return (Class<?>) rawType;
162+
}
163+
case GenericArrayType ignored -> {
164+
// TODO: Is this sufficient?
165+
return Object[].class;
166+
// TODO: Is this sufficient?
167+
}
168+
case TypeVariable<?> ignored -> {
169+
// we could use the variable's bounds, but that'll won't work if there are multiple.
170+
// having a raw type that's more general than necessary is okay
171+
return Object.class;
172+
// we could use the variable's bounds, but that'll won't work if there are multiple.
173+
// having a raw type that's more general than necessary is okay
174+
}
175+
case null -> throw new IllegalArgumentException("Unsupported type [null]");
176+
default -> throw new IllegalArgumentException(
172177
"Expected a Class, ParameterizedType, or " + "GenericArrayType, but <" + type + "> is of type " + type.getClass().getName()
173178
);
174179
}
@@ -227,7 +232,7 @@ public static boolean equals(Type a, Type b) {
227232
*/
228233
public static int hashCode(Type type) {
229234
return switch (type) {
230-
case Class ignored ->
235+
case Class<?> ignored ->
231236
// Class specifies hashCode().
232237
type.hashCode();
233238
case ParameterizedType p -> Arrays.hashCode(p.getActualTypeArguments()) ^ p.getRawType().hashCode() ^ hashCodeOrZero(
@@ -305,7 +310,7 @@ public static Class<? extends Member> memberType(Member member) {
305310
case MemberImpl memberImpl -> memberImpl.memberType;
306311
case Field ignored -> Field.class;
307312
case Method ignored -> Method.class;
308-
case Constructor ignored -> Constructor.class;
313+
case Constructor<?> ignored -> Constructor.class;
309314
default -> throw new IllegalArgumentException("Unsupported implementation class for Member, " + member.getClass());
310315
};
311316
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* SPDX-License-Identifier: Apache-2.0
3+
*
4+
* The OpenSearch Contributors require contributions made to
5+
* this file be licensed under the Apache-2.0 license or a
6+
* compatible open source license.
7+
*/
8+
9+
package org.opensearch.common.inject.internal;
10+
11+
import org.opensearch.test.OpenSearchTestCase;
12+
13+
import java.lang.reflect.Type;
14+
15+
public class MoreTypesTests extends OpenSearchTestCase {
16+
17+
public void testHashCode() {
18+
assertEquals(0, MoreTypes.hashCode(null));
19+
assertEquals(String.class.hashCode(), MoreTypes.hashCode(String.class));
20+
21+
MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
22+
int expectedParamHash = java.util.Arrays.hashCode(new Type[] { String.class }) ^ java.util.List.class.hashCode();
23+
assertEquals(expectedParamHash, MoreTypes.hashCode(paramType));
24+
25+
MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
26+
assertEquals(String.class.hashCode(), MoreTypes.hashCode(arrayType));
27+
28+
MoreTypes.WildcardTypeImpl wildcardType = new MoreTypes.WildcardTypeImpl(new Type[] { String.class }, new Type[] {});
29+
int expectedWildcardHash = java.util.Arrays.hashCode(new Type[] {}) ^ java.util.Arrays.hashCode(new Type[] { String.class });
30+
assertEquals(expectedWildcardHash, MoreTypes.hashCode(wildcardType));
31+
}
32+
33+
public void testToString() {
34+
assertEquals("java.lang.String", MoreTypes.toString(String.class));
35+
36+
MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
37+
assertEquals("java.util.List<java.lang.String>", MoreTypes.toString(paramType));
38+
39+
MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
40+
assertEquals("java.lang.String[]", MoreTypes.toString(arrayType));
41+
42+
MoreTypes.WildcardTypeImpl unboundedWildcard = new MoreTypes.WildcardTypeImpl(
43+
new java.lang.reflect.Type[] { Object.class },
44+
new java.lang.reflect.Type[] {}
45+
);
46+
assertEquals("?", MoreTypes.toString(unboundedWildcard));
47+
48+
MoreTypes.WildcardTypeImpl upperBoundedWildcard = new MoreTypes.WildcardTypeImpl(
49+
new java.lang.reflect.Type[] { String.class },
50+
new java.lang.reflect.Type[] {}
51+
);
52+
assertEquals("? extends java.lang.String", MoreTypes.toString(upperBoundedWildcard));
53+
54+
MoreTypes.WildcardTypeImpl lowerBoundedWildcard = new MoreTypes.WildcardTypeImpl(
55+
new java.lang.reflect.Type[] { Object.class },
56+
new java.lang.reflect.Type[] { String.class }
57+
);
58+
assertEquals("? super java.lang.String", MoreTypes.toString(lowerBoundedWildcard));
59+
60+
UnsupportedOperationException exception = expectThrows(
61+
UnsupportedOperationException.class,
62+
() -> { MoreTypes.toString((Type) null); }
63+
);
64+
assertEquals("Unsupported wildcard type [null]", exception.getMessage());
65+
}
66+
67+
public void testGetRawType() {
68+
assertEquals(String.class, MoreTypes.getRawType(String.class));
69+
70+
MoreTypes.ParameterizedTypeImpl paramType = new MoreTypes.ParameterizedTypeImpl(null, java.util.List.class, String.class);
71+
assertEquals(java.util.List.class, MoreTypes.getRawType(paramType));
72+
73+
MoreTypes.GenericArrayTypeImpl arrayType = new MoreTypes.GenericArrayTypeImpl(String.class);
74+
assertEquals(Object[].class, MoreTypes.getRawType(arrayType));
75+
76+
java.lang.reflect.TypeVariable<?> typeVar = java.util.List.class.getTypeParameters()[0];
77+
assertEquals(Object.class, MoreTypes.getRawType(typeVar));
78+
79+
MoreTypes.WildcardTypeImpl wildcardType = new MoreTypes.WildcardTypeImpl(new Type[] { String.class }, new Type[] {});
80+
IllegalArgumentException wildcardException = expectThrows(IllegalArgumentException.class, () -> MoreTypes.getRawType(wildcardType));
81+
assertTrue(wildcardException.getMessage().contains("Expected a Class, ParameterizedType, or GenericArrayType"));
82+
83+
IllegalArgumentException nullException = expectThrows(IllegalArgumentException.class, () -> MoreTypes.getRawType(null));
84+
assertTrue(nullException.getMessage().contains("Unsupported type [null]"));
85+
}
86+
}

0 commit comments

Comments
 (0)