summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2016-01-30 17:12:57 +0100
committerYves Fischer <yvesf-git@xapek.org>2016-01-30 17:12:57 +0100
commitd43f0042caf4ecafefdf4b243a4b9fb60dcb7922 (patch)
tree0676c4115b475d3779497cab2d5fa5ff39da95cb
parentde7422c1fa5d4c35b07ea58a6f3e91a0f94b7981 (diff)
downloadglocals-classifieds-d43f0042caf4ecafefdf4b243a4b9fb60dcb7922.tar.gz
glocals-classifieds-d43f0042caf4ecafefdf4b243a4b9fb60dcb7922.zip
fix test and in-parser exception reporting
-rw-r--r--src/main/groovy/org/xapek/yvesf/classifieds/Main.groovy6
-rw-r--r--src/main/groovy/org/xapek/yvesf/classifieds/Model.groovy7
-rw-r--r--src/main/groovy/org/xapek/yvesf/classifieds/util/ParserStaticMethods.groovy3
-rw-r--r--src/test/groovy/org/xapek/yvesf/classifieds/UnitTest.groovy12
4 files changed, 21 insertions, 7 deletions
diff --git a/src/main/groovy/org/xapek/yvesf/classifieds/Main.groovy b/src/main/groovy/org/xapek/yvesf/classifieds/Main.groovy
index 23fb434..67b2067 100644
--- a/src/main/groovy/org/xapek/yvesf/classifieds/Main.groovy
+++ b/src/main/groovy/org/xapek/yvesf/classifieds/Main.groovy
@@ -59,7 +59,11 @@ final class Main {
private static Failable<Model.Classified> handleClassified(Object p) {
ifType(p, Map) then { Map map ->
- success(new Model.Classified(map))
+ try {
+ return success(new Model.Classified(map))
+ } catch (Exception e) {
+ return fail("type=${e.class}: ${e.message}") as Failable<Model.Classified>
+ }
}
}
} \ No newline at end of file
diff --git a/src/main/groovy/org/xapek/yvesf/classifieds/Model.groovy b/src/main/groovy/org/xapek/yvesf/classifieds/Model.groovy
index cced09b..1887f30 100644
--- a/src/main/groovy/org/xapek/yvesf/classifieds/Model.groovy
+++ b/src/main/groovy/org/xapek/yvesf/classifieds/Model.groovy
@@ -57,7 +57,10 @@ class Model {
Object photo4
Classified(Map map) {
- metaClass.properties.each { MetaProperty property ->
+ metaClass.properties.grep { it instanceof MetaBeanProperty }.each { MetaProperty property ->
+ if (!(property as MetaBeanProperty).field) return // ignore setters/getters
+ if (property.modifiers != 1) return // only public fields
+
if (map.containsKey(property.name)) {
final value = map.get(property.name)
if (property.type.isAssignableFrom(value.getClass())) {
@@ -65,6 +68,8 @@ class Model {
} else {
throw new IllegalArgumentException("Cannot set field ${property.name} with object of type ${value.getClass()}")
}
+ } else {
+ throw new IllegalArgumentException("Missing data for field ${property.name}")
}
}
}
diff --git a/src/main/groovy/org/xapek/yvesf/classifieds/util/ParserStaticMethods.groovy b/src/main/groovy/org/xapek/yvesf/classifieds/util/ParserStaticMethods.groovy
index 15524c8..5ee4a24 100644
--- a/src/main/groovy/org/xapek/yvesf/classifieds/util/ParserStaticMethods.groovy
+++ b/src/main/groovy/org/xapek/yvesf/classifieds/util/ParserStaticMethods.groovy
@@ -1,9 +1,6 @@
package org.xapek.yvesf.classifieds.util
import groovy.transform.CompileStatic
-import org.xapek.yvesf.classifieds.util.Fail
-import org.xapek.yvesf.classifieds.util.Failable
-import org.xapek.yvesf.classifieds.util.Success
@CompileStatic
class ParserStaticMethods {
diff --git a/src/test/groovy/org/xapek/yvesf/classifieds/UnitTest.groovy b/src/test/groovy/org/xapek/yvesf/classifieds/UnitTest.groovy
index 7c477b4..6d6f764 100644
--- a/src/test/groovy/org/xapek/yvesf/classifieds/UnitTest.groovy
+++ b/src/test/groovy/org/xapek/yvesf/classifieds/UnitTest.groovy
@@ -1,8 +1,10 @@
package org.xapek.yvesf.classifieds
+import groovy.transform.CompileStatic
import org.junit.Test
import org.xapek.yvesf.classifieds.util.Success
+@CompileStatic
class UnitTest {
@Test
void testRead() {
@@ -15,7 +17,6 @@ class UnitTest {
final data = Main.handleData(Main.readInputStream(UnitTest.getResourceAsStream('test.json')))
assert data instanceof Success
assert data.success
- assert !data.error
assert data.value instanceof Model.ClassifiedsList
assert data.value.totalCount > 0
assert data.value.size() > 0
@@ -36,7 +37,14 @@ class UnitTest {
'title' : 'value=title',
'id' : 'value=id',
'mem_name' : 'value=mem_name',
+ 'mem_id' : '0',
'description': 'value=description',
+ 'photo1' : 'asd',
+ 'photo2' : 0,
+ 'photo3' : 0,
+ 'photo4' : 0,
+ 'date' : 'Apr 01, 99',
+ 'rooms' : '1',
]))
Dumper.dump(list, new PrintWriter(os))
final output = os.toString()
@@ -44,4 +52,4 @@ class UnitTest {
assert output.contains("value=title")
assert output.contains("value=description")
}
-} \ No newline at end of file
+}