forked from smartcat-labs/cassandra-migration-tool-java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MigrationEngineBooksTest.java
96 lines (76 loc) · 3.45 KB
/
MigrationEngineBooksTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package io.smartcat.migration;
import static junit.framework.Assert.assertEquals;
import org.cassandraunit.CQLDataLoader;
import org.cassandraunit.dataset.cql.ClassPathCQLDataSet;
import org.cassandraunit.utils.EmbeddedCassandraServerHelper;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.Statement;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import io.smartcat.migration.migrations.data.AddGenreMigration;
import io.smartcat.migration.migrations.data.InsertBooksMigration;
import io.smartcat.migration.migrations.schema.AddBookGenreFieldMigration;
public class MigrationEngineBooksTest extends BaseTest {
private static final Logger LOGGER = LoggerFactory.getLogger(MigrationEngineBooksTest.class);
private static final String CONTACT_POINT = "localhost";
private static final int PORT = 9142;
private static final String KEYSPACE = "migration_test_books";
private static final String CQL = "books.cql";
private static Session session;
private static Cluster cluster;
@BeforeClass
public static void init() throws Exception {
LOGGER.info("Starting embedded cassandra server");
EmbeddedCassandraServerHelper.startEmbeddedCassandra("another-cassandra.yaml");
LOGGER.info("Connect to embedded db");
cluster = Cluster.builder().addContactPoints(CONTACT_POINT).withPort(PORT).build();
session = cluster.connect();
LOGGER.info("Initialize keyspace");
final CQLDataLoader cqlDataLoader = new CQLDataLoader(session);
cqlDataLoader.load(new ClassPathCQLDataSet(CQL, false, true, KEYSPACE));
}
@AfterClass
public static void tearDown() {
if (cluster != null) {
cluster.close();
cluster = null;
}
}
@Test
public void test_schema_migration() {
final MigrationResources resources = new MigrationResources();
resources.addMigration(new AddBookGenreFieldMigration(1));
final boolean result = MigrationEngine.withSession(session).migrate(resources);
assertEquals(true, result);
}
@Test
public void test_data_migration() {
final MigrationResources initialResources = new MigrationResources();
initialResources.addMigration(new InsertBooksMigration(1));
MigrationEngine.withSession(session).migrate(initialResources);
final MigrationResources resources = new MigrationResources();
resources.addMigration(new AddBookGenreFieldMigration(1));
resources.addMigration(new AddGenreMigration(2));
MigrationEngine.withSession(session).migrate(resources);
final Statement select = QueryBuilder.select().all().from("books");
final ResultSet results = session.execute(select);
for (final Row row : results) {
final String genre = row.getString("genre");
final String name = row.getString("name");
if (name.equals("Journey to the Center of the Earth")) {
assertEquals("fantasy", genre);
} else if (name.equals("Fifty Shades of Grey")) {
assertEquals("erotica", genre);
} else {
assertEquals("programming", genre);
}
}
}
}