aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao
diff options
context:
space:
mode:
Diffstat (limited to 'catalog-dao')
-rw-r--r--catalog-dao/pom.xml45
-rw-r--r--catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilder.java107
-rw-r--r--catalog-dao/src/main/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtils.java68
-rw-r--r--catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java197
-rw-r--r--catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java172
-rw-r--r--catalog-dao/src/test/java/org/openecomp/sdc/be/dao/utils/JsonUtilTest.java21
-rw-r--r--catalog-dao/src/test/java/org/openecomp/sdc/be/utils/CassandraTestHelper.java53
7 files changed, 452 insertions, 211 deletions
diff --git a/catalog-dao/pom.xml b/catalog-dao/pom.xml
index ccb8085cd0..c15a585310 100644
--- a/catalog-dao/pom.xml
+++ b/catalog-dao/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>
@@ -261,6 +282,12 @@
<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>
@@ -268,6 +295,24 @@
<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>
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<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);
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<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();
+ }
}
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();
+ }
+}