public static SchemaComponent deSchema(String name, JsonNode n) { if (n.isObject()) return deSchema(name, (ObjectNode)n); if (n.isArray()) return deSchema(name, (ArrayNode)n); if (n.isBoolean()) return deSchema(name, (BooleanNode)n); if(n.isInt()) return deSchema(name, (IntNode)n); if (n.isFloatingPointNumber()) return deSchema(name, (DoubleNode)n); if (n.isNull()) return deSchema(name, (NullNode)n); return null; }
private Object getValue(String propName, JsonNode node) { JsonNode jsonNode = node.get(propName); if (jsonNode != null) { if (jsonNode instanceof BooleanNode) { BooleanNode boolNode = (BooleanNode) jsonNode; return boolNode.getBooleanValue() ? true : false; } else { return jsonNode.getBigIntegerValue().doubleValue(); } } return null; }
private JsonNode convertToJsonNode(AvroDefaultConfig defaultConfig) { switch (defaultConfig.avroType) { case BOOLEAN: return Boolean.parseBoolean(defaultConfig.defaultValue) ? BooleanNode.TRUE : BooleanNode.FALSE; case INTEGER: return new IntNode(Integer.parseInt(defaultConfig.defaultValue)); case LONG: return new LongNode(Long.parseLong(defaultConfig.defaultValue)); case FLOAT: // FloatNode is fairly recent and our Jackson version does not have it yet return new DoubleNode(Float.parseFloat(defaultConfig.defaultValue)); case DOUBLE: return new DoubleNode(Double.parseDouble(defaultConfig.defaultValue)); case STRING: return new TextNode(defaultConfig.defaultValue); default: throw new IllegalArgumentException("Unknown type: " + defaultConfig.avroType); } }
@Test public void testBufferedMessages() throws PacketSerializationException { BufferedMessageWithSender message1 = new BufferedMessageWithSender( new BufferedMessage(23, BooleanNode.valueOf(true)), getPlayer("Player #1") ); BufferedMessageWithSender message2 = new BufferedMessageWithSender( new BufferedMessage(42, BooleanNode.valueOf(true)), getPlayer("Player #2") ); Packet packet = new BufferedMessages(message1, message2); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"BUFFERED_MESSAGES\",\"messages\":[{\"type\":\"BUFFERED_MESSAGE\",\"timestamp\":23,\"payload\":true,\"sender\":\"Player #1\"},{\"type\":\"BUFFERED_MESSAGE\",\"timestamp\":42,\"payload\":true,\"sender\":\"Player #2\"}]}"; assertEquals(expected, actual); }
@Test public void testGameCreated() throws PacketSerializationException { Player player1 = getPlayer("Player #1"); Player player2 = getPlayer("Player #2"); Game game = new Game( "Name #1", "TOY_RACER", player1, new Players(player1, player2), 20, BooleanNode.valueOf(true) ); Packet packet = new GameCreated(game); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"GAME_CREATED\",\"game\":{\"name\":\"Name #1\"," + "\"type\":\"TOY_RACER\",\"slots\":20,\"takers\":[\"Player #1\",\"Player #2\"]" + ",\"info\":true}}"; assertEquals(expected, actual); }
@Test public void testGameDeleted() throws PacketSerializationException { Player player1 = getPlayer("Player #1"); Player player2 = getPlayer("Player #2"); Game game = new Game( "Name #1", "TOY_RACER", player1, new Players(player1, player2), 20, BooleanNode.valueOf(true) ); Packet packet = new GameDeleted(game); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"GAME_DELETED\",\"game\":\"Name #1\"}"; assertEquals(expected, actual); }
@Test public void testGameClosed() throws PacketSerializationException { Player player1 = getPlayer("Player #1"); Player player2 = getPlayer("Player #2"); Game game = new Game( "Name #1", "TOY_RACER", player1, new Players(player1, player2), 20, BooleanNode.valueOf(true) ); Packet packet = new GameClosed(game); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"GAME_CLOSED\",\"game\":\"Name #1\"}"; assertEquals(expected, actual); }
@Test public void testOpenGames() throws PacketSerializationException { Player player1 = getPlayer("Player #1"); Player player2 = getPlayer("Player #2"); Player player3 = getPlayer("Player #3"); Game game1 = new Game("Name #1", "TOY_RACER", player1, new Players(player1), 20, BooleanNode.valueOf(true) ); Game game2 = new Game("Name #2", "TOY_RACER", player2, new Players(player2, player3), 10, BooleanNode.valueOf(false) ); Packet packet = new OpenGames(Lists.newArrayList(game1, game2)); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"OPEN_GAMES\",\"games\":[{\"name\":\"Name #1\"," + "\"type\":\"TOY_RACER\",\"slots\":20,\"takers\":[\"Player #1\"],\"info\":true}," + "{\"name\":\"Name #2\",\"type\":\"TOY_RACER\",\"slots\":10,\"takers\":" + "[\"Player #2\",\"Player #3\"],\"info\":false}]}"; assertEquals(expected, actual); }
private Object processPrimitive(final JsonNode prim) { Object val; if (prim instanceof BooleanNode) { val = prim.getBooleanValue(); } else if (prim instanceof DoubleNode) { val = prim.getDoubleValue(); } else if (prim instanceof IntNode) { val = prim.getIntValue(); } else if (prim instanceof LongNode) { val = prim.getLongValue(); } else { val = prim.getTextValue(); } return val; }
/** * Creates the version field. * * @return the field */ private Field createVersionField() { Field versionField = new Field(VERSION, Schema.createUnion(Arrays.asList( Schema.create(Type.INT), Schema.create(Type.NULL))), null, null); versionField.addProp(DISPLAY_NAME, "Version"); versionField.addProp(DISPLAY_PROMPT, "Enter type version"); versionField.addProp(TYPE_VERSION, BooleanNode.valueOf(true)); versionField.addProp(FIELD_ACCESS, FieldAccess.HIDDEN.name().toLowerCase()); return versionField; }
/** * Creates the dependencies field. * * @return the field */ private Field createDependenciesField() { Schema dependencyType = Schema.createRecord(DEPENDENCY_FIELD_TYPE, null, BASE_SCHEMA_FORM_NAMESPACE, false); Field fqnField = new Field(FQN, Schema.create(Type.STRING), null, null); Field versionField = new Field(VERSION, Schema.create(Type.INT), null, null); dependencyType.setFields(Arrays.asList(fqnField, versionField)); Schema dependenciesArray = Schema.createArray(dependencyType); Field dependenciesField = new Field(DEPENDENCIES, Schema.createUnion(Arrays.asList( dependenciesArray, Schema.create(Type.NULL))), null, null); dependenciesField.addProp(DISPLAY_NAME, "Dependencies"); dependenciesField.addProp(TYPE_DEPENDENCIES, BooleanNode.valueOf(true)); dependenciesField.addProp(FIELD_ACCESS, FieldAccess.HIDDEN.name().toLowerCase()); return dependenciesField; }
/** * Creates the addressable field. * * @return the field */ private Field createAddressableField() { Field addressableField = new Field(ADDRESSABLE, Schema.createUnion(Arrays.asList( Schema.create(Type.BOOLEAN), Schema.create(Type.NULL))), null, null); addressableField.addProp(DISPLAY_NAME, "Is addressable"); addressableField.addProp(BY_DEFAULT, BooleanNode.valueOf(true)); addressableField.addProp(DISPLAY_PROMPT, "Click to enable/disable partial updates support"); return addressableField; }
@Override protected void customizeFieldSchema(Schema fieldSchema, GenericRecord fieldType) { if (fieldType != null && fieldType.getSchema().getName().equals(RECORD_FIELD_TYPE)) { Boolean addressable = (Boolean) fieldType.get(ADDRESSABLE); if (addressable != null && !addressable) { fieldSchema.addProp(ADDRESSABLE, BooleanNode.getFalse()); } } }
@Test public void testClientMessageWithSender() throws PacketSerializationException { ClientMessage message = new ClientMessage(1, BooleanNode.valueOf(true), "CHAT_MESSAGE"); Packet packet = new ClientMessageWithSender(getPlayer("Player #1"), message); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"CLIENT_MESSAGE\",\"timestamp\":1,\"payload\":true,\"contentType\":\"CHAT_MESSAGE\",\"sender\":\"Player #1\"}"; assertEquals(expected, actual); }
@Test public void testBufferedMessageWithSender() throws PacketSerializationException { BufferedMessage message = new BufferedMessage(1, BooleanNode.valueOf(true)); Packet packet = new BufferedMessageWithSender(message, getPlayer("Player #1")); String actual = packetSerializer.build(packet); String expected = "{\"type\":\"BUFFERED_MESSAGE\",\"timestamp\":1,\"payload\":true,\"sender\":\"Player #1\"}"; assertEquals(expected, actual); }
private static SchemaComponent deSchema(String name, BooleanNode b) { BooleanSC schema = new BooleanSC(); return schema; }