From 20da3d02c6c5507761c257b6ec6f58dcfc27d05a Mon Sep 17 00:00:00 2001 From: Piotr Darosz Date: Fri, 15 Jun 2018 08:28:00 +0200 Subject: 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 --- .../be/dao/cassandra/schema/SdcSchemaBuilder.java | 107 ++++++++++----------- .../be/dao/cassandra/schema/SdcSchemaUtils.java | 68 +++++++------ 2 files changed, 86 insertions(+), 89 deletions(-) (limited to 'catalog-dao/src/main/java/org') diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java index eaa0202be8..6c74543901 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java @@ -16,6 +16,8 @@ * 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; @@ -27,18 +29,31 @@ import com.datastax.driver.core.schemabuilder.SchemaBuilder; import com.datastax.driver.core.schemabuilder.SchemaStatement; import org.apache.commons.lang3.tuple.ImmutablePair; import org.openecomp.sdc.be.config.Configuration; -import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.be.dao.cassandra.schema.tables.OldExternalApiEventTableDesc; +import org.openecomp.sdc.be.resources.data.auditing.AuditingTypesConstants; import org.openecomp.sdc.common.log.enums.EcompLoggerErrorCode; import org.openecomp.sdc.common.log.wrappers.Logger; import java.util.*; import java.util.stream.Collectors; +import java.util.function.Supplier; public class SdcSchemaBuilder { - private static final String CREATE_KEYSPACE_SIMPLE_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy', %s};"; + private SdcSchemaUtils sdcSchemaUtils; + private Supplier cassandraConfigSupplier; + public SdcSchemaBuilder(SdcSchemaUtils sdcSchemaUtils, Supplier cassandraConfigSupplier) { + this.sdcSchemaUtils = sdcSchemaUtils; + this.cassandraConfigSupplier = cassandraConfigSupplier; + } + /** + * creat key space statment for SimpleStrategy + */ + private static final String CREATE_KEYSPACE_SIMPLE_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'SimpleStrategy', %s};"; + /** + * creat key space statment for NetworkTopologyStrategy + */ private static final String CREATE_KEYSPACE_NETWORK_TOPOLOGY_STRATEGY = "CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'NetworkTopologyStrategy', %s};"; private static Logger log = Logger.getLogger(SdcSchemaBuilder.class.getName()); @@ -58,22 +73,17 @@ public class SdcSchemaBuilder { * internal enums and external configuration for its operation * * @return true if the create operation was successful */ - public static boolean createSchema() { - Cluster cluster = null; - Session session = null; - try { + public boolean createSchema() { + boolean res = false; + try(Cluster cluster = sdcSchemaUtils.createCluster(); + Session session = cluster.connect()) { log.info("creating Schema for Cassandra."); - cluster = SdcSchemaUtils.createCluster(); - if (cluster == null) { - return false; - } - session = cluster.connect(); List keyspacesMetadateFromCassandra = cluster.getMetadata().getKeyspaces(); if (keyspacesMetadateFromCassandra == null) { - log.debug("filed to retrive a list of keyspaces from cassndra"); + log.debug("filed to retrieve a list of keyspaces from cassandra"); return false; } - log.debug("retrived Cassndra metadata."); + log.debug("retrieved Cassandra metadata."); Map>> cassndraMetadata = parseKeyspaceMetadata(keyspacesMetadateFromCassandra); Map>> metadataTablesStructure = getMetadataTablesStructure(keyspacesMetadateFromCassandra); Map> schemeData = getSchemeData(); @@ -87,54 +97,43 @@ public class SdcSchemaBuilder { Map> keyspaceMetadate = cassndraMetadata.get(keyspace.getKey()); createTables(keyspace.getValue(), keyspaceMetadate, session,metadataTablesStructure.get(keyspace.getKey())); } - return true; + res = true; } catch (Exception e) { - log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "creating Schema for Cassandra", "Cassandra", e.getLocalizedMessage()); - } finally { - if (session != null) { - session.close(); - } - if (cluster != null) { - cluster.close(); - } - - } - - return false; + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "creating Schema for Cassandra", "Cassandra", e.getLocalizedMessage()); + res = false; + } + return res; } - public static boolean deleteSchema() { - Cluster cluster = null; - Session session = null; - try { + public boolean deleteSchema() { + boolean res = false; + try(Cluster cluster = sdcSchemaUtils.createCluster(); + Session session = cluster.connect()) { log.info("delete Data from Cassandra."); - cluster = SdcSchemaUtils.createCluster(); - if (cluster == null) { - return false; - } - session = cluster.connect(); List keyspacesMetadateFromCassandra = cluster.getMetadata().getKeyspaces(); if (keyspacesMetadateFromCassandra == null) { - log.debug("filed to retrive a list of keyspaces from cassndra"); + log.debug("filed to retrieve a list of keyspaces from cassandra"); return false; } - log.debug("retrived Cassndra metadata."); + log.debug("retrieved Cassandra metadata."); Map>> cassndraMetadata = parseKeyspaceMetadata(keyspacesMetadateFromCassandra); - log.info("Cassandra Metadata: {}" ,cassndraMetadata); - return true; - } catch (Exception e) { - log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "deleting Schema for Cassandra", "Cassandra", e.getLocalizedMessage()); - } finally { - if (session != null) { - session.close(); - } - if (cluster != null) { - cluster.close(); - } + log.info("Cassandra Metadata: {}" ,cassndraMetadata); + cassndraMetadata.forEach((k, v) -> { + if (AuditingTypesConstants.TITAN_KEYSPACE.equals(k)) { + // session.execute("") + } else if (AuditingTypesConstants.ARTIFACT_KEYSPACE.equals(k)) { - } + } else if (AuditingTypesConstants.AUDIT_KEYSPACE.equals(k)) { - return false; + } + }); + + System.out.println(cassndraMetadata); + res = true; + } catch (Exception e) { + log.error(EcompLoggerErrorCode.SCHEMA_ERROR, "deleting Schema for Cassandra", "Cassandra", e.getLocalizedMessage()); + } + return res; } /** @@ -191,7 +190,7 @@ public class SdcSchemaBuilder { * the current tables columns that exist in the cassandra under this * keyspace */ - private static void createTables(List iTableDescriptions, Map> keyspaceMetadate, Session session, + private static void createTables(List iTableDescriptions, Map> keyspaceMetadate, Session session, Map> existingTablesMetadata) { for (ITableDescription tableDescription : iTableDescriptions) { String tableName = tableDescription.getTableName().toLowerCase(); @@ -261,7 +260,7 @@ public class SdcSchemaBuilder { Alter alter = SchemaBuilder.alterTable(tableDescription.getKeyspace(),tableDescription.getTableName()); SchemaStatement addColumn = alter.addColumn(columnName).type(column.getValue().getLeft()); log.trace("exacuting :{}", addColumn); - session.execute(addColumn); + session.execute(addColumn); } } } @@ -275,8 +274,8 @@ public class SdcSchemaBuilder { * @param session: the session object used for the execution of the query. * @return true in case the operation was successful */ - private static boolean createKeyspace(String keyspace, Map>> cassndraMetadata, Session session) { - List keyspaceConfigList = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getKeySpaces(); + private boolean createKeyspace(String keyspace, Map>> cassndraMetadata, Session session) { + List keyspaceConfigList = cassandraConfigSupplier.get().getKeySpaces(); log.info("creating keyspace:{}.", keyspace); if (!cassndraMetadata.keySet().contains(keyspace)) { return createKeyspaceIfNotExists(keyspace, session, keyspaceConfigList); diff --git a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java index 434a6e6eab..7531ad4c0a 100644 --- a/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java +++ b/catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java @@ -16,17 +16,20 @@ * 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 com.datastax.driver.core.SocketOptions; +import org.openecomp.sdc.be.config.Configuration; import org.openecomp.sdc.be.config.ConfigurationManager; import org.openecomp.sdc.common.log.wrappers.Logger; import java.util.List; +import java.util.function.Supplier; public class SdcSchemaUtils { @@ -38,8 +41,9 @@ public class SdcSchemaUtils { * * @return cluster object our null in case of an invalid configuration */ - public static Cluster createCluster() { - List nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts(); + public Cluster createCluster() { + final Configuration.CassandrConfig config = getCassandraConfig(); + List nodes = config.getCassandraHosts(); if (nodes == null) { log.info("no nodes were supplied in configuration."); return null; @@ -50,20 +54,18 @@ public class SdcSchemaUtils { clusterBuilder.withMaxSchemaAgreementWaitSeconds(60); - boolean authenticate = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isAuthenticate(); - if (authenticate) { - String username = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getUsername(); - String password = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getPassword(); + if (config.isAuthenticate()) { + String username = config.getUsername(); + String password = config.getPassword(); if (username == null || password == null) { log.info("authentication is enabled but username or password were not supplied."); return null; } clusterBuilder.withCredentials(username, password); } - boolean ssl = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().isSsl(); - if (ssl) { - String truststorePath = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePath(); - String truststorePassword = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getTruststorePassword(); + if (config.isSsl()) { + String truststorePath = config.getTruststorePath(); + String truststorePassword = config.getTruststorePassword(); if (truststorePath == null || truststorePassword == null) { log.info("ssl is enabled but truststorePath or truststorePassword were not supplied."); return null; @@ -73,12 +75,12 @@ public class SdcSchemaUtils { clusterBuilder.withSSL(); } SocketOptions socketOptions =new SocketOptions(); - Integer socketConnectTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketConnectTimeout(); + Integer socketConnectTimeout = config.getSocketConnectTimeout(); if( socketConnectTimeout!=null ){ log.info("SocketConnectTimeout was provided, setting Cassandra client to use SocketConnectTimeout: {} .",socketConnectTimeout); socketOptions.setConnectTimeoutMillis(socketConnectTimeout); } - Integer socketReadTimeout = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getSocketReadTimeout(); + Integer socketReadTimeout = config.getSocketReadTimeout(); if( socketReadTimeout != null ){ log.info("SocketReadTimeout was provided, setting Cassandra client to use SocketReadTimeout: {} .",socketReadTimeout); socketOptions.setReadTimeoutMillis(socketReadTimeout); @@ -87,37 +89,33 @@ public class SdcSchemaUtils { return clusterBuilder.build(); } - public static boolean executeStatement(String statement) { - return executeStatements(statement); + public boolean executeStatement(String statement) { + return executeStatement(this::createCluster, statement); } - public static boolean executeStatements(String ... statements) { - Cluster cluster = null; - Session session = null; - try { - cluster = createCluster(); - if (cluster == null) { - return false; - } - session = cluster.connect(); + public boolean executeStatements(String ... statements) { + return executeStatements(this::createCluster, statements); + } + + boolean executeStatement(Supplier clusterSupplier, String statement) { + return executeStatements(clusterSupplier, statement); + } + + boolean executeStatements(Supplier clusterSupplier, String ... statements) { + try(Cluster cluster = clusterSupplier.get(); + Session session = cluster.connect()) { for (String statement : statements) { session.execute(statement); } return true; } catch (RuntimeException e) { - log.error(String.format("could not execute statements"), e); - return false; - } finally { - if (session != null) { - session.close(); - } - if (cluster != null) { - cluster.close(); - } - + log.error("could not execute statements", e); } + return false; } - + Configuration.CassandrConfig getCassandraConfig() { + return ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig(); + } } -- cgit 1.2.3-korg