package org.sagebionetworks.web.unitclient.widget.asynch;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyInt;
import static org.mockito.Matchers.anyList;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.web.client.GWTWrapper;
import org.sagebionetworks.web.client.SynapseJavascriptClient;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.widget.asynch.EntityHeaderAsyncHandlerImpl;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;
import com.google.gwt.user.client.rpc.AsyncCallback;

public class EntityHeaderAsyncHandlerImplTest {
	EntityHeaderAsyncHandlerImpl entityHeaderAsyncHandler;
	@Mock
	SynapseJavascriptClient mockSynapseJavascriptClient;
	@Mock
	GWTWrapper mockGwt;
	String entityId = "syn239";
	@Mock
	AsyncCallback mockCallback;
	@Mock
	EntityHeader mockEntityHeader;
	List<EntityHeader> entityHeaderList;

	@Before
	public void setUp() throws Exception {
		MockitoAnnotations.initMocks(this);
		entityHeaderAsyncHandler = new EntityHeaderAsyncHandlerImpl(mockSynapseJavascriptClient, mockGwt);
		entityHeaderList = new ArrayList<EntityHeader>();
		AsyncMockStubber.callSuccessWith(entityHeaderList).when(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));
		when(mockEntityHeader.getId()).thenReturn(entityId);
	}

	@Test
	public void testConstructor() {
		verify(mockGwt).scheduleFixedDelay(any(Callback.class), anyInt());
	}

	@Test
	public void testSuccess() {
		// verify no rpc if no entity headers have been requested.
		entityHeaderAsyncHandler.executeRequests();
		verifyZeroInteractions(mockSynapseJavascriptClient);

		// add one, simulate single entity header response
		entityHeaderAsyncHandler.getEntityHeader(entityId, mockCallback);
		entityHeaderList.add(mockEntityHeader);

		entityHeaderAsyncHandler.executeRequests();
		verify(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));
		verify(mockCallback).onSuccess(mockEntityHeader);
	}

	@Test
	public void testFailure() {
		// simulate exception response
		Exception ex = new Exception("problem loading batch of entity headers");
		AsyncMockStubber.callFailureWith(ex).when(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));
		entityHeaderAsyncHandler.getEntityHeader(entityId, mockCallback);
		entityHeaderAsyncHandler.executeRequests();

		verify(mockCallback).onFailure(ex);
	}

	@Test
	public void testNotFound() {
		when(mockEntityHeader.getId()).thenReturn("another entity id");
		// add one, simulate single entity header response
		entityHeaderAsyncHandler.getEntityHeader(entityId, mockCallback);
		entityHeaderList.add(mockEntityHeader);

		entityHeaderAsyncHandler.executeRequests();
		verify(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));
		verify(mockCallback).onFailure(any(Throwable.class));
	}

}

@@ -783,16 +783,84 @@
 erImpl;%0A
+import org.sagebionetworks.web.shared.exceptions.NotFoundException;%0A
 import o
@@ -908,24 +908,24 @@
 ockStubber;%0A
-
 import com.g
@@ -1188,16 +1188,43 @@
 yn239%22;%0A
+%09Long entityVersion = 32L;%0A
 %09@Mock%0A%09
@@ -2413,32 +2413,32 @@
 llback.class));%0A
-
 %09%09verify(mockCal
@@ -2471,24 +2471,979 @@
 yHeader);%0A%09%7D
+%0A%09%0A%09@Test%0A%09public void testVersionedSuccess() %7B%0A%09%09entityHeaderAsyncHandler.getEntityHeader(entityId, entityVersion, mockCallback);%0A%09%09when(mockEntityHeader.getVersionNumber()).thenReturn(entityVersion);%0A%09%09entityHeaderList.add(mockEntityHeader);%0A%09%09%0A%09%09entityHeaderAsyncHandler.executeRequests();%0A%09%09verify(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));%0A%09%09verify(mockCallback).onSuccess(mockEntityHeader);%0A%09%7D%0A%0A%09@Test%0A%09public void testVersionedNotFound() %7B%0A%09%09entityHeaderAsyncHandler.getEntityHeader(entityId, entityVersion, mockCallback);%0A%09%09// different version%0A%09%09when(mockEntityHeader.getVersionNumber()).thenReturn(entityVersion + 1);%0A%09%09entityHeaderList.add(mockEntityHeader);%0A%09%09%0A%09%09entityHeaderAsyncHandler.executeRequests();%0A%09%09verify(mockSynapseJavascriptClient).getEntityHeaderBatchFromReferences(anyList(), any(AsyncCallback.class));%0A%09%09verify(mockCallback).onFailure(any(NotFoundException.class));%0A%09%7D
 %0A%0A%09@Test%0A%09pu

add tests for new entity header async handler (version support)