Skip to content

Commit

Permalink
Add custom migration tests for projection repository
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrykuzmin committed Sep 2, 2020
1 parent d5218a6 commit 4fcf821
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ void performCustomMigrationForMultiple() {
// Init filters by the `name` column.
TargetFilters filters = targetFilters(Project.Column.name(), SetTestProcessName.NEW_NAME);

// Check nothing is found as the entity state was not yet updated.
// Check nothing is found as the entity states were not yet updated.
Iterator<TestProcessManager> found =
repository.find(filters, ResponseFormat.getDefaultInstance());
assertThat(found.hasNext()).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
import io.spine.server.projection.given.ProjectionRepositoryTestEnv.NoOpTaskNamesRepository;
import io.spine.server.projection.given.ProjectionRepositoryTestEnv.SensoryDeprivedProjectionRepository;
import io.spine.server.projection.given.ProjectionRepositoryTestEnv.TestProjectionRepository;
import io.spine.server.projection.given.SetTestProjectionId;
import io.spine.server.projection.given.SetTestProjectionName;
import io.spine.server.projection.given.TestProjection;
import io.spine.server.projection.migration.MarkProjectionArchived;
import io.spine.server.projection.migration.MarkProjectionDeleted;
Expand Down Expand Up @@ -548,6 +550,75 @@ void notRegisterIfSubscribedToNothing() {
repo.registerWith(context));
}

@Test
@DisplayName("update entity via a custom migration")
void performCustomMigration() {
// Store a new process manager instance in the repository.
ProjectId id = createId(42);
TestProjectionRepository repository = repository();
TestProjection projection = new TestProjection(id);
repository.store(projection);

// Init filters by the `id_string` column.
TargetFilters targetFilters = targetFilters(Project.Column.idString(), id.toString());

// Check nothing is found as column now should be empty.
Iterator<TestProjection> found =
repository.find(targetFilters, ResponseFormat.getDefaultInstance());
assertThat(found.hasNext()).isFalse();

// Apply the columns update.
repository.applyMigration(id, new SetTestProjectionId());

// Check the entity is now found by the provided filters.
Iterator<TestProjection> afterMigration =
repository.find(targetFilters, ResponseFormat.getDefaultInstance());
assertThat(afterMigration.hasNext()).isTrue();

// Check the column value is propagated to the entity state.
TestProjection entityWithColumns = afterMigration.next();
assertThat(entityWithColumns.state()
.getIdString()).isEqualTo(id.toString());
}

@Test
@DisplayName("update multiple entities via a custom migration")
void performCustomMigrationForMultiple() {
// Store three entities to the repository.
ProjectId id1 = createId(1);
ProjectId id2 = createId(2);
ProjectId id3 = createId(3);
TestProjectionRepository repository = repository();
TestProjection projection1 = new TestProjection(id1);
TestProjection projection2 = new TestProjection(id2);
TestProjection projection3 = new TestProjection(id3);
repository.store(projection1);
repository.store(projection2);
repository.store(projection3);

// Init filters by the `name` column.
TargetFilters filters =
targetFilters(Project.Column.name(), SetTestProjectionName.NEW_NAME);

// Check nothing is found as the entity states were not yet updated.
Iterator<TestProjection> found =
repository.find(filters, ResponseFormat.getDefaultInstance());
assertThat(found.hasNext()).isFalse();

// Apply the column update to two of the three entities.
repository.applyMigration(ImmutableSet.of(id1, id2), new SetTestProjectionName());

// Check the entities are now found by the provided filters.
Iterator<TestProjection> foundAfterMigration =
repository.find(filters, ResponseFormat.getDefaultInstance());

ImmutableList<TestProjection> results = ImmutableList.copyOf(foundAfterMigration);
assertThat(results).hasSize(2);
assertThat(results)
.comparingElementsUsing(idCorrespondence())
.containsExactly(id1, id2);
}

@Test
@DisplayName("update columns through migration operation")
void updateColumns() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.server.projection.given;

import io.spine.server.projection.ProjectionMigration;
import io.spine.test.projection.Project;
import io.spine.test.projection.ProjectId;

/**
* Sets the {@code id_string} from the {@code id} field.
*/
public final class SetTestProjectionId
extends ProjectionMigration<ProjectId, TestProjection, Project, Project.Builder> {

@Override
public Project apply(Project project) {
ProjectId id = id();
Project result = project
.toBuilder()
.setIdString(id.toString())
.vBuild();
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 2020, TeamDev. All rights reserved.
*
* Redistribution and use in source and/or binary forms, with or without
* modification, must retain the above copyright notice and the following
* disclaimer.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

package io.spine.server.projection.given;

import io.spine.server.projection.ProjectionMigration;
import io.spine.test.projection.Project;
import io.spine.test.projection.ProjectId;

/**
* A migration which sets the projection name to a predefined {@linkplain #NEW_NAME value}.
*/
public final class SetTestProjectionName
extends ProjectionMigration<ProjectId, TestProjection, Project, Project.Builder> {

public static final String NEW_NAME = "Migrated projection";

@Override
public Project apply(Project project) {
Project result = project.toBuilder()
.setName(NEW_NAME)
.build();
return result;
}
}

0 comments on commit 4fcf821

Please sign in to comment.