2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
19 * Modifications copyright (c) 2018 Nokia
20 * ================================================================================
22 package org.openecomp.sdc.be.dao.cassandra.schema;
23 import com.datastax.driver.core.Cluster;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.mockito.Mockito;
28 import org.openecomp.sdc.be.config.Configuration;
29 import org.openecomp.sdc.be.utils.CassandraTestHelper;
31 import java.util.Collections;
32 import java.util.List;
34 import static org.mockito.Mockito.when;
36 public class SdcSchemaUtilsTest {
37 private static final String SINGLE_STATEMENT = "SELECT COUNT(*) FROM system.peers";
38 private static final String[] MULTIPLE_STATEMENTS = new String[] {SINGLE_STATEMENT, SINGLE_STATEMENT};
39 private static final List<String> CASSANDRA_HOSTS = Collections.singletonList(CassandraTestHelper.SERVER);
40 private static final Integer CASSANDRA_PORT = 9042;
41 private static final String CASSANDRA_USERNAME = "username";
42 private static final String CASSANDRA_PASSWORD = "password";
43 private static final String TRUSTSTORE_PATH = "pathToTruststore";
44 private static final String TRUSTSTORE_PASSWORD = "passwordToTruststore";
47 public static void startServer() {
48 CassandraTestHelper.startServer();
52 public void testExecuteSingleStatement() throws Exception {
53 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
54 final boolean result = sdcSchemaUtils.executeStatement(CassandraTestHelper::createCluster, SINGLE_STATEMENT);
55 Assert.assertTrue(result);
60 public void testExecuteStatementsSuccessfullScenario() throws Exception {
61 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
62 final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createCluster, MULTIPLE_STATEMENTS);
63 Assert.assertTrue(result);
67 public void testExecuteStatementsClusterFail() throws Exception {
68 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
69 final boolean result = sdcSchemaUtils.executeStatements(() -> null, MULTIPLE_STATEMENTS);
70 Assert.assertFalse(result);
74 public void testExecuteStatementsSessionFail() throws Exception {
75 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
76 final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createClusterWithNoSession, MULTIPLE_STATEMENTS);
77 Assert.assertFalse(result);
81 public void testCreateClusterNoAuthNoSsl() {
82 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
83 cfg.setCassandraHosts(CASSANDRA_HOSTS);
84 cfg.setCassandraPort(CASSANDRA_PORT);
86 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
87 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
88 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
90 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
91 Assert.assertNotNull(cluster);
96 public void testCreateClusterFailOnLackOfCassandraNodes() {
97 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
98 cfg.setCassandraHosts(null);
100 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
101 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
102 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
104 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
105 Assert.assertNull(cluster);
110 public void testCreateClusterWithDefaultOnLackOfCassandraPort() {
111 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
112 cfg.setCassandraHosts(CASSANDRA_HOSTS);
113 cfg.setCassandraPort(null);
115 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
116 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
117 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
119 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
120 Assert.assertNotNull(cluster);
125 public void testCreateClusterFailOnAuthEnabledWithNoCredentials() {
126 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
127 cfg.setAuthenticate(true);
128 cfg.setCassandraHosts(CASSANDRA_HOSTS);
129 cfg.setCassandraPort(CASSANDRA_PORT);
130 cfg.setUsername(null);
131 cfg.setPassword(null);
133 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
134 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
135 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
137 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
138 Assert.assertNull(cluster);
143 public void testCreateClusterFailOnSSLWithNoCredentials() {
144 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
145 cfg.setCassandraHosts(CASSANDRA_HOSTS);
146 cfg.setCassandraPort(CASSANDRA_PORT);
148 cfg.setTruststorePath(null);
149 cfg.setTruststorePassword(null);
151 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
152 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
153 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
155 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
156 Assert.assertNull(cluster);