- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
'Create Value For Stakeholder' refactoring
Showing
12 changed files
with
351 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
...contextmapper/dsl/ide/tests/commands/refactoring/CreateValue4StakeholderCommandTest.xtend
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2024 The Context Mapper Project Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.contextmapper.dsl.ide.tests.commands.refactoring | ||
|
||
import com.google.gson.JsonArray | ||
import com.google.gson.JsonPrimitive | ||
import org.contextmapper.dsl.ide.commands.CMLCommandService | ||
import org.contextmapper.dsl.ide.tests.commands.AbstractCMLCommandTest | ||
import org.eclipse.lsp4j.ExecuteCommandParams | ||
import org.junit.jupiter.api.Test | ||
|
||
class CreateValue4StakeholderCommandTest extends AbstractCMLCommandTest { | ||
|
||
@Test | ||
def void testARCommandExecution() { | ||
// given | ||
initializeCommandsDynamically() | ||
val model = ''' | ||
Stakeholders { | ||
Stakeholder Tester | ||
} | ||
''' | ||
val fileURI = 'test.cml'.writeFile(model) | ||
|
||
// when | ||
val refactoringParams = new JsonArray | ||
refactoringParams.add("Tester") | ||
val result = languageServer.executeCommand( | ||
new ExecuteCommandParams("cml.ar.createValueForStakeholder", #[new JsonPrimitive(fileURI), refactoringParams])) | ||
val resultVal = result.get as String | ||
|
||
// then | ||
CMLCommandService.COMMAND_EXECUTED_RETURN_VALUE.assertEquals(resultVal) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
...per.dsl.ide/src/org/contextmapper/dsl/ide/actions/impl/CreateValue4StakeholderAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 2024 The Context Mapper Project Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.contextmapper.dsl.ide.actions.impl; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import org.contextmapper.dsl.cml.CMLResource; | ||
import org.contextmapper.dsl.contextMappingDSL.Stakeholder; | ||
import org.contextmapper.dsl.ide.actions.CMLCodeAction; | ||
import org.eclipse.emf.ecore.EObject; | ||
import org.eclipse.lsp4j.Command; | ||
|
||
import com.google.common.collect.Lists; | ||
|
||
/** | ||
* Action that calls the "Create Value for Stakeholder" refactoring. | ||
* | ||
* @author Stefan Kapferer | ||
* | ||
*/ | ||
public class CreateValue4StakeholderAction implements CMLCodeAction { | ||
|
||
private CMLResource cmlResource; | ||
private List<EObject> editorSelection; | ||
|
||
public CreateValue4StakeholderAction(CMLResource cmlResource, List<EObject> editorSelection) { | ||
this.cmlResource = cmlResource; | ||
this.editorSelection = editorSelection; | ||
} | ||
|
||
@Override | ||
public boolean isApplicable() { | ||
Set<Stakeholder> stakeholders = getSelectedStakeholders(); | ||
return !(stakeholders.isEmpty() || stakeholders.size() > 1); | ||
} | ||
|
||
@Override | ||
public Command getCommand() { | ||
Stakeholder stakeholder = getSelectedStakeholder(); | ||
List<Object> commandArguments = Lists.newLinkedList(); | ||
commandArguments.add(cmlResource.getURI().toString()); | ||
commandArguments.add(stakeholder.getName()); | ||
return new Command("Create Value For Stakeholder", "cml.ar.createValueForStakeholder.proxy", commandArguments); | ||
} | ||
|
||
private Stakeholder getSelectedStakeholder() { | ||
return getSelectedStakeholders().iterator().next(); | ||
} | ||
|
||
private Set<Stakeholder> getSelectedStakeholders() { | ||
return editorSelection.stream().filter(o -> o instanceof Stakeholder).map(o -> (Stakeholder) o) | ||
.collect(Collectors.toSet()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...c/org/contextmapper/dsl/ide/commands/impl/refactoring/CreateValue4StakeholderCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* Copyright 2024 The Context Mapper Project Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.contextmapper.dsl.ide.commands.impl.refactoring; | ||
|
||
import org.contextmapper.dsl.ide.edit.WorkspaceEditRecorder; | ||
import org.contextmapper.dsl.refactoring.SemanticCMLRefactoring; | ||
import org.contextmapper.dsl.refactoring.stakeholders.CreateValue4StakeholderRefactoring; | ||
import org.eclipse.lsp4j.ExecuteCommandParams; | ||
|
||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonPrimitive; | ||
|
||
public class CreateValue4StakeholderCommand extends AbstractRefactoringCommand { | ||
|
||
public CreateValue4StakeholderCommand(WorkspaceEditRecorder editRecorder) { | ||
super(editRecorder); | ||
} | ||
|
||
@Override | ||
protected SemanticCMLRefactoring getRefactoring(ExecuteCommandParams params) { | ||
JsonArray refactoringParams = (JsonArray) params.getArguments().get(1); | ||
JsonPrimitive stakeholderName = (JsonPrimitive) refactoringParams.get(0); | ||
return new CreateValue4StakeholderRefactoring(stakeholderName.getAsString()); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
org.contextmapper.dsl.tests/integ-test-files/refactorings/create-value-for-stakeholder-1.cml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
Stakeholders { | ||
Stakeholder Tester | ||
} | ||
|
||
ValueRegister ExistingRegister { | ||
|
||
} |
4 changes: 4 additions & 0 deletions
4
org.contextmapper.dsl.tests/integ-test-files/refactorings/create-value-for-stakeholder-2.cml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
|
||
Stakeholders { | ||
Stakeholder Tester | ||
} |
47 changes: 47 additions & 0 deletions
47
...rg/contextmapper/dsl/refactoring/stakeholders/CreateValue4StakeholderRefactoringTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.contextmapper.dsl.refactoring.stakeholders; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
import java.io.IOException; | ||
|
||
import org.contextmapper.dsl.cml.CMLResource; | ||
import org.contextmapper.dsl.contextMappingDSL.ContextMappingModel; | ||
import org.contextmapper.dsl.refactoring.AbstractRefactoringTest; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class CreateValue4StakeholderRefactoringTest extends AbstractRefactoringTest { | ||
|
||
@Test | ||
void canCreateValueIfARegisterAlreadyExists() throws IOException { | ||
// given | ||
String inputModelName = "create-value-for-stakeholder-1.cml"; | ||
CMLResource input = getResourceCopyOfTestCML(inputModelName); | ||
|
||
// when | ||
new CreateValue4StakeholderRefactoring("Tester").refactor(input); | ||
|
||
// then | ||
ContextMappingModel model = input.getContextMappingModel(); | ||
assertEquals(1, model.getValueRegisters().size()); | ||
assertEquals(1, model.getValueRegisters().get(0).getValues().size()); | ||
assertEquals("To_Be_Defined", model.getValueRegisters().get(0).getValues().get(0).getName()); | ||
} | ||
|
||
@Test | ||
void canCreateValueIfNoRegisterExistsYet() throws IOException { | ||
// given | ||
String inputModelName = "create-value-for-stakeholder-2.cml"; | ||
CMLResource input = getResourceCopyOfTestCML(inputModelName); | ||
|
||
// when | ||
new CreateValue4StakeholderRefactoring("Tester").refactor(input); | ||
|
||
// then | ||
ContextMappingModel model = input.getContextMappingModel(); | ||
assertEquals(1, model.getValueRegisters().size()); | ||
assertEquals("Register_Name_To_Be_Changed", model.getValueRegisters().get(0).getName()); | ||
assertEquals(1, model.getValueRegisters().get(0).getValues().size()); | ||
assertEquals("To_Be_Defined", model.getValueRegisters().get(0).getValues().get(0).getName()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
....ui/src/org/contextmapper/dsl/ui/handler/stakeholders/CreateValue4StakeholderHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024 The Context Mapper Project Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.contextmapper.dsl.ui.handler.stakeholders; | ||
|
||
import org.contextmapper.dsl.cml.CMLResource; | ||
import org.contextmapper.dsl.contextMappingDSL.Stakeholder; | ||
import org.contextmapper.dsl.refactoring.SemanticCMLRefactoring; | ||
import org.contextmapper.dsl.refactoring.stakeholders.CreateValue4StakeholderRefactoring; | ||
import org.contextmapper.dsl.ui.handler.AbstractRefactoringHandler; | ||
import org.eclipse.core.commands.ExecutionEvent; | ||
import org.eclipse.emf.ecore.EObject; | ||
|
||
public class CreateValue4StakeholderHandler extends AbstractRefactoringHandler { | ||
|
||
@Override | ||
protected void executeRefactoring(CMLResource resource, ExecutionEvent event) { | ||
Stakeholder stakeholder = (Stakeholder) getSelectedElement(); | ||
SemanticCMLRefactoring ar = new CreateValue4StakeholderRefactoring(stakeholder.getName()); | ||
ar.refactor(resource, getAllResources()); | ||
ar.persistChanges(serializer); | ||
} | ||
|
||
@Override | ||
public boolean isEnabled() { | ||
EObject obj = getSelectedElement(); | ||
|
||
if (obj == null || !super.isEnabled()) | ||
return false; | ||
|
||
return (obj instanceof Stakeholder); | ||
} | ||
|
||
} |
63 changes: 63 additions & 0 deletions
63
...rc/org/contextmapper/dsl/refactoring/stakeholders/CreateValue4StakeholderRefactoring.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2024 The Context Mapper Project Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.contextmapper.dsl.refactoring.stakeholders; | ||
|
||
import org.contextmapper.dsl.contextMappingDSL.ContextMappingDSLFactory; | ||
import org.contextmapper.dsl.contextMappingDSL.Stakeholder; | ||
import org.contextmapper.dsl.contextMappingDSL.Value; | ||
import org.contextmapper.dsl.contextMappingDSL.ValueElicitation; | ||
import org.contextmapper.dsl.contextMappingDSL.ValueRegister; | ||
import org.contextmapper.dsl.refactoring.AbstractRefactoring; | ||
import org.contextmapper.dsl.refactoring.SemanticCMLRefactoring; | ||
import org.eclipse.xtext.EcoreUtil2; | ||
|
||
public class CreateValue4StakeholderRefactoring extends AbstractRefactoring implements SemanticCMLRefactoring { | ||
|
||
private String stakeholderName; | ||
|
||
public CreateValue4StakeholderRefactoring(String stakeholderName) { | ||
this.stakeholderName = stakeholderName; | ||
} | ||
|
||
@Override | ||
protected void doRefactor() { | ||
Stakeholder s = getSelectedStakeholder(); | ||
|
||
ValueRegister valueRegister = getOrCreateValueRegister(); | ||
Value value = ContextMappingDSLFactory.eINSTANCE.createValue(); | ||
value.setName("To_Be_Defined"); | ||
ValueElicitation elicitation = ContextMappingDSLFactory.eINSTANCE.createValueElicitation(); | ||
elicitation.setStakeholder(s); | ||
value.getElicitations().add(elicitation); | ||
valueRegister.getValues().add(value); | ||
} | ||
|
||
private Stakeholder getSelectedStakeholder() { | ||
return EcoreUtil2.<Stakeholder>getAllContentsOfType(model, Stakeholder.class).stream() | ||
.filter(s -> s.getName().equals(stakeholderName)).findFirst().get(); | ||
} | ||
|
||
private ValueRegister getOrCreateValueRegister() { | ||
if (model.getValueRegisters().isEmpty()) { | ||
ValueRegister valueRegister = ContextMappingDSLFactory.eINSTANCE.createValueRegister(); | ||
valueRegister.setName("Register_Name_To_Be_Changed"); | ||
model.getValueRegisters().add(valueRegister); | ||
return valueRegister; | ||
} | ||
return model.getValueRegisters().get(0); | ||
} | ||
|
||
} |