2 * ============LICENSE_START========================================== org.onap.music
3 * =================================================================== Copyright (c) 2017 AT&T
4 * Intellectual Property ===================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
6 * in compliance with the License. You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software distributed under the License
11 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12 * or implied. See the License for the specific language governing permissions and limitations under
15 * ============LICENSE_END=============================================
16 * ====================================================================
19 package org.onap.music.unittests;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertNotNull;
23 import static org.junit.Assert.assertTrue;
25 import java.util.List;
26 import org.apache.curator.test.TestingServer;
27 import org.junit.AfterClass;
28 import org.junit.BeforeClass;
29 import org.junit.FixMethodOrder;
30 import org.junit.Test;
31 import org.junit.runners.MethodSorters;
32 import org.onap.music.datastore.MusicDataStoreHandle;
33 import org.onap.music.datastore.PreparedQueryObject;
34 import org.onap.music.exceptions.MusicQueryException;
35 import org.onap.music.exceptions.MusicServiceException;
36 import org.onap.music.lockingservice.cassandra.MusicLockState;
37 import org.onap.music.lockingservice.cassandra.MusicLockState.LockStatus;
38 import org.onap.music.lockingservice.zookeeper.MusicLockingService;
39 import org.onap.music.main.MusicUtil;
40 import org.onap.music.main.ResultType;
41 import org.onap.music.main.ReturnType;
42 import org.onap.music.service.impl.MusicZKCore;
44 import com.datastax.driver.core.ResultSet;
45 import com.datastax.driver.core.Row;
47 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
48 public class TestMusicCoreIntegration {
50 static TestingServer zkServer;
51 static PreparedQueryObject testObject;
52 static String lockId = null;
53 static String lockName = "ks1.tb1.pk1";
54 static MusicZKCore musicZkCore ;
57 public static void init() throws Exception {
59 MusicDataStoreHandle.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
60 musicZkCore = MusicZKCore.getInstance();
61 zkServer = new TestingServer(2181, new File("/tmp/zk"));
62 MusicZKCore.mLockHandle = new MusicLockingService();
63 } catch (Exception e) {
66 System.out.println("####Port:" + zkServer.getPort());
70 public static void tearDownAfterClass() throws Exception {
71 System.out.println("After class");
72 testObject = new PreparedQueryObject();
73 testObject.appendQueryString(CassandraCQL.dropKeyspace);
74 musicZkCore.eventualPut(testObject);
75 musicZkCore.deleteLock(lockName);
76 MusicDataStoreHandle.mDstoreHandle.close();
77 MusicZKCore.mLockHandle.getzkLockHandle().close();
78 MusicZKCore.mLockHandle.close();
84 public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
85 MusicZKCore.mLockHandle = new MusicLockingService();
86 ResultType result = ResultType.FAILURE;
87 testObject = new PreparedQueryObject();
88 testObject.appendQueryString(CassandraCQL.createKeySpace);
89 musicZkCore.eventualPut(testObject);
90 testObject = new PreparedQueryObject();
91 testObject.appendQueryString(CassandraCQL.createTableEmployees);
92 result = musicZkCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
93 assertEquals(ResultType.SUCCESS, result);
97 public void Test2_atomicPut() throws Exception {
98 testObject = new PreparedQueryObject();
99 testObject = CassandraCQL.setPreparedInsertQueryObject1();
100 ReturnType returnType = musicZkCore.atomicPut("testCassa", "employees", "Mr Test one",
102 assertEquals(ResultType.SUCCESS, returnType.getResult());
106 public void Test3_atomicPutWithDeleteLock() throws Exception {
107 testObject = new PreparedQueryObject();
108 testObject = CassandraCQL.setPreparedInsertQueryObject2();
109 ReturnType returnType = musicZkCore.atomicPutWithDeleteLock("testCassa", "employees",
110 "Mr Test two", testObject, null);
111 assertEquals(ResultType.SUCCESS, returnType.getResult());
115 public void Test4_atomicGetWithDeleteLock() throws Exception {
116 testObject = new PreparedQueryObject();
117 testObject = CassandraCQL.setPreparedGetQuery();
118 ResultSet resultSet = musicZkCore.atomicGetWithDeleteLock("testCassa", "employees",
119 "Mr Test one", testObject);
120 List<Row> rows = resultSet.all();
121 assertEquals(1, rows.size());
125 public void Test5_atomicGet() throws Exception {
126 testObject = new PreparedQueryObject();
127 testObject = CassandraCQL.setPreparedGetQuery();
128 ResultSet resultSet =
129 musicZkCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
130 List<Row> rows = resultSet.all();
131 assertEquals(1, rows.size());
135 public void Test6_createLockReference() throws Exception {
136 lockId = musicZkCore.createLockReference(lockName);
137 assertNotNull(lockId);
141 public void Test7_acquireLockwithLease() throws Exception {
142 ReturnType lockLeaseStatus = musicZkCore.acquireLockWithLease(lockName, lockId, 1000);
143 assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
147 public void Test8_acquireLock() throws Exception {
148 ReturnType lockStatus = musicZkCore.acquireLock(lockName, lockId);
149 assertEquals(ResultType.SUCCESS, lockStatus.getResult());
153 public void Test9_release() throws Exception {
154 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
155 MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
156 musicZkCore.whoseTurnIsIt(lockName);
157 MusicLockState mls = MusicZKCore.getMusicLockState(lockName);
158 boolean voluntaryRelease = true;
159 MusicLockState mls1 = musicZkCore.releaseLock(lockId, voluntaryRelease);
160 assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
161 assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
165 public void Test10_create() {
166 MusicZKCore.pureZkCreate("/nodeName");
170 public void Test11_write() {
171 MusicZKCore.pureZkWrite("nodeName", "I'm Test".getBytes());
175 public void Test12_read() {
176 byte[] data = MusicZKCore.pureZkRead("nodeName");
177 String data1 = new String(data);
178 assertEquals("I'm Test", data1);