/// <summary> /// Update the schedule with name and replace the old object with the given /// schedule. Otherwise, if no schedule with name exists, a new schedule /// with name and the given schedule will be added to database. /// </summary> /// /// <param name="name">The name of the schedule. The name cannot be empty.</param> /// <param name="schedule">The Schedule to update or add.</param> /// <exception cref="GroupManagerDb.Error">if the name is empty, or other database error.</exception> public override void updateSchedule(String name, Schedule schedule) { if (!hasSchedule(name)) { addSchedule(name, schedule); return; } try { PreparedStatement statement = database_ .prepareStatement(net.named_data.jndn.encrypt.Sqlite3GroupManagerDbBase.UPDATE_updateSchedule); statement.setBytes(1, schedule.wireEncode().getImmutableArray()); statement.setString(2, name); try { statement.executeUpdate(); } finally { statement.close(); } } catch (SQLException exception) { throw new GroupManagerDb.Error( "Sqlite3GroupManagerDb.updateSchedule: SQLite error: " + exception); } }
/// <summary> /// Add a schedule with the given name. /// </summary> /// /// <param name="name">The name of the schedule. The name cannot be empty.</param> /// <param name="schedule">The Schedule to add.</param> /// <exception cref="GroupManagerDb.Error">if a schedule with the same name already exists,if the name is empty, or other database error.</exception> public override void addSchedule(String name, Schedule schedule) { net.named_data.jndn.encrypt.Sqlite3GroupManagerDbBase.checkAddSchedule(name); try { PreparedStatement statement = database_ .prepareStatement(net.named_data.jndn.encrypt.Sqlite3GroupManagerDbBase.INSERT_addSchedule); statement.setString(1, name); statement.setBytes(2, schedule.wireEncode().getImmutableArray()); try { statement.executeUpdate(); } finally { statement.close(); } } catch (SQLException exception) { throw new GroupManagerDb.Error( "Sqlite3GroupManagerDb.addSchedule: SQLite error: " + exception); } }
public void testDatabaseFunctions() { Blob scheduleBlob = new Blob(SCHEDULE, false); // Create a schedule. Schedule schedule = new Schedule(); try { schedule.wireDecode(scheduleBlob); } catch (EncodingException ex) { // We don't expect this to happen. Fail("Error decoding Schedule: " + ex.Message); } // Create a member. RsaKeyParams // Create a member. paras = new RsaKeyParams(); DecryptKey decryptKey; EncryptKey encryptKey; try { decryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.generateKey(paras); encryptKey = net.named_data.jndn.encrypt.algo.RsaAlgorithm.deriveEncryptKey(decryptKey.getKeyBits()); } catch (Exception ex_0) { // Don't expect this to happen. Fail("Error creating test keys: " + ex_0.Message); return; } Blob keyBlob = encryptKey.getKeyBits(); Name name1 = new Name("/ndn/BoyA/ksk-123"); Name name2 = new Name("/ndn/BoyB/ksk-1233"); Name name3 = new Name("/ndn/GirlC/ksk-123"); Name name4 = new Name("/ndn/GirlD/ksk-123"); Name name5 = new Name("/ndn/Hello/ksk-123"); // Add schedules into the database. try { database.addSchedule("work-time", schedule); database.addSchedule("rest-time", schedule); database.addSchedule("play-time", schedule); database.addSchedule("boelter-time", schedule); } catch (Exception ex_1) { Fail("Unexpected error adding a schedule: " + ex_1.Message); } // Throw an exception when adding a schedule with an existing name. bool gotError = true; try { database.addSchedule("boelter-time", schedule); gotError = false; } catch (GroupManagerDb.Error ex_2) { } if (!gotError) Fail("Expected an error adding a duplicate schedule"); // Add members into the database. try { database.addMember("work-time", name1, keyBlob); database.addMember("rest-time", name2, keyBlob); database.addMember("play-time", name3, keyBlob); database.addMember("play-time", name4, keyBlob); } catch (Exception ex_3) { Fail("Unexpected error adding a member: " + ex_3.Message); } // Throw an exception when adding a member with a non-existing schedule name. gotError = true; try { database.addMember("false-time", name5, keyBlob); gotError = false; } catch (GroupManagerDb.Error ex_4) { } if (!gotError) Fail("Expected an error adding a member with non-existing schedule"); try { database.addMember("boelter-time", name5, keyBlob); } catch (Exception ex_5) { Fail("Unexpected error adding a member: " + ex_5.Message); } // Throw an exception when adding a member having an existing identity. gotError = true; try { database.addMember("work-time", name5, keyBlob); gotError = false; } catch (GroupManagerDb.Error ex_6) { } if (!gotError) Fail("Expected an error adding a member with an existing identity"); // Test has functions. AssertEquals(true, database.hasSchedule("work-time")); AssertEquals(true, database.hasSchedule("rest-time")); AssertEquals(true, database.hasSchedule("play-time")); AssertEquals(false, database.hasSchedule("sleep-time")); AssertEquals(false, database.hasSchedule("")); AssertEquals(true, database.hasMember(new Name("/ndn/BoyA"))); AssertEquals(true, database.hasMember(new Name("/ndn/BoyB"))); AssertEquals(false, database.hasMember(new Name("/ndn/BoyC"))); // Get a schedule. Schedule scheduleResult = database.getSchedule("work-time"); AssertTrue(scheduleResult.wireEncode().equals(scheduleBlob)); scheduleResult = database.getSchedule("play-time"); AssertTrue(scheduleResult.wireEncode().equals(scheduleBlob)); // Throw an exception when when there is no such schedule in the database. gotError = true; try { database.getSchedule("work-time-11"); gotError = false; } catch (GroupManagerDb.Error ex_7) { } if (!gotError) Fail("Expected an error getting a non-existing schedule"); // List all schedule names. IList names = database.listAllScheduleNames(); AssertTrue(names.Contains("work-time")); AssertTrue(names.Contains("play-time")); AssertTrue(names.Contains("rest-time")); AssertTrue(!names.Contains("sleep-time")); // List members of a schedule. IDictionary memberMap = database.getScheduleMembers("play-time"); AssertTrue(memberMap.Count != 0); // When there's no such schedule, the return map's size should be 0. AssertEquals(0, database.getScheduleMembers("sleep-time").Count); // List all members. IList members = database.listAllMembers(); AssertTrue(members.Contains(new Name("/ndn/GirlC"))); AssertTrue(members.Contains(new Name("/ndn/GirlD"))); AssertTrue(members.Contains(new Name("/ndn/BoyA"))); AssertTrue(members.Contains(new Name("/ndn/BoyB"))); // Rename a schedule. AssertEquals(true, database.hasSchedule("boelter-time")); database.renameSchedule("boelter-time", "rieber-time"); AssertEquals(false, database.hasSchedule("boelter-time")); AssertEquals(true, database.hasSchedule("rieber-time")); AssertEquals("rieber-time", database.getMemberSchedule(new Name("/ndn/Hello"))); // Update a schedule. Schedule newSchedule = new Schedule(); try { newSchedule.wireDecode(scheduleBlob); } catch (EncodingException ex_8) { // We don't expect this to happen. Fail("Error decoding Schedule: " + ex_8.Message); } RepetitiveInterval repetitiveInterval = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150921T000000"), 2, 10, 5, net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.DAY); newSchedule.addWhiteInterval(repetitiveInterval); database.updateSchedule("rieber-time", newSchedule); scheduleResult = database.getSchedule("rieber-time"); AssertTrue(!scheduleResult.wireEncode().equals(scheduleBlob)); AssertTrue(scheduleResult.wireEncode().equals(newSchedule.wireEncode())); // Add a new schedule when updating a non-existing schedule. AssertEquals(false, database.hasSchedule("ralphs-time")); database.updateSchedule("ralphs-time", newSchedule); AssertEquals(true, database.hasSchedule("ralphs-time")); // Update the schedule of a member. database.updateMemberSchedule(new Name("/ndn/Hello"), "play-time"); AssertEquals("play-time", database.getMemberSchedule(new Name("/ndn/Hello"))); // Delete a member. AssertEquals(true, database.hasMember(new Name("/ndn/Hello"))); database.deleteMember(new Name("/ndn/Hello")); AssertEquals(false, database.hasMember(new Name("/ndn/Hello"))); // Delete a non-existing member. try { database.deleteMember(new Name("/ndn/notExisting")); } catch (Exception ex_9) { Fail("Unexpected error deleting a non-existing member: " + ex_9.Message); } // Delete a schedule. All the members using this schedule should be deleted. database.deleteSchedule("play-time"); AssertEquals(false, database.hasSchedule("play-time")); AssertEquals(false, database.hasMember(new Name("/ndn/GirlC"))); AssertEquals(false, database.hasMember(new Name("/ndn/GirlD"))); // Delete a non-existing schedule. try { database.deleteSchedule("not-existing-time"); } catch (Exception ex_10) { Fail("Unexpected error deleting a non-existing schedule: " + ex_10.Message); } }
public void testEncodeAndDecode() { Schedule schedule = new Schedule(); RepetitiveInterval interval1 = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150828T000000"), 5, 10, 2, net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.DAY); RepetitiveInterval interval2 = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150828T000000"), 6, 8, 1, net.named_data.jndn.encrypt.RepetitiveInterval.RepeatUnit.DAY); RepetitiveInterval interval3 = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150827T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150827T000000"), 7, 8); RepetitiveInterval interval4 = new RepetitiveInterval( net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T000000"), 4, 7); schedule.addWhiteInterval(interval1); schedule.addWhiteInterval(interval2); schedule.addWhiteInterval(interval4); schedule.addBlackInterval(interval3); Blob encoding = schedule.wireEncode(); Blob encoding2 = new Blob(SCHEDULE, false); Assert.AssertTrue(encoding.equals(encoding2)); Schedule schedule2 = new Schedule(); try { schedule2.wireDecode(encoding); } catch (EncodingException ex) { Assert.Fail("Error decoding Schedule: " + ex.Message); } Schedule.Result result; // timePoint1 --> positive 8.25 4-10 double timePoint1 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T063000"); result = schedule.getCoveringInterval(timePoint1); Assert.AssertEquals(true, result.isPositive); Assert.AssertEquals("20150825T040000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(result.interval.getStartTime())); Assert.AssertEquals("20150825T100000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(result.interval.getEndTime())); // timePoint2 --> positive 8.26 6-8 double timePoint2 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150826T073000"); result = schedule.getCoveringInterval(timePoint2); Assert.AssertEquals(true, result.isPositive); Assert.AssertEquals("20150826T060000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(result.interval.getStartTime())); Assert.AssertEquals("20150826T080000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(result.interval.getEndTime())); }