2 * ============LICENSE_START========================================== org.onap.music
3 * ===================================================================
4 * Copyright (c) 2017 AT&T Intellectual Property
5 * Modifications Copyright (c) 2019 IBM
6 * ===================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software distributed under the License
13 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
14 * or implied. See the License for the specific language governing permissions and limitations under
17 * ============LICENSE_END=============================================
18 * ====================================================================
21 package org.onap.music.unittests;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertTrue;
27 import java.util.List;
28 import org.apache.curator.test.TestingServer;
29 import org.junit.AfterClass;
30 import org.junit.BeforeClass;
31 import org.junit.FixMethodOrder;
32 import org.junit.Test;
33 import org.junit.runners.MethodSorters;
34 import org.onap.music.datastore.MusicDataStoreHandle;
35 import org.onap.music.datastore.PreparedQueryObject;
36 import org.onap.music.exceptions.MusicQueryException;
37 import org.onap.music.exceptions.MusicServiceException;
38 import org.onap.music.lockingservice.cassandra.MusicLockState;
39 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
40 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
41 import org.onap.music.main.MusicUtil;
42 import org.onap.music.main.ResultType;
43 import org.onap.music.main.ReturnType;
44 import org.onap.music.service.impl.MusicZKCore;
46 import com.datastax.driver.core.ResultSet;
47 import com.datastax.driver.core.Row;
49 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
50 public class TestMusicCoreIntegration {
52 static TestingServer zkServer;
53 static PreparedQueryObject testObject;
54 static String lockId = null;
55 static String lockName = "ks1.tb1.pk1";
56 static MusicZKCore musicZkCore ;
59 public static void init() throws Exception {
61 MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
62 musicZkCore = MusicZKCore.getInstance();
63 zkServer = new TestingServer(2181, new File("/tmp/zk"));
64 MusicZKCore.mLockHandle = new MusicLockingService();
65 } catch (Exception e) {
68 System.out.println("####Port:" + zkServer.getPort());
72 public static void tearDownAfterClass() throws Exception {
73 System.out.println("After class");
74 testObject = new PreparedQueryObject();
75 testObject.appendQueryString(CassandraCQL.dropKeyspace);
76 musicZkCore.eventualPut(testObject);
77 musicZkCore.deleteLock(lockName);
78 MusicDataStoreHandle.mDstoreHandle.close();
79 MusicZKCore.mLockHandle.getzkLockHandle().close();
80 MusicZKCore.mLockHandle.close();
86 public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
87 MusicZKCore.mLockHandle = new MusicLockingService();
88 ResultType result = ResultType.FAILURE;
89 testObject = new PreparedQueryObject();
90 testObject.appendQueryString(CassandraCQL.createKeySpace);
91 musicZkCore.eventualPut(testObject);
92 testObject = new PreparedQueryObject();
93 testObject.appendQueryString(CassandraCQL.createTableEmployees);
94 result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
95 assertEquals(ResultType.SUCCESS, result);
99 public void Test2_atomicPut() throws Exception {
100 testObject = new PreparedQueryObject();
101 testObject = CassandraCQL.setPreparedInsertQueryObject1();
102 ReturnType returnType = musicZkCore.atomicPut("testCassa", "employees", "Mr Test one",
104 assertEquals(ResultType.SUCCESS, returnType.getResult());
108 public void Test3_atomicPutWithDeleteLock() throws Exception {
109 testObject = new PreparedQueryObject();
110 testObject = CassandraCQL.setPreparedInsertQueryObject2();
111 ReturnType returnType = musicZkCore.atomicPutWithDeleteLock("testCassa", "employees",
112 "Mr Test two", testObject, null);
113 assertEquals(ResultType.SUCCESS, returnType.getResult());
117 public void Test4_atomicGetWithDeleteLock() throws Exception {
118 testObject = new PreparedQueryObject();
119 testObject = CassandraCQL.setPreparedGetQuery();
120 ResultSet resultSet = musicZkCore.atomicGetWithDeleteLock("testCassa", "employees",
121 "Mr Test one", testObject);
122 List<Row> rows = resultSet.all();
123 assertEquals(1, rows.size());
127 public void Test5_atomicGet() throws Exception {
128 testObject = new PreparedQueryObject();
129 testObject = CassandraCQL.setPreparedGetQuery();
130 ResultSet resultSet =
131 musicZkCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
132 List<Row> rows = resultSet.all();
133 assertEquals(1, rows.size());
137 public void Test6_createLockReference() throws Exception {
138 lockId = musicZkCore.createLockReference(lockName);
139 assertNotNull(lockId);
143 public void Test7_acquireLockwithLease() throws Exception {
144 ReturnType lockLeaseStatus = musicZkCore.acquireLockWithLease(lockName, lockId, 1000);
145 assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
149 public void Test8_acquireLock() throws Exception {
150 ReturnType lockStatus = musicZkCore.acquireLock(lockName, lockId);
151 assertEquals(ResultType.SUCCESS, lockStatus.getResult());
155 public void Test9_release() throws Exception {
156 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
157 MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
158 musicZkCore.whoseTurnIsIt(lockName);
159 MusicLockState mls = MusicZKCore.getMusicLockState(lockName);
160 boolean voluntaryRelease = true;
161 MusicLockState mls1 = musicZkCore.releaseLock(lockId, voluntaryRelease);
162 assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
163 assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
167 public void Test10_create() {
168 MusicZKCore.pureZkCreate("/nodeName");
172 public void Test11_write() {
173 MusicZKCore.pureZkWrite("nodeName", "I'm Test".getBytes());
177 public void Test12_read() {
178 byte[] data = MusicZKCore.pureZkRead("nodeName");
179 String data1 = new String(data);
180 assertEquals("I'm Test", data1);
183 public void Test13_ParameterizedConstructorCall() throws MusicServiceException, MusicQueryException {
184 MusicZKCore.mLockHandle = new MusicLockingService("localhost");
185 ResultType result = ResultType.FAILURE;
186 testObject = new PreparedQueryObject();
187 testObject.appendQueryString(CassandraCQL.createKeySpace);
188 musicZkCore.eventualPut(testObject);
189 testObject = new PreparedQueryObject();
190 testObject.appendQueryString(CassandraCQL.createTableEmployees);
191 result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
192 assertEquals(ResultType.SUCCESS, result);