TwilioAdapter { public static Session updateSessionWithCallTimes(Session session, Call call) throws ParseException { if (call != null && session != null) { String pattern = "EEE, dd MMM yyyy HH:mm:ss Z"; SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH); String created = call.getProperty("date_created"); session.setStartTimestamp(format.parse(created).getTime() + ""); if (call.getEndTime() != null) { session.setReleaseTimestamp(call.getEndTime().getTime() + ""); } if (call.getDuration() != null) { if(call.getDuration().equals("0")) { session.setAnswerTimestamp(session.getReleaseTimestamp()); } else if(call.getStartTime() != null) { session.setAnswerTimestamp(call.getStartTime().getTime() + ""); } } else { session.setAnswerTimestamp(null); session.setReleaseTimestamp(session.getStartTimestamp()); } if (session.getReleaseTimestamp() == null && session.getAnswerTimestamp() != null) { Long releaseTimeByDuration = Long.parseLong(session.getAnswerTimestamp()) + (Long.parseLong(call.getDuration()) * 1000); session.setReleaseTimestamp(releaseTimeByDuration + ""); } return session; } return null; }  static HashMap<String, String> dial(Map<String, String> addressNameMap, String dialogIdOrUrl,
        AdapterConfig config, String accountId, String applicationId, String bearerToken); @Path("new") @GET @Produces("application/xml") Response getNewDialog(@QueryParam("CallSid") String CallSid, @QueryParam("AccountSid") String AccountSid,
        @QueryParam("From") String localID, @QueryParam("To") String remoteID, @QueryParam("Direction") String direction, 
        @QueryParam("ForwardedFrom") String forwardedFrom, @QueryParam("CallStatus") String callStatus, @QueryParam("isTest") Boolean isTest); @Path("new") @POST @Produces("application/xml") Response getNewDialogPost(@FormParam("CallSid") String CallSid, @FormParam("AccountSid") String AccountSid,
        @FormParam("From") String localID, @FormParam("To") String remoteID, @FormParam("Direction") String direction,
        @FormParam("ForwardedFrom") String forwardedFrom, @FormParam("CallStatus") String callStatus, @QueryParam("isTest") Boolean isTest); @Path("answer") @GET @Produces("application/xml") Response answer(@QueryParam("answerId") String answer_id, @QueryParam("Digits") String answer_input,
        @QueryParam("From") String localID, @QueryParam("To") String remoteID,
        @QueryParam("Direction") String direction, @QueryParam("RecordingUrl") String recordingUrl,
        @QueryParam("DialCallStatus") String dialCallStatus, @QueryParam("DialCallSid") String dialCallSid,
        @QueryParam("CallSid") String callSid, @QueryParam("ConferenceSid") String conferenceSid, 
        @QueryParam("CallStatus") String callStatus); @Path("timeout") @GET @Produces("application/xml") Response timeout(@QueryParam("From") String localID, @QueryParam("To") String remoteID,
        @QueryParam("Direction") String direction, @QueryParam("CallSid") String callSid, @QueryParam("CallStatus") String callStatus); @Path("preconnect") @GET @Produces("application/xml") Response preconnect(@QueryParam("From") String localID, @QueryParam("AccountSid") String accountSid,
        @QueryParam("To") String remoteID, @QueryParam("Direction") String direction,
        @QueryParam("CallSid") String callSid, @QueryParam("ParentCallSid") String parentCallSid); @Path("cc") @GET Response receiveCCMessage(@QueryParam("CallSid") String callSid, @QueryParam("From") String localID,
        @QueryParam("To") String remoteID, @QueryParam("Direction") String direction,
        @QueryParam("CallStatus") String status); void answered(String direction, String remoteID, String localID, String sessionKey); Response hangup(Session session); static Session updateSessionWithCallTimes(Session session, Call call); Return formQuestion(Question question, String adapterID, String address, String ddrRecordId,
        Session session, Map<String, String> extraParams); static Account getTwilioAccount(String accountSid, String authToken); Session fetchSessionFromParent(String localID, String remoteID, String accountSid, String callSid,
        String parentCallSid);  }
@Test public void updateSessionWithCallTimesNullEndTimeTest() throws ParseException { Call call = Mockito.mock(Call.class); Mockito.when(call.getProperty("date_created")).thenReturn("Mon, 15 Aug 2005 15:52:01 +0000"); Mockito.when(call.getEndTime()).thenReturn(null); Mockito.when(call.getDuration()).thenReturn("30"); Mockito.when(call.getStartTime()).thenReturn( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:02 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .toDate()); Session session = new Session(); Session updateSessionWithCallTimes = TwilioAdapter.updateSessionWithCallTimes(session, call); Assert.assertThat(updateSessionWithCallTimes.getStartTimestamp(), Matchers.is( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:01 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + "")); Assert.assertThat(updateSessionWithCallTimes.getAnswerTimestamp(), Matchers.is( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:02 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + "")); Assert.assertThat(updateSessionWithCallTimes.getReleaseTimestamp(), Matchers.is( (TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:02 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + 30000) + "")); }
@Test public void updateSessionWithCallTimesTest() throws ParseException { Call call = Mockito.mock(Call.class); Mockito.when(call.getProperty("date_created")).thenReturn("Mon, 15 Aug 2005 15:52:01 +0000"); Mockito.when(call.getEndTime()).thenReturn( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:32 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .toDate()); Mockito.when(call.getDuration()).thenReturn("31"); Mockito.when(call.getStartTime()).thenReturn( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:02 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .toDate()); Session session = new Session(); Session updateSessionWithCallTimes = TwilioAdapter.updateSessionWithCallTimes(session, call); Assert.assertThat(updateSessionWithCallTimes.getStartTimestamp(), Matchers.is( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:01 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + "")); Assert.assertThat(updateSessionWithCallTimes.getAnswerTimestamp(), Matchers.is( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:02 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + "")); Assert.assertThat(updateSessionWithCallTimes.getReleaseTimestamp(), Matchers.is( TimeUtils.getTimeWithFormat("Mon, 15 Aug 2005 15:52:32 +0000", "EEE, dd MMM yyyy HH:mm:ss Z", null, null) .getMillis() + "")); }