Example usage for java.time LocalDate parse

List of usage examples for java.time LocalDate parse

Introduction

In this page you can find the example usage for java.time LocalDate parse.

Prototype

public static LocalDate parse(CharSequence text, DateTimeFormatter formatter) 

Source Link

Document

Obtains an instance of LocalDate from a text string using a specific formatter.

Usage

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testCreateRuleThrowsWhenNewNonRecurringRuleIsInThePast() throws Exception {

    // ARRANGE//from   w  ww  .  j  ava 2  s . com
    thrown.expect(Exception.class);
    thrown.expectMessage("Booking rule creation failed");

    // Set up a rule to create that clashes in required way with existing rules
    BookingRule pastRule = new BookingRule(existingThursdayNonRecurringRule);
    // Tweak so does not clash
    pastRule.getBooking().setCourt(pastRule.getBooking().getCourt() + pastRule.getBooking().getCourtSpan());
    pastRule.getBooking().setSlot(pastRule.getBooking().getSlot() + pastRule.getBooking().getSlotSpan());

    // Set current date to be ahead of this new rule
    ruleManager.setCurrentLocalDate(LocalDate
            .parse(pastRule.getBooking().getDate(), DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusDays(1));

    // ACT
    doTestCreateRuleClashesOrNotWithExistingRule(pastRule, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionHappyPathCallsTheOptimisticPersisterCorrectly() throws Exception {
    // Happy path where addRuleExclusion goes right through and creates the
    // exclusion.

    // ARRANGE// w w w. j a v  a 2 s .  c o  m
    initialiseRuleManager();
    int versionToUse = 22; // Arbitrary
    expectOptimisticPersisterToReturnVersionedAttributes(versionToUse);

    // Create an arbitrary, valid exclusion date to add
    String saturdayExclusionDate = LocalDate
            .parse(existingSaturdayRecurringRuleWithExclusion.getBooking().getDate(),
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            .plusWeeks(12).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    expectToAddOrDeleteRuleExclusionViaOptimisticPersister(versionToUse, saturdayExclusionDate, true,
            existingSaturdayRecurringRuleWithExclusion);

    // ACT
    // N.B. The third parameter is arbitrary here.
    Optional<BookingRule> updatedRule = ruleManager.addRuleExclusion(saturdayExclusionDate,
            existingSaturdayRecurringRuleWithExclusion, true);

    // ASSERT
    String[] newExcludeDates = new String[] { existingSaturdayRecurringRuleWithExclusion.getDatesToExclude()[0],
            saturdayExclusionDate };
    existingSaturdayRecurringRuleWithExclusion.setDatesToExclude(newExcludeDates);
    assertTrue("The updated rule should be returned", updatedRule.isPresent());
    assertTrue("The updated rule should be correct",
            updatedRule.get().equals(existingSaturdayRecurringRuleWithExclusion));
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionThrowsWhenTheOptimisticPersisterThrows() throws Exception {
    // N.B. This applies except when the optimistic persister throws a
    // conditional check failed exclusion, which is covered by other tests.

    // ARRANGE/*ww  w.j  av  a  2  s.  co  m*/
    thrown.expect(Exception.class);
    String message = "Test OptimisticPersister exception";
    thrown.expectMessage(message);

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2); // 2 arbitrary

    mockery.checking(new Expectations() {
        {
            oneOf(mockOptimisticPersister).put(with(equal(ruleItemName)), with(anything()), with(anything()));
            will(throwException(new Exception(message)));
        }
    });

    String existingDate = existingFridayRecurringRuleWithoutExclusions.getBooking().getDate();
    String exclusionDate = LocalDate.parse(existingDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusWeeks(1)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // This should throw
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(exclusionDate, existingFridayRecurringRuleWithoutExclusions, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionThrowsIfTheOptimisticPersisterThrowsAConditionalCheckFailedExceptionThreeTimesRunning()
        throws Exception {
    // The optimistic persister can throw a conditional check failed exclusion
    // if two database writes happen to get interleaved. Almost always, a retry
    // should fix this, and we allow up to three tries. This tests that if all
    // three tries fail then the rule manager will give up and throw.

    // ARRANGE//from  w ww  .  j  av a  2 s .  com
    thrown.expect(Exception.class);
    String message = "Database put failed - conditional check failed";
    thrown.expectMessage(message);

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2, 3); // 2 arbitrary

    mockery.checking(new Expectations() {
        {
            // All three tries throw
            exactly(3).of(mockOptimisticPersister).put(with(equal(ruleItemName)), with(anything()),
                    with(anything()));
            will(throwException(new Exception(message)));
        }
    });

    String existingDate = existingFridayRecurringRuleWithoutExclusions.getBooking().getDate();
    String exclusionDate = LocalDate.parse(existingDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusWeeks(1)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // This should throw - albeit after three tries internally
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(exclusionDate, existingFridayRecurringRuleWithoutExclusions, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionDoesNotThrowIfTheOptimisticPersisterThrowsAConditionalCheckFailedExceptionOnlyTwice()
        throws Exception {
    // The optimistic persister can throw a conditional check failed exclusion
    // if two database writes happen to get interleaved. Almost always, a retry
    // should fix this, and we allow up to three tries. This tests that if we
    // throw twice but the third try succeeds, then the rule manager does not
    // throw./*from www.  ja v  a  2  s.c  o  m*/

    // ARRANGE

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2, 3); // 2 arbitrary

    final Sequence retrySequence = mockery.sequence("retry");
    mockery.checking(new Expectations() {
        {
            // Two failures...
            exactly(2).of(mockOptimisticPersister).put(with(equal(ruleItemName)), with(anything()),
                    with(anything()));
            will(throwException(new Exception("Database put failed - conditional check failed")));
            inSequence(retrySequence);
            // ... but third attempt succeeds
            oneOf(mockOptimisticPersister).put(with(equal(ruleItemName)), with(anything()), with(anything()));
            will(returnValue(2));
            inSequence(retrySequence);
        }
    });

    String existingDate = existingFridayRecurringRuleWithoutExclusions.getBooking().getDate();
    String exclusionDate = LocalDate.parse(existingDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusWeeks(1)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // This should _not_ throw - we are allowed three tries internally
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(exclusionDate, existingFridayRecurringRuleWithoutExclusions, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionThrowsIfExclusionOccursBeforeRuleStarts() throws Exception {
    // Exclusions must be for dates after their rule begins.

    // ARRANGE//from   w ww. j  a v  a2 s.  c  om
    thrown.expect(Exception.class);
    thrown.expectMessage("Booking rule exclusion addition failed");

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2); // 2 arbitrary

    String ruleStartDate = existingSaturdayRecurringRuleWithExclusion.getBooking().getDate();
    String saturdayExclusionDateBeforeRuleStarts = LocalDate
            .parse(ruleStartDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).minusWeeks(1)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(saturdayExclusionDateBeforeRuleStarts,
            existingSaturdayRecurringRuleWithExclusion, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionThrowsIfExclusionOccursInThePast() throws Exception {
    // Exclusions must be for future dates (not just after their rule begins).
    // To test this we need to try adding an exclusion in the past, but after
    // the rule begins.

    // ARRANGE/*from  w  w  w .  j  a v  a  2s.co m*/
    thrown.expect(Exception.class);
    thrown.expectMessage("Booking rule exclusion addition failed");

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2); // 2 arbitrary

    // Set current date to a fortnight after the rule begins
    String startSaturday = existingSaturdayRecurringRuleWithExclusion.getBooking().getDate();
    String fortnightAfterStart = LocalDate.parse(startSaturday, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            .plusWeeks(2).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    ruleManager.setCurrentLocalDate(
            LocalDate.parse(fortnightAfterStart, DateTimeFormatter.ofPattern("yyyy-MM-dd")));

    // Choose exclusion date before the fake current date, but after the start
    // date.
    String weekAfterStart = LocalDate.parse(startSaturday, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            .plusWeeks(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(weekAfterStart, existingSaturdayRecurringRuleWithExclusion, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testAddRuleExclusionThrowsIfMaximumNumberOfExclusionsExists() throws Exception {
    // There is a certain maximum number of exclusions that can be present on
    // each rule. Trying to add another should throw.

    // ARRANGE//from   w ww. ja  va2s. c om
    thrown.expect(Exception.class);
    thrown.expectMessage("Booking rule exclusion addition failed");

    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(2); // 2 arbitrary

    // Set maximum exclusion number equal to that already present
    ruleManager.setMaxNumberOfDatesToExclude(1);
    // Create an arbitrary-but-otherwise-valid exclusion date to add
    String saturdayExclusionDate = LocalDate
            .parse(existingSaturdayRecurringRuleWithExclusion.getBooking().getDate(),
                    DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            .plusWeeks(12).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // This should throw as adding it would exceed the maximum number
    // N.B. The third parameter is arbitrary here.
    ruleManager.addRuleExclusion(saturdayExclusionDate, existingSaturdayRecurringRuleWithExclusion, true);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testApplyRulesCallsBookingManagerCorrectly_NonRecurringRuleDoesNotRecur() throws Exception {
    // A nonrecurring rule should apply for its date only and should not recur
    // in later weeks.

    // ARRANGE//from   ww w  .  ja  v  a  2 s  .c o m
    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(42);
    expectPurgeExpiredRulesAndRuleExclusions(42, existingBookingRules);
    // Set apply date to one week after the non-recurring rule - it should be
    // ignored.
    String existingDate = existingThursdayNonRecurringRule.getBooking().getDate();
    String applyDate = LocalDate.parse(existingDate, DateTimeFormatter.ofPattern("yyyy-MM-dd")).plusWeeks(1)
            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));

    // ACT
    // This should not create a booking
    ruleManager.applyRules(applyDate, false);
}

From source file:squash.booking.lambdas.core.RuleManagerTest.java

@Test
public void testApplyRulesCallsBookingManagerCorrectly_ApplicableRecurringRuleNoExclusionRecurrentBooking()
        throws Exception {
    // A recurring rule with no exclusion for the apply date should cause a
    // booking to be made. This tests for an apply date corresponding to a later
    // recurrence of the recurrent rule rather than the rule's initial booking.

    // ARRANGE//from www .  j a va2 s  .  co m
    initialiseRuleManager();
    expectOptimisticPersisterToReturnVersionedAttributes(42);

    // Set up booking for a recurrence of the rule's initial booking
    String initialDate = existingFridayRecurringRuleWithoutExclusions.getBooking().getDate();
    String newBookingDate = LocalDate.parse(initialDate, DateTimeFormatter.ofPattern("yyyy-MM-dd"))
            .plusWeeks(10).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
    Booking bookingToCreate = new Booking(existingFridayRecurringRuleWithoutExclusions.getBooking());
    bookingToCreate.setDate(newBookingDate);
    expectBookingManagerCall(bookingToCreate);
    expectPurgeExpiredRulesAndRuleExclusions(42, existingBookingRules);

    // ACT
    // This should create a booking
    ruleManager.applyRules(newBookingDate, false);
}