-
Notifications
You must be signed in to change notification settings - Fork 45
/
CreateInstanceOperationHandlerTests.java
177 lines (140 loc) · 8.29 KB
/
CreateInstanceOperationHandlerTests.java
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) 2019, 2024 Obeo and others.
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.sirius.components.compatibility.emf.compatibility.operations;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.text.MessageFormat;
import java.util.List;
import java.util.UUID;
import org.eclipse.emf.ecore.ENamedElement;
import org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain;
import org.eclipse.sirius.components.emf.EPackageService;
import org.eclipse.sirius.components.compatibility.emf.compatibility.services.EditingDomainFactory;
import org.eclipse.sirius.components.compatibility.emf.modeloperations.ChildModelOperationHandler;
import org.eclipse.sirius.components.compatibility.emf.modeloperations.CreateInstanceOperationHandler;
import org.eclipse.sirius.components.core.api.IEditingContext;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.representations.IStatus;
import org.eclipse.sirius.components.representations.Success;
import org.eclipse.sirius.viewpoint.description.tool.ChangeContext;
import org.eclipse.sirius.viewpoint.description.tool.CreateInstance;
import org.eclipse.sirius.viewpoint.description.tool.ToolFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* Tests of the CreateInstance operation handler.
*
* @author lfasani
*/
public class CreateInstanceOperationHandlerTests {
private static final String VARIABLE_NAME = "myVariableName";
private static final String REFERENCE_NAME = "eClassifiers";
private static final String TYPE_NAME = "ecore::EClass";
private CreateInstanceOperationHandler createInstanceOperationHandler;
private CreateInstance createInstanceOperation;
private OperationTestContext operationTestContext;
@BeforeEach
public void initialize() {
this.operationTestContext = new OperationTestContext();
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
var editingContext = new IEMFEditingContext() {
@Override
public String getId() {
return UUID.randomUUID().toString();
}
@Override
public AdapterFactoryEditingDomain getDomain() {
return editingDomain;
}
};
this.operationTestContext.getVariables().put(IEditingContext.EDITING_CONTEXT, editingContext);
this.createInstanceOperation = ToolFactory.eINSTANCE.createCreateInstance();
this.createInstanceOperationHandler = new CreateInstanceOperationHandler(this.operationTestContext.getObjectService(), this.operationTestContext.getRepresentationMetadataSearchService(),
this.operationTestContext.getIdentifierProvider(), this.operationTestContext.getInterpreter(), new EPackageService(), new ChildModelOperationHandler(List.of()),
this.createInstanceOperation);
}
@Test
public void createInstanceOperationHandlerNominalCaseTest() {
// used to check that the variable name is added in variable scope
String className = "newClass";
ChangeContext subChangeContext = ToolFactory.eINSTANCE.createChangeContext();
subChangeContext.setBrowseExpression("aql:" + VARIABLE_NAME + ".renameENamedElementService('" + className + "'))");
this.createInstanceOperation.getSubModelOperations().add(subChangeContext);
// check the nominal case
this.createInstanceOperation.setReferenceName(REFERENCE_NAME);
this.createInstanceOperation.setTypeName(TYPE_NAME);
this.createInstanceOperation.setVariableName(VARIABLE_NAME);
IStatus handleResult = this.createInstanceOperationHandler.handle(this.operationTestContext.getVariables());
assertTrue(handleResult instanceof Success);
assertEquals(2, this.operationTestContext.getRootPackage().getEClassifiers().size());
assertEquals(className, this.operationTestContext.getRootPackage().getEClassifiers().get(1).getName());
// check that an empty variable name is a valid case
this.createInstanceOperation.setVariableName("");
handleResult = this.createInstanceOperationHandler.handle(this.operationTestContext.getVariables());
assertTrue(handleResult instanceof Success);
assertEquals(3, this.operationTestContext.getRootPackage().getEClassifiers().size());
assertEquals(null, this.operationTestContext.getRootPackage().getEClassifiers().get(2).getName());
}
@Test
public void createInstanceOperationHandlerChangeSelfCaseTest() {
// used to check that the variable name is added in variable scope
String className = "newClass";
ChangeContext subChangeContext = ToolFactory.eINSTANCE.createChangeContext();
subChangeContext.setBrowseExpression("aql:self.renameENamedElementService('" + className + "'))");
this.createInstanceOperation.getSubModelOperations().add(subChangeContext);
// check the nominal case
this.createInstanceOperation.setReferenceName(REFERENCE_NAME);
this.createInstanceOperation.setTypeName(TYPE_NAME);
this.createInstanceOperation.setVariableName(VARIABLE_NAME);
IStatus handleResult = this.createInstanceOperationHandler.handle(this.operationTestContext.getVariables());
assertTrue(handleResult instanceof Success);
assertEquals(2, this.operationTestContext.getRootPackage().getEClassifiers().size());
assertEquals(className, this.operationTestContext.getRootPackage().getEClassifiers().get(1).getName());
// check that an empty variable name is a valid case
this.createInstanceOperation.setVariableName("");
handleResult = this.createInstanceOperationHandler.handle(this.operationTestContext.getVariables());
assertTrue(handleResult instanceof Success);
assertEquals(3, this.operationTestContext.getRootPackage().getEClassifiers().size());
assertEquals(className, this.operationTestContext.getRootPackage().getEClassifiers().get(2).getName());
}
/**
* Check that a null or empty data do not stop the handle of subOperations.</br>
*/
@Test
public void createInstanceOperationHandlerErrorCasesTest() {
// Add a SubModelOperations to check that it is handled
ChangeContext subChangeContext = ToolFactory.eINSTANCE.createChangeContext();
this.createInstanceOperation.getSubModelOperations().add(subChangeContext);
// Check null expression case
this.handleAndCheckExecution(null, null, null, this.operationTestContext.getRootPackage());
// Check empty expression case
this.handleAndCheckExecution("", "", "", this.operationTestContext.getRootPackage());
this.handleAndCheckExecution(REFERENCE_NAME, "UnknownClass", VARIABLE_NAME, this.operationTestContext.getRootPackage());
}
/**
* Execute the root operation and check that the sub ChangeContext operation is properly executed.
*/
private void handleAndCheckExecution(String referenceName, String typeName, String variableName, ENamedElement renamedElement) {
String newName = UUID.randomUUID().toString();
String renameExpression = MessageFormat.format(ModelOperationServices.AQL_RENAME_EXPRESSION, newName);
((ChangeContext) this.createInstanceOperation.getSubModelOperations().get(0)).setBrowseExpression(renameExpression);
// execute
this.createInstanceOperation.setReferenceName(referenceName);
this.createInstanceOperation.setTypeName(typeName);
this.createInstanceOperation.setVariableName(variableName);
IStatus handleResult = this.createInstanceOperationHandler.handle(this.operationTestContext.getVariables());
// check
assertTrue(handleResult instanceof Success);
assertEquals(newName, renamedElement.getName());
}
}