2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
23 package org.onap.music.unittests;
25 import static org.junit.Assert.*;
26 import java.math.BigInteger;
27 import java.util.ArrayList;
28 import java.util.HashMap;
29 import java.util.List;
31 import java.util.UUID;
32 import org.apache.cassandra.exceptions.PreparedQueryNotFoundException;
33 import org.junit.Test;
34 import org.onap.music.datastore.PreparedQueryObject;
35 import org.onap.music.exceptions.MusicServiceException;
36 import org.onap.music.main.MusicUtil;
37 import com.datastax.driver.core.DataType;
38 import javassist.CodeConverter.ArrayAccessReplacementMethodNames;
40 public class MusicUtilTest {
43 public void testGetCassName() {
44 MusicUtil.setCassName("Cassandra");
45 assertTrue(MusicUtil.getCassName().equals("Cassandra"));
49 public void testGetCassPwd() {
50 MusicUtil.setCassPwd("Cassandra");
51 assertTrue(MusicUtil.getCassPwd().equals("Cassandra"));
55 public void testGetAafEndpointUrl() {
56 MusicUtil.setAafEndpointUrl("url");
57 assertEquals(MusicUtil.getAafEndpointUrl(),"url");
61 public void testGetMyId() {
63 assertEquals(MusicUtil.getMyId(),1);
67 public void testGetAllIds() {
68 List<String> ids = new ArrayList<String>();
72 MusicUtil.setAllIds(ids);
73 assertEquals(MusicUtil.getAllIds().get(0),"1");
77 public void testGetPublicIp() {
78 MusicUtil.setPublicIp("10.0.0.1");
79 assertEquals(MusicUtil.getPublicIp(),"10.0.0.1");
83 public void testGetAllPublicIps() {
84 List<String> ips = new ArrayList<String>();
88 MusicUtil.setAllPublicIps(ips);
89 assertEquals(MusicUtil.getAllPublicIps().get(1),"10.0.0.2");
93 public void testGetPropkeys() {
94 assertEquals(MusicUtil.getPropkeys()[2],"music.ip");
98 public void testGetMusicRestIp() {
99 MusicUtil.setMusicRestIp("localhost");
100 assertEquals(MusicUtil.getMusicRestIp(),"localhost");
104 public void testGetMusicPropertiesFilePath() {
105 MusicUtil.setMusicPropertiesFilePath("filepath");
106 assertEquals(MusicUtil.getMusicPropertiesFilePath(),"filepath");
110 public void testGetDefaultLockLeasePeriod() {
111 MusicUtil.setDefaultLockLeasePeriod(5000);
112 assertEquals(MusicUtil.getDefaultLockLeasePeriod(),5000);
116 public void testIsDebug() {
117 MusicUtil.setDebug(true);
118 assertTrue(MusicUtil.isDebug());
122 public void testGetVersion() {
123 MusicUtil.setVersion("1.0.0");
124 assertEquals(MusicUtil.getVersion(),"1.0.0");
128 public void testGetMyZkHost() {
129 MusicUtil.setMyZkHost("10.0.0.2");
130 assertEquals(MusicUtil.getMyZkHost(),"10.0.0.2");
134 public void testGetMyCassaHost() {
135 MusicUtil.setMyCassaHost("10.0.0.2");
136 assertEquals(MusicUtil.getMyCassaHost(),"10.0.0.2");
140 public void testGetDefaultMusicIp() {
141 MusicUtil.setDefaultMusicIp("10.0.0.2");
142 assertEquals(MusicUtil.getDefaultMusicIp(),"10.0.0.2");
146 // public void testGetTestType() {
147 // fail("Not yet implemented"); // TODO
151 public void testIsValidQueryObject() {
152 PreparedQueryObject myQueryObject = new PreparedQueryObject();
153 myQueryObject.appendQueryString("select * from apple where type = ?");
154 myQueryObject.addValue("macintosh");
155 assertTrue(MusicUtil.isValidQueryObject(true,myQueryObject));
157 myQueryObject.appendQueryString("select * from apple");
158 assertTrue(MusicUtil.isValidQueryObject(false,myQueryObject));
160 myQueryObject.appendQueryString("select * from apple where type = ?");
161 assertFalse(MusicUtil.isValidQueryObject(true,myQueryObject));
163 myQueryObject = new PreparedQueryObject();
164 myQueryObject.appendQueryString("");
165 System.out.println("#######" + myQueryObject.getQuery().isEmpty());
166 assertFalse(MusicUtil.isValidQueryObject(false,myQueryObject));
172 public void testConvertToCQLDataType() {
173 Map<String,Object> myMap = new HashMap<String,Object>();
174 myMap.put("name","tom");
175 assertEquals(MusicUtil.convertToCQLDataType(DataType.varchar(),"Happy People"),"'Happy People'");
176 assertEquals(MusicUtil.convertToCQLDataType(DataType.uuid(),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab")),"29dc2afa-c2c0-47ae-afae-e72a645308ab");
177 assertEquals(MusicUtil.convertToCQLDataType(DataType.blob(),"Hi"),"Hi");
178 assertEquals(MusicUtil.convertToCQLDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),"{'name':'tom'}");
182 public void testConvertToActualDataType() throws Exception {
183 assertEquals(MusicUtil.convertToActualDataType(DataType.varchar(),"Happy People"),"Happy People");
184 assertEquals(MusicUtil.convertToActualDataType(DataType.uuid(),"29dc2afa-c2c0-47ae-afae-e72a645308ab"),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab"));
185 assertEquals(MusicUtil.convertToActualDataType(DataType.varint(),"1234"),BigInteger.valueOf(Long.parseLong("1234")));
186 assertEquals(MusicUtil.convertToActualDataType(DataType.bigint(),"123"),Long.parseLong("123"));
187 assertEquals(MusicUtil.convertToActualDataType(DataType.cint(),"123"),Integer.parseInt("123"));
188 assertEquals(MusicUtil.convertToActualDataType(DataType.cfloat(),"123.01"),Float.parseFloat("123.01"));
189 assertEquals(MusicUtil.convertToActualDataType(DataType.cdouble(),"123.02"),Double.parseDouble("123.02"));
190 assertEquals(MusicUtil.convertToActualDataType(DataType.cboolean(),"true"),Boolean.parseBoolean("true"));
191 Map<String,Object> myMap = new HashMap<String,Object>();
192 myMap.put("name","tom");
193 assertEquals(MusicUtil.convertToActualDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),myMap);
198 public void testJsonMaptoSqlString() {
199 Map<String,Object> myMap = new HashMap<>();
200 myMap.put("name","tom");
201 myMap.put("value",5);
202 String result = MusicUtil.jsonMaptoSqlString(myMap,",");
203 assertTrue(result.contains("name"));
204 assertTrue(result.contains("value"));
208 public void testSetErrorResponse() {
209 Map<String,Object> myMap = new HashMap<>();
210 myMap.put("name","tom");
211 Map<String,HashMap<String,Object>> result = MusicUtil.setErrorResponse(new MusicServiceException("This is the error"));
212 Map<String,Object> testMap = (Map<String,Object>) result.get("result");
213 Map<String,Object> error = (Map<String,Object>) testMap.get("error");
214 assertTrue(error.containsKey("error-description"));