aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-dao/src/test/java/org/openecomp/sdc/be/dao/cassandra/schema/SdcSchemaUtilsTest.java
blob: 126881381af0739e916f2b33a0ab1541b041dea0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*-
 * ============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.mockito.Mockito;
import org.openecomp.sdc.be.config.Configuration;
import org.openecomp.sdc.be.utils.CassandraTestHelper;

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 Integer CASSANDRA_PORT = 9042;
	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);
		cfg.setCassandraPort(CASSANDRA_PORT);

		SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
		when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
		when(sdcSchemaUtils.createCluster()).thenCallRealMethod();

		try(Cluster cluster = sdcSchemaUtils.createCluster()) {
			Assert.assertNotNull(cluster);
		}
	}

	@Test
	public void testCreateClusterFailOnLackOfCassandraNodes() {
		Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
		cfg.setCassandraHosts(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 testCreateClusterWithDefaultOnLackOfCassandraPort() {
		Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
		cfg.setCassandraHosts(CASSANDRA_HOSTS);
		cfg.setCassandraPort(null);

		SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
		when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
		when(sdcSchemaUtils.createCluster()).thenCallRealMethod();

		try(Cluster cluster = sdcSchemaUtils.createCluster()) {
			Assert.assertNotNull(cluster);
		}
	}

	@Test
	public void testCreateClusterFailOnAuthEnabledWithNoCredentials() {
		Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
		cfg.setAuthenticate(true);
		cfg.setCassandraHosts(CASSANDRA_HOSTS);
		cfg.setCassandraPort(CASSANDRA_PORT);
		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.setCassandraPort(CASSANDRA_PORT);
		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);
		}
	}
}