CADI and a few small updates.
[music.git] / src / test / java / org / onap / music / unittests / MusicUtilTest.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
4  * ===================================================================
5  *  Copyright (c) 2017 AT&T Intellectual Property
6  *  Modifications Copyright (C) 2019 IBM.
7  * ===================================================================
8  *  Licensed under the Apache License, Version 2.0 (the "License");
9  *  you may not use this file except in compliance with the License.
10  *  You may obtain a copy of the License at
11  * 
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  * 
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  * 
20  * ============LICENSE_END=============================================
21  * ====================================================================
22  */
23
24 package org.onap.music.unittests;
25
26 import static org.junit.Assert.*;
27 import java.math.BigInteger;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import java.util.UUID;
33 import org.junit.Test;
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.main.MusicUtil;
36 import com.datastax.driver.core.DataType;
37
38 public class MusicUtilTest {
39
40     @Test
41     public void testGetCassName() {
42         MusicUtil.setCassName("Cassandra");
43         assertTrue(MusicUtil.getCassName().equals("Cassandra"));
44     }
45
46     @Test
47     public void testGetCassPwd() {
48         MusicUtil.setCassPwd("Cassandra");
49         assertTrue(MusicUtil.getCassPwd().equals("Cassandra"));
50     }
51
52     @Test
53     public void testGetAafEndpointUrl() {
54         MusicUtil.setAafEndpointUrl("url");
55         assertEquals(MusicUtil.getAafEndpointUrl(),"url");
56     }
57
58     @Test
59     public void testGetPropkeys() {
60         assertEquals(MusicUtil.getPropkeys()[2],"debug");
61     }
62
63     @Test
64     public void testGetMusicPropertiesFilePath() {
65         MusicUtil.setMusicPropertiesFilePath("filepath");
66         assertEquals(MusicUtil.getMusicPropertiesFilePath(),"filepath");
67     }
68
69     @Test
70     public void testGetDefaultLockLeasePeriod() {
71         MusicUtil.setDefaultLockLeasePeriod(5000);
72         assertEquals(MusicUtil.getDefaultLockLeasePeriod(),5000);
73     }
74
75     @Test
76     public void testIsDebug() {
77         MusicUtil.setDebug(true);
78         assertTrue(MusicUtil.isDebug());
79     }
80
81     @Test
82     public void testGetVersion() {
83         MusicUtil.setVersion("1.0.0");
84         assertEquals(MusicUtil.getVersion(),"1.0.0");
85     }
86
87     /*@Test
88     public void testGetMyZkHost() {
89         MusicUtil.setMyZkHost("10.0.0.2");
90         assertEquals(MusicUtil.getMyZkHost(),"10.0.0.2");
91     }*/
92
93     @Test
94     public void testGetMyCassaHost() {
95         MusicUtil.setMyCassaHost("10.0.0.2");
96         assertEquals(MusicUtil.getMyCassaHost(),"10.0.0.2");
97     }
98
99     @Test
100     public void testGetDefaultMusicIp() {
101         MusicUtil.setDefaultMusicIp("10.0.0.2");
102         assertEquals(MusicUtil.getDefaultMusicIp(),"10.0.0.2");
103     }
104
105 //    @Test
106 //    public void testGetTestType() {
107 //      fail("Not yet implemented"); // TODO
108 //    }
109
110     @Test
111     public void testIsValidQueryObject() {
112         PreparedQueryObject myQueryObject = new PreparedQueryObject();
113         myQueryObject.appendQueryString("select * from apple where type = ?");
114         myQueryObject.addValue("macintosh");
115         assertTrue(MusicUtil.isValidQueryObject(true,myQueryObject));
116
117         myQueryObject.appendQueryString("select * from apple");
118         assertTrue(MusicUtil.isValidQueryObject(false,myQueryObject));
119
120         myQueryObject.appendQueryString("select * from apple where type = ?");
121         assertFalse(MusicUtil.isValidQueryObject(true,myQueryObject));
122
123         myQueryObject = new PreparedQueryObject();
124         myQueryObject.appendQueryString("");
125         System.out.println("#######" + myQueryObject.getQuery().isEmpty());
126         assertFalse(MusicUtil.isValidQueryObject(false,myQueryObject));
127
128     
129     }
130
131     @Test
132     public void testConvertToCQLDataType() throws Exception {
133         Map<String,Object> myMap = new HashMap<String,Object>();
134         myMap.put("name","tom");
135         assertEquals(MusicUtil.convertToCQLDataType(DataType.varchar(),"Happy People"),"'Happy People'");
136         assertEquals(MusicUtil.convertToCQLDataType(DataType.uuid(),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab")),"29dc2afa-c2c0-47ae-afae-e72a645308ab");
137         assertEquals(MusicUtil.convertToCQLDataType(DataType.blob(),"Hi"),"Hi");
138         assertEquals(MusicUtil.convertToCQLDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),"{'name':'tom'}");
139     }
140
141     @Test
142     public void testConvertToActualDataType() throws Exception {
143         assertEquals(MusicUtil.convertToActualDataType(DataType.varchar(),"Happy People"),"Happy People");
144         assertEquals(MusicUtil.convertToActualDataType(DataType.uuid(),"29dc2afa-c2c0-47ae-afae-e72a645308ab"),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab"));
145         assertEquals(MusicUtil.convertToActualDataType(DataType.varint(),"1234"),BigInteger.valueOf(Long.parseLong("1234")));
146         assertEquals(MusicUtil.convertToActualDataType(DataType.bigint(),"123"),Long.parseLong("123"));
147         assertEquals(MusicUtil.convertToActualDataType(DataType.cint(),"123"),Integer.parseInt("123"));
148         assertEquals(MusicUtil.convertToActualDataType(DataType.cfloat(),"123.01"),Float.parseFloat("123.01"));
149         assertEquals(MusicUtil.convertToActualDataType(DataType.cdouble(),"123.02"),Double.parseDouble("123.02"));
150         assertEquals(MusicUtil.convertToActualDataType(DataType.cboolean(),"true"),Boolean.parseBoolean("true"));
151         Map<String,Object> myMap = new HashMap<String,Object>();
152         myMap.put("name","tom");
153         assertEquals(MusicUtil.convertToActualDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),myMap);
154
155     }
156
157     @Test
158     public void testJsonMaptoSqlString() throws Exception {
159         Map<String,Object> myMap = new HashMap<>();
160         myMap.put("name","tom");
161         myMap.put("value",5);
162         String result = MusicUtil.jsonMaptoSqlString(myMap,",");
163         assertTrue(result.contains("name"));
164         assertTrue(result.contains("value"));
165     }
166     
167     @Test
168     public void test_generateUUID() {
169         //this function shouldn't be in cachingUtil
170         System.out.println("Testing getUUID");
171         String uuid1 = MusicUtil.generateUUID();
172         String uuid2 = MusicUtil.generateUUID();
173         assertFalse(uuid1==uuid2);
174     }
175
176
177     @Test
178     public void testIsValidConsistency(){
179         assertTrue(MusicUtil.isValidConsistency("ALL"));
180         assertFalse(MusicUtil.isValidConsistency("TEST"));
181     }
182
183 }