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