Add tests for SdcSchema classes 39/54939/10
authorPiotr Darosz <piotr.darosz@nokia.com>
Fri, 15 Jun 2018 06:28:00 +0000 (08:28 +0200)
committerTal Gitelman <tg851x@intl.att.com>
Wed, 29 Aug 2018 13:27:45 +0000 (13:27 +0000)
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>
asdctool/src/main/java/org/openecomp/sdc/asdctool/main/DataSchemaMenu.java
catalog-be/src/main/java/org/openecomp/sdc/be/components/impl/CassandraHealthCheck.java
catalog-be/src/test/resources/paths/path-context.xml
catalog-dao/pom.xml
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java
catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java
catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java
catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java
catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java
catalog-dao/src/test/java/org/openecomp/sdc/be/utils/CassandraTestHelper.java [new file with mode: 0644]
pom.xml

index 496eb18..fe117d1 100644 (file)
@@ -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.asdctool.main;
@@ -23,6 +25,7 @@ package org.openecomp.sdc.asdctool.main;
 import org.openecomp.sdc.asdctool.impl.TitanGraphInitializer;
 import org.openecomp.sdc.be.config.ConfigurationManager;
 import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaBuilder;
+import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils;
 import org.openecomp.sdc.common.api.ConfigurationSource;
 import org.openecomp.sdc.common.impl.ExternalConfiguration;
 import org.openecomp.sdc.common.impl.FSConfigurationSource;
@@ -34,7 +37,7 @@ public class DataSchemaMenu {
 
     public static void main(String[] args) {
 
-        String operation = args[0];
+               String operation = args[0];
 
         String appConfigDir = args[1];
 
@@ -45,10 +48,13 @@ public class DataSchemaMenu {
         ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
         ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
 
+        SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(new SdcSchemaUtils(),
+            ConfigurationManager.getConfigurationManager().getConfiguration()::getCassandraConfig);
+
         switch (operation.toLowerCase()) {
             case "create-cassandra-structures":
                 log.debug("Start create cassandra keyspace, tables and indexes");
-                if (SdcSchemaBuilder.createSchema()) {
+                if (sdcSchemaBuilder.createSchema()) {
                     log.debug("create cassandra keyspace, tables and indexes successfull");
                     System.exit(0);
                 } else {
@@ -69,7 +75,7 @@ public class DataSchemaMenu {
                 break;
             case "clean-cassndra":
                 log.debug("Start clean keyspace, tables");
-                if (SdcSchemaBuilder.deleteSchema()) {
+                if (sdcSchemaBuilder.deleteSchema()) {
                     log.debug(" successfull");
                     System.exit(0);
                 } else {
index 05a0eb5..bc5f77b 100644 (file)
@@ -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.components.impl;
 
 import com.datastax.driver.core.Cluster;
@@ -12,6 +33,7 @@ import org.openecomp.sdc.common.util.GeneralUtility;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.PostConstruct;
+
 import java.io.FileInputStream;
 import java.io.InputStream;
 import java.util.*;
@@ -28,6 +50,8 @@ public class CassandraHealthCheck {
 
     private int HC_FormulaNumber;
 
+    private SdcSchemaUtils sdcSchemaUtils;
+
     @PostConstruct
     private void init() {
 
@@ -61,14 +85,14 @@ public class CassandraHealthCheck {
         }
 
         log.info("All sdc keyspaces are : {}", sdcKeyspaces);
-
+        sdcSchemaUtils = new SdcSchemaUtils();
         //Calculate the Formula of Health Check
         Cluster cluster = null;
         try {
 
             log.info("creating cluster for Cassandra Health Check.");
             //Create cluster from nodes in cassandra configuration
-            cluster = SdcSchemaUtils.createCluster();
+            cluster = sdcSchemaUtils.createCluster();
             if (cluster == null) {
                 log.error("Failure create cassandra cluster.");
                 return;
@@ -141,7 +165,7 @@ public class CassandraHealthCheck {
         Session session = null;
         try {
             log.info("creating cluster for Cassandra for monitoring.");
-            cluster = SdcSchemaUtils.createCluster();
+            cluster = sdcSchemaUtils.createCluster();
             if (cluster == null) {
                 log.error("Failure create cassandra cluster.");
                 return false;
index e1e3997..b9415bf 100644 (file)
@@ -1,4 +1,25 @@
 <?xml version="1.0" encoding="UTF-8"?>
+<!--
+============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
+================================================================================
+-->
 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
   xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
   xsi:schemaLocation="
@@ -70,6 +91,7 @@
   <bean id="distributionEngine" class="org.openecomp.sdc.be.components.path.beans.DistributionEngineMock"/>
   <bean id="forwardingPathValidator" class="org.openecomp.sdc.be.components.path.beans.ForwardingPathValidatorMock" />
   <bean id="dataTypeValidatorConverter" class="org.openecomp.sdc.be.model.tosca.validators.DataTypeValidatorConverter" />
+  <bean id="sdcSchemaUtils" class="org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaUtils" />
   <util:properties id="elasticsearchConfig" location="paths/elasticsearch.yml" />
 
 </beans>
index ccb8085..c15a585 100644 (file)
@@ -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
+================================================================================
+-->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
                        <artifactId>cassandra-driver-core</artifactId>
                        <version>${cassandra.driver.version}</version>
                        <scope>provided</scope>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>io.netty</groupId>
+                                       <artifactId>netty-handler</artifactId>
+                               </exclusion>
+                       </exclusions>
                </dependency>
                <dependency>
                        <groupId>com.datastax.cassandra</groupId>
                        <version>${cassandra.driver.version}</version>
                        <scope>provided</scope>
                </dependency>
+               <dependency>
+                       <groupId>org.cassandraunit</groupId>
+                       <artifactId>cassandra-unit</artifactId>
+                       <version>${cassandra.unit.version}</version>
+                       <scope>test</scope>
+                       <exclusions>
+                               <exclusion>
+                                       <groupId>org.apache.cassandra</groupId>
+                                       <artifactId>cassandra-all</artifactId>
+                               </exclusion>
+                       </exclusions>
+               </dependency>
+               <dependency>
+                       <groupId>org.apache.cassandra</groupId>
+                       <artifactId>cassandra-all</artifactId>
+                       <version>3.11.3</version>
+                       <scope>test</scope>
+               </dependency>
                <!-- CASSANDRA END -->
        </dependencies>
 
index eaa0202..6c74543 100644 (file)
@@ -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<Configuration.CassandrConfig> cassandraConfigSupplier;
 
+       public SdcSchemaBuilder(SdcSchemaUtils sdcSchemaUtils, Supplier<Configuration.CassandrConfig> 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<KeyspaceMetadata> 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<String, Map<String, List<String>>> cassndraMetadata = parseKeyspaceMetadata(keyspacesMetadateFromCassandra);
                        Map<String, Map<String, List<String>>> metadataTablesStructure = getMetadataTablesStructure(keyspacesMetadateFromCassandra);
                        Map<String, List<ITableDescription>> schemeData = getSchemeData();
@@ -87,54 +97,43 @@ public class SdcSchemaBuilder {
                                Map<String, List<String>> 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<KeyspaceMetadata> 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<String, Map<String, List<String>>> 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<ITableDescription> iTableDescriptions, Map<String, List<String>> keyspaceMetadate, Session session, 
+       private static void createTables(List<ITableDescription> iTableDescriptions, Map<String, List<String>> keyspaceMetadate, Session session,
                        Map<String, List<String>> 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<String, Map<String, List<String>>> cassndraMetadata, Session session) {
-               List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfigList = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getKeySpaces();
+       private boolean createKeyspace(String keyspace, Map<String, Map<String, List<String>>> cassndraMetadata, Session session) {
+               List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfigList = cassandraConfigSupplier.get().getKeySpaces();
                log.info("creating keyspace:{}.", keyspace);
                if (!cassndraMetadata.keySet().contains(keyspace)) {
                        return createKeyspaceIfNotExists(keyspace, session, keyspaceConfigList);
index 434a6e6..7531ad4 100644 (file)
  * 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<String> nodes = ConfigurationManager.getConfigurationManager().getConfiguration().getCassandraConfig().getCassandraHosts();
+    public Cluster createCluster() {
+        final Configuration.CassandrConfig config = getCassandraConfig();
+        List<String> 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<Cluster> clusterSupplier, String statement) {
+        return executeStatements(clusterSupplier, statement);
+    }
+
+    boolean executeStatements(Supplier<Cluster> 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();
+    }
 
 }
index 1550e57..422dac7 100644 (file)
+/*-
+ * ============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);
+            });
+        }
+    }
+}
index 0744224..ca8bdea 100644 (file)
+/*-
+ * ============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
index b098cc6..5eccc78 100644 (file)
@@ -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 (file)
index 0000000..d97f7d3
--- /dev/null
@@ -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();
+    }
+}
diff --git a/pom.xml b/pom.xml
index 1fc2789..f36d805 100644 (file)
--- a/pom.xml
+++ b/pom.xml
@@ -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
+================================================================================
+-->
 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
@@ -22,6 +43,7 @@
         <servlet-api.version>3.1.0</servlet-api.version>
         <wire-mock.version>2.18.0</wire-mock.version>
         <ecomp.version>1.1.0</ecomp.version>
+        <cassandra.unit.version>3.5.0.1</cassandra.unit.version>
 
         <!-- Elastic Search mapper (reference the elastic search version actually). -->
         <elastic-search.version>2.4.0</elastic-search.version>