<commit_before>package org.sagebionetworks.web.unitclient.widget.entity.browse;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

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.Entity;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.FileEntity;
import org.sagebionetworks.repo.model.Folder;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.Reference;
import org.sagebionetworks.repo.model.VersionInfo;
import org.sagebionetworks.repo.model.request.ReferenceList;
import org.sagebionetworks.schema.adapter.AdapterFactory;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils.SelectedHandler;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.cache.ClientCache;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFilter;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinder;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinderArea;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinderView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.shared.PaginatedResults;
import org.sagebionetworks.web.shared.exceptions.ForbiddenException;
import org.sagebionetworks.web.shared.exceptions.NotFoundException;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

public class EntityFinderTest {

	EntityFinderView mockView;
	SynapseClientAsync mockSynapseClient;
	AdapterFactory adapterFactory;
	GlobalApplicationState mockGlobalApplicationState;
	AuthenticationController mockAuthenticationController;

	EntityFinder entityFinder;	
	PaginatedResults<EntityHeader> pr;
	@Mock
	ClientCache mockClientCache;
	@Mock
	SynapseAlert mockSynAlert;
	@Mock
	EntityHeader mockHeader;
	@Before
	public void before() throws JSONObjectAdapterException {
		MockitoAnnotations.initMocks(this);
		mockView = mock(EntityFinderView.class);
		mockSynapseClient = mock(SynapseClientAsync.class);
		adapterFactory = new AdapterFactoryImpl();
		mockGlobalApplicationState = mock(GlobalApplicationState.class);
		mockAuthenticationController = mock(AuthenticationController.class);
		
		entityFinder = new EntityFinder(mockView, mockSynapseClient, mockGlobalApplicationState, mockAuthenticationController, mockClientCache, mockSynAlert);
		verify(mockView).setPresenter(entityFinder);
		reset(mockView);
		when(mockView.isShowing()).thenReturn(false);
		
		pr = new PaginatedResults<EntityHeader>();
		pr.setResults(new ArrayList<EntityHeader>());
		pr.getResults().add(mockHeader);
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadEntity() throws Exception {
		SelectedHandler mockHandler = mock(SelectedHandler.class);
		entityFinder.configure(true, mockHandler);
		
		String name = "name";
		String id = "syn456";
		Entity entity = new Folder();
		entity.setId(id);
		entity.setName(name);
		Reference r = new Reference();
		r.setTargetId(id);
		ReferenceList refList = new ReferenceList();
		List<Reference> rList = new ArrayList<Reference>();
		refList.setReferences(rList);
		List<EntityHeader> headerList = new ArrayList<EntityHeader>();
		EntityHeader eh = new EntityHeader();
		eh.setId(id);
		eh.setName(name);
		eh.setType(Folder.class.getName());
		headerList.add(eh);
		
		AsyncMockStubber.callSuccessWith(headerList).when(mockSynapseClient).getEntityHeaderBatch(eq(refList), any(AsyncCallback.class));		
		AsyncCallback<List<EntityHeader>> mockCallback = mock(AsyncCallback.class);
		AsyncMockStubber.callSuccessWith(headerList).when(mockCallback).onSuccess(headerList);
		entityFinder.lookupEntity(id, mockCallback);
		
		verify(mockCallback).onSuccess(headerList);
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadEntityFail() throws Exception {
		String name = "name";
		String id = "syn456";
		Entity entity = new Folder();
		entity.setId(id);
		entity.setName(name);
		AsyncCallback<List<EntityHeader>> mockCallback = mock(AsyncCallback.class);
		
		Exception ex = new NotFoundException();
		AsyncMockStubber.callFailureWith(ex).when(mockSynapseClient).getEntity(eq(id), any(AsyncCallback.class));			
		entityFinder.lookupEntity(id, mockCallback);		
		verify(mockCallback).onFailure(any(Throwable.class));
		verify(mockSynAlert).handleException(ex);		
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadVersions() throws Exception {
		String id = "syn456";<commit_msg>Fixed load entity tests<commit_after>package org.sagebionetworks.web.unitclient.widget.entity.browse;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

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.Entity;
import org.sagebionetworks.repo.model.EntityHeader;
import org.sagebionetworks.repo.model.EntityType;
import org.sagebionetworks.repo.model.FileEntity;
import org.sagebionetworks.repo.model.Folder;
import org.sagebionetworks.repo.model.Project;
import org.sagebionetworks.repo.model.Reference;
import org.sagebionetworks.repo.model.VersionInfo;
import org.sagebionetworks.repo.model.request.ReferenceList;
import org.sagebionetworks.schema.adapter.AdapterFactory;
import org.sagebionetworks.schema.adapter.JSONObjectAdapterException;
import org.sagebionetworks.schema.adapter.org.json.AdapterFactoryImpl;
import org.sagebionetworks.web.client.DisplayConstants;
import org.sagebionetworks.web.client.DisplayUtils.SelectedHandler;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.cache.ClientCache;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFilter;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinder;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinderArea;
import org.sagebionetworks.web.client.widget.entity.browse.EntityFinderView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.shared.PaginatedResults;
import org.sagebionetworks.web.shared.exceptions.ForbiddenException;
import org.sagebionetworks.web.shared.exceptions.NotFoundException;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

public class EntityFinderTest {

	EntityFinderView mockView;
	SynapseClientAsync mockSynapseClient;
	AdapterFactory adapterFactory;
	GlobalApplicationState mockGlobalApplicationState;
	AuthenticationController mockAuthenticationController;

	EntityFinder entityFinder;	
	PaginatedResults<EntityHeader> pr;
	@Mock
	ClientCache mockClientCache;
	@Mock
	SynapseAlert mockSynAlert;
	@Mock
	EntityHeader mockHeader;
	@Before
	public void before() throws JSONObjectAdapterException {
		MockitoAnnotations.initMocks(this);
		mockView = mock(EntityFinderView.class);
		mockSynapseClient = mock(SynapseClientAsync.class);
		adapterFactory = new AdapterFactoryImpl();
		mockGlobalApplicationState = mock(GlobalApplicationState.class);
		mockAuthenticationController = mock(AuthenticationController.class);
		
		entityFinder = new EntityFinder(mockView, mockSynapseClient, mockGlobalApplicationState, mockAuthenticationController, mockClientCache, mockSynAlert);
		verify(mockView).setPresenter(entityFinder);
		reset(mockView);
		when(mockView.isShowing()).thenReturn(false);
		
		pr = new PaginatedResults<EntityHeader>();
		pr.setResults(new ArrayList<EntityHeader>());
		pr.getResults().add(mockHeader);
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadEntity() throws Exception {
		SelectedHandler mockHandler = mock(SelectedHandler.class);
		entityFinder.configure(true, mockHandler);
		
		String name = "name";
		String id = "syn456";
		
		when(mockHeader.getId()).thenReturn(id);
		when(mockHeader.getName()).thenReturn(name);
		when(mockHeader.getType()).thenReturn(Folder.class.getName());
		
		AsyncCallback<List<EntityHeader>> mockCallback = mock(AsyncCallback.class);
		AsyncMockStubber.callSuccessWith(pr).when(mockSynapseClient).getEntityHeaderBatch(any(ReferenceList.class), any(AsyncCallback.class));		
		entityFinder.lookupEntity(id, mockCallback);
		
		verify(mockCallback).onSuccess(pr.getResults());
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadEntityFail() throws Exception {
		SelectedHandler mockHandler = mock(SelectedHandler.class);
		entityFinder.configure(true, mockHandler);
		
		String name = "name";
		String id = "syn456";
		
		when(mockHeader.getId()).thenReturn(id);
		when(mockHeader.getName()).thenReturn(name);
		when(mockHeader.getType()).thenReturn(Folder.class.getName());
		
		AsyncCallback<List<EntityHeader>> mockCallback = mock(AsyncCallback.class);
		Exception ex = new NotFoundException();
		AsyncMockStubber.callFailureWith(ex).when(mockSynapseClient).getEntityHeaderBatch(any(ReferenceList.class), any(AsyncCallback.class));			
		entityFinder.lookupEntity(id, mockCallback);		
		verify(mockCallback).onFailure(any(Throwable.class));
		verify(mockSynAlert).handleException(ex);		
	}
	
	@SuppressWarnings("unchecked")
	@Test
	public void testLoadVersions() throws Exception {
		String id = "syn456";
{'fraction_non_alphanumeric': 0.10566829951014696, 'fraction_numerical': 0.0017994601619514146, 'mean_word_length': 7.061240934730057, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2409811', 'n_tokens_mistral': 3212, 'n_tokens_neox': 3060, 'n_words': 450}