Update Release Note
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / dao / cassandra / schema / SdcSchemaUtilsTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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  * ================================================================================
21  */
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;
30
31 import java.util.Collections;
32 import java.util.List;
33
34 import static org.mockito.Mockito.when;
35
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";
45
46         @BeforeClass
47         public static void startServer() {
48                 CassandraTestHelper.startServer();
49         }
50
51         @Test
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);
56         }
57
58
59         @Test
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);
64         }
65
66         @Test
67         public void testExecuteStatementsClusterFail() throws Exception {
68                 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
69                 final boolean result = sdcSchemaUtils.executeStatements(() -> null, MULTIPLE_STATEMENTS);
70                 Assert.assertFalse(result);
71         }
72
73         @Test
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);
78         }
79
80         @Test
81         public void testCreateClusterNoAuthNoSsl() {
82                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
83                 cfg.setCassandraHosts(CASSANDRA_HOSTS);
84                 cfg.setCassandraPort(CASSANDRA_PORT);
85
86                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
87                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
88                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
89
90                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
91                         Assert.assertNotNull(cluster);
92                 }
93         }
94
95         @Test
96         public void testCreateClusterFailOnLackOfCassandraNodes() {
97                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
98                 cfg.setCassandraHosts(null);
99
100                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
101                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
102                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
103
104                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
105                         Assert.assertNull(cluster);
106                 }
107         }
108
109         @Test
110         public void testCreateClusterWithDefaultOnLackOfCassandraPort() {
111                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
112                 cfg.setCassandraHosts(CASSANDRA_HOSTS);
113                 cfg.setCassandraPort(null);
114
115                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
116                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
117                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
118
119                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
120                         Assert.assertNotNull(cluster);
121                 }
122         }
123
124         @Test
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);
132
133                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
134                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
135                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
136
137                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
138                         Assert.assertNull(cluster);
139                 }
140         }
141
142         @Test
143         public void testCreateClusterFailOnSSLWithNoCredentials() {
144                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
145                 cfg.setCassandraHosts(CASSANDRA_HOSTS);
146                 cfg.setCassandraPort(CASSANDRA_PORT);
147                 cfg.setSsl(true);
148                 cfg.setTruststorePath(null);
149                 cfg.setTruststorePassword(null);
150
151                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
152                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
153                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
154
155                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
156                         Assert.assertNull(cluster);
157                 }
158         }
159 }