james-server-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From adup...@apache.org
Subject [38/41] james-project git commit: JAMES-2083 creation of CassandraSchemaVersionManager
Date Mon, 10 Jul 2017 17:54:50 GMT
JAMES-2083 creation of CassandraSchemaVersionManager

This class has the responsability to check that the cassandra schema version is
supported by the current code base. It also warn if the version is supported but
can be updated


Project: http://git-wip-us.apache.org/repos/asf/james-project/repo
Commit: http://git-wip-us.apache.org/repos/asf/james-project/commit/0672d3bb
Tree: http://git-wip-us.apache.org/repos/asf/james-project/tree/0672d3bb
Diff: http://git-wip-us.apache.org/repos/asf/james-project/diff/0672d3bb

Branch: refs/heads/master
Commit: 0672d3bbb87714d73b4330cd19dc3da19a3aa0b4
Parents: e01bcf4
Author: Luc DUZAN <lduzan@linagora.com>
Authored: Tue Jul 4 11:11:59 2017 +0200
Committer: Antoine Duprat <aduprat@linagora.com>
Committed: Mon Jul 10 14:24:00 2017 +0200

----------------------------------------------------------------------
 backends-common/cassandra/pom.xml               |  18 +-
 .../versions/CassandraSchemaVersionDAO.java     |  78 ++++++
 .../versions/CassandraSchemaVersionManager.java |  97 ++++++++
 .../versions/CassandraSchemaVersionModule.java  |  57 +++++
 .../table/CassandraSchemaVersionTable.java      |  29 +++
 .../versions/CassandraSchemaVersionDAOTest.java |  76 ++++++
 .../CassandraSchemaVersionManagerTest.java      | 240 +++++++++++++++++++
 7 files changed, 589 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/pom.xml
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/pom.xml b/backends-common/cassandra/pom.xml
index 599419b..f65b3e6 100644
--- a/backends-common/cassandra/pom.xml
+++ b/backends-common/cassandra/pom.xml
@@ -169,17 +169,23 @@
                     <scope>test</scope>
                 </dependency>
                 <dependency>
-                    <groupId>nl.jqno.equalsverifier</groupId>
-                    <artifactId>equalsverifier</artifactId>
-                    <version>1.7.6</version>
-                    <scope>test</scope>
-                </dependency>
-                <dependency>
                     <groupId>org.cassandraunit</groupId>
                     <artifactId>cassandra-unit</artifactId>
                     <version>2.1.9.2</version>
                     <scope>test</scope>
                 </dependency>
+                <dependency>
+                    <groupId>org.mockito</groupId>
+                    <artifactId>mockito-core</artifactId>
+                    <version>1.9.0</version>
+                    <scope>test</scope>
+                </dependency>
+                <dependency>
+                    <groupId>nl.jqno.equalsverifier</groupId>
+                    <artifactId>equalsverifier</artifactId>
+                    <version>1.7.6</version>
+                    <scope>test</scope>
+                </dependency>
             </dependencies>
             <build>
                 <plugins>

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAO.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAO.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAO.java
new file mode 100644
index 0000000..555e0d4
--- /dev/null
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAO.java
@@ -0,0 +1,78 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions;
+
+import static com.datastax.driver.core.querybuilder.QueryBuilder.bindMarker;
+import static org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable.KEY;
+import static org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable.KEY_FOR_VERSION;
+import static org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable.TABLE_NAME;
+import static org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable.VALUE;
+
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import org.apache.james.backends.cassandra.utils.CassandraAsyncExecutor;
+import org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable;
+
+import com.datastax.driver.core.PreparedStatement;
+import com.datastax.driver.core.Session;
+import com.datastax.driver.core.querybuilder.QueryBuilder;
+
+import javax.inject.Inject;
+
+public class CassandraSchemaVersionDAO {
+    private final PreparedStatement readVersionStatement;
+    private final PreparedStatement writeVersionStatement;
+    private final CassandraAsyncExecutor cassandraAsyncExecutor;
+
+    @Inject
+    public CassandraSchemaVersionDAO(Session session) {
+        cassandraAsyncExecutor = new CassandraAsyncExecutor(session);
+        readVersionStatement = prepareReadVersionStatement(session);
+        writeVersionStatement = prepareWriteVersionStatement(session);
+    }
+
+    private PreparedStatement prepareReadVersionStatement(Session session) {
+        return session.prepare(
+            QueryBuilder.select(VALUE)
+                .from(TABLE_NAME)
+                .where(QueryBuilder.eq(KEY, KEY_FOR_VERSION)));
+    }
+
+    private PreparedStatement prepareWriteVersionStatement(Session session) {
+        return session.prepare(
+            QueryBuilder.insertInto(CassandraSchemaVersionTable.TABLE_NAME).value(KEY, KEY_FOR_VERSION)
+                .value(VALUE, bindMarker(VALUE)));
+    }
+
+    public CompletableFuture<Optional<Integer>> getCurrentSchemaVersion() {
+        return cassandraAsyncExecutor.executeSingleRow(readVersionStatement.bind())
+            .thenApply(rowOptional ->
+                rowOptional
+                    .map(row -> row.getInt(VALUE)));
+    }
+
+    public CompletableFuture<Void> updateVersion(int newVersion) {
+        return cassandraAsyncExecutor.executeVoid(
+            writeVersionStatement.bind()
+                .setInt(VALUE, newVersion));
+    }
+}
+

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
new file mode 100644
index 0000000..84b25e5
--- /dev/null
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManager.java
@@ -0,0 +1,97 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.inject.Inject;
+
+public class CassandraSchemaVersionManager {
+    public static final int MIN_VERSION = 1;
+    public static final int MAX_VERSION = 1;
+    public static final int DEFAULT_VERSION = 1;
+
+    private static final Logger LOGGER = LoggerFactory.getLogger(CassandraSchemaVersionManager.class);
+
+    private final int minVersion;
+    private final int maxVersion;
+    private final int version;
+
+    @Inject
+    public CassandraSchemaVersionManager(CassandraSchemaVersionDAO schemaVersionDAO) {
+        this(schemaVersionDAO, MIN_VERSION, MAX_VERSION);
+    }
+
+    @VisibleForTesting
+    public CassandraSchemaVersionManager(CassandraSchemaVersionDAO schemaVersionDAO, int
minVersion, int maxVersion) {
+        Preconditions.checkArgument(minVersion > 0, "minVersion needs to be strictly positive");
+        Preconditions.checkArgument(maxVersion > 0, "maxVersion needs to be strictly positive");
+        Preconditions.checkArgument(maxVersion >= minVersion,
+            "maxVersion should not be inferior to minVersion");
+
+        this.minVersion = minVersion;
+        this.maxVersion = maxVersion;
+        this.version = schemaVersionDAO
+            .getCurrentSchemaVersion()
+            .join()
+            .orElseGet(() -> {
+                LOGGER.warn("No schema version information found on Cassandra, we assume
schema is at version {}",
+                    CassandraSchemaVersionManager.DEFAULT_VERSION);
+                return DEFAULT_VERSION;
+            });
+    }
+
+    public int getVersion() {
+        return version;
+    }
+
+    public int getMinimumSupportedVersion() {
+        return minVersion;
+    }
+
+    public int getMaximumSupportedVersion() {
+        return maxVersion;
+    }
+
+    public void ensureSchemaIsSupported() {
+        if (version < minVersion) {
+
+            throw new IllegalStateException(
+                String.format("Current schema version is %d whereas minimum required version
is %d. " +
+                    "Recommended version is %d", version, minVersion, maxVersion));
+        } else if (version < maxVersion) {
+
+            LOGGER.warn("Current schema version is %d. Recommended version is %d", version,
maxVersion);
+        } else if (version == maxVersion){
+
+            LOGGER.info("Schema version is up-to-date");
+        } else {
+
+            throw new IllegalStateException(
+                String.format("Current schema version is %d whereas the minimum supported
version is %d. " +
+                    "Recommended version is %d.", version, minVersion, maxVersion));
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionModule.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionModule.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionModule.java
new file mode 100644
index 0000000..decc422
--- /dev/null
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionModule.java
@@ -0,0 +1,57 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions;
+
+import static com.datastax.driver.core.DataType.cint;
+
+import java.util.List;
+
+import org.apache.james.backends.cassandra.components.CassandraModule;
+import org.apache.james.backends.cassandra.components.CassandraTable;
+import org.apache.james.backends.cassandra.components.CassandraType;
+import org.apache.james.backends.cassandra.versions.table.CassandraSchemaVersionTable;
+
+import com.datastax.driver.core.schemabuilder.SchemaBuilder;
+import com.google.common.collect.ImmutableList;
+
+public class CassandraSchemaVersionModule implements CassandraModule {
+
+    private final List<CassandraTable> tables;
+
+    public CassandraSchemaVersionModule() {
+        this.tables = ImmutableList.of(
+            new CassandraTable(CassandraSchemaVersionTable.TABLE_NAME,
+                SchemaBuilder.createTable(CassandraSchemaVersionTable.TABLE_NAME)
+                    .ifNotExists()
+                    .addPartitionKey(CassandraSchemaVersionTable.KEY, cint())
+                    .addClusteringColumn(CassandraSchemaVersionTable.VALUE, cint())));
+    }
+
+
+    @Override
+    public List<CassandraTable> moduleTables() {
+        return tables;
+    }
+
+    @Override
+    public List<CassandraType> moduleTypes() {
+        return ImmutableList.of();
+    }
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/table/CassandraSchemaVersionTable.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/table/CassandraSchemaVersionTable.java
b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/table/CassandraSchemaVersionTable.java
new file mode 100644
index 0000000..9b12cd2
--- /dev/null
+++ b/backends-common/cassandra/src/main/java/org/apache/james/backends/cassandra/versions/table/CassandraSchemaVersionTable.java
@@ -0,0 +1,29 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions.table;
+
+public interface CassandraSchemaVersionTable {
+    String TABLE_NAME = "schemaVersion";
+
+    String KEY = "key";
+    String VALUE = "value";
+
+    int KEY_FOR_VERSION = 0;
+}

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAOTest.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAOTest.java
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAOTest.java
new file mode 100644
index 0000000..e0834f3
--- /dev/null
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionDAOTest.java
@@ -0,0 +1,76 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.util.Optional;
+
+import org.apache.james.backends.cassandra.CassandraCluster;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class CassandraSchemaVersionDAOTest {
+
+    private CassandraCluster cassandra;
+
+    private CassandraSchemaVersionDAO testee;
+
+
+    @Before
+    public void setUp() {
+        cassandra = CassandraCluster.create(new CassandraSchemaVersionModule());
+        cassandra.ensureAllTables();
+
+        testee = new CassandraSchemaVersionDAO(cassandra.getConf());
+    }
+
+    @After
+    public void tearDown() {
+        cassandra.clearAllTables();
+        cassandra.close();
+    }
+
+    @Test
+    public void getCurrentSchemaVersionShouldReturnEmptyWhenTableIsEmpty() {
+        assertThat(testee.getCurrentSchemaVersion().join())
+            .isEqualTo(Optional.empty());
+    }
+
+    @Test
+    public void getCurrentSchemaVersionShouldReturnVersionPresentInTheTable() {
+        int version = 42;
+
+        testee.updateVersion(version).join();
+
+        assertThat(testee.getCurrentSchemaVersion().join()).isEqualTo(Optional.of(version));
+    }
+
+    @Test
+    public void getCurrentSchemaVersionShouldBeIdempotent() {
+        int version = 42;
+
+        testee.updateVersion(version + 1).join();
+        testee.updateVersion(version).join();
+
+        assertThat(testee.getCurrentSchemaVersion().join()).contains(version + 1);
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/james-project/blob/0672d3bb/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
----------------------------------------------------------------------
diff --git a/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
new file mode 100644
index 0000000..d6576dc
--- /dev/null
+++ b/backends-common/cassandra/src/test/java/org/apache/james/backends/cassandra/versions/CassandraSchemaVersionManagerTest.java
@@ -0,0 +1,240 @@
+/****************************************************************
+ * Licensed to the Apache Software Foundation (ASF) under one   *
+ * or more contributor license agreements.  See the NOTICE file *
+ * distributed with this work for additional information        *
+ * regarding copyright ownership.  The ASF licenses this file   *
+ * to you 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.apache.james.backends.cassandra.versions;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static uk.org.lidalia.slf4jtest.LoggingEvent.info;
+import static uk.org.lidalia.slf4jtest.LoggingEvent.warn;
+
+import java.util.Optional;
+import java.util.concurrent.CompletableFuture;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import uk.org.lidalia.slf4jtest.TestLogger;
+import uk.org.lidalia.slf4jtest.TestLoggerFactory;
+
+public class CassandraSchemaVersionManagerTest {
+
+    private final int minVersion = 2;
+    private final int maxVersion = 4;
+    private CassandraSchemaVersionDAO schemaVersionDAO;
+    private TestLogger logger = TestLoggerFactory.getTestLogger(CassandraSchemaVersionManager.class);
+
+    @Rule
+    public ExpectedException expectedException = ExpectedException.none();
+
+    @Before
+    public void setUp() {
+        schemaVersionDAO = mock(CassandraSchemaVersionDAO.class);
+    }
+
+    @After
+    public void tearDown() {
+        TestLoggerFactory.clear();
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldThrowIfSchemaVersionIsTooOld() {
+        expectedException.expect(IllegalStateException.class);
+
+        int currentVersion = minVersion - 1;
+
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(currentVersion)));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+
+        testee.ensureSchemaIsSupported();
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldThrowIfSchemaVersionIsTooRecent() {
+        expectedException.expect(IllegalStateException.class);
+
+        int currentVersion = maxVersion + 1;
+
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(currentVersion)));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+
+        testee.ensureSchemaIsSupported();
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldLogOkIfSchemaIsUpToDate() {
+        int currentVersion = maxVersion;
+
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(currentVersion)));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+
+        testee.ensureSchemaIsSupported();
+
+        assertThat(logger.getLoggingEvents()).containsExactly(info("Schema version is up-to-date"));
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldLogAsWarningIfSchemaIsSupportedButNotUpToDate()
{
+        int currentVersion = maxVersion - 1;
+
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(currentVersion)));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+
+        testee.ensureSchemaIsSupported();
+
+        assertThat(logger.getLoggingEvents())
+            .containsExactly(warn("Current schema version is %d. Recommended version is %d",
+                currentVersion,
+                maxVersion));
+    }
+
+    @Test
+    public void constructorShouldThrowOnNegativeMinVersion() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            -1,
+            maxVersion);
+    }
+
+    @Test
+    public void constructorShouldThrowOnZeroMinVersion() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            0,
+            maxVersion);
+    }
+
+    @Test
+    public void constructorShouldThrowOnNegativeMaxVersion() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            -1);
+    }
+
+    @Test
+    public void constructorShouldThrowOnZeroMaxVersion() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            0);
+    }
+
+    @Test
+    public void constructorShouldThrowMinVersionIsSuperiorToMaxVersion() {
+        expectedException.expect(IllegalArgumentException.class);
+
+        int minVersion = 4;
+        int maxVersion = 2;
+        new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldActAsUpToDateWhenMinMaxAndCurrentVersionsAreTheSame()
{
+        int minVersion = 4;
+        int maxVersion = 4;
+        int currentVersion = 4;
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(currentVersion)));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(
+            schemaVersionDAO,
+            minVersion,
+            maxVersion);
+
+        testee.ensureSchemaIsSupported();
+
+        assertThat(logger.getLoggingEvents()).containsExactly(info("Schema version is up-to-date"));
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldNotThrowOnNewCassandra() {
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.of(CassandraSchemaVersionManager.DEFAULT_VERSION)));
+
+        new CassandraSchemaVersionManager(schemaVersionDAO).ensureSchemaIsSupported();
+    }
+
+    @Test
+    public void ensureSchemaIsSupportedShouldNotThrowButLogWhenNoVersionNumberFoundOnCassandra()
{
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.empty()));
+
+        new CassandraSchemaVersionManager(schemaVersionDAO).ensureSchemaIsSupported();
+
+        assertThat(logger.getAllLoggingEvents())
+            .contains(warn("No schema version information found on Cassandra, we assume schema
is at version {}",
+                CassandraSchemaVersionManager.DEFAULT_VERSION));
+    }
+
+    @Test
+    public void ensureSchemaDefaultConstructorUseCorrectMinVersion() {
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.empty()));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(schemaVersionDAO);
+
+        assertThat(testee.getMinimumSupportedVersion()).isEqualTo(CassandraSchemaVersionManager.MIN_VERSION);
+    }
+
+    @Test
+    public void ensureSchemaDefaultConstructorUseCorrectMaxVersion() {
+        when(schemaVersionDAO.getCurrentSchemaVersion())
+            .thenReturn(CompletableFuture.completedFuture(Optional.empty()));
+
+        CassandraSchemaVersionManager testee = new CassandraSchemaVersionManager(schemaVersionDAO);
+
+        assertThat(testee.getMinimumSupportedVersion()).isEqualTo(CassandraSchemaVersionManager.MAX_VERSION);
+    }
+}
\ No newline at end of file


---------------------------------------------------------------------
To unsubscribe, e-mail: server-dev-unsubscribe@james.apache.org
For additional commands, e-mail: server-dev-help@james.apache.org


Mime
View raw message