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

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

import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.sagebionetworks.repo.model.UserProfile;
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.DisplayUtils;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.cache.ClientCache;
import org.sagebionetworks.web.client.widget.user.UserBadge;
import org.sagebionetworks.web.client.widget.user.UserBadgeView;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

/**
 * Unit test for the Summary widget.
 * @author dburdick
 *
 */
public class UserBadgeTest {

	AdapterFactory adapterFactory = new AdapterFactoryImpl();
	SynapseClientAsync mockSynapseClient;
	GlobalApplicationState mockGlobalApplicationState;
	SynapseJSNIUtils mockSynapseJSNIUtils;
	UserBadgeView mockView;
	UserBadge userBadge;
	UserProfile profile;
	
	ClientCache mockCache;
	String principalId = "id1";
	int max=10;
	String displayName;
	
	@Before
	public void before(){
		profile = new UserProfile();
		profile.setFirstName("John");
		profile.setLastName("Doe");
		profile.setUserName("doeboy");
		displayName = DisplayUtils.getDisplayName(profile);
		profile.setOwnerId(principalId);
		mockSynapseClient = Mockito.mock(SynapseClientAsync.class);
		mockView = mock(UserBadgeView.class);
		mockCache = mock(ClientCache.class);
		mockGlobalApplicationState = mock(GlobalApplicationState.class);
		mockSynapseJSNIUtils = mock(SynapseJSNIUtils.class);
		userBadge = new UserBadge(mockView, mockSynapseClient, mockGlobalApplicationState, mockSynapseJSNIUtils);
	}
	
	@Test
	public void testConfigureStatic(){
		userBadge.configure(profile);		
		verify(mockView).setDisplayName(displayName, displayName);
	}
	
	@Test
	public void testConfigureAsync() throws Exception {
		AsyncMockStubber.callSuccessWith(profile).when(mockSynapseClient).getUserProfile(eq(principalId), any(AsyncCallback.class));
		userBadge.setMaxNameLength(max);
		userBadge.configure(profile);
		verify(mockView).setDisplayName(eq(displayName), anyString());
	}
	
	@Test
	public void testConfigureAsyncFail() throws Exception {
		AsyncMockStubber.callFailureWith(new Exception()).when(mockSynapseClient).getUserProfile(eq(principalId), any(AsyncCallback.class));		
		profile.setDisplayName("name");
		userBadge.configure(principalId);
		verify(mockView).showLoadError(principalId);
	}
	
	@Test
	public void testConfigureNullPrincipalId() throws Exception {
		userBadge.configure((String)null);
		verify(mockView).showLoadError(anyString());
	}
	
	@Test
	public void testConfigureEmptyPrincipalId() throws Exception {
		userBadge.configure("");
		verify(mockView).showLoadError(anyString());
	}
}

@@ -576,24 +576,77 @@
 tionState;%0D%0A
+import org.sagebionetworks.web.client.PlaceChanger;%0D%0A
 import org.s
@@ -692,16 +692,16 @@
 Async;%0D%0A
-
 import o
@@ -803,24 +803,78 @@
 ientCache;%0D%0A
+import org.sagebionetworks.web.client.place.Profile;%0D%0A
 import org.s
@@ -1049,24 +1049,130 @@
 Stubber;%0D%0A%0D%0A
+import com.google.gwt.event.dom.client.ClickEvent;%0D%0Aimport com.google.gwt.event.dom.client.ClickHandler;%0D%0A
 import com.g
@@ -1473,16 +1473,49 @@
 State;%0D%0A
+%09PlaceChanger mockPlaceChanger;%0D%0A
 %09Synapse
@@ -2030,24 +2030,72 @@
 nc.class);%0D%0A
+%09%09mockPlaceChanger = mock(PlaceChanger.class);%0D%0A
 %09%09mockView =
@@ -2227,24 +2227,108 @@
 te.class);%0D%0A
+%09%09when(mockGlobalApplicationState.getPlaceChanger()).thenReturn(mockPlaceChanger);%0D%0A
 %09%09mockSynaps
@@ -3561,24 +3561,24 @@
 igure(%22%22);%0D%0A
-
 %09%09verify(moc
@@ -3617,11 +3617,490 @@
 );%0D%0A%09%7D%0D%0A
+%09%0D%0A%09@Test%0D%0A%09public void testBadgeClicked() %7B%0D%0A%09%09userBadge.configure(profile);%0D%0A%09%09userBadge.badgeClicked(null);%0D%0A%09%09verify(mockPlaceChanger).goTo(any(Profile.class));%0D%0A%09%7D%0D%0A%09%0D%0A%09@Test%0D%0A%09public void testBadgeClickedCustomClickHandler() %7B%0D%0A%09%09userBadge.configure(profile);%0D%0A%09%09ClickHandler mockClickHandler = mock(ClickHandler.class);%0D%0A%09%09userBadge.setCustomClickHandler(mockClickHandler);%0D%0A%09%09userBadge.badgeClicked(null);%0D%0A%09%09verify(mockClickHandler).onClick(any(ClickEvent.class));%0D%0A%09%7D%0D%0A
 %7D%0D%0A

add test for badge clicked and custom click handler