summaryrefslogtreecommitdiff
path: root/p2pvr/daemon/unittests/testMaint.cpp
blob: ec04649256010e573fb9df1672be0fda7bf36240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#define BOOST_TEST_MODULE Maintenance
#include <boost/test/unit_test.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <testOptionsSource.h>
#include <Ice/ObjectAdapter.h>
#include <Ice/Service.h>
#include <maintenance.h>
#include <mockTuner.h>
#include "mockDevices.h"
#include "mockScheduler.h"
#include <si.h>
#include <recordings.h>
#include <linux/dvb/frontend.h>
#include <definedDirs.h>
#include "mockDefs.h"
#include <slicer/db/sqlSelectDeserializer.h>
#include "commonHelpers.h"
#include <slicer/slicer.h>
#include <testAppInstance.h>

class Core : public CommonObjects, public TestAppInstance {
	public:
		Core()
		{
			int paramCount = 0;
			ic = Ice::initialize(paramCount, NULL);
			auto adapter = ic->createObjectAdapterWithEndpoints("Adp", "tcp -p 12000");
			TestOptionsSource::LoadTestOptions({
					{ "common.datasourceRoot", (rootDir / "datasources").string() },
				});
			adapter->add(new MockDevices(), ic->stringToIdentity("GlobalDevices"));
			adapter->add(new MockScheduler(), ic->stringToIdentity("Schedules"));
			adapter->add(new SI(), ic->stringToIdentity("SI"));
			adapter->add(new Recordings(), ic->stringToIdentity("Recordings"));
			adapter->add(new Maintenance(adapter, NULL), ic->stringToIdentity("Maintenance"));
			adapter->activate();

			r = P2PVR::RecordingsPrx::checkedCast(ic->stringToProxy("Recordings"));
			BOOST_REQUIRE(r);
			r->ice_ping();

			s = P2PVR::SIPrx::checkedCast(ic->stringToProxy("SI"));
			BOOST_REQUIRE(s);
			s->ice_ping();

			m = P2PVR::MaintenancePrx::checkedCast(ic->stringToProxy("Maintenance"));
			BOOST_REQUIRE(m);
			m->ice_ping();
		}

		~Core()
		{
			ic->destroy();
		}

		P2PVR::MaintenancePrx m;
		P2PVR::RecordingsPrx r;
		P2PVR::SIPrx s;

	private:
		Ice::CommunicatorPtr ic;
};

BOOST_GLOBAL_FIXTURE( SchemaOnlyMockDatabase );

BOOST_FIXTURE_TEST_SUITE( MaintCore, Core );

const int serviceId = 4170;
const int transportId = 20544;
const int eventId1 = 31;
const int eventId2 = 915;

//
// Run all these tests when there's no data
// This tests behaviour from clean startup/fresh install
//
BOOST_AUTO_TEST_CASE( GetNetworks_emptySet )
{
	BOOST_REQUIRE(s->GetNetworks().empty());
}

BOOST_AUTO_TEST_CASE( GetAllDeliveries_emptySet )
{
	BOOST_REQUIRE(s->GetAllDeliveries().empty());
}

BOOST_AUTO_TEST_CASE( GetDeliveryForService_notFound )
{
	BOOST_REQUIRE_THROW(s->GetDeliveryForService(serviceId), P2PVR::NotFound);
}

BOOST_AUTO_TEST_CASE( GetDeliveryForTransport_notFound )
{
	BOOST_REQUIRE_THROW(s->GetDeliveryForTransport(transportId), P2PVR::NotFound);
}

BOOST_AUTO_TEST_CASE( GetDeliveryForSi_null )
{
	BOOST_REQUIRE_EQUAL(s->GetDeliveryForSi(), DVBSI::DeliveryPtr());
}

BOOST_AUTO_TEST_CASE( GetServices_emptySet )
{
	BOOST_REQUIRE(s->GetServices().empty());
}

BOOST_AUTO_TEST_CASE( GetService_notFound )
{
	BOOST_REQUIRE_THROW(s->GetService(serviceId), P2PVR::NotFound);
}

BOOST_AUTO_TEST_CASE( GetSpecificEvents_notFound )
{
	BOOST_REQUIRE_THROW(s->GetEvents({ eventId1, eventId2 }), P2PVR::NotFound);
}

//
// Now scan for and configure a network
//

BOOST_AUTO_TEST_CASE( update_network )
{
	m->UpdateNetwork(FE_OFDM);
}

BOOST_AUTO_TEST_CASE( GetNetworks )
{
	auto ns = s->GetNetworks();
	BOOST_REQUIRE_EQUAL(ns.size(), 1);
	BOOST_REQUIRE_EQUAL(ns[0]->NetworkId, 12333);
	BOOST_REQUIRE_EQUAL(ns[0]->Name, "Yorkshire");
}

BOOST_AUTO_TEST_CASE( GetDeliveryForService )
{
	auto del = s->GetDeliveryForService(serviceId);
	BOOST_REQUIRE(del);
	BOOST_REQUIRE_EQUAL(del->ice_id(), "::DVBSI::TerrestrialDelivery");
	BOOST_REQUIRE_EQUAL(del->Frequency, 682000000);
}

BOOST_AUTO_TEST_CASE( GetDeliveryForTransport )
{
	auto del = s->GetDeliveryForTransport(transportId);
	BOOST_REQUIRE(del);
	BOOST_REQUIRE_EQUAL(del->ice_id(), "::DVBSI::TerrestrialDelivery");
	BOOST_REQUIRE_EQUAL(del->Frequency, 722000000);
}

BOOST_AUTO_TEST_CASE( GetServices_stubs )
{
	auto services = s->GetServices();
	BOOST_REQUIRE_EQUAL(services.size(), 145);
	BOOST_REQUIRE_EQUAL(services[0]->ServiceId, 4170);
	BOOST_REQUIRE_EQUAL(services[0]->TransportStreamId, 4170);
	// Definitely a stub
	BOOST_REQUIRE(!services[0]->Name);
}

BOOST_AUTO_TEST_CASE( GetService_stubs )
{
	auto service = s->GetService(serviceId);
	BOOST_REQUIRE_EQUAL(service->ServiceId, serviceId);
	// Definitely a stub
	BOOST_REQUIRE(!service->Name);
}

BOOST_AUTO_TEST_CASE( GetDeliveryForSi_any )
{
	auto del = s->GetDeliveryForSi();
	BOOST_REQUIRE(del);
	// Any yes, but is our case it should be DVB-T
	BOOST_REQUIRE_EQUAL(del->ice_id(), "::DVBSI::TerrestrialDelivery");
}

BOOST_AUTO_TEST_CASE( UpdateNetwork_prepopulated )
{
	// This should now run using existing transport data (although this test doesn't prove that)
	m->UpdateNetwork(FE_OFDM);
}

//
// Load in the service details
// Should give things names etc, but not add/remove
//
BOOST_AUTO_TEST_CASE( update_services )
{
	m->UpdateServices();
}

BOOST_AUTO_TEST_CASE( GetServices )
{
	auto services = s->GetServices();
	BOOST_REQUIRE_EQUAL(services.size(), 145);
	BOOST_REQUIRE_EQUAL(services[0]->ServiceId, 4170);
	BOOST_REQUIRE_EQUAL(services[0]->TransportStreamId, 4170);
	BOOST_REQUIRE_EQUAL(services[0]->Name, "BBC ONE Yorks");
	BOOST_REQUIRE_EQUAL(services[0]->DefaultAuthority, "fp.bbc.co.uk");
}

BOOST_AUTO_TEST_CASE( GetService )
{
	auto service = s->GetService(serviceId);
	BOOST_REQUIRE_EQUAL(service->ServiceId, serviceId);
	BOOST_REQUIRE_EQUAL(service->TransportStreamId, 4170);
	BOOST_REQUIRE_EQUAL(service->Name, "BBC ONE Yorks");
	BOOST_REQUIRE_EQUAL(service->DefaultAuthority, "fp.bbc.co.uk");
}

BOOST_AUTO_TEST_CASE( update_events )
{
	BOOST_CHECKPOINT("Get a DB connection for faking stuff");
	auto db = this->dataSource<RdbmsDataSource>("postgres")->getReadonly(); // Naughty, but don't want txs

	BOOST_CHECKPOINT("Write first events");
	MockTuner::SetEventsSet(0);
	m->UpdateEvents();
	auto dayOneEvents = s->EventSearch(IceUtil::Optional<std::string>(), IceUtil::Optional<int>(),
			Common::DateTime {2014, 12, 18, 3, 0}, Common::DateTime {2014, 12, 19, 3, 0});
	BOOST_REQUIRE_EQUAL(dayOneEvents.size(), 3345);
	BOOST_REQUIRE(s->GetEvent(14448, 27052));
	BOOST_REQUIRE_THROW(s->GetEvent(15856, 3591), P2PVR::NotFound);

	BOOST_CHECKPOINT("Fake some recorded stuff");
	auto keyEvent1 = s->GetEvent(25664, 55689);
	auto keyEvent2 = s->GetEvent(24256, 1014);
	db->execute("INSERT INTO schedules(repeats) VALUES(false)");
	auto irecorded = boost::shared_ptr<DB::ModifyCommand>(
			db->newModifyCommand("INSERT INTO recorded(scheduleId, eventUid) VALUES(?, ?)"));
	irecorded->bindParamI(0, 1);
	irecorded->bindParamI(1, keyEvent1->EventUid);
	irecorded->execute();
	auto irecordings = boost::shared_ptr<DB::ModifyCommand>(
			db->newModifyCommand("INSERT INTO recordings(storageAddress, guid, scheduleId, eventUid) VALUES('', '', ?, ?)"));
	irecordings->bindParamI(0, 1);
	irecordings->bindParamI(1, keyEvent2->EventUid);
	irecordings->execute();
	BOOST_REQUIRE(keyEvent1);
	BOOST_REQUIRE(keyEvent2);
	BOOST_REQUIRE(keyEvent1->Current);
	BOOST_REQUIRE_EQUAL(keyEvent1->StartTime, Common::DateTime({2014, 12, 18, 21, 0}));
	BOOST_REQUIRE(keyEvent2->Current);
	BOOST_REQUIRE_EQUAL(keyEvent2->StartTime, Common::DateTime({2014, 12, 17, 5, 30}));

	BOOST_CHECKPOINT("Write second events");
	MockTuner::SetEventsSet(1);
	m->UpdateEvents();
	BOOST_REQUIRE_THROW(s->GetEvent(14448, 27052), P2PVR::NotFound);
	BOOST_REQUIRE(s->GetEvent(15856, 3591));

	BOOST_CHECKPOINT("Check our faked stuff is still there and right");
	auto dayOneEventsOnDayTwo = s->EventSearch(IceUtil::Optional<std::string>(), IceUtil::Optional<int>(),
			Common::DateTime {2014, 12, 18, 3, 0}, Common::DateTime {2014, 12, 19, 3, 0});
	// Some datetime range overlap, but most are gone
	BOOST_REQUIRE_EQUAL(dayOneEventsOnDayTwo.size(), 373);
	BOOST_REQUIRE_EQUAL(std::count_if(dayOneEventsOnDayTwo.begin(), dayOneEventsOnDayTwo.end(),
				[](const P2PVR::EventPtr & e) {
					return (e->ServiceId == 25664 && e->EventId == 55689)
						|| (e->ServiceId == 24256 && e->EventId == 1014); }), 0);
	auto keyEventsOnDayTwo = s->GetEvents({keyEvent1->EventUid, keyEvent2->EventUid});
	BOOST_REQUIRE_EQUAL(keyEventsOnDayTwo.size(), 2);
	BOOST_REQUIRE(!keyEventsOnDayTwo[0]->Current);
	BOOST_REQUIRE(!keyEventsOnDayTwo[1]->Current);

	BOOST_REQUIRE_EQUAL(keyEvent1->Title, keyEventsOnDayTwo[0]->Title);
	BOOST_REQUIRE_EQUAL(keyEvent2->Title, keyEventsOnDayTwo[1]->Title);
}

BOOST_AUTO_TEST_CASE( test_titleFiltering )
{
	auto thingsWithNew = s->EventSearch("new", IceUtil::Optional<int>(),
			IceUtil::Optional<Common::DateTime>(), IceUtil::Optional<Common::DateTime>());
	BOOST_REQUIRE_EQUAL(0, std::count_if(thingsWithNew.begin(), thingsWithNew.end(), [](auto e) {
			return boost::algorithm::starts_with(e->Title, "New:");
		}));
}

BOOST_AUTO_TEST_SUITE_END()