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 * ====================================================================
22 package org.onap.music.unittests;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
29 import java.util.List;
31 import org.apache.curator.test.TestingServer;
32 import org.junit.AfterClass;
33 import org.junit.BeforeClass;
34 import org.junit.FixMethodOrder;
35 import org.junit.Test;
36 import org.junit.runners.MethodSorters;
37 import org.onap.music.datastore.PreparedQueryObject;
38 import org.onap.music.exceptions.MusicQueryException;
39 import org.onap.music.exceptions.MusicServiceException;
40 import org.onap.music.lockingservice.MusicLockState;
41 import org.onap.music.lockingservice.MusicLockingService;
42 import org.onap.music.lockingservice.MusicLockState.LockStatus;
43 import org.onap.music.main.MusicCore;
44 import org.onap.music.main.MusicUtil;
45 import org.onap.music.main.ResultType;
46 import org.onap.music.main.ReturnType;
48 import com.datastax.driver.core.ResultSet;
49 import com.datastax.driver.core.Row;
51 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
52 public class TestMusicCoreIntegration {
54 static TestingServer zkServer;
55 static PreparedQueryObject testObject;
56 static String lockId = null;
57 static String lockName = "ks1.tb1.pk1";
60 public static void init() throws Exception {
62 MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
63 zkServer = new TestingServer(2181, new File("/tmp/zk"));
64 MusicCore.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 MusicCore.eventualPut(testObject);
77 MusicCore.deleteLock(lockName);
78 MusicCore.mDstoreHandle.close();
79 MusicCore.mLockHandle.getzkLockHandle().close();
80 MusicCore.mLockHandle.close();
86 public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
87 MusicCore.mLockHandle = new MusicLockingService();
88 boolean result = false;
89 testObject = new PreparedQueryObject();
90 testObject.appendQueryString(CassandraCQL.createKeySpace);
91 MusicCore.eventualPut(testObject);
92 testObject = new PreparedQueryObject();
93 testObject.appendQueryString(CassandraCQL.createTableEmployees);
94 result = MusicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
99 public void Test2_atomicPut() throws Exception {
100 testObject = new PreparedQueryObject();
101 testObject = CassandraCQL.setPreparedInsertQueryObject1();
102 ReturnType returnType = MusicCore.atomicPut("testCassa", "employees", "Mr Test one", testObject, null);
103 assertEquals(ResultType.SUCCESS, returnType.getResult());
107 public void Test3_atomicPutWithDeleteLock() throws Exception {
108 testObject = new PreparedQueryObject();
109 testObject = CassandraCQL.setPreparedInsertQueryObject2();
110 ReturnType returnType = MusicCore.atomicPutWithDeleteLock("testCassa", "employees", "Mr Test two", testObject,
112 assertEquals(ResultType.SUCCESS, returnType.getResult());
116 public void Test4_atomicGetWithDeleteLock() throws Exception {
117 testObject = new PreparedQueryObject();
118 testObject = CassandraCQL.setPreparedGetQuery();
119 ResultSet resultSet = MusicCore.atomicGetWithDeleteLock("testCassa", "employees", "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 = MusicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
129 List<Row> rows = resultSet.all();
130 assertEquals(1, rows.size());
134 public void Test6_createLockReference() throws Exception {
135 lockId = MusicCore.createLockReference(lockName);
136 assertNotNull(lockId);
140 public void Test7_acquireLockwithLease() throws Exception {
141 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000);
142 assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
146 public void Test8_acquireLock() throws Exception {
147 ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId);
148 assertEquals(ResultType.SUCCESS, lockStatus.getResult());
152 public void Test9_release() throws Exception {
153 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
154 MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
155 MusicCore.whoseTurnIsIt(lockName);
156 MusicLockState mls = MusicCore.getMusicLockState(lockName);
157 boolean voluntaryRelease = true;
158 MusicLockState mls1 = MusicCore.releaseLock(lockId, voluntaryRelease);
159 assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
160 assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
164 public void Test10_create() {
165 MusicCore.pureZkCreate("/nodeName");
169 public void Test11_write() {
170 MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes());
174 public void Test12_read() {
175 byte[] data = MusicCore.pureZkRead("nodeName");
176 String data1 = new String(data);
177 assertEquals("I'm Test", data1);