diff options
Diffstat (limited to 'catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java')
-rw-r--r-- | catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java | 197 |
1 files changed, 99 insertions, 98 deletions
diff --git a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java index 1550e5708e..422dac7114 100644 --- a/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java +++ b/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaBuilderTest.java @@ -1,104 +1,105 @@ +/*- + * ============LICENSE_START======================================================= + * SDC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END========================================================= + * Modifications copyright (c) 2018 Nokia + * ================================================================================ + */ package org.openecomp.sdc.be.dao.cassandra.schema; +import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Session; -import mockit.Deencapsulation; +import org.junit.Assert; +import org.junit.BeforeClass; import org.junit.Test; -import org.mockito.Mockito; -import org.openecomp.sdc.be.config.Configuration.CassandrConfig.KeyspaceConfig; -import org.openecomp.sdc.be.dao.cassandra.schema.SdcSchemaBuilder.ReplicationStrategy; -import org.openecomp.sdc.be.utils.DAOConfDependentTest; +import org.openecomp.sdc.be.config.Configuration; +import org.openecomp.sdc.be.utils.CassandraTestHelper; -import java.util.HashMap; -import java.util.LinkedList; +import java.util.Arrays; +import java.util.Collections; import java.util.List; -import java.util.Map; - -public class SdcSchemaBuilderTest extends DAOConfDependentTest{ - - private SdcSchemaBuilder createTestSubject() { - return new SdcSchemaBuilder(); - } - - @Test - public void testHandle1707OSMigration() throws Exception { - Map<String, Map<String, List<String>>> cassndraMetadata = new HashMap<>(); - Map<String, List<ITableDescription>> schemeData = new HashMap<>(); - - // default test - Deencapsulation.invoke(SdcSchemaBuilder.class, "handle1707OSMigration", cassndraMetadata, schemeData); - } - - @Test - public void testCreateSchema() throws Exception { - boolean result; - - // default test - result = SdcSchemaBuilder.createSchema(); - } - - @Test - public void testDeleteSchema() throws Exception { - boolean result; - - // default test - result = SdcSchemaBuilder.deleteSchema(); - } - - @Test - public void testCreateIndexName() throws Exception { - String table = ""; - String column = ""; - String result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createIndexName", table, column); - } - - @Test - public void testCreateKeyspace() throws Exception { - String keyspace = "mock"; - Map<String, Map<String, List<String>>> cassndraMetadata = new HashMap<>(); - Session session = Mockito.mock(Session.class); - boolean result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspace", - keyspace, cassndraMetadata, session); - - cassndraMetadata.put(keyspace, new HashMap<>()); - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspace", - keyspace, cassndraMetadata, session); - } - - @Test - public void testGetSchemeData() throws Exception { - Map<String, List<ITableDescription>> result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "getSchemeData"); - } - - @Test - public void testCreateKeyspaceQuereyString() throws Exception { - String keyspace = "mock"; - KeyspaceConfig keyspaceInfo = new KeyspaceConfig(); - String result; - - // default test - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - - keyspaceInfo.setReplicationStrategy(ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.getStrategyName()); - LinkedList<String> replicationInfo = new LinkedList<>(); - keyspaceInfo.setReplicationInfo(replicationInfo); - //Test1 - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - replicationInfo.add("mock1"); - replicationInfo.add("mock2"); - - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - - //Test2 - keyspaceInfo.setReplicationStrategy(ReplicationStrategy.SIMPLE_STRATEGY.getStrategyName()); - result = Deencapsulation.invoke(SdcSchemaBuilder.class, "createKeyspaceQuereyString", keyspace, keyspaceInfo); - } -}
\ No newline at end of file +import java.util.Set; +import java.util.stream.Collectors; + +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; + +public class SdcSchemaBuilderTest { + + @BeforeClass + public static void startServer() { + CassandraTestHelper.startServer(); + } + + @Test + public void testCreateSchema() { + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(CassandraTestHelper.createCluster()); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.createSchema(); + Assert.assertTrue(result); + } + + @Test + public void testDeleteSchemaNoKeyspaces() { + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(CassandraTestHelper.createCluster()); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.deleteSchema(); + Assert.assertTrue(result); + } + + @Test + public void testDeleteSchemaWithKeyspacesExisting() { + Configuration.CassandrConfig throwAwayConfig = createCassandraConfig(); + Cluster cluster = CassandraTestHelper.createCluster(); + createTestKeyspaces(cluster, throwAwayConfig.getKeySpaces()); + SdcSchemaUtils utils = mock(SdcSchemaUtils.class); + when(utils.createCluster()).thenReturn(cluster); + SdcSchemaBuilder sdcSchemaBuilder = new SdcSchemaBuilder(utils, SdcSchemaBuilderTest::createCassandraConfig); + final boolean result = sdcSchemaBuilder.deleteSchema(); + Assert.assertTrue(result); + } + + private static Configuration.CassandrConfig createCassandraConfig() { + Configuration.CassandrConfig cfg = new Configuration.CassandrConfig(); + Set<String> requiredKeyspaces = Arrays.stream(Table.values()) + .map(t -> t.getTableDescription().getKeyspace().toLowerCase()) + .collect(Collectors.toSet()); + List<Configuration.CassandrConfig.KeyspaceConfig> createdKeyspaces = requiredKeyspaces.stream() + .map(k -> { + Configuration.CassandrConfig.KeyspaceConfig keyspace = new Configuration.CassandrConfig.KeyspaceConfig(); + keyspace.setName(k); + keyspace.setReplicationInfo(Collections.singletonList("1")); + keyspace.setReplicationStrategy(SdcSchemaBuilder.ReplicationStrategy.SIMPLE_STRATEGY.getStrategyName()); + return keyspace;}) + .collect(Collectors.toList()); + cfg.setKeySpaces(createdKeyspaces); + return cfg; + } + + private static void createTestKeyspaces(Cluster cluster, List<Configuration.CassandrConfig.KeyspaceConfig> keyspaceConfig) { + try(Session session = cluster.connect()) { + keyspaceConfig.forEach(keyspace -> { + String query = String + .format("CREATE KEYSPACE IF NOT EXISTS %s WITH replication = {'class':'%s', 'replication_factor' : %s};", + keyspace.getName(), keyspace.getReplicationStrategy(), keyspace.getReplicationInfo().get(0)); + session.execute(query); + }); + } + } +} |