diff options
author | Piotr Darosz <piotr.darosz@nokia.com> | 2018-06-15 08:28:00 +0200 |
---|---|---|
committer | Tal Gitelman <tg851x@intl.att.com> | 2018-08-29 13:27:45 +0000 |
commit | 20da3d02c6c5507761c257b6ec6f58dcfc27d05a (patch) | |
tree | 6fef3ccd72cb8c092adac410b77a5da25284055a /catalog-dao/src/test/java | |
parent | 8eff5a1ff79ca212a42d0e69326af7dc00b7c503 (diff) |
Add tests for SdcSchema classes
Introduce Cassandra Unit tool and tests for schema-related classes
Change-Id: I14da602c42056730e7992dd92b4da8b87dd9fb0d
Issue-ID: SDC-1358
Signed-off-by: Piotr Darosz <piotr.darosz@nokia.com>
Diffstat (limited to 'catalog-dao/src/test/java')
4 files changed, 321 insertions, 122 deletions
diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java index 1550e5708e..422dac7114 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java @@ -1,104 +1,105 @@ +/*- + * ============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 com.datastax.driver.core.Session; -import mockit.Deencapsulation; +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mockito; -import org.openecomp.sdc.be.config.Configuration.CassandrConfig.KeyspaceConfig; -import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaBuilder.ReplicationStrategy; -import org.openecomp.sdc.be.utils.DAOConfDependentTest; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.utils.CassandraTestHelper; -import java.util.HashMap; -import java.util.LinkedList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; -import java.util.Map; - -public class SdcSchemaBuilderTest extends DAOConfDependentTest{ - - private SdcSchemaBuilder createTestSubject() { - return new SdcSchemaBuilder(); - } - - @Test - public void testHandle1707OSMigration() throws Exception { - Map<String, Map<String, List<String>>> cassndraMetadata = new HashMap<>(); - Map<String, List<ITableDescription>> schemeData = new HashMap<>(); - - // default test - Deencapsulation.invoke(SdcSchemaBuilder.class, "handle1707OSMigration", cassndraMetadata, schemeData); - } - - @Test - public void testCreateSchema() throws Exception { - boolean result; - - // default test - result = SdcSchemaBuilder.createSchema(); - } - - @Test - public void testDeleteSchema() throws Exception { - boolean result; - - // default test - result = SdcSchemaBuilder.deleteSchema(); - } - - @Test - public void testCreateIndexName() throws Exception { - String table = ""; - String column = ""; - String result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createIndexName", table, column); - } - - @Test - public void testCreateKeyspace() throws Exception { - String keyspace = "mock"; - Map<String, Map<String, List<String>>> cassndraMetadata = new HashMap<>(); - Session session = Mockito.mock(Session.class); - boolean result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspace", - keyspace, cassndraMetadata, session); - - cassndraMetadata.put(keyspace, new HashMap<>()); - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspace", - keyspace, cassndraMetadata, session); - } - - @Test - public void testGetSchemeData() throws Exception { - Map<String, List<ITableDescription>> result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "getSchemeData"); - } - - @Test - public void testCreateKeyspaceQuereyString() throws Exception { - String keyspace = "mock"; - KeyspaceConfig keyspaceInfo = new KeyspaceConfig(); - String result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - - keyspaceInfo.setReplicationStrategy(ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.getStrategyName()); - LinkedList<String> replicationInfo = new LinkedList<>(); - keyspaceInfo.setReplicationInfo(replicationInfo); - //Test1 - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - replicationInfo.add("mock1"); - replicationInfo.add("mock2"); - - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - - //Test2 - keyspaceInfo.setReplicationStrategy(ReplicationStrategy.SIMPLE_STRATEGY.getStrategyName()); - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - } -}
\ No newline at end of file +import java.util.Set; +import java.util.stream.Collectors; + +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; + +public class SdcSchemaBuilderTest { + + @BeforeClass + public static void startServer() { + CassandraTestHelper.startServer(); + } + + @Test + public void testCreateSchema() { + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(CassandraTestHelper.createCluster()); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.createSchema(); + Assert.assertTrue(result); + } + + @Test + public void testDeleteSchemaNoKeyspaces() { + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(CassandraTestHelper.createCluster()); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.deleteSchema(); + Assert.assertTrue(result); + } + + @Test + public void testDeleteSchemaWithKeyspacesExisting() { + Configuration.CassandrConfig throwAwayConfig = createCassandraConfig(); + Cluster cluster = CassandraTestHelper.createCluster(); + createTestKeyspaces(cluster, throwAwayConfig.getKeySpaces()); + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(cluster); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.deleteSchema(); + Assert.assertTrue(result); + } + + private static Configuration.CassandrConfig createCassandraConfig() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + Set<String> requiredKeyspaces = Arrays.stream(Table.values()) + .map(t -> t.getTableDescription().getKeyspace().toLowerCase()) + .collect(Collectors.toSet()); + List<Configuration.CassandrConfig.KeyspaceConfig> createdKeyspaces = requiredKeyspaces.stream() + .map(k -> { + Configuration.CassandrConfig.KeyspaceConfig keyspace = new Configuration.CassandrConfig.KeyspaceConfig(); + keyspace.setName(k); + keyspace.setReplicationInfo(Collections.singletonList("1")); + keyspace.setReplicationStrategy(SdcSchemaBuilder.ReplicationStrategy.SIMPLE_STRATEGY.getStrategyName()); + return keyspace;}) + .collect(Collectors.toList()); + cfg.setKeySpaces(createdKeyspaces); + return cfg; + } + + private static void createTestKeyspaces(Cluster cluster, List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfig) { + try(Session session = cluster.connect()) { + keyspaceConfig.forEach(keyspace -> { + String query = String + .format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'%s', 'replication_factor' : %s};", + keyspace.getName(), keyspace.getReplicationStrategy(), keyspace.getReplicationInfo().get(0)); + session.execute(query); + }); + } + } +} 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 diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java index b098cc6a17..5eccc780e5 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java @@ -1,3 +1,24 @@ +/*- + * ============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.utils; import com.fasterxml.jackson.databind.ObjectMapper; diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/utils/CassandraTestHelper.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/utils/CassandraTestHelper.java new file mode 100644 index 0000000000..d97f7d3fa4 --- /dev/null +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/utils/CassandraTestHelper.java @@ -0,0 +1,53 @@ +/*- + * ============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.utils; + +import com.datastax.driver.core.Cluster; +import org.apache.cassandra.exceptions.ConfigurationException; +import org.apache.thrift.transport.TTransportException; +import org.cassandraunit.utils.EmbeddedCassandraServerHelper; + +import java.io.IOException; + +public class CassandraTestHelper { + public static final String SERVER = "localhost"; + public static final int BINARY_PORT = 9142; + + public CassandraTestHelper() { + } + + public static void startServer() { + try { + EmbeddedCassandraServerHelper.startEmbeddedCassandra(80000); + } catch(TTransportException | ConfigurationException | IOException ex) { + throw new RuntimeException(ex); + } + } + + public static Cluster createCluster() { + return Cluster.builder().addContactPoint(SERVER).withPort(BINARY_PORT).build(); + } + + public static Cluster createClusterWithNoSession() { + return Cluster.builder().addContactPoint(SERVER).build(); + } +} |