package com.microsoft.azure.eventhubs.exceptioncontracts;

import java.util.Collection;
import java.util.concurrent.ExecutionException;

import org.junit.Assume;
import org.junit.Test;

import com.microsoft.azure.eventhubs.*;
import com.microsoft.azure.eventhubs.lib.*;
import com.microsoft.azure.servicebus.*;

public class ReceiverEpoch extends TestBase
{

	@Test (expected = ReceiverDisconnectedException.class)
	public void testEpochReceiver() throws Throwable
	{
		Assume.assumeTrue(TestBase.isServiceRun());
		
		TestEventHubInfo eventHubInfo = TestBase.checkoutTestEventHub();
		try 
		{
			ConnectionStringBuilder connectionString = TestBase.getConnectionString(eventHubInfo);
			EventHubClient ehClient = EventHubClient.createFromConnectionString(connectionString.toString()).get();		
			
			try
			{
				String cgName = eventHubInfo.getRandomConsumerGroup();
				String partitionId = "0";
				long epoch = 345632;
				ehClient.createEpochReceiver(cgName, partitionId, PartitionReceiver.StartOfStream, false, epoch, new EventCounter()).get();
			
				try
				{
					ehClient.createEpochReceiver(cgName, partitionId, epoch - 10).get();
				}
				catch(ExecutionException exp)
				{
					throw exp.getCause();
				}
			}
			finally
			{
				ehClient.close();
			}
		}
		finally
		{
			TestBase.checkinTestEventHub(eventHubInfo.getName());
		}
	}

	public static final class EventCounter extends ReceiveHandler
	{
		private long count;
		
		public EventCounter()
		{ 
			count = 0;
		}
		
		@Override
		public void onReceive(Collection<EventData> events)
		{
			for(EventData event: events)
			{
				System.out.println(String.format("Counter: %s, Offset: %s, SeqNo: %s, EnqueueTime: %s, PKey: %s", 
						 this.count, event.getSystemProperties().getOffset(), event.getSystemProperties().getSequenceNumber(), event.getSystemProperties().getEnqueuedTime(), event.getSystemProperties().getPartitionKey()));
			}		
			
			count++;
		}
		
	}
}

@@ -56,37 +56,8 @@
 s;%0A%0A
-import java.util.Collection;%0A
 impo
@@ -100,17 +100,16 @@
 eption;%0A
-%0A
 import o
@@ -887,24 +887,53 @@
 345632;%0A%09%09%09%09
+PartitionReceiver receiver = 
 ehClient.cre
@@ -1015,18 +1015,56 @@
 e, epoch
-, 
+).get();%0A%09%09%09%09receiver.setReceiveHandler(
 new Even
@@ -1074,27 +1074,22 @@
 unter())
-.get()
 ;%0A%09%09%09
+%09
 %0A%09%09%09%09try
@@ -1427,16 +1427,25 @@
 extends 
+Partition
 ReceiveH
@@ -1526,19 +1526,17 @@
  0;%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09@Overr
@@ -1567,18 +1567,16 @@
 ive(
-Collection
+Iterable
 %3CEve
@@ -1951,16 +1951,16 @@
 %7D%09%09%0A%09%09%09%0A
-
 %09%09%09count
@@ -1962,16 +1962,84 @@
 count++;
+%09%09%09%0A%09%09%7D%0A%0A%09%09@Override%0A%09%09public void onError(Exception exception)%0A%09%09%7B%09
 %0A%09%09%7D%0A%09%09%0A

Fix build issue - onReceive test