<commit_before>package mesfavoris.internal.service.operations;

import static mesfavoris.tests.commons.bookmarks.BookmarkBuilder.bookmark;
import static mesfavoris.tests.commons.bookmarks.BookmarkBuilder.bookmarkFolder;
import static mesfavoris.tests.commons.bookmarks.BookmarksTreeBuilder.bookmarksTree;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import org.assertj.core.util.Lists;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.google.common.collect.ImmutableMap;

import mesfavoris.bookmarktype.IBookmarkPropertiesProvider;
import mesfavoris.model.Bookmark;
import mesfavoris.model.BookmarkDatabase;
import mesfavoris.model.BookmarkId;
import mesfavoris.model.BookmarksTree;
import mesfavoris.problems.BookmarkProblem;
import mesfavoris.problems.IBookmarkProblems;
import mesfavoris.tests.commons.bookmarks.BookmarksTreeBuilder;
import mesfavoris.tests.commons.waits.Waiter;

public class CheckBookmarkPropertiesOperationTest {
	private CheckBookmarkPropertiesOperation operation;
	private BookmarkDatabase bookmarkDatabase;
	private IBookmarkPropertiesProvider bookmarkPropertiesProvider = mock(IBookmarkPropertiesProvider.class);
	private IWorkbenchPart workbenchPart = mock(IWorkbenchPart.class);
	private IWorkbenchPage workbenchPage = mock(IWorkbenchPage.class);
	private ISelection selection = mock(ISelection.class);
	private IBookmarkProblems bookmarkProblems = mock(IBookmarkProblems.class);
	private Set<String> nonUpdatableProperties = new HashSet<>();

	@Before
	public void setUp() {
		nonUpdatableProperties.addAll(
				Lists.newArrayList(Bookmark.PROPERTY_NAME, Bookmark.PROPERTY_COMMENT, Bookmark.PROPERTY_COMMENT));
		bookmarkDatabase = new BookmarkDatabase("test", createBookmarksTree());
		operation = new CheckBookmarkPropertiesOperation(bookmarkDatabase, nonUpdatableProperties,
				bookmarkPropertiesProvider, bookmarkProblems);
		IWorkbenchPartSite workbenchPartSite = mock(IWorkbenchPartSite.class);
		when(workbenchPart.getSite()).thenReturn(workbenchPartSite);
		when(workbenchPartSite.getPage()).thenReturn(workbenchPage);
	}

	@Test
	public void testGetPropertiesNeedingUpdate() throws Exception {
		// Given
		BookmarkId bookmarkId = new BookmarkId("bookmark12");
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			bookmarksTreeModifier.addBookmarks(new BookmarkId("folder1"), Lists.newArrayList(new Bookmark(bookmarkId,
					ImmutableMap.of(Bookmark.PROPERTY_NAME, "bookmark12", "prop1", "value1", "prop2", "value2"))));
		});
		doPutPropertiesWhenAddBookmarkPropertiesCalled(bookmarkPropertiesProvider, selection,
				ImmutableMap.of("prop1", "value1", "prop2", "newValue2", "prop3", "value3"));

		// When
		Map<String, String> propertiesNeedingUpdate = operation.getPropertiesNeedingUpdate(bookmarkId, workbenchPart,
				selection, new NullProgressMonitor());

		// Then
		assertThat(propertiesNeedingUpdate).containsExactly(entry("prop2", "newValue2"), entry("prop3", "value3"));
	}

	@Test
	public void testCheckBookmarkPropertiesProblem() throws Exception {
		// Given
		BookmarkId bookmarkId = new BookmarkId("bookmark12");
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			bookmarksTreeModifier.addBookmarks(new BookmarkId("folder1"),
					Lists.newArrayList(new Bookmark(bookmarkId, ImmutableMap.of(Bookmark.PROPERTY_NAME, "bookmark12",
							Bookmark.PROPERTY_COMMENT, "comment", "prop1", "value1", "prop2", "value2"))));<commit_msg>fixed test and added another one<commit_after>package mesfavoris.internal.service.operations;

import static mesfavoris.tests.commons.bookmarks.BookmarkBuilder.bookmark;
import static mesfavoris.tests.commons.bookmarks.BookmarkBuilder.bookmarkFolder;
import static mesfavoris.tests.commons.bookmarks.BookmarksTreeBuilder.bookmarksTree;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeoutException;

import org.assertj.core.util.Lists;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.IWorkbenchPartSite;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import com.google.common.collect.ImmutableMap;

import mesfavoris.BookmarksException;
import mesfavoris.bookmarktype.IBookmarkPropertiesProvider;
import mesfavoris.internal.placeholders.PathPlaceholdersMap;
import mesfavoris.model.Bookmark;
import mesfavoris.model.BookmarkDatabase;
import mesfavoris.model.BookmarkId;
import mesfavoris.model.BookmarksTree;
import mesfavoris.placeholders.PathPlaceholder;
import mesfavoris.problems.BookmarkProblem;
import mesfavoris.problems.IBookmarkProblems;
import mesfavoris.tests.commons.bookmarks.BookmarksTreeBuilder;
import mesfavoris.tests.commons.waits.Waiter;

public class CheckBookmarkPropertiesOperationTest {
	private CheckBookmarkPropertiesOperation operation;
	private BookmarkDatabase bookmarkDatabase;
	private IBookmarkPropertiesProvider bookmarkPropertiesProvider = mock(IBookmarkPropertiesProvider.class);
	private IWorkbenchPart workbenchPart = mock(IWorkbenchPart.class);
	private IWorkbenchPage workbenchPage = mock(IWorkbenchPage.class);
	private ISelection selection = mock(ISelection.class);
	private IBookmarkProblems bookmarkProblems = mock(IBookmarkProblems.class);
	private PathPlaceholdersMap pathPlaceholders = new PathPlaceholdersMap();
	private Set<String> nonUpdatableProperties = new HashSet<>();

	@Before
	public void setUp() {
		nonUpdatableProperties.addAll(
				Lists.newArrayList(Bookmark.PROPERTY_NAME, Bookmark.PROPERTY_COMMENT, Bookmark.PROPERTY_COMMENT));
		bookmarkDatabase = new BookmarkDatabase("test", createBookmarksTree());
		operation = new CheckBookmarkPropertiesOperation(bookmarkDatabase, nonUpdatableProperties,
				bookmarkPropertiesProvider, pathPlaceholders, bookmarkProblems);
		IWorkbenchPartSite workbenchPartSite = mock(IWorkbenchPartSite.class);
		when(workbenchPart.getSite()).thenReturn(workbenchPartSite);
		when(workbenchPartSite.getPage()).thenReturn(workbenchPage);
	}

	@Test
	public void testGetPropertiesNeedingUpdate() throws Exception {
		// Given
		BookmarkId bookmarkId = new BookmarkId("bookmark12");
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			bookmarksTreeModifier.addBookmarks(new BookmarkId("folder1"), Lists.newArrayList(new Bookmark(bookmarkId,
					ImmutableMap.of(Bookmark.PROPERTY_NAME, "bookmark12", "prop1", "value1", "prop2", "value2"))));
		});
		doPutPropertiesWhenAddBookmarkPropertiesCalled(bookmarkPropertiesProvider, selection,
				ImmutableMap.of("prop1", "value1", "prop2", "newValue2", "prop3", "value3"));

		// When
		Map<String, String> propertiesNeedingUpdate = operation.getPropertiesNeedingUpdate(bookmarkId, workbenchPart,
				selection, new NullProgressMonitor());

		// Then
		assertThat(propertiesNeedingUpdate).containsExactly(entry("prop2", "newValue2"), entry("prop3", "value3"));
	}

	@Test
	public void testGetPropertiesUsingUndefinedPlaceholder() throws BookmarksException {
		// Given
		pathPlaceholders.add(new PathPlaceholder("PLACEHOLDER2", new Path("/var/workspace/project")));
		BookmarkId bookmarkId = new BookmarkId("bookmark12");
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			bookmarksTreeModifier.addBookmarks(new BookmarkId("folder1"),
					Lists.newArrayList(new Bookmark(bookmarkId, ImmutableMap.of("filePath",
							"${PLACEHOLDER1}/myFile.txt", "folderPath", "${PLACEHOLDER2}/myFolder"))));
		});

		// When
		Map<String, String> propertiesUsingUndefinedPlaceholder = operation
				.getPropertiesUsingUndefinedPlaceholder(bookmarkId);

		// Then
		assertThat(propertiesUsingUndefinedPlaceholder)
				.containsExactly(entry("filePath", "${PLACEHOLDER1}/myFile.txt"));

	}

	@Test
	public void testCheckBookmarkPropertiesProblem() throws Exception {
		// Given
		BookmarkId bookmarkId = new BookmarkId("bookmark12");
		bookmarkDatabase.modify(bookmarksTreeModifier -> {
			bookmarksTreeModifier.addBookmarks(new BookmarkId("folder1"),
					Lists.newArrayList(new Bookmark(bookmarkId, ImmutableMap.of(Bookmark.PROPERTY_NAME, "bookmark12",
							Bookmark.PROPERTY_COMMENT, "comment", "prop1", "value1", "prop2", "value2"))));
{'fraction_non_alphanumeric': 0.10205144225762783, 'fraction_numerical': 0.006560449859418931, 'mean_word_length': 7.365853658536586, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 15, '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': '6445870', 'n_tokens_mistral': 3030, 'n_tokens_neox': 2880, 'n_words': 480}