aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/music/unittests/MusicUtilTest.java
blob: 930959baedca05abcd45ad9068a2a16a61a63fea (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
/*
 * ============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.unittests;

import static org.junit.Assert.*;
import java.math.BigInteger;
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.PropertiesLoader;
import org.springframework.test.context.TestPropertySource;
import com.datastax.driver.core.DataType;

public class MusicUtilTest {

    @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 testGetAafEndpointUrl() {
        MusicUtil.setAafEndpointUrl("url");
        assertEquals(MusicUtil.getAafEndpointUrl(),"url");
    }

    @Test
    public void testGetPropkeys() {
        assertEquals(MusicUtil.getPropkeys()[2],"debug");
    }

    @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 testGetMyZkHost() {
        MusicUtil.setMyZkHost("10.0.0.2");
        assertEquals(MusicUtil.getMyZkHost(),"10.0.0.2");
    }*/

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

    @Test
    public void testGetDefaultMusicIp() {
        MusicUtil.setDefaultMusicIp("10.0.0.2");
        assertEquals(MusicUtil.getDefaultMusicIp(),"10.0.0.2");
    }

//    @Test
//    public void testGetTestType() {
//      fail("Not yet implemented"); // TODO
//    }

    @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
    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"));
        Map<String,Object> myMap = new HashMap<String,Object>();
        myMap.put("name","tom");
        assertEquals(MusicUtil.convertToActualDataType(DataType.map(DataType.varchar(),DataType.varchar()),myMap),myMap);

    }

    @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 testAAFAdminUrl() {
        MusicUtil.setAafAdminUrl("aafAdminURL.com");
        assertEquals("aafAdminURL.com", MusicUtil.getAafAdminUrl());
    }
    
    @Test
    public void testAAFEndpointUrl() {
        MusicUtil.setAafEndpointUrl("aafEndpointURL.com");
        assertEquals("aafEndpointURL.com", MusicUtil.getAafEndpointUrl());
    }
    
    @Test
    public void testNamespace() {
        MusicUtil.setMusicNamespace("musicNamespace");
        assertEquals("musicNamespace", MusicUtil.getMusicNamespace());
    }
    
    @Test
    public void testAAFRole() {
        MusicUtil.setAdminAafRole("aafRole");
        assertEquals("aafRole", MusicUtil.getAdminAafRole());
    }
    
    @Test
    public void testAdminId() {
        MusicUtil.setAdminId("adminId");
        assertEquals("adminId", MusicUtil.getAdminId());
    }
    
    @Test
    public void testAdminPass() {
        MusicUtil.setAdminPass("pass");
        assertEquals("pass", MusicUtil.getAdminPass());
    }
    
    @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 testNotifyInterval() {
        MusicUtil.setNotifyInterval(123);
        assertEquals(123, MusicUtil.getNotifyInterval());
    }
    
    @Test
    public void testNotifyTimeout() {
        MusicUtil.setNotifyTimeOut(789);
        assertEquals(789, MusicUtil.getNotifyTimeout());
    }
    
    @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("conversationIdRequired");
        assertEquals("conversationIdRequired", MusicUtil.getConversationIdRequired());
    }
    
    @Test
    public void testClientIdRequired() {
        MusicUtil.setClientIdRequired("conversationIdRequired");
        assertEquals("conversationIdRequired", MusicUtil.getClientIdRequired());
    }
    
    @Test
    public void testMessageIdRequired() {
        MusicUtil.setMessageIdRequired("msgIdRequired");
        assertEquals("msgIdRequired", MusicUtil.getMessageIdRequired());
    }
    
    @Test
    public void testLoadProperties() {
        PropertiesLoader pl = new PropertiesLoader();
        pl.loadProperties();
    }
    
}