<commit_before>package org.sagebionetworks.web.unitclient.widget.verification;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.UserProfile;
import org.sagebionetworks.repo.model.file.FileHandleAssociateType;
import org.sagebionetworks.repo.model.file.FileHandleAssociation;
import org.sagebionetworks.repo.model.verification.AttachmentMetadata;
import org.sagebionetworks.repo.model.verification.VerificationState;
import org.sagebionetworks.repo.model.verification.VerificationStateEnum;
import org.sagebionetworks.repo.model.verification.VerificationSubmission;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.UserProfileClientAsync;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.MarkdownWidget;
import org.sagebionetworks.web.client.widget.entity.PromptModalView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.client.widget.upload.FileHandleList;
import org.sagebionetworks.web.client.widget.verification.VerificationSubmissionModalViewImpl;
import org.sagebionetworks.web.client.widget.verification.VerificationSubmissionWidget;
import org.sagebionetworks.web.shared.WikiPageKey;
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 VerificationSubmissionWidgetTest {
	@Mock
	VerificationSubmissionModalViewImpl mockView; 
	@Mock
	UserProfileClientAsync mockUserProfileClient;
	@Mock
	PortalGinInjector mockGinInjector;
	@Mock
	MarkdownWidget mockMarkdownWidget;
	@Mock
	SynapseClientAsync mockSynapseClient;
	@Mock
	SynapseAlert mockSynapseAlert;
	@Mock
	FileHandleList mockFileHandleList;
	@Mock
	SynapseJSNIUtils mockSynapseJSNIUtils;
	@Mock
	PromptModalView mockPromptModalView;
	@Mock
	CookieProvider mockCookieProvider;
	@Mock
	GlobalApplicationState mockGlobalApplicationState;
	@Mock
	VerificationSubmission mockSubmission;
	@Mock
	UserProfile mockProfile;
	
	PromptModalView.Presenter reasonPromptCallback;
	
	VerificationSubmissionWidget widget;
	String fileUrl = "https://s3/file.txt";
	String submissionId = "5432";
	String submissionFirstName = "Dubois";
	String submissionLastName = "Harris";
	String submissionLocation = "Cloud Arc";
	String submissionCompany = "Various";
	String submissionOrcId = "http://orcid.org/983";
	List<String> submissionEmails = Collections.singletonList("<EMAIL>");
	List<AttachmentMetadata> submissionAttachments;
	List<String> fileHandleIds;
	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		when(mockGinInjector.getVerificationSubmissionModalViewImpl()).thenReturn(mockView);
		widget = new VerificationSubmissionWidget(mockGinInjector, mockUserProfileClient, mockMarkdownWidget, mockSynapseClient, mockSynapseAlert, mockFileHandleList, mockSynapseJSNIUtils, mockPromptModalView, mockCookieProvider, mockGlobalApplicationState);
		
		ArgumentCaptor<PromptModalView.Presenter> captor = ArgumentCaptor.forClass(PromptModalView.Presenter.class);
		verify(mockPromptModalView).setPresenter(captor.capture());
		reasonPromptCallback = captor.getValue();
		
		when(mockSubmission.getId()).thenReturn(submissionId);
		when(mockSubmission.getFirstName()).thenReturn(submissionFirstName);
		when(mockSubmission.getLastName()).thenReturn(submissionLastName);
		when(mockSubmission.getLocation()).thenReturn(submissionLocation);
		when(mockSubmission.getCompany()).thenReturn(submissionCompany);
		when(mockSubmission.getEmails()).thenReturn(submissionEmails);
		when(mockSubmission.getOrcid()).thenReturn(submissionOrcId);
		AttachmentMetadata meta = new AttachmentMetadata();
		meta.setId("12836");
		meta.setFileName("abc.txt");
		submissionAttachments = Collections.singletonList(meta);
		when(mockSubmission.getAttachments()).thenReturn(submissionAttachments);
		
		when(mockFileHandleList.configure(any(CallbackP.class))).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setUploadButtonText(anyString())).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setCanDelete(anyBoolean())).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setCanUpload(anyBoolean())).thenReturn(mockFileHandleList);
		
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).updateVerificationState(anyLong(), any(VerificationState.class), any(AsyncCallback.class));
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).createVerificationSubmission(any(VerificationSubmission.class), any(AsyncCallback.class));
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).deleteVerificationSubmission(anyLong(), any(AsyncCallback.class));
		fileHandleIds = new ArrayList<String>();
		when(mockFileHandleList.getFileHandleIds()).thenReturn(fileHandleIds);
	}
	private void configureWithMockSubmission(){
		boolean isACTMember = false;
		boolean isModal = true;
		widget.configure(mockSubmission, isACTMember, isModal);
	}

	@Test
	public void testConfigureEditAsModal() {
		configureWithMockSubmission();
		
		assertFalse(widget.isNewSubmission());
		verify(mockGinInjector).getVerificationSubmissionModalViewImpl();
		verify(mockView).setFileHandleList(any(Widget.class));
		verify(mockView).setWikiPage(any(Widget.class));
		verify(mockView).setPromptModal(any(Widget.class));
		verify(mockView).setSynAlert(any(Widget.class));
		verify(mockView).setPresenter(widget);
		
		widget.asWidget();
		verify(mockView).asWidget();
	}
	
	@Test
	public void testConfigureNewAsModal() {
		boolean isACTMember = false;
		boolean isModal = true;
		String orcId = "http://orcid.org/123";
		widget.configure(mockProfile, orcId, isACTMember, isModal);
		
		assertTrue(widget.isNewSubmission());
		verify(mockGinInjector).getVerificationSubmissionModalViewImpl();
		verify(mockView).setFileHandleList(any(Widget.class));
		verify(mockView).setWikiPage(any(Widget.class));
		verify(mockView).setPromptModal(any(Widget.class));
		verify(mockView).setSynAlert(any(Widget.class));
		verify(mockView).setPresenter(widget);
	}

	
	@Test
	public void testConfigureAsTableRow() {
		//TODO: for ACT place
	}

	@Test
	public void testGetVerificationSubmissionHandleUrlOpen() {
		configureWithMockSubmission();
		
		String fileHandleId = "8888";
		widget.getVerificationSubmissionHandleUrlAndOpen(fileHandleId);
		ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
		verify(mockView).openWindow(captor.capture());
		
		String url = captor.getValue();
		assertTrue(url.contains(submissionId));
		assertTrue(url.contains(fileHandleId));
		assertTrue(url.contains(FileHandleAssociateType.VerificationSubmission.toString()));
	}
	
	@Test
	public void testRawFileHandleUrlAndOpen() {
		configureWithMockSubmission();
		String fileHandleId = "982";
		widget.getRawFileHandleUrlAndOpen(fileHandleId);
		verify(mockSynapseJSNIUtils).getBaseFileHandleUrl();
		verify(mockView).openWindow(anyString());
	}

	private UserProfile getPopulatedProfile() {
		UserProfile profile = new UserProfile();
		profile.setFirstName("Bruce");
		profile.setLastName("Banner");
		profile.setCompany("U.S. Government");
		profile.setLocation("Unknown");
		profile.setEmails(Collections.singletonList("<EMAIL>"));
		return profile;
	}<commit_msg>and fix test<commit_after>package org.sagebionetworks.web.unitclient.widget.verification;

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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

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.UserProfile;
import org.sagebionetworks.repo.model.file.FileHandleAssociateType;
import org.sagebionetworks.repo.model.file.FileHandleAssociation;
import org.sagebionetworks.repo.model.verification.AttachmentMetadata;
import org.sagebionetworks.repo.model.verification.VerificationState;
import org.sagebionetworks.repo.model.verification.VerificationStateEnum;
import org.sagebionetworks.repo.model.verification.VerificationSubmission;
import org.sagebionetworks.web.client.GlobalApplicationState;
import org.sagebionetworks.web.client.PortalGinInjector;
import org.sagebionetworks.web.client.SynapseClientAsync;
import org.sagebionetworks.web.client.SynapseJSNIUtils;
import org.sagebionetworks.web.client.UserProfileClientAsync;
import org.sagebionetworks.web.client.cookie.CookieProvider;
import org.sagebionetworks.web.client.utils.CallbackP;
import org.sagebionetworks.web.client.widget.entity.MarkdownWidget;
import org.sagebionetworks.web.client.widget.entity.PromptModalView;
import org.sagebionetworks.web.client.widget.entity.controller.SynapseAlert;
import org.sagebionetworks.web.client.widget.upload.FileHandleList;
import org.sagebionetworks.web.client.widget.verification.VerificationSubmissionModalViewImpl;
import org.sagebionetworks.web.client.widget.verification.VerificationSubmissionWidget;
import org.sagebionetworks.web.shared.WikiPageKey;
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 VerificationSubmissionWidgetTest {
	@Mock
	VerificationSubmissionModalViewImpl mockView; 
	@Mock
	UserProfileClientAsync mockUserProfileClient;
	@Mock
	PortalGinInjector mockGinInjector;
	@Mock
	MarkdownWidget mockMarkdownWidget;
	@Mock
	SynapseClientAsync mockSynapseClient;
	@Mock
	SynapseAlert mockSynapseAlert;
	@Mock
	FileHandleList mockFileHandleList;
	@Mock
	SynapseJSNIUtils mockSynapseJSNIUtils;
	@Mock
	PromptModalView mockPromptModalView;
	@Mock
	CookieProvider mockCookieProvider;
	@Mock
	GlobalApplicationState mockGlobalApplicationState;
	@Mock
	VerificationSubmission mockSubmission;
	@Mock
	UserProfile mockProfile;
	
	PromptModalView.Presenter reasonPromptCallback;
	
	VerificationSubmissionWidget widget;
	String fileUrl = "https://s3/file.txt";
	String submissionId = "5432";
	String submissionFirstName = "Dubois";
	String submissionLastName = "Harris";
	String submissionLocation = "Cloud Arc";
	String submissionCompany = "Various";
	String submissionOrcId = "http://orcid.org/983";
	List<String> submissionEmails = Collections.singletonList("<EMAIL>");
	List<AttachmentMetadata> submissionAttachments;
	List<String> fileHandleIds;
	@Before
	public void before(){
		MockitoAnnotations.initMocks(this);
		when(mockGinInjector.getVerificationSubmissionModalViewImpl()).thenReturn(mockView);
		widget = new VerificationSubmissionWidget(mockGinInjector, mockUserProfileClient, mockMarkdownWidget, mockSynapseClient, mockSynapseAlert, mockFileHandleList, mockSynapseJSNIUtils, mockPromptModalView, mockCookieProvider, mockGlobalApplicationState);
		
		ArgumentCaptor<PromptModalView.Presenter> captor = ArgumentCaptor.forClass(PromptModalView.Presenter.class);
		verify(mockPromptModalView).setPresenter(captor.capture());
		reasonPromptCallback = captor.getValue();
		
		when(mockSubmission.getId()).thenReturn(submissionId);
		when(mockSubmission.getFirstName()).thenReturn(submissionFirstName);
		when(mockSubmission.getLastName()).thenReturn(submissionLastName);
		when(mockSubmission.getLocation()).thenReturn(submissionLocation);
		when(mockSubmission.getCompany()).thenReturn(submissionCompany);
		when(mockSubmission.getEmails()).thenReturn(submissionEmails);
		when(mockSubmission.getOrcid()).thenReturn(submissionOrcId);
		AttachmentMetadata meta = new AttachmentMetadata();
		meta.setId("12836");
		meta.setFileName("abc.txt");
		submissionAttachments = Collections.singletonList(meta);
		when(mockSubmission.getAttachments()).thenReturn(submissionAttachments);
		
		when(mockFileHandleList.configure(any(CallbackP.class))).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setUploadButtonText(anyString())).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setCanDelete(anyBoolean())).thenReturn(mockFileHandleList);
		when(mockFileHandleList.setCanUpload(anyBoolean())).thenReturn(mockFileHandleList);
		
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).updateVerificationState(anyLong(), any(VerificationState.class), any(AsyncCallback.class));
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).createVerificationSubmission(any(VerificationSubmission.class), any(AsyncCallback.class));
		AsyncMockStubber.callSuccessWith(null).when(mockUserProfileClient).deleteVerificationSubmission(anyLong(), any(AsyncCallback.class));
		fileHandleIds = new ArrayList<String>();
		when(mockFileHandleList.getFileHandleIds()).thenReturn(fileHandleIds);
	}
	private void configureWithMockSubmission(){
		boolean isACTMember = false;
		boolean isModal = true;
		widget.configure(mockSubmission, isACTMember, isModal);
	}

	@Test
	public void testConfigureEditAsModal() {
		configureWithMockSubmission();
		
		assertFalse(widget.isNewSubmission());
		verify(mockGinInjector).getVerificationSubmissionModalViewImpl();
		verify(mockView).setFileHandleList(any(Widget.class));
		verify(mockView).setWikiPage(any(Widget.class));
		verify(mockView).setPromptModal(any(Widget.class));
		verify(mockView).setSynAlert(any(Widget.class));
		verify(mockView).setPresenter(widget);
		
		widget.asWidget();
		verify(mockView).asWidget();
	}
	
	@Test
	public void testConfigureNewAsModal() {
		boolean isACTMember = false;
		boolean isModal = true;
		String orcId = "http://orcid.org/123";
		widget.configure(mockProfile, orcId, isACTMember, isModal);
		
		assertTrue(widget.isNewSubmission());
		verify(mockGinInjector).getVerificationSubmissionModalViewImpl();
		verify(mockView).setFileHandleList(any(Widget.class));
		verify(mockView).setWikiPage(any(Widget.class));
		verify(mockView).setPromptModal(any(Widget.class));
		verify(mockView).setSynAlert(any(Widget.class));
		verify(mockView).setPresenter(widget);
	}

	
	@Test
	public void testConfigureAsTableRow() {
		//TODO: for ACT place
	}

	@Test
	public void testGetVerificationSubmissionHandleUrlOpen() {
		configureWithMockSubmission();
		
		String fileHandleId = "8888";
		widget.getVerificationSubmissionHandleUrlAndOpen(fileHandleId);
		ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
		verify(mockSynapseJSNIUtils).getFileHandleAssociationUrl(submissionId, FileHandleAssociateType.VerificationSubmission, fileHandleId);
		verify(mockView).openWindow(captor.capture());
	}
	
	@Test
	public void testRawFileHandleUrlAndOpen() {
		configureWithMockSubmission();
		String fileHandleId = "982";
		widget.getRawFileHandleUrlAndOpen(fileHandleId);
		verify(mockSynapseJSNIUtils).getBaseFileHandleUrl();
		verify(mockView).openWindow(anyString());
	}

	private UserProfile getPopulatedProfile() {
		UserProfile profile = new UserProfile();
		profile.setFirstName("Bruce");
		profile.setLastName("Banner");
		profile.setCompany("U.S. Government");
		profile.setLocation("Unknown");
		profile.setEmails(Collections.singletonList("<EMAIL>"));
		return profile;
	}
{'fraction_non_alphanumeric': 0.1072909357657715, 'fraction_numerical': 0.002934234866364738, 'mean_word_length': 8.08342989571263, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '3234443', 'n_tokens_mistral': 5068, 'n_tokens_neox': 4807, 'n_words': 615}