Skip to content

Commit

Permalink
Testing first refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-ka committed Aug 15, 2024
1 parent 0d64c2a commit 3f40de2
Show file tree
Hide file tree
Showing 9 changed files with 267 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* 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.actions

import org.junit.jupiter.api.Test

class MoveStakeholderToGroupActionTest extends AbstractBoundedContextCodeActionTest {

@Test
def void canOfferAction2MoveStakeholderToGroup() {
testCodeAction [
model = '''
Stakeholders {
Stakeholder Tester
}
'''
line = 1
expectedCodeActions = '''
command : cml.ar.moveStakeholderToGroup.proxy
title : Move Stakeholder To New Group
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 MoveStakeholderToGroupCommandTest 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.moveStakeholderToGroup", #[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
Expand Up @@ -32,6 +32,7 @@
import org.contextmapper.dsl.ide.actions.impl.ExtractSharedKernelAction;
import org.contextmapper.dsl.ide.actions.impl.MergeAggregatesAction;
import org.contextmapper.dsl.ide.actions.impl.MergeBoundedContextsAction;
import org.contextmapper.dsl.ide.actions.impl.MoveStakeholderToGroupAction;
import org.contextmapper.dsl.ide.actions.impl.SplitAggregateByEntitiesAction;
import org.contextmapper.dsl.ide.actions.impl.SplitBoundedContextByFeaturesAction;
import org.contextmapper.dsl.ide.actions.impl.SplitBoundedContextByOwnerAction;
Expand Down Expand Up @@ -110,6 +111,7 @@ private List<CMLCodeAction> getAllActions(CMLResource resource, List<EObject> se
codeActions.add(new SuspendPartnershipAction(resource, selectedObjects));
codeActions.add(new SwitchFromPartnershipToSharedKernelAction(resource, selectedObjects));
codeActions.add(new SwitchFromSharedKernelToPartnershipAction(resource, selectedObjects));
codeActions.add(new MoveStakeholderToGroupAction(resource, selectedObjects));

return Lists.newLinkedList(codeActions);
}
Expand Down
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 "Move Stakeholder to new Group" refactoring.
*
* @author Stefan Kapferer
*
*/
public class MoveStakeholderToGroupAction implements CMLCodeAction {

private CMLResource cmlResource;
private List<EObject> editorSelection;

public MoveStakeholderToGroupAction(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("Move Stakeholder To New Group", "cml.ar.moveStakeholderToGroup.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
Expand Up @@ -32,6 +32,7 @@
import org.contextmapper.dsl.ide.commands.impl.refactoring.ExtractSharedKernelCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.MergeAggregatesCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.MergeBoundedContextsCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.MoveStakeholderToGroupCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.SplitAggregateByEntitiesRefactoringCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.SplitBoundedContextByFeaturesRefactoringCommand;
import org.contextmapper.dsl.ide.commands.impl.refactoring.SplitBoundedContextByOwnerRefactoringCommand;
Expand Down Expand Up @@ -85,6 +86,7 @@ private void registerCommands() {
commandMap.put("cml.ar.switchPartnershipToSharedKernel", new SwitchFromPartnershipToSharedKernelCommand(editRecorder));
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));
}

public CMLResourceCommand getCommand(String commandId) {
Expand Down
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.MoveStakeholderToNewStakeholderGroupRefactoring;
import org.eclipse.lsp4j.ExecuteCommandParams;

import com.google.gson.JsonArray;
import com.google.gson.JsonPrimitive;

public class MoveStakeholderToGroupCommand extends AbstractRefactoringCommand {

public MoveStakeholderToGroupCommand(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 MoveStakeholderToNewStakeholderGroupRefactoring(stakeholderName.getAsString());
}

}
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,6 @@

Stakeholders {
StakeholderGroup Testers {
Stakeholder Tester
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package org.contextmapper.dsl.refactoring.stakeholders;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.IOException;

import org.contextmapper.dsl.cml.CMLResource;
import org.contextmapper.dsl.contextMappingDSL.ContextMappingModel;
import org.contextmapper.dsl.contextMappingDSL.StakeholderGroup;
import org.contextmapper.dsl.refactoring.AbstractRefactoringTest;
import org.junit.jupiter.api.Test;

public class MoveStakeholderToNewStakeholderGroupRefactoringTest extends AbstractRefactoringTest {

@Test
void canMoveStakeholderIntoGroup() throws IOException {
// given
String inputModelName = "move-stakeholder-to-group-1.cml";
CMLResource input = getResourceCopyOfTestCML(inputModelName);

// when
new MoveStakeholderToNewStakeholderGroupRefactoring("Tester").refactor(input);

// then
ContextMappingModel model = input.getContextMappingModel();
assertEquals(1, model.getStakeholders().get(0).getStakeholders().size());
assertEquals("Groupname_To_Be_Changed", model.getStakeholders().get(0).getStakeholders().get(0).getName());
assertTrue(model.getStakeholders().get(0).getStakeholders().get(0) instanceof StakeholderGroup);
assertEquals(1,
((StakeholderGroup) model.getStakeholders().get(0).getStakeholders().get(0)).getStakeholders().size());
assertEquals("Tester", ((StakeholderGroup) model.getStakeholders().get(0).getStakeholders().get(0))
.getStakeholders().get(0).getName());
}

@Test
void canMoveStakeholderFromOneGroupToOther() throws IOException {
// given
String inputModelName = "move-stakeholder-to-group-2.cml";
CMLResource input = getResourceCopyOfTestCML(inputModelName);

// when
new MoveStakeholderToNewStakeholderGroupRefactoring("Tester").refactor(input);

// then
ContextMappingModel model = input.getContextMappingModel();
assertEquals(2, model.getStakeholders().get(0).getStakeholders().size());
assertTrue(model.getStakeholders().get(0).getStakeholders().get(0) instanceof StakeholderGroup);
assertTrue(model.getStakeholders().get(0).getStakeholders().get(1) instanceof StakeholderGroup);
StakeholderGroup newGroup = (StakeholderGroup) model.getStakeholders().get(0).getStakeholders().stream()
.filter(s -> s.getName().equals("Groupname_To_Be_Changed")).findFirst().get();
assertEquals("Tester", newGroup.getStakeholders().get(0).getName());
}

}

0 comments on commit 3f40de2

Please sign in to comment.