1/*
2 * Copyright 2017, Andrew Lindesay <apl@lindesay.co.nz>.
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5
6#include "StandardMetaData.h"
7#include "StandardMetaDataJsonEventListener.h"
8#include "StandardMetaDataJsonEventListenerTest.h"
9
10#include <stdio.h>
11
12#include <AutoDeleter.h>
13#include <Json.h>
14
15#include <cppunit/TestCaller.h>
16#include <cppunit/TestSuite.h>
17
18
19#define INPUT_TOP_LEVEL "{\"createTimestamp\":123456," \
20	"\"dataModifiedTimestamp\":789012}"
21
22// This example is from the HDS server and comes back as part of the icons
23// bundle; real-world example.
24
25#define INPUT_ICON "{\"createTimestamp\":1501456139480," \
26	"\"createTimestampIso\":\"2017-07-30 23:08:59\"," \
27	"\"dataModifiedTimestamp\":1500536391000," \
28	"\"dataModifiedTimestampIso\":\"2017-07-20 07:39:51\"," \
29	"\"agent\":\"hds\"," \
30	"\"agentVersion\":\"1.0.86\"" \
31	"}"
32
33#define INPUT_THIRD_LEVEL "{" \
34	"\"createTimestamp\":99999," \
35	"\"gonk\":{\"zink\":[ 123, { \"a\":\"b\" } ]}\n," \
36	"\"rink\":{" \
37		"\"tonk\":{" \
38			"\"createTimestamp\":665544," \
39			"\"dataModifiedTimestamp\":554433" \
40		"}" \
41	"}," \
42	"\"cake\": { \"type\": \"bananas\" }," \
43	"\"createTimestamp\":99999," \
44"}"
45
46#define INPUT_BROKEN "{\"broken\",,"
47
48
49StandardMetaDataJsonEventListenerTest::StandardMetaDataJsonEventListenerTest()
50{
51}
52
53
54StandardMetaDataJsonEventListenerTest::~StandardMetaDataJsonEventListenerTest()
55{
56}
57
58
59void
60StandardMetaDataJsonEventListenerTest::TestGeneric(
61	const char* input, const char* jsonPath, status_t expectedStatus,
62	uint64_t expectedCreateTimestamp, uint64_t expectedDataModifiedTimestamp)
63{
64	StandardMetaData metaData;
65	StandardMetaDataJsonEventListener listener(jsonPath, metaData);
66
67	BDataIO* inputData = new BMemoryIO(input, strlen(input));
68	ObjectDeleter<BDataIO> inputDataDeleter(inputData);
69	BMallocIO* outputData = new BMallocIO();
70	ObjectDeleter<BMallocIO> outputDataDeleter(outputData);
71
72// ----------------------
73	BPrivate::BJson::Parse(inputData, &listener);
74// ----------------------
75
76	CPPUNIT_ASSERT_EQUAL(expectedStatus, listener.ErrorStatus());
77	CPPUNIT_ASSERT_EQUAL(expectedCreateTimestamp,
78		metaData.GetCreateTimestamp());
79	CPPUNIT_ASSERT_EQUAL(expectedDataModifiedTimestamp,
80		metaData.GetDataModifiedTimestamp());
81}
82
83
84void
85StandardMetaDataJsonEventListenerTest::TestTopLevel()
86{
87	TestGeneric(INPUT_TOP_LEVEL, "$", B_OK, 123456L, 789012L);
88}
89
90
91void
92StandardMetaDataJsonEventListenerTest::TestIcon()
93{
94	TestGeneric(INPUT_ICON, "$", B_OK, 1501456139480ULL, 1500536391000ULL);
95}
96
97
98void
99StandardMetaDataJsonEventListenerTest::TestThirdLevel()
100{
101	TestGeneric(INPUT_THIRD_LEVEL, "$.rink.tonk", B_OK, 665544L, 554433L);
102}
103
104
105void
106StandardMetaDataJsonEventListenerTest::TestBroken()
107{
108	TestGeneric(INPUT_BROKEN, "$", B_BAD_DATA, 0L, 0L);
109}
110
111
112/*static*/ void
113StandardMetaDataJsonEventListenerTest::AddTests(BTestSuite& parent)
114{
115	CppUnit::TestSuite& suite = *new CppUnit::TestSuite(
116		"StandardMetaDataJsonEventListenerTest");
117
118	suite.addTest(
119		new CppUnit::TestCaller<StandardMetaDataJsonEventListenerTest>(
120			"StandardMetaDataJsonEventListenerTest::TestTopLevel",
121			&StandardMetaDataJsonEventListenerTest::TestTopLevel));
122
123	suite.addTest(
124		new CppUnit::TestCaller<StandardMetaDataJsonEventListenerTest>(
125			"StandardMetaDataJsonEventListenerTest::TestIcon",
126			&StandardMetaDataJsonEventListenerTest::TestIcon));
127
128	suite.addTest(
129		new CppUnit::TestCaller<StandardMetaDataJsonEventListenerTest>(
130			"StandardMetaDataJsonEventListenerTest::TestThirdLevel",
131			&StandardMetaDataJsonEventListenerTest::TestThirdLevel));
132
133	suite.addTest(
134		new CppUnit::TestCaller<StandardMetaDataJsonEventListenerTest>(
135			"StandardMetaDataJsonEventListenerTest::TestBroken",
136			&StandardMetaDataJsonEventListenerTest::TestBroken));
137
138	parent.addTest("StandardMetaDataJsonEventListenerTest", &suite);
139}