Push variuos changes
[music.git] / jar / src / test / java / org / onap / music / unittests / MusicDataStoreTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
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  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22
23 package org.onap.music.unittests;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.FixMethodOrder;
31 import org.junit.Test;
32 import org.junit.runners.MethodSorters;
33 import org.mockito.Mock;
34 import org.onap.music.exceptions.MusicQueryException;
35 import org.onap.music.exceptions.MusicServiceException;
36
37 import org.onap.music.datastore.MusicDataStore;
38 import org.onap.music.datastore.PreparedQueryObject;
39
40 import com.datastax.driver.core.DataType;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43 import com.datastax.driver.core.TableMetadata;
44
45 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 public class MusicDataStoreTest {
47
48     static MusicDataStore dataStore;
49     static PreparedQueryObject testObject;
50
51     @BeforeClass
52     public static void init() {
53         dataStore = CassandraCQL.connectToEmbeddedCassandra();
54
55     }
56
57     @AfterClass
58     public static void close() throws MusicServiceException, MusicQueryException {
59  
60         testObject = new PreparedQueryObject();
61         testObject.appendQueryString(CassandraCQL.dropKeyspace);
62         dataStore.executePut(testObject, "eventual");
63         dataStore.close();
64
65     }
66
67     @Test
68     public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
69         boolean result = false;
70         testObject = new PreparedQueryObject();
71         testObject.appendQueryString(CassandraCQL.createKeySpace);
72         result = dataStore.executePut(testObject, "eventual");;
73         testObject = new PreparedQueryObject();
74         testObject.appendQueryString(CassandraCQL.createTableEmployees);
75         result = dataStore.executePut(testObject, "eventual");
76         assertEquals(true, result);
77
78     }
79
80     @Test
81     public void Test2_ExecutePut_eventual_insert() throws MusicServiceException, MusicQueryException {
82         testObject = CassandraCQL.setPreparedInsertQueryObject1();
83         boolean result = dataStore.executePut(testObject, "eventual");
84         assertEquals(true, result);
85     }
86
87     @Test
88     public void Test3_ExecutePut_critical_insert() throws MusicServiceException, MusicQueryException {
89         testObject = CassandraCQL.setPreparedInsertQueryObject2();
90         boolean result = dataStore.executePut(testObject, "Critical");
91         assertEquals(true, result);
92     }
93
94     @Test
95     public void Test4_ExecutePut_eventual_update() throws MusicServiceException, MusicQueryException {
96         testObject = CassandraCQL.setPreparedUpdateQueryObject();
97         boolean result = false;
98         result = dataStore.executePut(testObject, "eventual");
99         assertEquals(true, result);
100     }
101
102     @Test
103     public void Test5_ExecuteEventualGet() throws MusicServiceException, MusicQueryException {
104         testObject = new PreparedQueryObject();
105         testObject.appendQueryString(CassandraCQL.selectALL);
106         boolean result = false;
107         int count = 0;
108         ResultSet output = null;
109         output = dataStore.executeEventualGet(testObject);
110         System.out.println(output);
111         ;
112         for (Row row : output) {
113             count++;
114             System.out.println(row.toString());
115         }
116         if (count == 2) {
117             result = true;
118         }
119         assertEquals(true, result);
120     }
121
122     @Test
123     public void Test6_ExecuteCriticalGet() throws MusicServiceException, MusicQueryException {
124         testObject = CassandraCQL.setPreparedGetQuery();
125         boolean result = false;
126         int count = 0;
127         ResultSet output = null;
128         output = dataStore.executeCriticalGet(testObject);
129         System.out.println(output);
130         ;
131         for (Row row : output) {
132             count++;
133             System.out.println(row.toString());
134         }
135         if (count == 1) {
136             result = true;
137         }
138         assertEquals(true, result);
139     }
140
141     @Test(expected = NullPointerException.class)
142     public void Test7_exception() {
143         PreparedQueryObject queryObject = null;
144         try {
145             dataStore.executePut(queryObject, "critical");
146         } catch (MusicQueryException | MusicServiceException e) {
147             System.out.println(e.getMessage());
148         }
149     }
150     
151     @Test
152     public void Test8_columnDataType() {
153         DataType data = dataStore.returnColumnDataType("testCassa", "employees", "empName");
154         String datatype = data.toString();
155         assertEquals("text",datatype);
156     }
157     
158     @Test
159     public void Test8_columnMetdaData() {
160         TableMetadata data = dataStore.returnColumnMetadata("testCassa", "employees");
161         assertNotNull(data);
162     }
163 }