<commit_before>package tools.vitruv.framework.views.changederivation

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import tools.vitruv.framework.change.echange.eobject.CreateEObject
import tools.vitruv.framework.change.echange.eobject.DeleteEObject
import tools.vitruv.framework.change.echange.feature.attribute.ReplaceSingleValuedEAttribute
import tools.vitruv.framework.change.echange.root.InsertRootEObject
import tools.vitruv.framework.change.echange.root.RemoveRootEObject
import tools.vitruv.framework.util.Capture

import static org.hamcrest.MatcherAssert.assertThat
import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertTrue
import static tools.vitruv.testutils.matchers.ModelMatchers.containsModelOf
import static tools.vitruv.testutils.metamodels.AllElementTypesCreators.aet

import static extension edu.kit.ipd.sdq.commons.util.org.eclipse.emf.ecore.resource.ResourceSetUtil.withGlobalFactories
import static extension tools.vitruv.framework.util.Capture.operator_doubleGreaterThan

class BasicStateChangePropagationTest extends StateChangePropagationTest {
	private def getTestUri() {
		getModelURI("Test.allElementTypes")
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("create new resource and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void createNewResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new ResourceSetImpl().createResource(testUri) => [
			contents += aet.Root => [
				id = "Root"
			]
		]
		
		val changes = strategyToTest.getChangeSequenceForCreated(modelResource)
		assertEquals(3, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		assertEquals(1, changes.EChanges.filter(CreateEObject).size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		// Create empty resource to apply generated changes to
		val validationResourceSet = new ResourceSetImpl()
		changes.unresolve().resolveAndApply(validationResourceSet)
		
		modelResource.save(null)
		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("delete existing resource and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void deleteResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		resourceSet.record [
			createResource(testUri) => [
				contents += aet.Root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)
		val changes = strategyToTest.getChangeSequenceForDeleted(-modelResource)
		assertEquals(2, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(DeleteEObject).size)

		// Load resource to apply generated changes to
		val validationResourceSet = new ResourceSetImpl()
		validationResourceSet.getResource(testUri, true)
		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertTrue(validationResourceSet.resources.get(0).contents.empty)
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("replace root element and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void replaceRootElement(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		resourceSet.record [
			createResource(testUri) => [
				contents += aet.Root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		(-modelResource).record [
			contents.clear()
			contents += aet.Root => [
					id = "Root2"
				]
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("change a root element property and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void changeRootElementFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		resourceSet.record [
			root.singleValuedEAttribute = 2
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(1, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("change a non-root element property and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void changeNonRootElementFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		val containedRoot = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
					recursiveRoot = containedRoot => [
						id = "ContainedRoot"
						singleValuedEAttribute = 0
					]
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		resourceSet.record [
			containedRoot.singleValuedEAttribute = 1
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("move a resource to new location and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void moveResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)

		val movedResourceUri = getModelURI("moved.allElementTypes")
		resourceSet.record [
			createResource(movedResourceUri) => [
				contents += root
			] >> modelResource
		]

		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(2, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		
		changes.unresolve().resolveAndApply(validationResourceSet)

		(-modelResource).save(null)
		assertEquals(2, validationResourceSet.resources.size)
		assertThat(validationResourceSet.getResource(movedResourceUri, false), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("move a resource to new location changing root feature and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void moveResourceAndChangeRootFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)

		val movedResourceUri = getModelURI("moved.allElementTypes")
		resourceSet.record [
			(-modelResource).contents -= root
			root.singleValuedEAttribute = 2
			createResource(movedResourceUri) => [
				contents += root
			] >> modelResource
		]
		
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(3, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		changes.unresolve().resolveAndApply(validationResourceSet)

		(-modelResource).save(null)
		assertEquals(2, validationResourceSet.resources.size)
		assertThat(validationResourceSet.getResource(movedResourceUri, false), containsModelOf(-modelResource))
	}

}
<commit_msg>added tests for changing id of root and non-root elements<commit_after>package tools.vitruv.framework.views.changederivation

import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.junit.jupiter.api.DisplayName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import tools.vitruv.framework.change.echange.eobject.CreateEObject
import tools.vitruv.framework.change.echange.eobject.DeleteEObject
import tools.vitruv.framework.change.echange.feature.attribute.ReplaceSingleValuedEAttribute
import tools.vitruv.framework.change.echange.root.InsertRootEObject
import tools.vitruv.framework.change.echange.root.RemoveRootEObject
import tools.vitruv.framework.util.Capture

import static org.hamcrest.MatcherAssert.assertThat
import static org.junit.jupiter.api.Assertions.assertEquals
import static org.junit.jupiter.api.Assertions.assertTrue
import static tools.vitruv.testutils.matchers.ModelMatchers.containsModelOf
import static tools.vitruv.testutils.metamodels.AllElementTypesCreators.aet

import static extension edu.kit.ipd.sdq.commons.util.org.eclipse.emf.ecore.resource.ResourceSetUtil.withGlobalFactories
import static extension tools.vitruv.framework.util.Capture.operator_doubleGreaterThan

class BasicStateChangePropagationTest extends StateChangePropagationTest {
	private def getTestUri() {
		getModelURI("Test.allElementTypes")
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("create new resource and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void createNewResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new ResourceSetImpl().createResource(testUri) => [
			contents += aet.Root => [
				id = "Root"
			]
		]
		
		val changes = strategyToTest.getChangeSequenceForCreated(modelResource)
		assertEquals(3, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		assertEquals(1, changes.EChanges.filter(CreateEObject).size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		// Create empty resource to apply generated changes to
		val validationResourceSet = new ResourceSetImpl()
		changes.unresolve().resolveAndApply(validationResourceSet)
		
		modelResource.save(null)
		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("delete existing resource and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void deleteResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		resourceSet.record [
			createResource(testUri) => [
				contents += aet.Root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)
		val changes = strategyToTest.getChangeSequenceForDeleted(-modelResource)
		assertEquals(2, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(DeleteEObject).size)

		// Load resource to apply generated changes to
		val validationResourceSet = new ResourceSetImpl()
		validationResourceSet.getResource(testUri, true)
		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertTrue(validationResourceSet.resources.get(0).contents.empty)
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("replace root element and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void replaceRootElement(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		resourceSet.record [
			createResource(testUri) => [
				contents += aet.Root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		(-modelResource).record [
			contents.clear()
			contents += aet.Root => [
					id = "Root2"
				]
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("change a root element property and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void changeRootElementFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		resourceSet.record [
			root.singleValuedEAttribute = 2
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(1, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}
	
	@ParameterizedTest(name = "{1}")
    @DisplayName("change a root element's id and calculate state-based difference")
    @MethodSource("strategiesToTest")
    def void changeRootElementId(StateBasedChangeResolutionStrategy strategyToTest, String name) {
        val modelResource = new Capture<Resource>
        val root = aet.Root
        resourceSet.record [
            createResource(testUri) => [
                contents += root => [
                    id = "Root"
                ]
            ] >> modelResource
        ]
        (-modelResource).save(null)

        resourceSet.record [
            root.id = "Root2"
        ]

        val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
        val oldState = validationResourceSet.getResource(testUri, true)
        val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
        assertEquals(1, changes.EChanges.size)
        assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

        changes.unresolve().resolveAndApply(validationResourceSet)

        assertEquals(1, validationResourceSet.resources.size)
        assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
    }

	@ParameterizedTest(name = "{1}")
	@DisplayName("change a non-root element property and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void changeNonRootElementFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		val containedRoot = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
					recursiveRoot = containedRoot => [
						id = "ContainedRoot"
						singleValuedEAttribute = 0
					]
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		resourceSet.record [
			containedRoot.singleValuedEAttribute = 1
		]

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(1, changes.EChanges.size)
        assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		changes.unresolve().resolveAndApply(validationResourceSet)

		assertEquals(1, validationResourceSet.resources.size)
		assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
	}
	
	@ParameterizedTest(name = "{1}")
    @DisplayName("change a non-root element's id and calculate state-based difference")
    @MethodSource("strategiesToTest")
    def void changeNonRootElementId(StateBasedChangeResolutionStrategy strategyToTest, String name) {
        val modelResource = new Capture<Resource>
        val root = aet.Root
        val containedRoot = aet.Root
        resourceSet.record [
            createResource(testUri) => [
                contents += root => [
                    id = "Root"
                    recursiveRoot = containedRoot => [
                        id = "ContainedRoot"
                        singleValuedEAttribute = 0
                    ]
                ]
            ] >> modelResource
        ]
        (-modelResource).save(null)

        resourceSet.record [
            containedRoot.id = "ContainedRoot2"
        ]

        val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
        val oldState = validationResourceSet.getResource(testUri, true)
        val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
        assertEquals(1, changes.EChanges.size)
        assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

        changes.unresolve().resolveAndApply(validationResourceSet)

        assertEquals(1, validationResourceSet.resources.size)
        assertThat(validationResourceSet.resources.get(0), containsModelOf(-modelResource))
    }

	@ParameterizedTest(name = "{1}")
	@DisplayName("move a resource to new location and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void moveResource(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)

		val movedResourceUri = getModelURI("moved.allElementTypes")
		resourceSet.record [
			createResource(movedResourceUri) => [
				contents += root
			] >> modelResource
		]

		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(2, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		
		changes.unresolve().resolveAndApply(validationResourceSet)

		(-modelResource).save(null)
		assertEquals(2, validationResourceSet.resources.size)
		assertThat(validationResourceSet.getResource(movedResourceUri, false), containsModelOf(-modelResource))
	}

	@ParameterizedTest(name = "{1}")
	@DisplayName("move a resource to new location changing root feature and calculate state-based difference")
	@MethodSource("strategiesToTest")
	def void moveResourceAndChangeRootFeature(StateBasedChangeResolutionStrategy strategyToTest, String name) {
		val modelResource = new Capture<Resource>
		val root = aet.Root
		resourceSet.record [
			createResource(testUri) => [
				contents += root => [
					id = "Root"
				]
			] >> modelResource
		]
		(-modelResource).save(null)

		val validationResourceSet = new ResourceSetImpl().withGlobalFactories()
		val oldState = validationResourceSet.getResource(testUri, true)

		val movedResourceUri = getModelURI("moved.allElementTypes")
		resourceSet.record [
			(-modelResource).contents -= root
			root.singleValuedEAttribute = 2
			createResource(movedResourceUri) => [
				contents += root
			] >> modelResource
		]
		
		val changes = strategyToTest.getChangeSequenceBetween(-modelResource, oldState)
		assertEquals(3, changes.EChanges.size)
		assertEquals(1, changes.EChanges.filter(RemoveRootEObject).size)
		assertEquals(1, changes.EChanges.filter(InsertRootEObject).size)
		assertEquals(1, changes.EChanges.filter(ReplaceSingleValuedEAttribute).size)

		changes.unresolve().resolveAndApply(validationResourceSet)

		(-modelResource).save(null)
		assertEquals(2, validationResourceSet.resources.size)
		assertThat(validationResourceSet.getResource(movedResourceUri, false), containsModelOf(-modelResource))
	}

}

{'fraction_non_alphanumeric': 0.09892669559260105, 'fraction_numerical': 0.004338890157570221, 'mean_word_length': 5.02863436123348, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 94, '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': '4338776', 'n_tokens_mistral': 6678, 'n_tokens_neox': 6051, 'n_words': 1086}