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