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