1// bulk_test.go - uses Handler and Writer functions to process some streams as a demo.
2
3package mxj
4
5import (
6	"bytes"
7	"fmt"
8	"testing"
9)
10
11func TestBulkRawHeader(t *testing.T) {
12	fmt.Println("\n----------------  bulkraw_test.go ...")
13}
14
15// use data from bulk_test.go
16
17var jsonWriterRaw = new(bytes.Buffer)
18var xmlWriterRaw = new(bytes.Buffer)
19
20var jsonErrLogRaw = new(bytes.Buffer)
21var xmlErrLogRaw = new(bytes.Buffer)
22
23func TestXmlReaderRaw(t *testing.T) {
24	// create Reader for xmldata
25	xmlReader := bytes.NewReader(xmldata)
26
27	// read XML from Reader and pass Map value with the raw XML to handler
28	err := HandleXmlReaderRaw(xmlReader, bxmaphandlerRaw, bxerrhandlerRaw)
29	if err != nil {
30		t.Fatal("err:", err.Error())
31	}
32
33	// get the JSON
34	j := make([]byte, jsonWriterRaw.Len())
35	_, _ = jsonWriterRaw.Read(j)
36
37	// get the errors
38	e := make([]byte, xmlErrLogRaw.Len())
39	_, _ = xmlErrLogRaw.Read(e)
40
41	// print the input
42	fmt.Println("XmlReaderRaw, xmldata:\n", string(xmldata))
43	// print the result
44	fmt.Println("XmlReaderRaw, result :\n", string(j))
45	// print the errors
46	fmt.Println("XmlReaderRaw, errors :\n", string(e))
47}
48
49func bxmaphandlerRaw(m Map, raw []byte) bool {
50	j, err := m.JsonIndent("", "  ", true)
51	if err != nil {
52		return false
53	}
54
55	_, _ = jsonWriterRaw.Write(j)
56	// put in a NL to pretty up printing the Writer
57	_, _ = jsonWriterRaw.Write([]byte("\n"))
58	return true
59}
60
61func bxerrhandlerRaw(err error, raw []byte) bool {
62	// write errors to file
63	_, _ = xmlErrLogRaw.Write([]byte(err.Error()))
64	_, _ = xmlErrLogRaw.Write([]byte("\n")) // pretty up
65	_, _ = xmlErrLogRaw.Write(raw)
66	_, _ = xmlErrLogRaw.Write([]byte("\n")) // pretty up
67	return true
68}
69
70func TestJsonReaderRaw(t *testing.T) {
71	jsonReader := bytes.NewReader(jsondata)
72
73	// read all the JSON
74	err := HandleJsonReaderRaw(jsonReader, bjmaphandlerRaw, bjerrhandlerRaw)
75	if err != nil {
76		t.Fatal("err:", err.Error())
77	}
78
79	// get the XML
80	x := make([]byte, xmlWriterRaw.Len())
81	_, _ = xmlWriterRaw.Read(x)
82
83	// get the errors
84	e := make([]byte, jsonErrLogRaw.Len())
85	_, _ = jsonErrLogRaw.Read(e)
86
87	// print the input
88	fmt.Println("JsonReaderRaw, jsondata:\n", string(jsondata))
89	// print the result
90	fmt.Println("JsonReaderRaw, result  :\n", string(x))
91	// print the errors
92	fmt.Println("JsonReaderRaw, errors :\n", string(e))
93}
94
95func bjmaphandlerRaw(m Map, raw []byte) bool {
96	x, err := m.XmlIndent("  ", "  ")
97	if err != nil {
98		return false
99	}
100	_, _ = xmlWriterRaw.Write(x)
101	// put in a NL to pretty up printing the Writer
102	_, _ = xmlWriterRaw.Write([]byte("\n"))
103	return true
104}
105
106func bjerrhandlerRaw(err error, raw []byte) bool {
107	// write errors to file
108	_, _ = jsonErrLogRaw.Write([]byte(err.Error()))
109	_, _ = jsonErrLogRaw.Write([]byte("\n")) // pretty up, Error() from json.Unmarshal !NL
110	_, _ = jsonErrLogRaw.Write(raw)
111	_, _ = jsonErrLogRaw.Write([]byte("\n")) // pretty up
112	return true
113}
114