diff options
Diffstat (limited to 'catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java')
-rw-r--r-- | catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java | 172 |
1 files changed, 148 insertions, 24 deletions
diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java index 0744224be8..ca8bdea30a 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java @@ -1,38 +1,162 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed 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. + * ============LICENSE_END========================================================= + * Modifications copyright (c) 2018 Nokia + * ================================================================================ + */ package org.openecomp.sdc.be.dao.cassandra.schema; - +import com.datastax.driver.core.Cluster; +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; -import org.openecomp.sdc.be.config.ConfigurationManager; -import org.openecomp.sdc.be.utils.DAOConfDependentTest; +import org.mockito.Mockito; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.utils.CassandraTestHelper; -import java.util.LinkedList; +import java.util.Collections; import java.util.List; +import static org.mockito.Mockito.when; + +public class SdcSchemaUtilsTest { + private static final String SINGLE_STATEMENT = "SELECT COUNT(*) FROM system.peers"; + private static final String[] MULTIPLE_STATEMENTS = new String[] {SINGLE_STATEMENT, SINGLE_STATEMENT}; + private static final List<String> CASSANDRA_HOSTS = Collections.singletonList(CassandraTestHelper.SERVER); + private static final String CASSANDRA_USERNAME = "username"; + private static final String CASSANDRA_PASSWORD = "password"; + private static final String TRUSTSTORE_PATH = "pathToTruststore"; + private static final String TRUSTSTORE_PASSWORD = "passwordToTruststore"; + + @BeforeClass + public static void startServer() { + CassandraTestHelper.startServer(); + } + + @Test + public void testExecuteSingleStatement() throws Exception { + SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils(); + final boolean result = sdcSchemaUtils.executeStatement(CassandraTestHelper::createCluster, SINGLE_STATEMENT); + Assert.assertTrue(result); + } + + + @Test + public void testExecuteStatementsSuccessfullScenario() throws Exception { + SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils(); + final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createCluster, MULTIPLE_STATEMENTS); + Assert.assertTrue(result); + } + + @Test + public void testExecuteStatementsClusterFail() throws Exception { + SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils(); + final boolean result = sdcSchemaUtils.executeStatements(() -> null, MULTIPLE_STATEMENTS); + Assert.assertFalse(result); + } + + @Test + public void testExecuteStatementsSessionFail() throws Exception { + SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils(); + final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createClusterWithNoSession, MULTIPLE_STATEMENTS); + Assert.assertFalse(result); + } + + @Test + public void testCreateClusterNoAuthNoSsl() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + cfg.setCassandraHosts(CASSANDRA_HOSTS); + + SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class); + when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg); + when(sdcSchemaUtils.createCluster()).thenCallRealMethod(); + + try(Cluster cluster = sdcSchemaUtils.createCluster()) { + Assert.assertNotNull(cluster); + } + } -public class SdcSchemaUtilsTest extends DAOConfDependentTest{ - @Test - public void testExecuteStatement() throws Exception { - String statement = ""; - boolean result; + public void testCreateClusterFailOnLackOfCassandraNodes() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + cfg.setCassandraHosts(null); - // default test - result = SdcSchemaUtils.executeStatement(statement); - - List<String> cassandraHosts = new LinkedList<>(); - ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().setCassandraHosts(cassandraHosts); - ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().setAuthenticate(true); - ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().setSsl(true); - - result = SdcSchemaUtils.executeStatement(statement); + SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class); + when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg); + when(sdcSchemaUtils.createCluster()).thenCallRealMethod(); + + try(Cluster cluster = sdcSchemaUtils.createCluster()) { + Assert.assertNull(cluster); + } } - @Test - public void testExecuteStatements() throws Exception { - String[] statements = new String[] { "" }; - boolean result; + public void testCreateClusterFailOnAuthEnabledWithNoCredentials() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + cfg.setAuthenticate(true); + cfg.setCassandraHosts(CASSANDRA_HOSTS); + cfg.setUsername(null); + cfg.setPassword(null); + + SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class); + when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg); + when(sdcSchemaUtils.createCluster()).thenCallRealMethod(); + + try(Cluster cluster = sdcSchemaUtils.createCluster()) { + Assert.assertNull(cluster); + } + } + + @Test + public void testCreateClusterFailOnSSLWithNoCredentials() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + cfg.setCassandraHosts(CASSANDRA_HOSTS); + cfg.setSsl(true); + cfg.setTruststorePath(null); + cfg.setTruststorePassword(null); + + SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class); + when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg); + when(sdcSchemaUtils.createCluster()).thenCallRealMethod(); + + try(Cluster cluster = sdcSchemaUtils.createCluster()) { + Assert.assertNull(cluster); + } + } + + @Test + public void testCreateClusterWithAuthSsl() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + cfg.setAuthenticate(true); + cfg.setCassandraHosts(CASSANDRA_HOSTS); + cfg.setUsername(CASSANDRA_USERNAME); + cfg.setPassword(CASSANDRA_PASSWORD); + cfg.setSsl(true); + cfg.setTruststorePath(TRUSTSTORE_PATH); + cfg.setTruststorePassword(TRUSTSTORE_PASSWORD); + + SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class); + when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg); + when(sdcSchemaUtils.createCluster()).thenCallRealMethod(); - // default test - result = SdcSchemaUtils.executeStatements(statements); + try(Cluster cluster = sdcSchemaUtils.createCluster()) { + Assert.assertNotNull(cluster); + Assert.assertEquals(System.getProperty("javax.net.ssl.trustStore"), TRUSTSTORE_PATH); + Assert.assertEquals(System.getProperty("javax.net.ssl.trustStorePassword"), TRUSTSTORE_PASSWORD); + } } }
\ No newline at end of file |