Catalog alignment
[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.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;
31
32 import java.util.Collections;
33 import java.util.List;
34
35 import static org.mockito.Mockito.when;
36
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";
46
47         @BeforeClass
48         public static void startServer() {
49                 CassandraTestHelper.startServer();
50         }
51
52         @Test
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);
57         }
58
59
60         @Test
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);
65         }
66
67         @Test
68         public void testExecuteStatementsClusterFail() throws Exception {
69                 SdcSchemaUtils sdcSchemaUtils = new SdcSchemaUtils();
70                 final boolean result = sdcSchemaUtils.executeStatements(() -> null, MULTIPLE_STATEMENTS);
71                 Assert.assertFalse(result);
72         }
73
74         @Test
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);
79         }
80
81         @Test
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();
90
91                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
92                         Assert.assertNotNull(cluster);
93                 }
94         }
95
96         @Test
97         public void testCreateClusterFailOnLackOfCassandraNodes() {
98                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
99                 cfg.setCassandraHosts(null);
100
101                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
102                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
103                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
104
105                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
106                         Assert.assertNull(cluster);
107                 }
108         }
109
110         @Test
111         public void testCreateClusterFailOnLackOfCassandraPort() {
112                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
113                 cfg.setCassandraHosts(CASSANDRA_HOSTS);
114                 cfg.setCassandraPort(null);
115
116                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
117                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
118                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
119
120                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
121                         Assert.assertNotNull(cluster);
122                 }
123         }
124
125         @Test
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));
132                 
133                 cfg.setUsername(null);
134                 cfg.setPassword(null);
135
136                 SdcSchemaUtils sdcSchemaUtils = Mockito.mock(SdcSchemaUtils.class);
137                 when(sdcSchemaUtils.getCassandraConfig()).thenReturn(cfg);
138                 when(sdcSchemaUtils.createCluster()).thenCallRealMethod();
139
140                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
141                         Assert.assertNull(cluster);
142                 }
143         }
144
145         @Test
146         public void testCreateClusterFailOnSSLWithNoCredentials() {
147                 Configuration.CassandrConfig cfg = new Configuration.CassandrConfig();
148                 cfg.setCassandraHosts(CASSANDRA_HOSTS);
149                 cfg.setCassandraPort(CASSANDRA_PORT);
150                 cfg.setSsl(true);
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();
157
158                 try(Cluster cluster = sdcSchemaUtils.createCluster()) {
159                         Assert.assertNull(cluster);
160                 }
161         }
162 /*
163         @Test
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);
171                 cfg.setSsl(true);
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();
178
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);
183                 }
184         }*/
185 }