Initial OpenECOMP SDC commit
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / resources / CassandraTest.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  */
20
21 package org.openecomp.sdc.be.resources;
22
23 import com.datastax.driver.core.Cluster;
24 import com.datastax.driver.core.Session;
25 import com.datastax.driver.mapping.Mapper;
26 import com.datastax.driver.mapping.MappingManager;
27
28 import org.openecomp.sdc.be.dao.Account;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 public class CassandraTest {
33         private static Logger log = LoggerFactory.getLogger(CassandraTest.class.getName());
34         private Cluster cluster;
35
36         // #\@Test
37         public void testCrud() {
38                 String node = "mtanjv9sdcg44";
39
40                 cluster = Cluster.builder().addContactPoint(node).build();
41
42                 // Query
43                 String query = "CREATE KEYSPACE IF NOT EXISTS dstest WITH replication "
44                                 + "= {'class':'SimpleStrategy', 'replication_factor':1};";
45
46                 String queryTable = "CREATE TABLE IF NOT EXISTS accounts(email varchar  PRIMARY KEY, name varchar);";
47
48                 Session session = cluster.connect();
49                 // Executing the query
50                 session.execute(query);
51                 // //using the KeySpace
52                 session.execute("USE dstest");
53                 session.execute(queryTable);
54
55                 Mapper<Account> mapper = new MappingManager(session).mapper(Account.class);
56                 Account account = new Account("John Doe", "jd@example.com");
57                 // Class<? extends Account> class1 = account.getClass();
58                 // Class class2 = Account.class;
59                 mapper.save(account);
60
61                 Account whose = mapper.get("jd@example.com");
62                 log.debug("Account name: {}", whose.getName());
63
64                 account.setName("Samanta Smit");
65                 mapper.save(account);
66                 whose = mapper.get("jd@example.com");
67                 log.debug("Account name: {}", whose.getName());
68
69                 mapper.delete(account);
70                 whose = mapper.get("jd@example.com");
71
72                 cluster.close();
73         }
74 }