<commit_before>/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.bugzilla.tests;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttribute;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaTaskAttachmentHandler;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.tasks.core.data.FileTaskAttachmentSource;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.ITask.SynchronizationState;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataModel;
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
import org.eclipse.mylyn.tasks.ui.TasksUi;

public class BugzillaRepositoryConnectorTest2 extends AbstractBugzillaTest {

	public void testAttachToExistingReport() throws Exception {
		init222();
		String taskNumber = "33";
		ITask task = generateLocalTaskAndDownload(taskNumber);
		assertNotNull(task);
		TaskDataModel model = createModel(task);
		TaskData taskData = model.getTaskData();
		assertNotNull(taskData);
		assertEquals(SynchronizationState.SYNCHRONIZED, task.getSynchronizationState());
		assertEquals(taskNumber, taskData.getTaskId());
//		int numAttached = taskData.getAttributeMapper()
//				.getAttributesByType(taskData, TaskAttribute.TYPE_ATTACHMENT)
//				.size();
//		String fileName = "test-attach-" + System.currentTimeMillis() + ".txt";

		assertNotNull(repository.getUserName());
		assertNotNull(repository.getPassword());

		TaskAttribute attrAttachment = taskData.getAttributeMapper().createTaskAttachment(taskData);
		TaskAttachmentMapper attachmentMapper = TaskAttachmentMapper.createFrom(attrAttachment);

		/* Initialize a local attachment */
		attachmentMapper.setDescription("Test attachment " + new Date());
		attachmentMapper.setContentType("text/plain");
		attachmentMapper.setPatch(false);
		attachmentMapper.setComment("Automated JUnit attachment test");

		/* Test attempt to upload a non-existent file */
		String filePath = "/this/is/not/a/real-file";
		BugzillaTaskAttachmentHandler.AttachmentPartSource source = new BugzillaTaskAttachmentHandler.AttachmentPartSource(
				new FileTaskAttachmentSource(new File(filePath)));
		BugzillaClient client = connector.getClientManager().getClient(repository, new NullProgressMonitor());
		try {
			client.postAttachment(taskNumber, attachmentMapper.getComment(), attachmentMapper.getDescription(),
					"application/octet-stream", false, source, new NullProgressMonitor());
			fail();
		} catch (Exception e) {
		}
//		// attachmentHandler.uploadAttachment(repository, task, comment,
//		// summary, file, contentType, isPatch, proxySettings)
//		// assertFalse(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		assertEquals(SynchronizationState.SYNCHRONIZED, task.getSynchronizationState());
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//
//		assertEquals(numAttached, taskData.getAttachments().size());
//
//		/* Test attempt to upload an empty file */
//		File attachFile = new File(fileName);
//		attachment.setFilePath(attachFile.getAbsolutePath());
//		BufferedWriter write = new BufferedWriter(new FileWriter(attachFile));
//		attachFile = new File(attachment.getFilePath());
//		attachment.setFile(attachFile);
//		attachment.setFilename(attachFile.getName());
//		// assertFalse(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		try {
//			client.postAttachment(attachment.getReport().getTaskId(), attachment.getComment(), attachment, null);
//			fail();
//		} catch (Exception e) {
//		}
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//		taskData = TasksUiPlugin.getTaskDataStorageManager().getNewTaskData(task.getRepositoryUrl(), task.getTaskId());
//		assertEquals(numAttached, taskData.getAttachments().size());
//
//		/* Test uploading a proper file */
//		write.write("test file");
//		write.close();
//		attachment.setFilePath(attachFile.getAbsolutePath());
//		// assertTrue(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		File fileToAttach = new File(attachment.getFilePath());
//		assertTrue(fileToAttach.exists());
//		attachment.setFile(fileToAttach);
//		attachment.setFilename(fileToAttach.getName());
//		client.postAttachment(attachment.getReport().getTaskId(), attachment.getComment(), attachment, null);
//
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//		taskData = TasksUiPlugin.getTaskDataStorageManager().getNewTaskData(task.getRepositoryUrl(), task.getTaskId());
//		assertEquals(numAttached + 1, taskData.getAttachments().size());
//
//		// use assertion to track clean-up
//		assertTrue(attachFile.delete());
	}

	public void testDataRetrieval() throws CoreException, ParseException {
		init(IBugzillaConstants.TEST_BUGZILLA_30_URL);
		TaskData data = connector.getTaskData(repository, "2", new NullProgressMonitor());
		assertNotNull(data);
		TaskMapper mapper = new TaskMapper(data);
		assertEquals("2", data.getTaskId());
		assertEquals("New bug submit", mapper.getSummary());
		assertEquals("Test new bug submission", mapper.getDescription());
		assertEquals(PriorityLevel.P2, mapper.getPriorityLevel());
		assertEquals("TestComponent", mapper.getComponent());
		assertEquals("<EMAIL>", mapper.getOwner());
		assertEquals("TestProduct", mapper.getProduct());
		assertEquals("PC", mapper.getTaskData()
				.getRoot()
				.getMappedAttribute(BugzillaAttribute.REP_PLATFORM.getKey())
				.getValue());
		assertEquals("Windows", mapper.getTaskData()
				.getRoot()
				.getMappedAttribute(BugzillaAttribute.OP_SYS.getKey())
				.getValue());
		assertEquals("ASSIGNED", mapper.getTaskData().getRoot().getMappedAttribute(
				BugzillaAttribute.BUG_STATUS.getKey()).getValue());
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		assertEquals(format1.parse("2007-03-20 16:37"), mapper.getCreationDate());
		assertEquals(format2.parse("2008-05-28 17:17:21"), mapper.getModificationDate());

		//assertEquals("", mapper.getTaskUrl());<commit_msg>ASSIGNED - bug 242480: port Bugzilla tests to Myly 3.0 framework https://bugs.eclipse.org/bugs/show_bug.cgi?id=242480<commit_after>/*******************************************************************************
 * Copyright (c) 2004, 2008 Tasktop Technologies and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Tasktop Technologies - initial API and implementation
 *******************************************************************************/

package org.eclipse.mylyn.bugzilla.tests;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.mylyn.commons.net.AuthenticationCredentials;
import org.eclipse.mylyn.commons.net.AuthenticationType;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaAttribute;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaClient;
import org.eclipse.mylyn.internal.bugzilla.core.BugzillaTaskAttachmentHandler;
import org.eclipse.mylyn.internal.bugzilla.core.IBugzillaConstants;
import org.eclipse.mylyn.internal.context.core.ContextCorePlugin;
import org.eclipse.mylyn.internal.tasks.core.data.FileTaskAttachmentSource;
import org.eclipse.mylyn.internal.tasks.ui.TasksUiPlugin;
import org.eclipse.mylyn.internal.tasks.ui.util.AttachmentUtil;
import org.eclipse.mylyn.tasks.core.ITask;
import org.eclipse.mylyn.tasks.core.ITask.PriorityLevel;
import org.eclipse.mylyn.tasks.core.ITask.SynchronizationState;
import org.eclipse.mylyn.tasks.core.data.ITaskDataWorkingCopy;
import org.eclipse.mylyn.tasks.core.data.TaskAttachmentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskAttribute;
import org.eclipse.mylyn.tasks.core.data.TaskAttributeMapper;
import org.eclipse.mylyn.tasks.core.data.TaskCommentMapper;
import org.eclipse.mylyn.tasks.core.data.TaskData;
import org.eclipse.mylyn.tasks.core.data.TaskDataModel;
import org.eclipse.mylyn.tasks.core.data.TaskMapper;
import org.eclipse.mylyn.tasks.ui.TasksUi;

public class BugzillaRepositoryConnectorTest2 extends AbstractBugzillaTest {

	public void testAttachToExistingReport() throws Exception {
		init222();
		String taskNumber = "33";
		ITask task = generateLocalTaskAndDownload(taskNumber);
		assertNotNull(task);
		TaskDataModel model = createModel(task);
		TaskData taskData = model.getTaskData();
		assertNotNull(taskData);
		assertEquals(SynchronizationState.SYNCHRONIZED, task.getSynchronizationState());
		assertEquals(taskNumber, taskData.getTaskId());
//		int numAttached = taskData.getAttributeMapper()
//				.getAttributesByType(taskData, TaskAttribute.TYPE_ATTACHMENT)
//				.size();
//		String fileName = "test-attach-" + System.currentTimeMillis() + ".txt";

		assertNotNull(repository.getUserName());
		assertNotNull(repository.getPassword());

		TaskAttribute attrAttachment = taskData.getAttributeMapper().createTaskAttachment(taskData);
		TaskAttachmentMapper attachmentMapper = TaskAttachmentMapper.createFrom(attrAttachment);

		/* Initialize a local attachment */
		attachmentMapper.setDescription("Test attachment " + new Date());
		attachmentMapper.setContentType("text/plain");
		attachmentMapper.setPatch(false);
		attachmentMapper.setComment("Automated JUnit attachment test");

		/* Test attempt to upload a non-existent file */
		String filePath = "/this/is/not/a/real-file";
		BugzillaTaskAttachmentHandler.AttachmentPartSource source = new BugzillaTaskAttachmentHandler.AttachmentPartSource(
				new FileTaskAttachmentSource(new File(filePath)));
		BugzillaClient client = connector.getClientManager().getClient(repository, new NullProgressMonitor());
		try {
			client.postAttachment(taskNumber, attachmentMapper.getComment(), attachmentMapper.getDescription(),
					"application/octet-stream", false, source, new NullProgressMonitor());
			fail();
		} catch (Exception e) {
		}
//		// attachmentHandler.uploadAttachment(repository, task, comment,
//		// summary, file, contentType, isPatch, proxySettings)
//		// assertFalse(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		assertEquals(SynchronizationState.SYNCHRONIZED, task.getSynchronizationState());
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//
//		assertEquals(numAttached, taskData.getAttachments().size());
//
//		/* Test attempt to upload an empty file */
//		File attachFile = new File(fileName);
//		attachment.setFilePath(attachFile.getAbsolutePath());
//		BufferedWriter write = new BufferedWriter(new FileWriter(attachFile));
//		attachFile = new File(attachment.getFilePath());
//		attachment.setFile(attachFile);
//		attachment.setFilename(attachFile.getName());
//		// assertFalse(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		try {
//			client.postAttachment(attachment.getReport().getTaskId(), attachment.getComment(), attachment, null);
//			fail();
//		} catch (Exception e) {
//		}
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//		taskData = TasksUiPlugin.getTaskDataStorageManager().getNewTaskData(task.getRepositoryUrl(), task.getTaskId());
//		assertEquals(numAttached, taskData.getAttachments().size());
//
//		/* Test uploading a proper file */
//		write.write("test file");
//		write.close();
//		attachment.setFilePath(attachFile.getAbsolutePath());
//		// assertTrue(attachmentHandler.uploadAttachment(attachment,
//		// repository.getUserName(), repository.getPassword(),
//		// Proxy.NO_PROXY));
//		File fileToAttach = new File(attachment.getFilePath());
//		assertTrue(fileToAttach.exists());
//		attachment.setFile(fileToAttach);
//		attachment.setFilename(fileToAttach.getName());
//		client.postAttachment(attachment.getReport().getTaskId(), attachment.getComment(), attachment, null);
//
//		task = TasksUiInternal.createTask(repository, taskNumber, new NullProgressMonitor());
//		TasksUiInternal.synchronizeTask(connector, task, true, null);
//		taskData = TasksUiPlugin.getTaskDataStorageManager().getNewTaskData(task.getRepositoryUrl(), task.getTaskId());
//		assertEquals(numAttached + 1, taskData.getAttachments().size());
//
//		// use assertion to track clean-up
//		assertTrue(attachFile.delete());
	}

	public void testDataRetrieval() throws CoreException, ParseException {
		init(IBugzillaConstants.TEST_BUGZILLA_30_URL);
		TaskData data = connector.getTaskData(repository, "2", new NullProgressMonitor());
		assertNotNull(data);
		TaskMapper mapper = new TaskMapper(data);
		assertEquals("2", data.getTaskId());
		assertEquals("New bug submit", mapper.getSummary());
		assertEquals("Test new bug submission", mapper.getDescription());
		assertEquals(PriorityLevel.P2, mapper.getPriorityLevel());
		assertEquals("TestComponent", mapper.getComponent());
		assertEquals("<EMAIL>", mapper.getOwner());
		assertEquals("TestProduct", mapper.getProduct());
		assertEquals("PC", mapper.getTaskData()
				.getRoot()
				.getMappedAttribute(BugzillaAttribute.REP_PLATFORM.getKey())
				.getValue());
		assertEquals("Windows", mapper.getTaskData()
				.getRoot()
				.getMappedAttribute(BugzillaAttribute.OP_SYS.getKey())
				.getValue());
		assertEquals("ASSIGNED", mapper.getTaskData().getRoot().getMappedAttribute(
				BugzillaAttribute.BUG_STATUS.getKey()).getValue());
		SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		assertEquals(format1.parse("2007-03-20 16:37"), mapper.getCreationDate());
		assertEquals(format2.parse("2008-09-24 13:33:02"), mapper.getModificationDate());

		//assertEquals("", mapper.getTaskUrl());
{'fraction_non_alphanumeric': 0.1511244194573454, 'fraction_numerical': 0.007455389880224884, 'mean_word_length': 5.838696197241956, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4946073', 'n_tokens_mistral': 5017, 'n_tokens_neox': 4733, 'n_words': 797}