Skip to content

Commit

Permalink
implement tests for IdentityMappingViewType#commitViewChanges
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Wittler committed Jan 11, 2023
1 parent 8967ef3 commit 42b7f9c
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import tools.vitruv.framework.views.selectors.DirectViewElementSelector
import tools.vitruv.framework.views.util.ResourceCopier

import static com.google.common.base.Preconditions.checkArgument
import static com.google.common.base.Preconditions.checkNotNull

import static extension tools.vitruv.framework.views.util.ResourceCopier.requiresFullCopy

Expand Down Expand Up @@ -49,6 +50,8 @@ class IdentityMappingViewType extends AbstractViewType<DirectViewElementSelector
}

override commitViewChanges(ModifiableView view, VitruviusChange viewChange) {
checkNotNull(view, "view must not be null")
checkNotNull(viewChange, "view change must not be null");
view.viewSource.propagateChange(viewChange)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static tools.vitruv.testutils.matchers.ModelMatchers.equalsDeeply;
import static tools.vitruv.testutils.metamodels.AllElementTypesCreators.aet;

import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.resource.Resource;
Expand All @@ -26,10 +30,17 @@
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.mockito.ArgumentCaptor;

import com.google.common.collect.FluentIterable;

import allElementTypes.Root;
import tools.vitruv.change.atomic.EChange;
import tools.vitruv.change.composite.description.VitruviusChange;
import tools.vitruv.change.composite.description.VitruviusChangeFactory;
import tools.vitruv.change.composite.recording.ChangeRecorder;
import tools.vitruv.framework.views.ChangeableViewSource;
import tools.vitruv.framework.views.View;
import tools.vitruv.framework.views.ViewType;
Expand Down Expand Up @@ -339,4 +350,82 @@ public void removingUnselectedRoot() throws Exception {
}
}
}

@Nested
@DisplayName("commit view changes")
class CommitViewChanges {
private IdentityMappingViewType basicViewType;
private ResourceSet testResourceSet;
private ModifiableView view;
private ChangeableViewSource viewSource;

@BeforeEach
void initializeViewTypeAndResourceSetAndViewSource() {
this.basicViewType = new IdentityMappingViewType("name");
this.testResourceSet = withGlobalFactories(new ResourceSetImpl());
this.view = mock(ModifiableView.class);
this.viewSource = mock(ChangeableViewSource.class);
when(view.getViewSource()).thenReturn(viewSource);
when(viewSource.getViewSourceModels()).thenReturn(testResourceSet.getResources());
}

private Root createResourceWithSingleRoot(URI uri) {
Resource resource = testResourceSet.createResource(uri);
Root rootElement = aet.Root();
rootElement.setId("testid");
resource.getContents().add(rootElement);
return rootElement;
}

@Test
@DisplayName("with null changes")
void withNull() {
assertThrows(NullPointerException.class, () -> basicViewType.commitViewChanges(view, null));
}

@Test
@DisplayName("with null view")
void withNullView() {
VitruviusChange someChange = VitruviusChangeFactory.getInstance().createTransactionalChange(Set.of());
assertThrows(NullPointerException.class, () -> basicViewType.commitViewChanges(null, someChange));
}

@ParameterizedTest
@MethodSource("testEmptyChanges")
@DisplayName("with empty changes")
void withEmptyChanges(VitruviusChange change) {
ArgumentCaptor<VitruviusChange> changeArgument = ArgumentCaptor.forClass(VitruviusChange.class);
basicViewType.commitViewChanges(view, change);
verify(viewSource).propagateChange(changeArgument.capture());
assertEquals(changeArgument.getValue(), change);
}

private static Stream<VitruviusChange> testEmptyChanges() {
VitruviusChangeFactory factory = VitruviusChangeFactory.getInstance();
return Stream.of(
factory.createTransactionalChange(Set.of()),
factory.createCompositeChange(Set.of())
);
}

@Test
@DisplayName("with non-empty change")
void withNonEmptyChange() {
Root root = createResourceWithSingleRoot(URI.createURI("test://test.aet"));
try (ChangeRecorder changeRecorder = new ChangeRecorder(testResourceSet)) {
changeRecorder.addToRecording(root);
changeRecorder.beginRecording();
root.setId("testid2");
changeRecorder.endRecording();
VitruviusChange change = changeRecorder.getChange().unresolve();

ArgumentCaptor<VitruviusChange> changeArgument = ArgumentCaptor.forClass(VitruviusChange.class);
basicViewType.commitViewChanges(view, change);
verify(viewSource).propagateChange(changeArgument.capture());
List<EChange> eChanges = changeArgument.getValue().unresolve().getEChanges();
assertThat(eChanges.size(), is(1));
assertThat(eChanges.get(0), equalsDeeply(change.getEChanges().get(0)));
}
}
}
}

0 comments on commit 42b7f9c

Please sign in to comment.