Skip to content

Commit 50cb681

Browse files
committed
throw IllegalArgument for wrong concreteType
1 parent 4c9a5e8 commit 50cb681

File tree

10 files changed

+35
-14
lines changed

10 files changed

+35
-14
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>org.sagebionetworks</groupId>
66
<artifactId>schema-to-pojo</artifactId>
7-
<version>0.6.7</version>
7+
<version>0.6.8</version>
88
<packaging>pom</packaging>
99
<name>schema-to-pojo</name>
1010
<description>Container for the rest of the sub-projects</description>

schema-to-pojo-core/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.7</version>
8+
<version>0.6.8</version>
99
</parent>
1010
<artifactId>schema-to-pojo-core</artifactId>
1111
<name>schema-to-pojo-core</name>

schema-to-pojo-gwt/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>schema-to-pojo</artifactId>
66
<groupId>org.sagebionetworks</groupId>
7-
<version>0.6.7</version>
7+
<version>0.6.8</version>
88
</parent>
99
<artifactId>schema-to-pojo-gwt</artifactId>
1010
<packaging>jar</packaging>

schema-to-pojo-integration-tests/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.7</version>
8+
<version>0.6.8</version>
99
</parent>
1010
<artifactId>schema-to-pojo-integration-tests</artifactId>
1111
<name>schema-to-pojo-integration-tests</name>

schema-to-pojo-integration-tests/src/test/java/org/sagebionetworks/jstp20/InterfaceTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,12 @@ public void testInterfaceWithMissingConcreteType() throws IOException, JSONObjec
4545
// The interfaceField should have a concreteType since it's defined as a SomeInterface that does not have a default concrete type
4646
String json = "{\"interfaceField\":{\"fromOne\":\"123\"}}";
4747

48-
JSONObjectAdapterException ex = assertThrows(JSONObjectAdapterException.class, () -> {
48+
String message = assertThrows(IllegalArgumentException.class, () -> {
4949
// Call under test
5050
EntityFactory.createEntityFromJSONString(json, HasInterfaceField.class);
51-
});
51+
}).getMessage();
5252

53-
assertEquals("Missing 'concreteType' property, cannot discriminate polymorphic type org.sagebionetworks.jstp20.SomeInterface", ex.getCause().getMessage());
53+
assertEquals("Missing 'concreteType' property, cannot discriminate polymorphic type org.sagebionetworks.jstp20.SomeInterface", message);
5454

5555
}
5656

@@ -105,12 +105,12 @@ public void testArraOfInterfaces() throws JSONObjectAdapterException{
105105
public void testArrayOfInterfacesWithMissingConcreteType() throws JSONObjectAdapterException {
106106
String json = "{\"list\":[{\"concreteType\":\"org.sagebionetworks.jstp20.OneImpl\",\"fromOne\":\"123\"},{\"fromOne\":\"456\"}]}";
107107

108-
JSONObjectAdapterException ex = assertThrows(JSONObjectAdapterException.class, () -> {
108+
String message = assertThrows(IllegalArgumentException.class, () -> {
109109
// Call under test
110110
EntityFactory.createEntityFromJSONString(json, HasListOfInterface.class);
111-
});
111+
}).getMessage();
112112

113-
assertEquals("Missing 'concreteType' property, cannot discriminate polymorphic type org.sagebionetworks.jstp20.SomeInterface", ex.getCause().getMessage());
113+
assertEquals("Missing 'concreteType' property, cannot discriminate polymorphic type org.sagebionetworks.jstp20.SomeInterface", message);
114114

115115
}
116116

schema-to-pojo-lib/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.7</version>
8+
<version>0.6.8</version>
99
</parent>
1010
<artifactId>schema-to-pojo-lib</artifactId>
1111
<name>schema-to-pojo-lib</name>

schema-to-pojo-maven-plugin/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<artifactId>schema-to-pojo</artifactId>
66
<groupId>org.sagebionetworks</groupId>
7-
<version>0.6.7</version>
7+
<version>0.6.8</version>
88
</parent>
99
<modelVersion>4.0.0</modelVersion>
1010
<artifactId>schema-to-pojo-maven-plugin</artifactId>

schema-to-pojo-org-json/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>schema-to-pojo</artifactId>
77
<groupId>org.sagebionetworks</groupId>
8-
<version>0.6.7</version>
8+
<version>0.6.8</version>
99
</parent>
1010
<artifactId>schema-to-pojo-org-json</artifactId>
1111
<name>schema-to-pojo-org-json</name>

schema-to-pojo-org-json/src/main/java/org/sagebionetworks/schema/adapter/org/json/EntityFactory.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,12 +159,18 @@ private static <T extends JSONEntity> T createEntityFromAdapter(Class<? extends
159159
if(clazz.isInterface()){
160160
String concreteType = extractConcreteType(adapter, clazz);
161161
// Use the concrete type to instantiate the object.
162-
newInstance = (T) Class.forName(concreteType).newInstance();
162+
try {
163+
newInstance = (T) Class.forName(concreteType).newInstance();
164+
} catch (ClassNotFoundException e) {
165+
throw new IllegalArgumentException(String.format("Unknown %s : '%s'",ObjectSchema.CONCRETE_TYPE, concreteType), e);
166+
}
163167
}else{
164168
newInstance = clazz.newInstance();
165169
}
166170
newInstance.initializeFromJSONObject(adapter);
167171
return newInstance;
172+
} catch (IllegalArgumentException e) {
173+
throw e;
168174
} catch (Exception e) {
169175
throw new JSONObjectAdapterException(e);
170176
}

schema-to-pojo-org-json/src/test/java/org/sagebionetworks/schema/adapter/org/json/EntityFactoryTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.json.JSONObject;
1212
import org.junit.jupiter.api.Test;
13+
import org.sagebionetworks.schema.ObjectSchema;
1314
import org.sagebionetworks.schema.adapter.JSONEntity;
1415
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
1516

@@ -221,4 +222,18 @@ public void testReadFromJSONArrayStringWithNullClazz() throws JSONObjectAdapterE
221222
}).getMessage();
222223
assertEquals("JSONEntity class cannot be null", message);
223224
}
225+
226+
/**
227+
* Test added for PLFM-6906
228+
* @throws JSONObjectAdapterException
229+
*/
230+
@Test
231+
public void testCreateEntityFromJSONStringWithClassNotFound() throws JSONObjectAdapterException {
232+
JSONObject jsonEntity = new JSONObject();
233+
jsonEntity.put(ObjectSchema.CONCRETE_TYPE, "not.a.real.class");
234+
String message = assertThrows(IllegalArgumentException.class, ()->{
235+
EntityFactory.createEntityFromJSONObject(jsonEntity, SimpleInterface.class);
236+
}).getMessage();
237+
assertEquals("Unknown concreteType : 'not.a.real.class'", message);
238+
}
224239
}

0 commit comments

Comments
 (0)