Skip to main content
summaryrefslogtreecommitdiffstats
blob: 021363897ee4ef755d0109663a0e6b33cbf62247 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*******************************************************************************
 * Copyright (c) 2016-2017, Thales Global Services S.A.S.
 * This program and the accompanying materials are made
 * available under the terms of the Eclipse Public License 2.0
 * which is available at https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Abel Hegedus, Tamas Borbas, Balazs Grill, Peter Lunk, Daniel Segesdi (IncQuery Labs Ltd.) - initial API and implementation
 *******************************************************************************/
package org.eclipse.emf.diffmerge.patch.tests.emf

import java.io.File
import java.util.Collection
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.diffmerge.patch.api.EntryType
import org.eclipse.emf.diffmerge.patch.api.ModelPatchFilterApplier
import org.eclipse.emf.diffmerge.patch.api.ModelPatchReverser
import org.eclipse.emf.diffmerge.patch.api.filters.EntryTypeFilter
import org.eclipse.emf.diffmerge.patch.serializer.gson.GsonModelPatchSerializer
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.AppState
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.ApplicationInstance
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.CyberPhysicalSystemPackage
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.HostInstance
import org.eclipse.viatra.examples.cps.cyberPhysicalSystem.Identifiable
import org.junit.BeforeClass
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
import org.junit.runners.Parameterized.Parameter

import static org.junit.Assert.*

@RunWith(Parameterized)
class CPSModelPatchApplierTest extends CPSModelPatchTest{
  private extension val ModelPatchReverser modelPatchReverser = ModelPatchReverser.INSTANCE

  public static val FIRSTAPPCLASS_INSTANCE_ID = "simple.cps.app.FirstAppClass0.inst0"
  public static val FIRSTHOSTCLASS_INSTANCE_ID = "simple.cps.host.FirstHostClass0.inst0"
  public static val SECONDHOSTCLASS_INSTANCE_ID = "simple.cps.host.SecondHostClass0.inst0"


  @Parameterized.Parameters(name = "{0}")
  def static Collection<Object[]> testData() {
    return newArrayList(
      #[
        "Demo model",
        '''«MODEL_FOLDER»/demo_original.«MODEL_FILE_EXTENSION»''',
        '''«MODEL_FOLDER»/demo_modified.«MODEL_FILE_EXTENSION»''',
        '''«MODEL_FOLDER»/demo-patch-generated.«PATCH_FILE_EXTENSION»'''
      ]
    )
  }


  @Parameter(0)
  public String testName
  @Parameter(1)
  public String originalModelPath
  @Parameter(2)
  public String modifiedModelPath
  @Parameter(3)
  public String patchFilePath

  @BeforeClass
  public static def void initCPS() {
    Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(MODEL_FILE_EXTENSION, new XMIResourceFactoryImpl());
    CyberPhysicalSystemPackage.eINSTANCE.eClass();
  }

  @Test
  public def void applyPatch() {
    // Load model
    val resourceSet = new ResourceSetImpl();
    val resource = resourceSet.getResource(URI.createFileURI(originalModelPath), true);

    // Load patch file
    val patchFile = new File(patchFilePath);
    val modelPatch = GsonModelPatchSerializer.create(this.class.classLoader).load(patchFile);

    // Apply patch
    val patchApplier = preparePatchApplier(resourceSet)
    val app = patchApplier.apply(modelPatch, resourceSet)

    // Check the result
    val cpsElements = resource.allContents.toList.filter(Identifiable)
    assertEquals(AppState.STOPPED, (cpsElements.findFirst[FIRSTAPPCLASS_INSTANCE_ID == it.identifier] as ApplicationInstance).state)
    assertTrue("Missing application instance reference from host instance",
      (cpsElements.findFirst[SECONDHOSTCLASS_INSTANCE_ID == it.identifier] as HostInstance)
        .applications.exists[FIRSTAPPCLASS_INSTANCE_ID == it.identifier]
    )
    assertFalse("Host class has not been removed", cpsElements.exists[FIRSTHOSTCLASS_INSTANCE_ID == it.identifier])
  }



  @Test
  public def void applyReversePatch() {
    // Load model
    val resourceSet = new ResourceSetImpl();
    val resource = resourceSet.getResource(URI.createFileURI(modifiedModelPath), true);

    // Load and reverse patch file
    val patchFile = new File(patchFilePath);
    val modelPatch = GsonModelPatchSerializer.create(this.class.classLoader).load(patchFile);
    val reversePatch = modelPatch.reverse

    // Apply reverse patch
    val patchApplier = preparePatchApplier(resourceSet)
    patchApplier.apply(reversePatch, resourceSet)

    // Check the result
    val cpsElements = resource.allContents.toList.filter(Identifiable)
    assertEquals(AppState.RUNNING, (cpsElements.findFirst[FIRSTAPPCLASS_INSTANCE_ID == it.identifier] as ApplicationInstance).state)
    assertFalse("Application instance reference has not been removed from host instance",
      (cpsElements.findFirst[SECONDHOSTCLASS_INSTANCE_ID == it.identifier] as HostInstance)
        .applications.exists[FIRSTAPPCLASS_INSTANCE_ID == it.identifier]
    )
    assertTrue("Host class has not been added", cpsElements.exists[FIRSTHOSTCLASS_INSTANCE_ID == it.identifier])
  }

  @Test
  public def void applyReversePatchOnOriginal() {
    // Load model
    val resourceSet = new ResourceSetImpl();
    val resource = resourceSet.getResource(URI.createFileURI(originalModelPath), true);

    // Load and reverse patch file
    val patchFile = new File(patchFilePath);
    val modelPatch = GsonModelPatchSerializer.create(this.class.classLoader).load(patchFile);
    val reversePatch = modelPatch.reverse

    // Apply reverse patch
    val patchApplier = preparePatchApplier(resourceSet)
    val patchApplication = patchApplier.apply(reversePatch, resourceSet)

    // Assert
    assertTrue(patchApplication.diagnostics.numOfExceptions > 0);
    val diags = patchApplication.diagnostics.diagnosticElements
    assertTrue(diags.size > 0)
    val diag = diags.head
    assertNotNull(diag.caughtException)
    assertNotNull(diag.message)
    assertNotNull(diag.problematicEntry)
  }

  @Test
  public def void applyFilteredPatch() {
    // Load model
    val resourceSet = new ResourceSetImpl()
    val resource = resourceSet.getResource(URI.createFileURI(originalModelPath), true)

    // Load patch file
    val patchFile = new File(patchFilePath)
    val ModelPatchFilterApplier filterApplier = new ModelPatchFilterApplier
    val originalPatch = GsonModelPatchSerializer.create(this.class.classLoader).load(patchFile)
    val filteredPatch = filterApplier.applyFilter(originalPatch, new EntryTypeFilter(EntryType.ELEMENT, EntryType.REFERENCE))

    // Apply patch
    val patchApplier = preparePatchApplier(resourceSet)
    val patchApplication = patchApplier.apply(filteredPatch, resourceSet)
    val diags = patchApplication.diagnostics

    // Check the result
    val cpsElements = resource.allContents.toList.filter(Identifiable)
    assertEquals(AppState.STOPPED, (cpsElements.findFirst[FIRSTAPPCLASS_INSTANCE_ID == it.identifier] as ApplicationInstance).state)
    assertFalse("Reference should not been set up (REFERNCE entries are filtered)",
      (cpsElements.findFirst[SECONDHOSTCLASS_INSTANCE_ID == it.identifier] as HostInstance)
        .applications.exists[FIRSTAPPCLASS_INSTANCE_ID == it.identifier]
    )
    assertTrue("Host class should not been removed", cpsElements.exists[FIRSTHOSTCLASS_INSTANCE_ID == it.identifier])
  }
}

Back to the top