Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
'Create Value For Stakeholder' refactoring
Browse files Browse the repository at this point in the history
stefan-ka committed Aug 15, 2024
1 parent 3f40de2 commit b1d0829
Showing 12 changed files with 351 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -17,10 +17,10 @@ package org.contextmapper.dsl.ide.tests.actions

import org.junit.jupiter.api.Test

class MoveStakeholderToGroupActionTest extends AbstractBoundedContextCodeActionTest {
class StakeholdersActionsTest extends AbstractBoundedContextCodeActionTest {

@Test
def void canOfferAction2MoveStakeholderToGroup() {
def void canOfferActionsOnStakeholder() {
testCodeAction [
model = '''
Stakeholders {
@@ -33,6 +33,10 @@ class MoveStakeholderToGroupActionTest extends AbstractBoundedContextCodeActionT
title : Move Stakeholder To New Group
args :
file://«this.root»/MyModel.cml,Tester
command : cml.ar.createValueForStakeholder.proxy
title : Create Value For Stakeholder
args :
file://«this.root»/MyModel.cml,Tester
'''
]
}
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)
}

}
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@

import org.contextmapper.dsl.cml.CMLResource;
import org.contextmapper.dsl.exception.ContextMapperApplicationException;
import org.contextmapper.dsl.ide.actions.impl.CreateValue4StakeholderAction;
import org.contextmapper.dsl.ide.actions.impl.DeriveBoundedContextFromSubdomainsAction;
import org.contextmapper.dsl.ide.actions.impl.DeriveFrontendAndBackendFromFeatureBCAction;
import org.contextmapper.dsl.ide.actions.impl.DeriveSubdomainFromUserRequirementsAction;
@@ -112,6 +113,7 @@ private List<CMLCodeAction> getAllActions(CMLResource resource, List<EObject> se
codeActions.add(new SwitchFromPartnershipToSharedKernelAction(resource, selectedObjects));
codeActions.add(new SwitchFromSharedKernelToPartnershipAction(resource, selectedObjects));
codeActions.add(new MoveStakeholderToGroupAction(resource, selectedObjects));
codeActions.add(new CreateValue4StakeholderAction(resource, selectedObjects));

return Lists.newLinkedList(codeActions);
}
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());
}

}
Original file line number Diff line number Diff line change
@@ -24,6 +24,7 @@
import org.contextmapper.dsl.ide.commands.impl.generation.PlantUMLGenerationCommand;
import org.contextmapper.dsl.ide.commands.impl.generation.SketchMinerGenerationCommand;
import org.contextmapper.dsl.ide.commands.impl.quickfix.SplitStoryByVerbCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.CreateValue4StakeholderCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.DeriveBoundedContextFromSubdomainsCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.DeriveFrontendAndBackendFromFeatureBCCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.DeriveSubdomainFromUserRequirementsCommand;
@@ -87,6 +88,7 @@ private void registerCommands() {
commandMap.put("cml.ar.switchSharedKernelToPartnership", new SwitchFromSharedKernelToPartnershipCommand(editRecorder));
commandMap.put("cml.quickfix.command.splitStoryByVerb", new SplitStoryByVerbCommand(editRecorder));
commandMap.put("cml.ar.moveStakeholderToGroup", new MoveStakeholderToGroupCommand(editRecorder));
commandMap.put("cml.ar.createValueForStakeholder", new CreateValue4StakeholderCommand(editRecorder));
}

public CMLResourceCommand getCommand(String commandId) {
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());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

Stakeholders {
Stakeholder Tester
}

ValueRegister ExistingRegister {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Stakeholders {
Stakeholder Tester
}
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());
}

}
15 changes: 15 additions & 0 deletions org.contextmapper.dsl.ui/plugin.xml
Original file line number Diff line number Diff line change
@@ -571,6 +571,15 @@
</reference>
</activeWhen>
</handler>
<handler
class="org.contextmapper.dsl.ui.ContextMappingDSLExecutableExtensionFactory:org.contextmapper.dsl.ui.handler.stakeholders.CreateValue4StakeholderHandler"
commandId="org.contextmapper.dsl.ui.handler.stakeholders.CreateValue4StakeholderRefactoringCommand">
<activeWhen>
<reference
definitionId="org.contextmapper.dsl.ContextMappingDSL.Editor.opened">
</reference>
</activeWhen>
</handler>
</extension>
<extension
point="org.eclipse.ui.commands">
@@ -680,6 +689,12 @@
id="org.contextmapper.dsl.ui.handler.stakeholders.MoveStakeholderToNewGroupRefactoringCommand">
</command>
</extension>
<extension
point="org.eclipse.ui.commands">
<command name="Create Value For Stakeholder" categoryId="org.eclipse.xtext.ui.category.refactoring"
id="org.contextmapper.dsl.ui.handler.stakeholders.CreateValue4StakeholderRefactoringCommand">
</command>
</extension>
<extension point="org.eclipse.ui.menus">
<menuContribution
locationURI="popup:#TextEditorContext?after=group.edit">
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);
}

}
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);
}

}

0 comments on commit b1d0829

Please sign in to comment.