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