<commit_before>package org.sagebionetworks.web.unitclient.presenter;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static junit.framework.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.repo.model.TeamMembershipStatus;
import org.sagebionetworks.schema.adapter.JSONObjectAdapter;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.presenter.TeamPresenter;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.view.TeamView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.client.widget.team.InviteWidget;
import org.sagebionetworks.web.client.widget.team.JoinTeamWidget;
import org.sagebionetworks.web.client.widget.team.MemberListWidget;
import org.sagebionetworks.web.client.widget.team.OpenMembershipRequestsWidget;
import org.sagebionetworks.web.client.widget.team.OpenUserInvitationsWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamDeleteModalWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamEditModalWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamLeaveModalWidget;
import org.sagebionetworks.web.shared.TeamBundle;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

public class TeamPresenterTest {

	TeamPresenter presenter;
	TeamView mockView;
	AuthenticationController mockAuthenticationController;
	GlobalApplicationState mockGlobalAppState;
	SynapseClientAsync mockSynClient;
	SynapseAlert mockSynAlert;
	TeamLeaveModalWidget mockLeaveModal;
	TeamDeleteModalWidget mockDeleteModal;
	TeamEditModalWidget mockEditModal;
	InviteWidget mockInviteModal;
	JoinTeamWidget mockJoinWidget;
	MemberListWidget mockMemberListWidget;
	OpenMembershipRequestsWidget mockOpenMembershipRequestsWidget;
	OpenUserInvitationsWidget mockOpenUserInvitationsWidget;
	Team mockTeam;
	TeamBundle mockTeamBundle;
	TeamMembershipStatus mockTeamMembershipStatus;
	
	String teamId = "123";
	String teamName = "testTeam";
	boolean canPublicJoin = true;
	String teamIcon = "teamIcon";
	Long totalMembershipCount = 3L;
	Exception caught = new Exception("this is an exception");
	
	String newName = "newName";
	String newDesc = "newDesc";
	boolean newPublicJoin = false;
	String newIcon = "newIcon";
	
	@Before
	public void setup() {
		mockView = mock(TeamView.class);
		mockAuthenticationController = mock(AuthenticationController.class);
		mockGlobalAppState = mock(GlobalApplicationState.class);
		mockSynClient = mock(SynapseClientAsync.class);
		mockSynAlert = mock(SynapseAlert.class);
		mockLeaveModal = mock(TeamLeaveModalWidget.class);
		mockDeleteModal = mock(TeamDeleteModalWidget.class);
		mockEditModal = mock(TeamEditModalWidget.class);
		mockInviteModal = mock(InviteWidget.class);
		mockJoinWidget = mock(JoinTeamWidget.class);
		mockMemberListWidget = mock(MemberListWidget.class);
		mockOpenMembershipRequestsWidget = mock(OpenMembershipRequestsWidget.class);
		mockOpenUserInvitationsWidget = mock(OpenUserInvitationsWidget.class);
		presenter = new TeamPresenter(mockView, mockAuthenticationController, mockGlobalAppState, mockSynClient, mockSynAlert, mockLeaveModal, mockDeleteModal, mockEditModal, mockInviteModal, mockJoinWidget, mockMemberListWidget, mockOpenMembershipRequestsWidget, mockOpenUserInvitationsWidget);
		mockTeam = mock(Team.class);
		when(mockTeam.getName()).thenReturn(teamName);
		mockTeamBundle = mock(TeamBundle.class);
		mockTeamMembershipStatus = mock(TeamMembershipStatus.class);
		AsyncMockStubber.callSuccessWith(mockTeamBundle).when(mockSynClient)
		.getTeamBundle(anyString(), anyString(), anyBoolean(), any(AsyncCallback.class));
		
		//team bundle
		when(mockTeamBundle.getTeam()).thenReturn(mockTeam);
		when(mockTeamBundle.getTeamMembershipStatus()).thenReturn(mockTeamMembershipStatus);
		when(mockTeamBundle.getTotalMemberCount()).thenReturn(totalMembershipCount);
		
		//team
		when(mockTeam.getCanPublicJoin()).thenReturn(canPublicJoin);
		when(mockTeam.getId()).thenReturn(teamId);
		when(mockTeam.getIcon()).thenReturn(teamIcon);
	}
	
	@Test
	public void testRefreshFailure() {
		AsyncMockStubber.callFailureWith(caught).when(mockSynClient).getTeamBundle(anyString(), anyString(), anyBoolean(), any(AsyncCallback.class));
		presenter.refresh(teamId);
		verify(mockSynAlert).clear();
		verify(mockSynAlert).handleException(caught);
	}
	
	@Test
	public void testRefreshAdmin() {
		boolean isAdmin = true;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(true);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(canPublicJoin);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockView).showMemberMenuItems();
		verify(mockOpenMembershipRequestsWidget).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget).configure(eq(teamId), any(Callback.class));
		verify(mockView).showAdminMenuItems();
		verify(mockView).setTeamEmailAddress(anyString());
		//never
		verify(mockJoinWidget, never()).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
	}
	
	@Test
	public void testRefreshNotMember() {
		boolean isAdmin = false;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(false);
		//SWC-2655: also test null canPublicJoin
		when(mockTeam.getCanPublicJoin()).thenReturn(null);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(false);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockJoinWidget).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
	
		//never
		verify(mockView, never()).showMemberMenuItems();
		verify(mockOpenMembershipRequestsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockView, never()).showAdminMenuItems();
	}
	
	@Test
	public void testRefreshMember() {	
		boolean isAdmin = false;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(true);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(canPublicJoin);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockView).showMemberMenuItems();
		
		//never
		verify(mockJoinWidget, never()).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
		verify(mockOpenMembershipRequestsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockView, never()).showAdminMenuItems();
	}

	@Test
	public void testGetTeamEmail() {
		when(mockAuthenticationController.isLoggedIn()).thenReturn(true);
		assertEquals("<EMAIL>", presenter.getTeamEmail("basic"));
		assertEquals("<EMAIL>", presenter.getTeamEmail("Standard Case Here"));
		assertEquals("<EMAIL>", presenter.getTeamEmail(" \n\r unlikely\t case "));
		assertEquals("<EMAIL>", presenter.getTeamEmail(" %^$##* Another_Unlikely\t &*#$)(!!@~Case "));
		when(mockAuthenticationController.isLoggedIn()).thenReturn(false);
		assertEquals("", presenter.getTeamEmail("basic"));
	}
	
}<commit_msg>fix and add test to team presenter<commit_after>package org.sagebionetworks.web.unitclient.presenter;

import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static junit.framework.Assert.*;

import org.junit.Before;
import org.junit.Test;
import org.sagebionetworks.repo.model.Team;
import org.sagebionetworks.repo.model.TeamMembershipStatus;
import org.sagebionetworks.schema.adapter.JSONObjectAdapter;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.presenter.TeamPresenter;
import org.sagebionetworks.web.client.security.AuthenticationController;
import org.sagebionetworks.web.client.utils.Callback;
import org.sagebionetworks.web.client.view.TeamView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.client.widget.sharing.TeamAccessControlListModalWidget;
import org.sagebionetworks.web.client.widget.team.InviteWidget;
import org.sagebionetworks.web.client.widget.team.JoinTeamWidget;
import org.sagebionetworks.web.client.widget.team.MemberListWidget;
import org.sagebionetworks.web.client.widget.team.OpenMembershipRequestsWidget;
import org.sagebionetworks.web.client.widget.team.OpenUserInvitationsWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamDeleteModalWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamEditModalWidget;
import org.sagebionetworks.web.client.widget.team.controller.TeamLeaveModalWidget;
import org.sagebionetworks.web.shared.TeamBundle;
import org.sagebionetworks.web.test.helper.AsyncMockStubber;

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

public class TeamPresenterTest {

	TeamPresenter presenter;
	TeamView mockView;
	AuthenticationController mockAuthenticationController;
	GlobalApplicationState mockGlobalAppState;
	SynapseClientAsync mockSynClient;
	SynapseAlert mockSynAlert;
	TeamLeaveModalWidget mockLeaveModal;
	TeamDeleteModalWidget mockDeleteModal;
	TeamEditModalWidget mockEditModal;
	InviteWidget mockInviteModal;
	JoinTeamWidget mockJoinWidget;
	MemberListWidget mockMemberListWidget;
	OpenMembershipRequestsWidget mockOpenMembershipRequestsWidget;
	OpenUserInvitationsWidget mockOpenUserInvitationsWidget;
	TeamAccessControlListModalWidget mockTeamAccessControlListModalWidget;
	Team mockTeam;
	TeamBundle mockTeamBundle;
	TeamMembershipStatus mockTeamMembershipStatus;
	
	String teamId = "123";
	String teamName = "testTeam";
	boolean canPublicJoin = true;
	String teamIcon = "teamIcon";
	Long totalMembershipCount = 3L;
	Exception caught = new Exception("this is an exception");
	
	String newName = "newName";
	String newDesc = "newDesc";
	boolean newPublicJoin = false;
	String newIcon = "newIcon";
	
	@Before
	public void setup() {
		mockView = mock(TeamView.class);
		mockAuthenticationController = mock(AuthenticationController.class);
		mockGlobalAppState = mock(GlobalApplicationState.class);
		mockSynClient = mock(SynapseClientAsync.class);
		mockSynAlert = mock(SynapseAlert.class);
		mockLeaveModal = mock(TeamLeaveModalWidget.class);
		mockDeleteModal = mock(TeamDeleteModalWidget.class);
		mockEditModal = mock(TeamEditModalWidget.class);
		mockInviteModal = mock(InviteWidget.class);
		mockJoinWidget = mock(JoinTeamWidget.class);
		mockMemberListWidget = mock(MemberListWidget.class);
		mockOpenMembershipRequestsWidget = mock(OpenMembershipRequestsWidget.class);
		mockOpenUserInvitationsWidget = mock(OpenUserInvitationsWidget.class);
		mockTeamAccessControlListModalWidget = mock(TeamAccessControlListModalWidget.class);
		presenter = new TeamPresenter(mockView, mockAuthenticationController, mockGlobalAppState, 
				mockSynClient, mockSynAlert, mockLeaveModal, mockDeleteModal, mockEditModal, 
				mockInviteModal, mockJoinWidget, mockMemberListWidget, 
				mockOpenMembershipRequestsWidget, mockOpenUserInvitationsWidget,
				mockTeamAccessControlListModalWidget);
		mockTeam = mock(Team.class);
		when(mockTeam.getName()).thenReturn(teamName);
		mockTeamBundle = mock(TeamBundle.class);
		mockTeamMembershipStatus = mock(TeamMembershipStatus.class);
		AsyncMockStubber.callSuccessWith(mockTeamBundle).when(mockSynClient)
		.getTeamBundle(anyString(), anyString(), anyBoolean(), any(AsyncCallback.class));
		
		//team bundle
		when(mockTeamBundle.getTeam()).thenReturn(mockTeam);
		when(mockTeamBundle.getTeamMembershipStatus()).thenReturn(mockTeamMembershipStatus);
		when(mockTeamBundle.getTotalMemberCount()).thenReturn(totalMembershipCount);
		
		//team
		when(mockTeam.getCanPublicJoin()).thenReturn(canPublicJoin);
		when(mockTeam.getId()).thenReturn(teamId);
		when(mockTeam.getIcon()).thenReturn(teamIcon);
	}
	
	@Test
	public void testConstruction() {
		verify(mockView).setPresenter(presenter);
		verify(mockView).setSynAlertWidget(any(Widget.class));
		verify(mockView).setLeaveTeamWidget(any(Widget.class));
		verify(mockView).setDeleteTeamWidget(any(Widget.class));
		verify(mockView).setEditTeamWidget(any(Widget.class));
		verify(mockView).setInviteMemberWidget(any(Widget.class));
		verify(mockView).setJoinTeamWidget(any(Widget.class));
		verify(mockView).setOpenMembershipRequestWidget(any(Widget.class));
		verify(mockView).setOpenUserInvitationsWidget(any(Widget.class));
		verify(mockView).setMemberListWidget(any(Widget.class));
		verify(mockView).setAclModalWidget(any(Widget.class));
		verify(mockLeaveModal).setRefreshCallback(any(Callback.class));
		verify(mockEditModal).setRefreshCallback(any(Callback.class));
		verify(mockDeleteModal).setRefreshCallback(any(Callback.class));
		verify(mockInviteModal).setRefreshCallback(any(Callback.class));
	}
	
	@Test
	public void testRefreshFailure() {
		AsyncMockStubber.callFailureWith(caught).when(mockSynClient).getTeamBundle(anyString(), anyString(), anyBoolean(), any(AsyncCallback.class));
		presenter.refresh(teamId);
		verify(mockSynAlert).clear();
		verify(mockSynAlert).handleException(caught);
	}
	
	@Test
	public void testRefreshAdmin() {
		boolean isAdmin = true;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(true);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(canPublicJoin);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockView).showMemberMenuItems();
		verify(mockOpenMembershipRequestsWidget).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget).configure(eq(teamId), any(Callback.class));
		verify(mockView).showAdminMenuItems();
		verify(mockView).setTeamEmailAddress(anyString());
		verify(mockView).setShareButtonVisible(true);
		//never
		verify(mockJoinWidget, never()).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
	}
	
	@Test
	public void testRefreshNotMember() {
		boolean isAdmin = false;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(false);
		//SWC-2655: also test null canPublicJoin
		when(mockTeam.getCanPublicJoin()).thenReturn(null);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(false);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockJoinWidget).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
		verify(mockView).setShareButtonVisible(false);
		
		//never
		verify(mockView, never()).showMemberMenuItems();
		verify(mockOpenMembershipRequestsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockView, never()).showAdminMenuItems();
	}
	
	@Test
	public void testRefreshMember() {	
		boolean isAdmin = false;
		when(mockTeamBundle.isUserAdmin()).thenReturn(isAdmin);
		when(mockTeamMembershipStatus.getIsMember()).thenReturn(true);
		presenter.refresh(teamId);
		
		//once
		verify(mockView).setPublicJoinVisible(canPublicJoin);
		verify(mockView).setTotalMemberCount(totalMembershipCount.toString());
		verify(mockView).setMediaObjectPanel(mockTeam);
		verify(mockMemberListWidget).configure(eq(teamId), eq(isAdmin), any(Callback.class));
		verify(mockView).showMemberMenuItems();
		verify(mockView).setShareButtonVisible(false);
		//never
		verify(mockJoinWidget, never()).configure(eq(teamId), anyBoolean(), eq(mockTeamMembershipStatus), 
				any(Callback.class), anyString(), anyString(), anyString(), anyString(), anyBoolean());
		verify(mockOpenMembershipRequestsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockOpenUserInvitationsWidget, never()).configure(eq(teamId), any(Callback.class));
		verify(mockView, never()).showAdminMenuItems();
	}

	@Test
	public void testGetTeamEmail() {
		when(mockAuthenticationController.isLoggedIn()).thenReturn(true);
		assertEquals("<EMAIL>", presenter.getTeamEmail("basic"));
		assertEquals("<EMAIL>", presenter.getTeamEmail("Standard Case Here"));
		assertEquals("<EMAIL>", presenter.getTeamEmail(" \n\r unlikely\t case "));
		assertEquals("<EMAIL>", presenter.getTeamEmail(" %^$##* Another_Unlikely\t &*#$)(!!@~Case "));
		when(mockAuthenticationController.isLoggedIn()).thenReturn(false);
		assertEquals("", presenter.getTeamEmail("basic"));
	}
	@Test
	public void testShareButtonClicked() {
		presenter.refresh(teamId);
		presenter.shareButtonClicked();
		verify(mockTeamAccessControlListModalWidget).configure(mockTeam);
		verify(mockTeamAccessControlListModalWidget).showSharing(any(Callback.class));
	}
}
{'fraction_non_alphanumeric': 0.12053783284998681, 'fraction_numerical': 0.0008436593725283417, 'mean_word_length': 8.027129938124702, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '6443537', 'n_tokens_mistral': 5948, 'n_tokens_neox': 5628, 'n_words': 767}