package org.sagebionetworks.web.unitclient.widget.table.v2.results.cell;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;

import java.util.ArrayList;

import org.gwtbootstrap3.client.ui.constants.IconType;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.widget.lazyload.LazyLoadHelper;
import org.sagebionetworks.web.client.widget.table.v2.results.cell.EntityIdCellRendererImpl;
import org.sagebionetworks.web.client.widget.table.v2.results.cell.EntityIdCellRendererView;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

import com.google.gwt.user.client.rpc.AsyncCallback;

public class EntityIdCellRendererImplTest {

	EntityIdCellRendererImpl renderer;
	@Mock
	EntityIdCellRendererView mockView;
	@Mock
	SynapseClientAsync mockSynapseClient;
	@Mock
	LazyLoadHelper mockLazyLoadHelper;
	@Mock
	EntityHeader mockProjectHeader;
	private static final String PROJECT_NAME = "Project Win";
	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		renderer = new EntityIdCellRendererImpl(mockView, mockSynapseClient, mockLazyLoadHelper);
		ArrayList<EntityHeader> returnList = new ArrayList<EntityHeader>();
		returnList.add(mockProjectHeader);
		AsyncMockStubber.callSuccessWith(returnList).when(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		when(mockProjectHeader.getName()).thenReturn(PROJECT_NAME);
	}
	
	private void simulateInView() {
		ArgumentCaptor<Callback> captor = ArgumentCaptor.forClass(Callback.class);
		verify(mockLazyLoadHelper).configure(captor.capture(), eq(mockView));
		captor.getValue().invoke();
	}
	
	@Test
	public void testAsWidget(){
		renderer.asWidget();
		verify(mockView).asWidget();
	}
	
	@Test
	public void testSetValue(){
		String entityId = "syn987654";
		renderer.setValue(entityId);
		verify(mockLazyLoadHelper).setIsConfigured();
		verify(mockView).setLinkHref("#!Synapse:syn987654");
		verifyZeroInteractions(mockSynapseClient);
		
		simulateInView();
		verify(mockView).showLoadingIcon();
		verify(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		verify(mockView).setIcon(any(IconType.class));
		verify(mockView).setLinkText(PROJECT_NAME);
		
		//verify that attempting to load data again is a no-op
		reset(mockSynapseClient);
		renderer.loadData();
		verifyZeroInteractions(mockSynapseClient);
	}
	
	@Test
	public void testSetValueRpcFailure(){
		String errorMessage = "error";
		AsyncMockStubber.callFailureWith(new Exception(errorMessage)).when(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		String entityId = "syn987654";
		renderer.setValue(entityId);
		
		simulateInView();
		verify(mockView).showLoadingIcon();
		verify(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		verify(mockView).showErrorIcon(errorMessage);
		verify(mockView).setLinkText(entityId);
	}
	
	@Test
	public void testSetValueInvalidResponse(){
		AsyncMockStubber.callSuccessWith(new ArrayList<EntityHeader>()).when(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		String entityId = "syn987654";
		renderer.setValue(entityId);
		
		simulateInView();
		verify(mockView).showLoadingIcon();
		verify(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));
		verify(mockView).showErrorIcon(DisplayConstants.ERROR_LOADING);
		verify(mockView).setLinkText(entityId);
	}
}

@@ -111,46 +111,8 @@
 ny;%0A
-import static org.mockito.Matchers.*;%0A
 impo
@@ -332,37 +332,8 @@
 n;%0A%0A
-import java.util.ArrayList;%0A%0A
 impo
@@ -620,23 +620,25 @@
 o.model.
-Project
+Reference
 ;%0Aimport
@@ -669,32 +669,30 @@
 .client.
-DisplayConstants
+utils.Callback
 ;%0Aimport
@@ -727,80 +727,46 @@
 ent.
-SynapseClientAsync;%0Aimport org.sagebionetworks.web.client.utils.Callback
+widget.asynch.EntityHeaderAsyncHandler
 ;%0Aim
@@ -1271,44 +1271,61 @@
 ck%0A%09
-SynapseClientAsync mockSynapseClient
+EntityHeaderAsyncHandler mockEntityHeaderAsyncHandler
 ;%0A%09@
@@ -1464,16 +1464,18 @@
 t Win%22;%0A
+%09%0A
 %09@Before
@@ -1596,251 +1596,179 @@
 mock
-SynapseClient, mockLazyLoadHelper);%0A%09%09ArrayList%3CEntityHeader%3E returnList = new ArrayList%3CEntityHeader%3E();%0A%09%09returnList.add(mockProjectHeader);%0A%09%09AsyncMockStubber.callSuccessWith(returnList).when(mockSynapseClient).getEntityHeaderBatch(anyList(
+LazyLoadHelper, mockEntityHeaderAsyncHandler);%0A%09%09AsyncMockStubber.callSuccessWith(mockProjectHeader).when(mockEntityHeaderAsyncHandler).getEntityHeader(any(Reference.class
 ), a
@@ -2400,37 +2400,48 @@
 actions(mock
-SynapseClient
+EntityHeaderAsyncHandler
 );%0A%09%09%0A%09%09simu
@@ -2509,52 +2509,69 @@
 mock
-SynapseClient).getEntityHeaderBatch(anyList(
+EntityHeaderAsyncHandler).getEntityHeader(any(Reference.class
 ), a
@@ -2759,37 +2759,48 @@
 %09%09reset(mock
-SynapseClient
+EntityHeaderAsyncHandler
 );%0A%09%09rendere
@@ -2842,29 +2842,40 @@
 ons(mock
-SynapseClient
+EntityHeaderAsyncHandler
 );%0A%09%7D%0A%09%0A
@@ -3030,559 +3030,69 @@
 mock
-SynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));%0A%09%09String entityId = %22syn987654%22;%0A%09%09renderer.setValue(entityId);%0A%09%09%0A%09%09simulateInView();%0A%09%09verify(mockView).showLoadingIcon();%0A%09%09verify(mockSynapseClient).getEntityHeaderBatch(anyList(), any(AsyncCallback.class));%0A%09%09verify(mockView).showErrorIcon(errorMessage);%0A%09%09verify(mockView).setLinkText(entityId);%0A%09%7D%0A%09%0A%09@Test%0A%09public void testSetValueInvalidResponse()%7B%0A%09%09AsyncMockStubber.callSuccessWith(new ArrayList%3CEntityHeader%3E()).when(mockSynapseClient).getEntityHeaderBatch(anyList(
+EntityHeaderAsyncHandler).getEntityHeader(any(Reference.class
 ), a
@@ -3259,52 +3259,69 @@
 mock
-SynapseClient).getEntityHeaderBatch(anyList(
+EntityHeaderAsyncHandler).getEntityHeader(any(Reference.class
 ), a
@@ -3383,38 +3383,20 @@
 con(
-DisplayConstants.ERROR_LOADING
+errorMessage
 );%0A%09

fix test to use EntityHeaderAsyncHandler