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.Ignore;
27 import org.junit.Test;
28 import org.mockito.Mockito;
29 import org.openecomp.sdc.be.config.Configuration;
30 import org.openecomp.sdc.be.utils.CassandraTestHelper;
32 import java.util.Collections;
33 import java.util.List;
35 import static org.mockito.Mockito.when;
37 public class SdcSchemaUtilsTest {
38 private static final String SINGLE_STATEMENT = "SELECT COUNT(*) FROM system.peers";
39 private static final String[] MULTIPLE_STATEMENTS = new String[] {SINGLE_STATEMENT, SINGLE_STATEMENT};
40 private static final List<String> CASSANDRA_HOSTS = Collections.singletonList(CassandraTestHelper.SERVER);
41 private static final Integer CASSANDRA_PORT = 9042;
42 private static final String CASSANDRA_USERNAME = "username";
43 private static final String CASSANDRA_PASSWORD = "password";
44 private static final String TRUSTSTORE_PATH = "pathToTruststore";
45 private static final String TRUSTSTORE_PASSWORD = "passwordToTruststore";
48 public static void startServer() {
49 CassandraTestHelper.startServer();
53 public void testExecuteSingleStatement() throws Exception {
54 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
55 final boolean result = sdcSchemaUtils.executeStatement(CassandraTestHelper::createCluster, SINGLE_STATEMENT);
56 Assert.assertTrue(result);
61 public void testExecuteStatementsSuccessfullScenario() throws Exception {
62 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
63 final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createCluster, MULTIPLE_STATEMENTS);
64 Assert.assertTrue(result);
68 public void testExecuteStatementsClusterFail() throws Exception {
69 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
70 final boolean result = sdcSchemaUtils.executeStatements(() -> null, MULTIPLE_STATEMENTS);
71 Assert.assertFalse(result);
75 public void testExecuteStatementsSessionFail() throws Exception {
76 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
77 final boolean result = sdcSchemaUtils.executeStatements(CassandraTestHelper::createClusterWithNoSession, MULTIPLE_STATEMENTS);
78 Assert.assertFalse(result);
82 public void testCreateClusterNoAuthNoSsl() {
83 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
84 cfg.setCassandraHosts(CASSANDRA_HOSTS);
85 cfg.setCassandraPort(CASSANDRA_PORT);
86 cfg.setReconnectTimeout(new Long(30000));
87 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
88 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
89 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
91 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
92 Assert.assertNotNull(cluster);
97 public void testCreateClusterFailOnLackOfCassandraNodes() {
98 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
99 cfg.setCassandraHosts(null);
101 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
102 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
103 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
105 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
106 Assert.assertNull(cluster);
111 public void testCreateClusterFailOnLackOfCassandraPort() {
112 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
113 cfg.setCassandraHosts(CASSANDRA_HOSTS);
114 cfg.setCassandraPort(null);
116 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
117 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
118 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
120 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
121 Assert.assertNotNull(cluster);
126 public void testCreateClusterFailOnAuthEnabledWithNoCredentials() {
127 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
128 cfg.setAuthenticate(true);
129 cfg.setCassandraHosts(CASSANDRA_HOSTS);
130 cfg.setCassandraPort(CASSANDRA_PORT);
131 cfg.setReconnectTimeout(new Long(30000));
133 cfg.setUsername(null);
134 cfg.setPassword(null);
136 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
137 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
138 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
140 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
141 Assert.assertNull(cluster);
146 public void testCreateClusterFailOnSSLWithNoCredentials() {
147 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
148 cfg.setCassandraHosts(CASSANDRA_HOSTS);
149 cfg.setCassandraPort(CASSANDRA_PORT);
151 cfg.setTruststorePath(null);
152 cfg.setTruststorePassword(null);
153 cfg.setReconnectTimeout(new Long(30000));
154 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
155 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
156 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
158 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
159 Assert.assertNull(cluster);
164 public void testCreateClusterWithAuthSsl() {
165 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
166 cfg.setAuthenticate(true);
167 cfg.setCassandraHosts(CASSANDRA_HOSTS);
168 cfg.setCassandraPort(CASSANDRA_PORT);
169 cfg.setUsername(CASSANDRA_USERNAME);
170 cfg.setPassword(CASSANDRA_PASSWORD);
172 cfg.setTruststorePath(TRUSTSTORE_PATH);
173 cfg.setTruststorePassword(TRUSTSTORE_PASSWORD);
174 cfg.setReconnectTimeout(new Long(30000));
175 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
176 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
177 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
179 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
180 Assert.assertNotNull(cluster);
181 Assert.assertEquals(System.getProperty("javax.net.ssl.trustStore"), TRUSTSTORE_PATH);
182 Assert.assertEquals(System.getProperty("javax.net.ssl.trustStorePassword"), TRUSTSTORE_PASSWORD);