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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.verify;
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.Mockito;
import org.mockito.MockitoAnnotations;
import org.sagebionetworks.repo.model.EntityBundle;
import org.sagebionetworks.repo.model.EntityChildrenRequest;
import org.sagebionetworks.repo.model.EntityChildrenResponse;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.auth.UserEntityPermissions;
import org.sagebionetworks.repo.model.entity.Direction;
import org.sagebionetworks.repo.model.entity.SortBy;
import org.sagebionetworks.web.client.DisplayUtils;
import org.sagebionetworks.web.client.SynapseJavascriptClient;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.widget.LoadMoreWidgetContainer;
import org.sagebionetworks.web.client.widget.entity.controller.PreflightController;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.client.widget.table.TableListWidget;
import org.sagebionetworks.web.client.widget.table.TableListWidgetView;
import org.sagebionetworks.web.client.widget.table.modal.fileview.CreateTableViewWizard;
import org.sagebionetworks.web.client.widget.table.modal.upload.UploadTableModalWidget;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

public class TableListWidgetTest {

	private static final String ENTITY_ID = "syn123";
	private TableListWidgetView mockView;
	private TableListWidget widget;
	private EntityBundle parentBundle;
	private UserEntityPermissions permissions;
	@Mock
	CreateTableViewWizard mockCreateTableViewWizard;
	@Mock
	CookieProvider mockCookies;
	@Mock
	LoadMoreWidgetContainer mockLoadMoreWidgetContainer;
	@Mock
	EntityChildrenResponse mockResults;
	@Mock
	SynapseJavascriptClient mockSynapseJavascriptClient;
	@Mock
	SynapseAlert mockSynAlert;
	List<EntityHeader> searchResults;
	
	
	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		permissions = new UserEntityPermissions();
		permissions.setCanEdit(true);
		Project project = new Project();
		project.setId(ENTITY_ID);
		parentBundle = new EntityBundle();
		parentBundle.setEntity(project);
		parentBundle.setPermissions(permissions);
		mockView = Mockito.mock(TableListWidgetView.class);
		widget = new TableListWidget(mockView, mockSynapseJavascriptClient, mockLoadMoreWidgetContainer, mockSynAlert);
		AsyncMockStubber.callSuccessWith(mockResults).when(mockSynapseJavascriptClient).getEntityChildren(any(EntityChildrenRequest.class), any(AsyncCallback.class));
		searchResults = new ArrayList<EntityHeader>();
		when(mockResults.getPage()).thenReturn(searchResults);
		when(mockCookies.getCookie(DisplayUtils.SYNAPSE_TEST_WEBSITE_COOKIE_KEY)).thenReturn("true");
	}
	
	@Test
	public void testCreateQuery(){
		String parentId = ENTITY_ID;
		EntityChildrenRequest query = widget.createQuery(parentId);
		assertEquals(parentId, query.getParentId());
		assertTrue(query.getIncludeTypes().contains(EntityType.entityview));
		assertTrue(query.getIncludeTypes().contains(EntityType.table));
		assertEquals(SortBy.CREATED_ON, query.getSortBy());
		assertEquals(Direction.DESC, query.getSortDirection());
	}
	
	@Test
	public void testConfigureUnderPageSize(){
		widget.configure(parentBundle);
		verify(mockView).resetSortUI();
		verify(mockLoadMoreWidgetContainer).setIsMore(false);
	}
	
	@Test
	public void testConfigureOverPageSize(){
		when(mockResults.getNextPageToken()).thenReturn("ismore");
		widget.configure(parentBundle);
		verify(mockView).resetSortUI();
		verify(mockLoadMoreWidgetContainer).setIsMore(true);
	}
	
	@Test
	public void testConfigureFailure(){
		parentBundle.getPermissions().setCanEdit(false);
		String error = "an error";
		Throwable th = new Throwable(error);
		AsyncMockStubber.callFailureWith(th).when(mockSynapseJavascriptClient).getEntityChildren(any(EntityChildrenRequest.class), any(AsyncCallback.class));
		widget.configure(parentBundle);
		verify(mockSynAlert).handleException(th);
	}
}

@@ -180,24 +180,66 @@
 chers.any;%0D%0A
+import static org.mockito.Mockito.times;%0D%0A
 import stati
@@ -265,24 +265,24 @@
 to.verify;%0D%0A
-
 import stati
@@ -1195,24 +1195,80 @@
 eProvider;%0D%0A
+import org.sagebionetworks.web.client.utils.CallbackP;%0D%0A
 import org.s
@@ -1330,93 +1330,8 @@
 r;%0D%0A
-import org.sagebionetworks.web.client.widget.entity.controller.PreflightController;%0D%0A
 impo
@@ -1640,97 +1640,8 @@
 d;%0D%0A
-import org.sagebionetworks.web.client.widget.table.modal.upload.UploadTableModalWidget;%0D%0A
 impo
@@ -2308,16 +2308,73 @@
 Alert;%0D%0A
+%09@Mock%0D%0A%09CallbackP%3CString%3E mockTableClickedCallback;%0D%0A%09%0D%0A
 %09List%3CEn
@@ -3810,32 +3810,77 @@
 parentBundle);%0D%0A
+%09%09verify(mockView, times(2)).hideLoading();%0D%0A
 %09%09verify(mockVie
@@ -4556,32 +4556,32 @@
 parentBundle);%0D%0A
-
 %09%09verify(mockSyn
@@ -4613,11 +4613,296 @@
 );%0D%0A%09%7D%0D%0A
+%09%0D%0A%09@Test%0D%0A%09public void testOnTableClicked() %7B%0D%0A%09%09widget.setTableClickedCallback(mockTableClickedCallback);%0D%0A%09%09widget.onTableClicked(ENTITY_ID);%0D%0A%09%09%0D%0A%09%09verify(mockView).showLoading();%0D%0A%09%09verify(mockView).clearTableWidgets();%0D%0A%09%09verify(mockTableClickedCallback).invoke(ENTITY_ID);%0D%0A%09%7D%0D%0A
 %7D%0D%0A

add test for table list widget click handler