aboutsummaryrefslogtreecommitdiffstats
path: root/music-core/src/test/java/org/onap/music/cassandra/MusicUtilTest.java
blob: b7245d78ee77ce14355091ebb759d12d5420415f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
 * ============LICENSE_START==========================================
 * org.onap.music
 * ===================================================================
 *  Copyright (c) 2017 AT&T Intellectual Property
 *  Modifications Copyright (C) 2019 IBM.
 * ===================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 * ============LICENSE_END=============================================
 * ====================================================================
 */

package org.onap.music.cassandra;

import static org.junit.Assert.*;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.junit.Test;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.main.MusicUtil;
//import org.onap.music.main.CorePropertiesLoader;
import org.onap.music.service.MusicCoreService;

import com.datastax.driver.core.ConsistencyLevel;
import com.datastax.driver.core.DataType;

public class MusicUtilTest {

    private static final String XLATESTVERSION = "X-latestVersion";
    private static final String XMINORVERSION = "X-minorVersion";
    private static final String XPATCHVERSION = "X-patchVersion";

    @Test
    public void testGetCassName() {
        MusicUtil.setCassName("Cassandra");
        assertTrue(MusicUtil.getCassName().equals("Cassandra"));
    }

    @Test
    public void testGetCassPwd() {
        MusicUtil.setCassPwd("Cassandra");
        assertTrue(MusicUtil.getCassPwd().equals("Cassandra"));
    }

    @Test
    public void testMusicAafNs() {
        MusicUtil.setMusicAafNs("ns");
        assertTrue("ns".equals(MusicUtil.getMusicAafNs()));
    }

    @Test
    public void testMusicCoreService() {
        MusicUtil.setLockUsing(MusicUtil.CASSANDRA);
        MusicCoreService mc = null;
        mc = MusicUtil.getMusicCoreService();
        assertTrue(mc != null);        
        MusicUtil.setLockUsing("nothing");
        mc = null;
        mc = MusicUtil.getMusicCoreService();
        assertTrue(mc != null);        

    }

    @Test
    public void testCipherEncKey() {
        MusicUtil.setCipherEncKey("cipherEncKey");
        assertTrue("cipherEncKey".equals(MusicUtil.getCipherEncKey()));        
    }

    @Test
    public void testGetMusicPropertiesFilePath() {
        MusicUtil.setMusicPropertiesFilePath("filepath");
        assertEquals(MusicUtil.getMusicPropertiesFilePath(),"filepath");
    }

    @Test
    public void testGetDefaultLockLeasePeriod() {
        MusicUtil.setDefaultLockLeasePeriod(5000);
        assertEquals(MusicUtil.getDefaultLockLeasePeriod(),5000);
    }

    @Test
    public void testIsDebug() {
        MusicUtil.setDebug(true);
        assertTrue(MusicUtil.isDebug());
    }

    @Test
    public void testGetVersion() {
        MusicUtil.setVersion("1.0.0");
        assertEquals(MusicUtil.getVersion(),"1.0.0");
    }

    @Test 
    public void testBuildVersionA() {
        assertEquals(MusicUtil.buildVersion("1","2","3"),"1.2.3");
    }

    @Test 
    public void testBuildVersionB() {
        assertEquals(MusicUtil.buildVersion("1",null,"3"),"1");
    }

    @Test 
    public void testBuildVersionC() {
        assertEquals(MusicUtil.buildVersion("1","2",null),"1.2");
    }

    /*
    @Test
    public void testBuileVersionResponse() {
        assertTrue(MusicUtil.buildVersionResponse("1","2","3").getClass().getSimpleName().equals("Builder"));
        assertTrue(MusicUtil.buildVersionResponse("1",null,"3").getClass().getSimpleName().equals("Builder"));
        assertTrue(MusicUtil.buildVersionResponse("1","2",null).getClass().getSimpleName().equals("Builder"));
        assertTrue(MusicUtil.buildVersionResponse(null,null,null).getClass().getSimpleName().equals("Builder"));
    }
     */
    @Test
    public void testGetConsistency() {
        assertTrue(ConsistencyLevel.ONE.equals(MusicUtil.getConsistencyLevel("one")));
    }

    @Test
    public void testRetryCount() {
        MusicUtil.setRetryCount(1);
        assertEquals(MusicUtil.getRetryCount(),1);
    }

    @Test
    public void testIsCadi() {
        MusicUtil.setIsCadi(true);
        assertEquals(MusicUtil.getIsCadi(),true);
    }


    @Test
    public void testGetMyCassaHost() {
        MusicUtil.setMyCassaHost("10.0.0.2");
        assertEquals(MusicUtil.getMyCassaHost(),"10.0.0.2");
    }

    @Test
    public void testIsValidQueryObject() {
        PreparedQueryObject myQueryObject = new PreparedQueryObject();
        myQueryObject.appendQueryString("select * from apple where type = ?");
        myQueryObject.addValue("macintosh");
        assertTrue(MusicUtil.isValidQueryObject(true,myQueryObject));

        myQueryObject.appendQueryString("select * from apple");
        assertTrue(MusicUtil.isValidQueryObject(false,myQueryObject));

        myQueryObject.appendQueryString("select * from apple where type = ?");
        assertFalse(MusicUtil.isValidQueryObject(true,myQueryObject));

        myQueryObject = new PreparedQueryObject();
        myQueryObject.appendQueryString("");
        System.out.println("#######" + myQueryObject.getQuery().isEmpty());
        assertFalse(MusicUtil.isValidQueryObject(false,myQueryObject));


    }




    @Test(expected = IllegalStateException.class)
    public void testMusicUtil() {
        System.out.println("MusicUtil Constructor Test");
        MusicUtil mu = new MusicUtil();
        System.out.println(mu.toString());
    }

    @Test
    public void testConvertToCQLDataType() throws Exception {
        Map<String,Object> myMap = new HashMap<String,Object>();
        myMap.put("name","tom");
        assertEquals(MusicUtil.convertToCQLDataType(DataType.varchar(),"Happy People"),"'Happy People'");
        assertEquals(MusicUtil.convertToCQLDataType(DataType.uuid(),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab")),"29dc2afa-c2c0-47ae-afae-e72a645308ab");
        assertEquals(MusicUtil.convertToCQLDataType(DataType.blob(),"Hi"),"Hi");
        assertEquals(MusicUtil.convertToCQLDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),"{'name':'tom'}");
    }

    @Test
    public void testConvertToActualDataType() throws Exception {
        assertEquals(MusicUtil.convertToActualDataType(DataType.varchar(),"Happy People"),"Happy People");
        assertEquals(MusicUtil.convertToActualDataType(DataType.uuid(),"29dc2afa-c2c0-47ae-afae-e72a645308ab"),UUID.fromString("29dc2afa-c2c0-47ae-afae-e72a645308ab"));
        assertEquals(MusicUtil.convertToActualDataType(DataType.varint(),"1234"),BigInteger.valueOf(Long.parseLong("1234")));
        assertEquals(MusicUtil.convertToActualDataType(DataType.bigint(),"123"),Long.parseLong("123"));
        assertEquals(MusicUtil.convertToActualDataType(DataType.cint(),"123"),Integer.parseInt("123"));
        assertEquals(MusicUtil.convertToActualDataType(DataType.cfloat(),"123.01"),Float.parseFloat("123.01"));
        assertEquals(MusicUtil.convertToActualDataType(DataType.cdouble(),"123.02"),Double.parseDouble("123.02"));
        assertEquals(MusicUtil.convertToActualDataType(DataType.cboolean(),"true"),Boolean.parseBoolean("true"));
        List<String> myList = new ArrayList<String>();
        List<String> newList = myList;
        myList.add("TOM");
        assertEquals(MusicUtil.convertToActualDataType(DataType.list(DataType.varchar()),myList),newList);
        Map<String,Object> myMap = new HashMap<String,Object>();
        myMap.put("name","tom");
        Map<String,Object> newMap = myMap;
        assertEquals(MusicUtil.convertToActualDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),newMap);
    }

    @Test
    public void testConvertToActualDataTypeByte() throws Exception {
        byte[] testByte = "TOM".getBytes();
        assertEquals(MusicUtil.convertToActualDataType(DataType.blob(),testByte),ByteBuffer.wrap(testByte));

    }

    @Test
    public void testJsonMaptoSqlString() throws Exception {
        Map<String,Object> myMap = new HashMap<>();
        myMap.put("name","tom");
        myMap.put("value",5);
        String result = MusicUtil.jsonMaptoSqlString(myMap,",");
        assertTrue(result.contains("name"));
        assertTrue(result.contains("value"));
    }

    @Test
    public void test_generateUUID() {
        //this function shouldn't be in cachingUtil
        System.out.println("Testing getUUID");
        String uuid1 = MusicUtil.generateUUID();
        String uuid2 = MusicUtil.generateUUID();
        assertFalse(uuid1==uuid2);
    }


    @Test
    public void testIsValidConsistency(){
        assertTrue(MusicUtil.isValidConsistency("ALL"));
        assertFalse(MusicUtil.isValidConsistency("TEST"));
    }

    @Test
    public void testLockUsing() {
        MusicUtil.setLockUsing("testlock");
        assertEquals("testlock", MusicUtil.getLockUsing());
    }

    @Test
    public void testCassaPort() {
        MusicUtil.setCassandraPort(1234);
        assertEquals(1234, MusicUtil.getCassandraPort());
    }

    @Test
    public void testBuild() {
        MusicUtil.setBuild("testbuild");
        assertEquals("testbuild", MusicUtil.getBuild());
    }

    @Test
    public void testTransId() {
        MusicUtil.setTransIdPrefix("prefix");
        assertEquals("prefix-", MusicUtil.getTransIdPrefix());
    }


    @Test
    public void testConversationIdPrefix() {
        MusicUtil.setConversationIdPrefix("prefix-");
        assertEquals("prefix-", MusicUtil.getConversationIdPrefix());
    }

    @Test
    public void testClientIdPrefix() {
        MusicUtil.setClientIdPrefix("clientIdPrefix");
        assertEquals("clientIdPrefix-", MusicUtil.getClientIdPrefix());
    }

    @Test
    public void testMessageIdPrefix() {
        MusicUtil.setMessageIdPrefix("clientIdPrefix");
        assertEquals("clientIdPrefix-", MusicUtil.getMessageIdPrefix());
    }

    @Test
    public void testTransIdPrefix() {
        MusicUtil.setTransIdPrefix("transIdPrefix");
        assertEquals("transIdPrefix-", MusicUtil.getTransIdPrefix());
    }

    @Test
    public void testConvIdReq() {
        MusicUtil.setConversationIdRequired(true);
        assertEquals(true, MusicUtil.getConversationIdRequired());
    }

    @Test
    public void testClientIdRequired() {
        MusicUtil.setClientIdRequired(true);
        assertEquals(true, MusicUtil.getClientIdRequired());
    }

    @Test
    public void testMessageIdRequired() {
        MusicUtil.setMessageIdRequired(true);
        assertEquals(true, MusicUtil.getMessageIdRequired());
    }

    @Test
    public void testTransIdRequired() {
        MusicUtil.setTransIdRequired(true);
        assertEquals(true,MusicUtil.getTransIdRequired());
    }

    @Test
    public void testGetCassandraConnectTimeOutMS() {
        MusicUtil.setCassandraConnectTimeOutMS(2000);
        assertEquals(2000,MusicUtil.getCassandraConnectTimeOutMS());
    }

    @Test
    public void testGetCassandraReadTimeOutMS() {
        MusicUtil.setCassandraReadTimeOutMS(2000);
        assertEquals(2000,MusicUtil.getCassandraReadTimeOutMS());
    }

    /*
    @Test
    public void testLoadProperties() {
        PropertiesLoader pl = new PropertiesLoader();
        pl.loadProperties();
    }
     */
}