Bug 515373 - Add MergeResolutionManagerTest

This commit migrates the changes of
https://git.eclipse.org/r/#/c/59322/ to Papyrus Compare.

Change-Id: I38a6f7a4b79d2f6bccde378f0c1a7e1b9d44e104
Signed-off-by: Philip Langer <planger@eclipsesource.com>
diff --git a/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/META-INF/MANIFEST.MF b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/META-INF/MANIFEST.MF
index 7f361c9..d50cde2 100644
--- a/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/META-INF/MANIFEST.MF
+++ b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/META-INF/MANIFEST.MF
@@ -17,4 +17,8 @@
  org.eclipse.papyrus.uml.tools,
  org.eclipse.emf.compare.rcp,
  org.eclipse.emf.compare.ide.ui.tests.git.framework,
- org.eclipse.emf.compare.ide.ui.tests.framework
+ org.eclipse.emf.compare.ide.ui.tests.framework,
+ org.mockito;bundle-version="1.8.0",
+ org.hamcrest;bundle-version="1.1.0",
+ org.eclipse.emf.common;bundle-version="2.11.0"
+Import-Package: org.eclipse.emf.compare.ide.ui.tests
diff --git a/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/egit/mergeresolution/MergeResolutionManagerTest.java b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/egit/mergeresolution/MergeResolutionManagerTest.java
new file mode 100644
index 0000000..1aa0cdf
--- /dev/null
+++ b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/egit/mergeresolution/MergeResolutionManagerTest.java
@@ -0,0 +1,155 @@
+/*******************************************************************************
+ * Copyright (C) 2015 EclipseSource Services Gmbh 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:
+ *     Michael Borkowski - initial API and implementation
+ *******************************************************************************/
+package org.eclipse.papyrus.compare.diagram.tests.egit.mergeresolution;
+
+import static org.mockito.Mockito.atLeastOnce;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+
+import org.eclipse.emf.common.util.BasicEList;
+import org.eclipse.emf.common.util.EList;
+import org.eclipse.emf.compare.Comparison;
+import org.eclipse.emf.compare.Conflict;
+import org.eclipse.emf.compare.Diff;
+import org.eclipse.emf.compare.DifferenceState;
+import org.eclipse.emf.compare.Match;
+import org.eclipse.emf.compare.ide.ui.internal.mergeresolution.MergeResolutionListenerRegistry;
+import org.eclipse.emf.compare.ide.ui.internal.structuremergeviewer.provider.TreeNodeCompareInput;
+import org.eclipse.emf.compare.ide.ui.mergeresolution.MergeResolutionManager;
+import org.junit.Before;
+import org.junit.Test;
+
+@SuppressWarnings("restriction")
+public class MergeResolutionManagerTest {
+	MergeResolutionManager sut;
+
+	MergeResolutionListenerRegistry registry;
+
+	@Before
+	public void setUp() {
+		registry = mock(MergeResolutionListenerRegistry.class);
+
+		sut = new MergeResolutionManager(registry);
+	}
+
+	@Test
+	public void testInvalidInput() {
+		sut.handleFlush(null);
+		sut.handleFlush("not a valid input");
+
+		verifyNoMoreInteractions(registry);
+	}
+
+	@Test
+	public void testNoConflicts() {
+		EList<Conflict> conflicts = new BasicEList<>();
+
+		Comparison comparison = mock(Comparison.class);
+		when(comparison.getConflicts()).thenReturn(conflicts);
+
+		sut.handleFlush(pack(comparison));
+		verify(comparison).getConflicts();
+		verifyNoMoreInteractions(registry);
+	}
+
+	@Test
+	public void testOnlyUnresolvedConflicts() {
+		EList<Conflict> conflicts = new BasicEList<>();
+
+		Conflict c1 = mock(Conflict.class);
+		EList<Diff> c1d = new BasicEList<>();
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		when(c1.getDifferences()).thenReturn(c1d);
+		conflicts.add(c1);
+
+		Conflict c2 = mock(Conflict.class);
+		EList<Diff> c2d = new BasicEList<>();
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		when(c2.getDifferences()).thenReturn(c2d);
+		conflicts.add(c2);
+
+		Comparison comparison = mock(Comparison.class);
+		when(comparison.getConflicts()).thenReturn(conflicts);
+
+		sut.handleFlush(pack(comparison));
+		verify(comparison, atLeastOnce()).getConflicts();
+		verifyNoMoreInteractions(registry);
+	}
+
+	@Test
+	public void testMixedConflicts() {
+		EList<Conflict> conflicts = new BasicEList<>();
+
+		Conflict c1 = mock(Conflict.class);
+		EList<Diff> c1d = new BasicEList<>();
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.UNRESOLVED).getMock());
+		when(c1.getDifferences()).thenReturn(c1d);
+		conflicts.add(c1);
+
+		Conflict c2 = mock(Conflict.class);
+		EList<Diff> c2d = new BasicEList<>();
+		c2d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.MERGED).getMock());
+		c2d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.DISCARDED).getMock());
+		when(c2.getDifferences()).thenReturn(c2d);
+		conflicts.add(c2);
+
+		Comparison comparison = mock(Comparison.class);
+		when(comparison.getConflicts()).thenReturn(conflicts);
+
+		sut.handleFlush(pack(comparison));
+		verify(comparison, atLeastOnce()).getConflicts();
+		verifyNoMoreInteractions(registry);
+	}
+
+	@Test
+	public void testOnlyResolvedConflicts() {
+		EList<Conflict> conflicts = new BasicEList<>();
+
+		Conflict c1 = mock(Conflict.class);
+		EList<Diff> c1d = new BasicEList<>();
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.DISCARDED).getMock());
+		c1d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.MERGED).getMock());
+		when(c1.getDifferences()).thenReturn(c1d);
+		conflicts.add(c1);
+
+		Conflict c2 = mock(Conflict.class);
+		EList<Diff> c2d = new BasicEList<>();
+		c2d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.MERGED).getMock());
+		c2d.add((Diff)when(mock(Diff.class).getState()).thenReturn(DifferenceState.DISCARDED).getMock());
+		when(c2.getDifferences()).thenReturn(c2d);
+		conflicts.add(c2);
+
+		Comparison comparison = mock(Comparison.class);
+		when(comparison.getConflicts()).thenReturn(conflicts);
+
+		sut.handleFlush(pack(comparison));
+		verify(comparison, atLeastOnce()).getConflicts();
+		verify(registry).mergeResolutionCompleted(comparison);
+		verifyNoMoreInteractions(registry);
+	}
+
+	private Object pack(Comparison comparison) {
+		Match match = mock(Match.class);
+		when(match.getComparison()).thenReturn(comparison).getMock();
+
+		TreeNodeCompareInput treeNodeCompareInput = mock(TreeNodeCompareInput.class);
+		when(treeNodeCompareInput.getComparisonObject()).thenReturn(match);
+
+		return treeNodeCompareInput;
+
+	}
+}
diff --git a/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/suite/PapyrusGitTests.java b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/suite/PapyrusGitTests.java
index f1935c9..236c0f4 100644
--- a/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/suite/PapyrusGitTests.java
+++ b/plugins/compare/org.eclipse.papyrus.compare.diagram.tests.git/src/org/eclipse/papyrus/compare/diagram/tests/suite/PapyrusGitTests.java
@@ -25,6 +25,7 @@
 import org.eclipse.papyrus.compare.diagram.tests.egit.ResourceAttachmentChangeDelete1GitMergeTest;
 import org.eclipse.papyrus.compare.diagram.tests.egit.ResourceAttachmentChangeDelete2GitMergeTest;
 import org.eclipse.papyrus.compare.diagram.tests.egit.StereotypeConflictTest;
+import org.eclipse.papyrus.compare.diagram.tests.egit.mergeresolution.MergeResolutionManagerTest;
 import org.eclipse.papyrus.compare.diagram.tests.merge.AdditiveMergeDiagramTests;
 import org.eclipse.papyrus.compare.diagram.tests.resourceattachmentchange.implication.AttachmentChangeImplicationTest;
 import org.eclipse.papyrus.compare.diagram.tests.resourceattachmentchange.move.ResourceAttachmentChangeMoveConflictTests;
@@ -50,7 +51,8 @@
 		ResourceAttachmentChangeDelete2GitMergeTest.class, ResourceAttachmentChangeMoveConflictTests.class,
 		ResourceAttachmentChangeMoveNoConflictTests.class, ResourceAttachmentChangeMoveOrderTests.class,
 		StereotypeConflictTest.class, IgnoreDiFileChangesInGitMergeTest.class,
-		MoveOfDiagramConflictDetectionTest.class, AdditiveMergeDiagramTests.class, })
+		MoveOfDiagramConflictDetectionTest.class, AdditiveMergeDiagramTests.class,
+		MergeResolutionManagerTest.class, })
 public class PapyrusGitTests {
 	/**
 	 * Launches the test with the given arguments.