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.PreparedQueryObject;
33 import org.onap.music.exceptions.MusicQueryException;
34 import org.onap.music.exceptions.MusicServiceException;
35 import org.onap.music.lockingservice.MusicLockState;
36 import org.onap.music.lockingservice.MusicLockingService;
37 import org.onap.music.lockingservice.MusicLockState.LockStatus;
38 import org.onap.music.main.MusicCore;
39 import org.onap.music.main.MusicUtil;
40 import org.onap.music.main.ResultType;
41 import org.onap.music.main.ReturnType;
42 import com.datastax.driver.core.ResultSet;
43 import com.datastax.driver.core.Row;
45 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
46 public class TestMusicCoreIntegration {
48 static TestingServer zkServer;
49 static PreparedQueryObject testObject;
50 static String lockId = null;
51 static String lockName = "ks1.tb1.pk1";
54 public static void init() throws Exception {
56 MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
57 zkServer = new TestingServer(2181, new File("/tmp/zk"));
58 MusicCore.mLockHandle = new MusicLockingService();
59 } catch (Exception e) {
62 System.out.println("####Port:" + zkServer.getPort());
66 public static void tearDownAfterClass() throws Exception {
67 System.out.println("After class");
68 testObject = new PreparedQueryObject();
69 testObject.appendQueryString(CassandraCQL.dropKeyspace);
70 MusicCore.eventualPut(testObject);
71 MusicCore.deleteLock(lockName);
72 MusicCore.mDstoreHandle.close();
73 MusicCore.mLockHandle.getzkLockHandle().close();
74 MusicCore.mLockHandle.close();
80 public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
81 MusicCore.mLockHandle = new MusicLockingService();
82 ResultType result = ResultType.FAILURE;
83 testObject = new PreparedQueryObject();
84 testObject.appendQueryString(CassandraCQL.createKeySpace);
85 MusicCore.eventualPut(testObject);
86 testObject = new PreparedQueryObject();
87 testObject.appendQueryString(CassandraCQL.createTableEmployees);
88 result = MusicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
89 assertEquals(ResultType.SUCCESS, result);
93 public void Test2_atomicPut() throws Exception {
94 testObject = new PreparedQueryObject();
95 testObject = CassandraCQL.setPreparedInsertQueryObject1();
96 ReturnType returnType = MusicCore.atomicPut("testCassa", "employees", "Mr Test one",
98 assertEquals(ResultType.SUCCESS, returnType.getResult());
102 public void Test3_atomicPutWithDeleteLock() throws Exception {
103 testObject = new PreparedQueryObject();
104 testObject = CassandraCQL.setPreparedInsertQueryObject2();
105 ReturnType returnType = MusicCore.atomicPutWithDeleteLock("testCassa", "employees",
106 "Mr Test two", testObject, null);
107 assertEquals(ResultType.SUCCESS, returnType.getResult());
111 public void Test4_atomicGetWithDeleteLock() throws Exception {
112 testObject = new PreparedQueryObject();
113 testObject = CassandraCQL.setPreparedGetQuery();
114 ResultSet resultSet = MusicCore.atomicGetWithDeleteLock("testCassa", "employees",
115 "Mr Test one", testObject);
116 List<Row> rows = resultSet.all();
117 assertEquals(1, rows.size());
121 public void Test5_atomicGet() throws Exception {
122 testObject = new PreparedQueryObject();
123 testObject = CassandraCQL.setPreparedGetQuery();
124 ResultSet resultSet =
125 MusicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
126 List<Row> rows = resultSet.all();
127 assertEquals(1, rows.size());
131 public void Test6_createLockReference() throws Exception {
132 lockId = MusicCore.createLockReference(lockName);
133 assertNotNull(lockId);
137 public void Test7_acquireLockwithLease() throws Exception {
138 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000);
139 assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
143 public void Test8_acquireLock() throws Exception {
144 ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId);
145 assertEquals(ResultType.SUCCESS, lockStatus.getResult());
149 public void Test9_release() throws Exception {
150 MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
151 MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
152 MusicCore.whoseTurnIsIt(lockName);
153 MusicLockState mls = MusicCore.getMusicLockState(lockName);
154 boolean voluntaryRelease = true;
155 MusicLockState mls1 = MusicCore.releaseLock(lockId, voluntaryRelease);
156 assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
157 assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
161 public void Test10_create() {
162 MusicCore.pureZkCreate("/nodeName");
166 public void Test11_write() {
167 MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes());
171 public void Test12_read() {
172 byte[] data = MusicCore.pureZkRead("nodeName");
173 String data1 = new String(data);
174 assertEquals("I'm Test", data1);