private ContainerNode buildAvroCompositeType(String avroRecordTypeName, final ArrayNode avroFields, boolean isArray, boolean isOptional) throws Xsd2AvroTranslatorException { ObjectNode avroRecord = buildAvroRecordType(avroRecordTypeName, avroFields); if (isArray) { ObjectNode avroArray = MAPPER.createObjectNode(); avroArray.put("type", "array"); avroArray.put("items", avroRecord); return avroArray; } if (isOptional) { ArrayNode typesArray = MAPPER.createArrayNode(); typesArray.add(avroRecord); typesArray.add("null"); return typesArray; } return avroRecord; }
/** * Process a regular XML schema element. * * @param xmlSchema the input XML schema * @param xsdElement the XML Schema element to process * @param level the current level in the elements hierarchy. * @param avroFields array of fields being populated * @throws Xsd2AvroTranslatorException if processing fails */ private void visit(XmlSchema xmlSchema, final XmlSchemaElement xsdElement, final int level, final ArrayNode avroFields) throws Xsd2AvroTranslatorException { /* * If this element is referencing another, it might not be useful to * process it. */ if (xsdElement.getRef().getTarget() != null) { return; } log.debug("process started for element = " + xsdElement.getName()); if (xsdElement.getSchemaType() instanceof XmlSchemaComplexType) { XmlSchemaComplexType xsdType = (XmlSchemaComplexType) xsdElement .getSchemaType(); int nextLevel = level + 1; final ArrayNode avroChildrenFields = MAPPER.createArrayNode(); visit(xmlSchema, xsdType, nextLevel, avroChildrenFields); ContainerNode avroRecordType = buildAvroCompositeType( getAvroTypeName(xsdType), avroChildrenFields, xsdElement.getMaxOccurs() > 1, xsdElement.getMinOccurs() == 0 && xsdElement.getMaxOccurs() == 1); ObjectNode avroRecordElement = MAPPER.createObjectNode(); avroRecordElement.put("type", avroRecordType); avroRecordElement.put("name", getAvroFieldName(xsdElement)); avroFields.add(avroRecordElement); } else if (xsdElement.getSchemaType() instanceof XmlSchemaSimpleType) { visit((XmlSchemaSimpleType) xsdElement.getSchemaType(), level, getAvroFieldName(xsdElement), xsdElement.getMinOccurs(), xsdElement.getMaxOccurs(), avroFields); } log.debug("process ended for element = " + xsdElement.getName()); }
@Test public void testToJsonFormatLog() throws Throwable { DBAuditMetadataLogEntity amle = new DBAuditMetadataLogEntity("serviceName", "userName", "impersonator", "ipAddress", "operation", "eventTime", "operationText", "allowed", "objectType", "component", "databaseName", "tableName", "columnName", "resourcePath"); String jsonAuditLog = amle.toJsonFormatLog(); ContainerNode rootNode = AuditMetadataLogEntity.parse(jsonAuditLog); assertEntryEquals(rootNode, Constants.LOG_FIELD_SERVICE_NAME, "serviceName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_USER_NAME, "userName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_IMPERSONATOR, "impersonator"); assertEntryEquals(rootNode, Constants.LOG_FIELD_IP_ADDRESS, "ipAddress"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OPERATION, "operation"); assertEntryEquals(rootNode, Constants.LOG_FIELD_EVENT_TIME, "eventTime"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OPERATION_TEXT, "operationText"); assertEntryEquals(rootNode, Constants.LOG_FIELD_ALLOWED, "allowed"); assertEntryEquals(rootNode, Constants.LOG_FIELD_DATABASE_NAME, "databaseName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_TABLE_NAME, "tableName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_COLUMN_NAME, "columnName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_RESOURCE_PATH, "resourcePath"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OBJECT_TYPE, "objectType"); }
/** * For use in tests * * @param json incoming JSON to parse * @return a node tree * @throws IOException on any parsing problems */ public static ContainerNode parse(String json) throws IOException { ObjectMapper mapper = new ObjectMapper(factory); JsonNode jsonNode = mapper.readTree(json); if (!(jsonNode instanceof ContainerNode)) { throw new IOException("Wrong JSON data: " + json); } return (ContainerNode) jsonNode; }
@Test public void testNestedException() throws Throwable { Exception e = new NoRouteToHostException("that box caught fire 3 years ago"); Exception ioe = new IOException("Datacenter problems", e); ThrowableInformation ti = new ThrowableInformation(ioe); Log4Json l4j = new Log4Json(); long timeStamp = Time.now(); String outcome = l4j.toJson(new StringWriter(), "testNestedException", timeStamp, "INFO", "quoted\"", "new line\n and {}", ti) .toString(); println("testNestedException", outcome); ContainerNode rootNode = Log4Json.parse(outcome); assertEntryEquals(rootNode, Log4Json.LEVEL, "INFO"); assertEntryEquals(rootNode, Log4Json.NAME, "testNestedException"); assertEntryEquals(rootNode, Log4Json.TIME, timeStamp); assertEntryEquals(rootNode, Log4Json.EXCEPTION_CLASS, ioe.getClass().getName()); JsonNode node = assertNodeContains(rootNode, Log4Json.STACK); assertTrue("Not an array: " + node, node.isArray()); node = assertNodeContains(rootNode, Log4Json.DATE); assertTrue("Not a string: " + node, node.isTextual()); //rather than try and make assertions about the format of the text //message equalling another ISO date, this test asserts that the hypen //and colon characters are in the string. String dateText = node.getTextValue(); assertTrue("No '-' in " + dateText, dateText.contains("-")); assertTrue("No '-' in " + dateText, dateText.contains(":")); }
private JsonNode assertNodeContains(ContainerNode rootNode, String key) { JsonNode node = rootNode.get(key); if (node == null) { fail("No entry of name \"" + key + "\" found in " + rootNode.toString()); } return node; }
@Test public void testToJsonFormatLog() throws Throwable { Map<String, String> privilegesMap = new HashMap<String, String>(); privilegesMap.put("resourceType1", "resourceName1"); privilegesMap.put("resourceType2", "resourceName2"); privilegesMap.put("resourceType3", "resourceName3"); privilegesMap.put("resourceType4", "resourceName4"); GMAuditMetadataLogEntity gmamle = new GMAuditMetadataLogEntity("serviceName", "userName", "impersonator", "ipAddress", "operation", "eventTime", "operationText", "allowed", "objectType", "component", privilegesMap); String jsonAuditLog = gmamle.toJsonFormatLog(); ContainerNode rootNode = AuditMetadataLogEntity.parse(jsonAuditLog); assertEntryEquals(rootNode, Constants.LOG_FIELD_SERVICE_NAME, "serviceName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_USER_NAME, "userName"); assertEntryEquals(rootNode, Constants.LOG_FIELD_IMPERSONATOR, "impersonator"); assertEntryEquals(rootNode, Constants.LOG_FIELD_IP_ADDRESS, "ipAddress"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OPERATION, "operation"); assertEntryEquals(rootNode, Constants.LOG_FIELD_EVENT_TIME, "eventTime"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OPERATION_TEXT, "operationText"); assertEntryEquals(rootNode, Constants.LOG_FIELD_ALLOWED, "allowed"); assertEntryEquals(rootNode, Constants.LOG_FIELD_OBJECT_TYPE, "objectType"); assertEntryEquals(rootNode, Constants.LOG_FIELD_COMPONENT, "component"); assertEntryEquals(rootNode, "resourceType1", "resourceName1"); assertEntryEquals(rootNode, "resourceType2", "resourceName2"); assertEntryEquals(rootNode, "resourceType3", "resourceName3"); assertEntryEquals(rootNode, "resourceType4", "resourceName4"); }
void assertEntryEquals(ContainerNode rootNode, String key, String value) { JsonNode node = assertNodeContains(rootNode, key); assertEquals(value, node.getTextValue()); }
void assertEntryEquals(ContainerNode rootNode, String key, long value) { JsonNode node = assertNodeContains(rootNode, key); assertEquals(value, node.getNumberValue()); }