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