Skip to content

Commit

Permalink
Add a test for batch custom migration application
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrykuzmin committed Sep 2, 2020
1 parent 4263d1f commit d5218a6
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import io.spine.server.procman.given.repo.RememberingSubscriber;
import io.spine.server.procman.given.repo.SensoryDeprivedPmRepository;
import io.spine.server.procman.given.repo.SetTestProcessId;
import io.spine.server.procman.given.repo.SetTestProcessName;
import io.spine.server.procman.given.repo.TestProcessManager;
import io.spine.server.procman.given.repo.TestProcessManagerRepository;
import io.spine.server.procman.migration.MarkPmArchived;
Expand Down Expand Up @@ -90,7 +91,6 @@
import io.spine.type.TypeUrl;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -656,8 +656,8 @@ void whenCreating() {
}

@Test
@DisplayName("update columns via migration operation")
void updateColumns() {
@DisplayName("update entity via a custom migration")
void performCustomMigration() {
// Store a new process manager instance in the repository.
ProjectId id = createId(42);
TestProcessManagerRepository repository = repository();
Expand All @@ -673,7 +673,7 @@ void updateColumns() {
assertThat(found.hasNext()).isFalse();

// Apply the columns update.
repository.applyMigration(id, new UpdatePmColumns<>());
repository.applyMigration(id, new SetTestProcessId());

// Check the entity is now found by the provided filters.
Iterator<TestProcessManager> afterMigration =
Expand All @@ -687,8 +687,45 @@ void updateColumns() {
}

@Test
@DisplayName("update entity via a custom migration")
void performCustomMigration() {
@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);
TestProcessManagerRepository repository = repository();
TestProcessManager pm1 = new TestProcessManager(id1);
TestProcessManager pm2 = new TestProcessManager(id2);
TestProcessManager pm3 = new TestProcessManager(id3);
repository.store(pm1);
repository.store(pm2);
repository.store(pm3);

// 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.
Iterator<TestProcessManager> 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 SetTestProcessName());

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

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

@Test
@DisplayName("update columns via migration operation")
void updateColumns() {
// Store a new process manager instance in the repository.
ProjectId id = createId(42);
TestProcessManagerRepository repository = repository();
Expand All @@ -704,7 +741,7 @@ void performCustomMigration() {
assertThat(found.hasNext()).isFalse();

// Apply the columns update.
repository.applyMigration(id, new SetTestProcessId());
repository.applyMigration(id, new UpdatePmColumns<>());

// Check the entity is now found by the provided filters.
Iterator<TestProcessManager> afterMigration =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import io.spine.test.procman.ProjectId;

/**
* Sets the `id_string` from the `id` field.
* Sets the {@code id_string} from the {@code id} field.
*/
public final class SetTestProcessId
extends ProcessManagerMigration<ProjectId, TestProcessManager, Project, Project.Builder> {
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.procman.given.repo;

import io.spine.server.procman.ProcessManagerMigration;
import io.spine.test.procman.Project;
import io.spine.test.procman.ProjectId;

/**
* A migration which sets the process name to a predefined {@linkplain #NEW_NAME value}.
*/
public final class SetTestProcessName
extends ProcessManagerMigration<ProjectId, TestProcessManager, Project, Project.Builder> {

public static final String NEW_NAME = "Migrated project";

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

0 comments on commit d5218a6

Please sign in to comment.