use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ResultParserTest method manualJSONTest.
private void manualJSONTest() throws Exception {
String jsonData = "{\n" + " \"colors\": [\n" + " {\n" + " \"color\": \"black\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,255,255,1],\n" + " \"hex\": \"#000\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"white\",\n" + " \"category\": \"value\",\n" + " \"code\": {\n" + " \"rgba\": [0,0,0,1],\n" + " \"hex\": \"#FFF\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"red\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,0,0,1],\n" + " \"hex\": \"#FF0\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"blue\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [0,0,255,1],\n" + " \"hex\": \"#00F\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"yellow\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,255,0,1],\n" + " \"hex\": \"#FF0\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"green\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"secondary\",\n" + " \"code\": {\n" + " \"rgba\": [0,255,0,1],\n" + " \"hex\": \"#0F0\"\n" + " }\n" + " },\n" + " ]\n" + "}";
class Color extends BaseEntity {
}
Tag type = new Tag("type");
Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
return new Color();
}).build();
class Colors extends EntityList<Color> {
}
EntityType.registerList(Colors.class, Color.class, cls -> {
return new Colors();
});
Tag colors = new Tag("colors");
class ColorSet extends BaseEntity {
}
EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
return new ColorSet();
}).build();
ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
ColorSet colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
Colors theColors = (Colors) colorSet.get(colors);
assertEqual(6, theColors.size());
assertEqual(6, colorSet.getEntityList(colors).size());
assertEqual("black", theColors.get(0).getText(Thing.name));
assertEqual("hue", theColors.get(0).getText(Product.category));
assertEqual("primary", theColors.get(0).getText(type));
assertEqual(255, (int) theColors.get(0).getInt(red));
assertEqual(255, (int) theColors.get(0).getInt(green));
assertEqual(255, (int) theColors.get(0).getInt(blue));
assertEqual(1, (int) theColors.get(0).getInt(alpha));
assertEqual("green", theColors.get(5).getText(Thing.name));
assertEqual(0, (int) theColors.get(5).getInt(red));
assertEqual(255, (int) theColors.get(5).getInt(green));
assertEqual(0, (int) theColors.get(5).getInt(blue));
assertEqual(1, (int) theColors.get(5).getInt(alpha));
class CodeParser implements PropertyParserCallback {
private int index;
CodeParser(int index) {
this.index = index;
}
@Override
public Object parse(Object codeMap) {
if (!(codeMap instanceof Map)) {
return 0;
}
Map m = (Map) codeMap;
List rgba = (List) m.get("rgba");
if (rgba == null) {
return 0;
}
if (index < 0 || index >= rgba.size()) {
return 0;
}
return rgba.get(index);
}
}
parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
colorSet = (ColorSet) parser.parseJSON(jsonData, new ColorSet());
theColors = (Colors) colorSet.get(colors);
assertEqual(6, theColors.size());
assertEqual(6, colorSet.getEntityList(colors).size());
assertEqual("black", theColors.get(0).getText(Thing.name));
assertEqual("hue", theColors.get(0).getText(Product.category));
assertEqual("primary", theColors.get(0).getText(type));
assertEqual(255, (int) theColors.get(0).getInt(red));
assertEqual(255, (int) theColors.get(0).getInt(green));
assertEqual(255, (int) theColors.get(0).getInt(blue));
assertEqual(1, (int) theColors.get(0).getInt(alpha));
assertEqual("green", theColors.get(5).getText(Thing.name));
assertEqual(0, (int) theColors.get(5).getInt(red));
assertEqual(255, (int) theColors.get(5).getInt(green));
assertEqual(0, (int) theColors.get(5).getInt(blue));
assertEqual(1, (int) theColors.get(5).getInt(alpha));
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ResultParserTest method nestedXMLAttsTest.
// Disabling nested XML Atts tests because I can't figure out how
// Result works with XML...
private void nestedXMLAttsTest() throws Exception {
assertTrue(personType.newInstance() instanceof PersonModel, "persontype should create a Person, but created " + personType.newInstance());
ResultParser parser = new ResultParser(personType).property("@name", Person.name).property("@email", Person.email).property("@dob", Person.birthDate, new SimpleDateFormat("MMM d, yyyy")).property("./children/person", Person.children).property("./publication", publications);
ResultParser publicationParser = new ResultParser(publicationType).property("@name", Thing.name).property(".", Thing.description);
parser.add(publicationParser);
String json = "<person name=\"Paul\" email=\"paul@example.com\" dob=\"December 27, 1978\">" + "<children><person name=\"Jim\" email=\"jim@example.com\" dob=\"January 10, 1979\"/>" + "<person name=\"Jill\" email=\"jill@example.com\" dob=\"January 11, 1979\"/></children><publication name=\"Time Magazine\">Political and current event stories</publication>" + "<publication name=\"Vancouver Sun\"/></person>";
XMLParser xparser = new XMLParser();
Element root = xparser.parse(new StringReader("<?xml version='1.0'?>\n" + json));
Entity person = parser.parseRow(Result.fromContent(root), personType.newInstance());
assertEqual("Paul", person.getEntity().getText(Person.name));
assertEqual("paul@example.com", person.getEntity().getText(Person.email));
People children = (People) person.getEntity().get(Person.children);
assertEqual(2, children.size());
assertEqual("Jim", children.get(0).get(Person.name));
assertEqual("jim@example.com", children.get(0).get(Person.email));
assertEqual("Jill", children.get(1).get(Person.name));
assertEqual("jill@example.com", children.get(1).get(Person.email));
Publications pubs = (Publications) person.getEntity().get(publications);
assertEqual(2, pubs.size());
assertEqual("Time Magazine", pubs.get(0).get(Thing.name));
assertEqual("Vancouver Sun", pubs.get(1).get(Thing.name));
assertEqual("Political and current event stories", pubs.get(0).get(Thing.description));
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ResultParserTest method parserServiceTest.
private void parserServiceTest() throws Exception {
String jsonData = "{\n" + " \"colors\": [\n" + " {\n" + " \"color\": \"black\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,255,255,1],\n" + " \"hex\": \"#000\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"white\",\n" + " \"category\": \"value\",\n" + " \"code\": {\n" + " \"rgba\": [0,0,0,1],\n" + " \"hex\": \"#FFF\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"red\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,0,0,1],\n" + " \"hex\": \"#FF0\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"blue\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [0,0,255,1],\n" + " \"hex\": \"#00F\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"yellow\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"primary\",\n" + " \"code\": {\n" + " \"rgba\": [255,255,0,1],\n" + " \"hex\": \"#FF0\"\n" + " }\n" + " },\n" + " {\n" + " \"color\": \"green\",\n" + " \"category\": \"hue\",\n" + " \"type\": \"secondary\",\n" + " \"code\": {\n" + " \"rgba\": [0,255,0,1],\n" + " \"hex\": \"#0F0\"\n" + " }\n" + " },\n" + " ]\n" + "}";
class Color extends BaseEntity {
}
Tag type = new Tag("type");
Tag red = new Tag("red"), green = new Tag("green"), blue = new Tag("blue"), alpha = new Tag("alpha");
EntityType colorType = entityTypeBuilder(Color.class).string(Thing.name).string(Product.category).string(type).Integer(red).Integer(green).Integer(blue).Integer(alpha).factory(cls -> {
return new Color();
}).build();
class Colors extends EntityList<Color> {
}
EntityType.registerList(Colors.class, Color.class, cls -> {
return new Colors();
});
Tag colors = new Tag("colors");
class ColorSet extends BaseEntity {
}
EntityType colorsetType = entityTypeBuilder(ColorSet.class).list(Colors.class, colors).factory(cls -> {
return new ColorSet();
}).build();
ResultParser parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).Integer("code/rgba[0]", red).Integer("code/rgba[1]", green).Integer("code/rgba[2]", blue).Integer("code/rgba[3]", alpha);
ParsingService parserService = new ParsingService();
Throwable[] errors = new Throwable[1];
parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
Colors theColors = (Colors) colorSet.get(colors);
assertEqual(6, theColors.size());
assertEqual(6, colorSet.getEntityList(colors).size());
assertEqual("black", theColors.get(0).getText(Thing.name));
assertEqual("hue", theColors.get(0).getText(Product.category));
assertEqual("primary", theColors.get(0).getText(type));
assertEqual(255, (int) theColors.get(0).getInt(red));
assertEqual(255, (int) theColors.get(0).getInt(green));
assertEqual(255, (int) theColors.get(0).getInt(blue));
assertEqual(1, (int) theColors.get(0).getInt(alpha));
assertEqual("green", theColors.get(5).getText(Thing.name));
assertEqual(0, (int) theColors.get(5).getInt(red));
assertEqual(255, (int) theColors.get(5).getInt(green));
assertEqual(0, (int) theColors.get(5).getInt(blue));
assertEqual(1, (int) theColors.get(5).getInt(alpha));
System.out.println("Finished ready callback");
}).except(err -> {
errors[0] = err;
}).await();
System.out.println("Finished await");
if (errors[0] != null) {
if (errors[0] instanceof RuntimeException) {
throw (RuntimeException) errors[0];
} else {
throw (Exception) errors[0];
}
}
class CodeParser implements PropertyParserCallback {
private int index;
CodeParser(int index) {
this.index = index;
}
@Override
public Object parse(Object codeMap) {
if (!(codeMap instanceof Map)) {
return 0;
}
Map m = (Map) codeMap;
List rgba = (List) m.get("rgba");
if (rgba == null) {
return 0;
}
if (index < 0 || index >= rgba.size()) {
return 0;
}
return rgba.get(index);
}
}
parser = resultParser(ColorSet.class).property("colors", colors).entityType(Color.class).string("color", Thing.name).string("category", Product.category).string("type", type).property("code", red, new CodeParser(0)).property("code", green, new CodeParser(1)).property("code", blue, new CodeParser(2)).property("code", alpha, new CodeParser(3));
parserService.parseJSON(jsonData, parser, new ColorSet()).ready(colorSet -> {
Colors theColors = (Colors) colorSet.get(colors);
assertEqual(6, theColors.size());
assertEqual(6, colorSet.getEntityList(colors).size());
assertEqual("black", theColors.get(0).getText(Thing.name));
assertEqual("hue", theColors.get(0).getText(Product.category));
assertEqual("primary", theColors.get(0).getText(type));
assertEqual(255, (int) theColors.get(0).getInt(red));
assertEqual(255, (int) theColors.get(0).getInt(green));
assertEqual(255, (int) theColors.get(0).getInt(blue));
assertEqual(1, (int) theColors.get(0).getInt(alpha));
assertEqual("green", theColors.get(5).getText(Thing.name));
assertEqual(0, (int) theColors.get(5).getInt(red));
assertEqual(255, (int) theColors.get(5).getInt(green));
assertEqual(0, (int) theColors.get(5).getInt(blue));
assertEqual(1, (int) theColors.get(5).getInt(alpha));
System.out.println("Finished ready callback");
}).except(err -> {
errors[0] = err;
}).await();
System.out.println("Finished await");
if (errors[0] != null) {
if (errors[0] instanceof RuntimeException) {
throw (RuntimeException) errors[0];
} else {
throw (Exception) errors[0];
}
}
parserService.stop();
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ResultParserTest method nestedJSONTest.
private void nestedJSONTest() throws Exception {
EntityType personType = new EntityTypeBuilder().string(Person.name).string(Person.email).Date(Person.birthDate).list(Publications.class, publications).build();
ResultParser parser = new ResultParser(personType).property("name", Person.name).property("email", Person.email).property("dob", Person.birthDate, dateStr -> {
if (!(dateStr instanceof String)) {
return null;
}
String str = (String) dateStr;
if (str.isEmpty()) {
return null;
}
SimpleDateFormat fmt = new SimpleDateFormat("MMM d, yyyy");
try {
return fmt.parse(str);
} catch (ParseException ex) {
Log.e(ex);
return null;
}
}).property("publications", publications);
ResultParser publicationParser = new ResultParser(publicationType).property("name", Thing.name).property("description", Thing.description);
parser.add(publicationParser);
String json = "{\"name\":\"Paul\", " + "\"email\":\"paul@example.com\", " + "\"dob\" : \"December 27, 1978\", " + "\"publications\" : [" + " {\"name\":\"Time Magazine\", \"description\" : \"Political and current event stories\"}, " + " {\"name\":\"Vancouver Sun\"}" + "]" + "}";
Entity person = parser.parseRow(Result.fromContent(json, Result.JSON), personType.newInstance());
assertEqual("Paul", person.getEntity().getText(Person.name));
assertEqual("paul@example.com", person.getEntity().getText(Person.email));
Publications pubs = (Publications) person.getEntity().get(publications);
assertEqual(2, pubs.size());
assertEqual("Time Magazine", pubs.get(0).get(Thing.name));
assertEqual("Vancouver Sun", pubs.get(1).get(Thing.name));
assertEqual("Political and current event stories", pubs.get(0).get(Thing.description));
}
use of com.codename1.rad.models.Property in project CodeRAD by shannah.
the class ResultParserTest method dateFormatXMLAttsTest.
private void dateFormatXMLAttsTest() throws Exception {
EntityType personType = new EntityTypeBuilder().string(Person.name).string(Person.email).Date(Person.birthDate).build();
ResultParser parser = new ResultParser(personType).property("/person[0]/@name", Person.name).property("/person[0]/@email", Person.email).property("/person[0]/@dob", Person.birthDate, new SimpleDateFormat("MMM d, yyyy"));
String json = "<person name=\"Paul\" email=\"paul@example.com\" dob=\"December 27, 1978\"></person>";
XMLParser xparser = new XMLParser();
Element root = xparser.parse(new StringReader("<?xml version='1.0'?>\n" + json));
Entity person = parser.parseRow(Result.fromContent(root), personType.newInstance());
assertEqual("Paul", person.getEntity().getText(Person.name));
assertEqual("paul@example.com", person.getEntity().getText(Person.email));
}
Aggregations