Skip to content

Commit

Permalink
error in createAppointment
Browse files Browse the repository at this point in the history
  • Loading branch information
Choon-Chern Lim committed Dec 13, 2016
1 parent 14b655d commit 68aac7c
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,7 @@ class Mapper {
endDateTime: new org.joda.time.DateTime(appointment.end),
subject: appointment.subject,
location: appointment.location,
reminderMinutesBeforeStart: appointment.reminderMinutesBeforeStart,
// TODO test this!
body: appointment.getBody().toString()
reminderMinutesBeforeStart: appointment.reminderMinutesBeforeStart
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ class UserConfig {
String googleCalendarName
Integer totalSyncDays
Integer nextSyncInMinutes
Boolean includeCanceledEvent
Boolean includeEventBody
Boolean includeCanceledEvents
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.github.choonchernlim.calsync.core.CalSyncEvent
import com.github.choonchernlim.calsync.core.Mapper
import com.github.choonchernlim.calsync.core.UserConfig
import com.google.inject.Inject
import microsoft.exchange.webservices.data.core.service.item.Appointment
import org.joda.time.DateTime
import org.slf4j.Logger
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -32,20 +33,25 @@ class ExchangeService {
*
* @param startDateTime Start datetime
* @param endDateTime End datetime
* @param includeCanceledEvents Whether to include canceled events or not
* @return Events if there's any, otherwise empty list
*/
List<CalSyncEvent> getEvents(DateTime startDateTime, DateTime endDateTime) {
List<CalSyncEvent> getEvents(DateTime startDateTime, DateTime endDateTime, Boolean includeCanceledEvents) {
assert startDateTime && endDateTime && startDateTime <= endDateTime
assert includeCanceledEvents != null

LOGGER.info("Retrieving events from ${startDateTime} to ${endDateTime}...")

List<CalSyncEvent> events = exchangeClient.
getEvents(startDateTime, endDateTime)?.
collect { Mapper.toCalSyncEvent(it) } ?: []
List<Appointment> exchangeEvents = exchangeClient.getEvents(startDateTime, endDateTime) ?: []

LOGGER.info("Total events found: ${events.size()}...")
LOGGER.info("\tTotal events found: ${exchangeEvents.size()}...")

return events
if (!includeCanceledEvents) {
exchangeEvents = exchangeEvents.findAll { !it.isCancelled }
LOGGER.info("\tTotal events after excluding canceled events: ${exchangeEvents.size()}...")
}

return exchangeEvents.collect { Mapper.toCalSyncEvent(it) }
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import com.google.api.services.calendar.model.Event
import com.google.api.services.calendar.model.EventDateTime
import com.google.api.services.calendar.model.EventReminder
import microsoft.exchange.webservices.data.core.ExchangeService
import microsoft.exchange.webservices.data.core.PropertySet
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion
import microsoft.exchange.webservices.data.core.response.ServiceResponse
import microsoft.exchange.webservices.data.core.response.ServiceResponseCollection
import microsoft.exchange.webservices.data.core.service.item.Appointment
import microsoft.exchange.webservices.data.property.complex.ItemId
import spock.lang.Specification
import spock.lang.Unroll

Expand Down Expand Up @@ -106,8 +102,6 @@ class MapperSpec extends Specification {
}
def appointment = new Appointment(exchangeService)

// TODO fix this!

appointment.start = startDateTime.toDate()
appointment.end = endDateTime.toDate()
appointment.subject = 'summary'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ class UserConfigSpec extends Specification {
googleCalendarName: null,
totalSyncDays: null,
nextSyncInMinutes: null,
includeCanceledEvent: null,
includeEventBody: null
includeCanceledEvents: null
)

then:
Expand All @@ -26,8 +25,7 @@ class UserConfigSpec extends Specification {
userConfig.googleCalendarName == null
userConfig.totalSyncDays == null
userConfig.nextSyncInMinutes == null
userConfig.includeCanceledEvent == null
userConfig.includeEventBody == null
userConfig.includeCanceledEvents == null
}

def 'userConfig - given valid params, should return valid values'() {
Expand All @@ -40,8 +38,7 @@ class UserConfigSpec extends Specification {
googleCalendarName: 'googleCalendarName',
totalSyncDays: 1,
nextSyncInMinutes: 2,
includeCanceledEvent: true,
includeEventBody: false
includeCanceledEvents: true
)

then:
Expand All @@ -52,7 +49,6 @@ class UserConfigSpec extends Specification {
userConfig.googleCalendarName == 'googleCalendarName'
userConfig.totalSyncDays == 1
userConfig.nextSyncInMinutes == 2
userConfig.includeCanceledEvent
!userConfig.includeEventBody
userConfig.includeCanceledEvents
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class ExchangeServiceSpec extends Specification {
@Unroll
def 'getEvents - given #label, should throw exception'() {
when:
service.getEvents(startDateTime, endDateTime)
service.getEvents(startDateTime, endDateTime, true)

then:
0 * _
Expand All @@ -50,13 +50,27 @@ class ExchangeServiceSpec extends Specification {
'startDateTime > endDateTime' | DateTime.now() | DateTime.now().minusDays(1)
}

def 'getEvents - given includeCanceledEvents == null, should throw exception'() {
given:
def startDateTime = DateTime.now()
def endDateTime = startDateTime.plusDays(1)

when:
service.getEvents(startDateTime, endDateTime, null)

then:
0 * _
thrown AssertionError
}


def 'getEvents - given no events, should return empty list'() {
given:
def startDateTime = DateTime.now()
def endDateTime = startDateTime.plusDays(1)

when:
def events = service.getEvents(startDateTime, endDateTime)
def events = service.getEvents(startDateTime, endDateTime, true)

then:
1 * exchangeClient.getEvents(startDateTime, endDateTime) >> null
Expand All @@ -71,25 +85,44 @@ class ExchangeServiceSpec extends Specification {
def endDateTime = startDateTime.plusDays(1)

when:
def events = service.getEvents(startDateTime, endDateTime)
def events = service.getEvents(startDateTime, endDateTime, true)

then:
1 * exchangeClient.getEvents(startDateTime, endDateTime) >> {
def appointment = new Appointment(Mock(microsoft.exchange.webservices.data.core.ExchangeService) {
getRequestedServerVersion() >> ExchangeVersion.Exchange2010_SP2
})
appointment.start = startDateTime.toDate()
appointment.end = endDateTime.toDate()
appointment.subject = 'summary'
appointment.location = 'location'
appointment.reminderMinutesBeforeStart = 15

return [appointment]
}
1 * exchangeClient.getEvents(startDateTime, endDateTime) >> [
createAppointment(startDateTime, endDateTime, 'summary1', 'location1', 15, true),
createAppointment(startDateTime, endDateTime, 'summary2', 'location2', 15, false)
]
0 * _

events.size() == 1
events*.subject == ['summary']
events.size() == 2
events*.subject == ['summary1', 'summary2']
}

private Appointment createAppointment(
DateTime startDateTime,
DateTime endDateTime,
String subject,
String location,
Integer reminderMinutesBeforeStart,
Boolean isCancelled) {

def exchangeService = Mock(microsoft.exchange.webservices.data.core.ExchangeService) {
getRequestedServerVersion() >> ExchangeVersion.Exchange2010_SP2
}

def appointment = new Appointment(exchangeService)

// TODO You must load or assign this property before you can read its value.
// appointment.load(new PropertySet(BasePropertySet.FirstClassProperties))

appointment.start = startDateTime.toDate()
appointment.end = endDateTime.toDate()
appointment.subject = subject
appointment.location = location
appointment.reminderMinutesBeforeStart = reminderMinutesBeforeStart
appointment.isCancelled == isCancelled

return appointment
}


Expand Down

0 comments on commit 68aac7c

Please sign in to comment.