823badaa0975592042c9c9dc09aff01f7c95aba9
[music.git] / src / test / java / org / onap / music / unittests / TestMusicCoreIntegration.java
1 /*
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
7  * 
8  * http://www.apache.org/licenses/LICENSE-2.0
9  * 
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
13  * the License.
14  * 
15  * ============LICENSE_END=============================================
16  * ====================================================================
17  */
18 package org.onap.music.unittests;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertTrue;
23 import java.io.File;
24 import java.util.List;
25 import org.apache.curator.test.TestingServer;
26 import org.junit.AfterClass;
27 import org.junit.BeforeClass;
28 import org.junit.FixMethodOrder;
29 import org.junit.Test;
30 import org.junit.runners.MethodSorters;
31 import org.onap.music.datastore.PreparedQueryObject;
32 import org.onap.music.exceptions.MusicQueryException;
33 import org.onap.music.exceptions.MusicServiceException;
34 import org.onap.music.lockingservice.MusicLockState;
35 import org.onap.music.lockingservice.MusicLockingService;
36 import org.onap.music.lockingservice.MusicLockState.LockStatus;
37 import org.onap.music.main.MusicCore;
38 import org.onap.music.main.MusicUtil;
39 import org.onap.music.main.ResultType;
40 import org.onap.music.main.ReturnType;
41 import com.datastax.driver.core.ResultSet;
42 import com.datastax.driver.core.Row;
43
44 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
45 public class TestMusicCoreIntegration {
46
47     static TestingServer zkServer;
48     static PreparedQueryObject testObject;
49     static String lockId = null;
50     static String lockName = "ks1.tb1.pk1";
51
52     @BeforeClass
53     public static void init() throws Exception {
54         try {
55             MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
56             zkServer = new TestingServer(2181, new File("/tmp/zk"));
57             MusicCore.mLockHandle = new MusicLockingService();
58         } catch (Exception e) {
59             e.printStackTrace();
60         }
61         System.out.println("####Port:" + zkServer.getPort());
62     }
63
64     @AfterClass
65     public static void tearDownAfterClass() throws Exception {
66         System.out.println("After class");
67         testObject = new PreparedQueryObject();
68         testObject.appendQueryString(CassandraCQL.dropKeyspace);
69         MusicCore.eventualPut(testObject);
70         MusicCore.deleteLock(lockName);
71         MusicCore.mDstoreHandle.close();
72         MusicCore.mLockHandle.getzkLockHandle().close();
73         MusicCore.mLockHandle.close();
74         zkServer.stop();
75
76     }
77
78     @Test
79     public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
80         MusicCore.mLockHandle = new MusicLockingService();
81         boolean result = false;
82         testObject = new PreparedQueryObject();
83         testObject.appendQueryString(CassandraCQL.createKeySpace);
84         MusicCore.eventualPut(testObject);
85         testObject = new PreparedQueryObject();
86         testObject.appendQueryString(CassandraCQL.createTableEmployees);
87         result = MusicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
88         assertTrue(result);
89     }
90
91     @Test
92     public void Test2_atomicPut() throws Exception {
93         testObject = new PreparedQueryObject();
94         testObject = CassandraCQL.setPreparedInsertQueryObject1();
95         ReturnType returnType = MusicCore.atomicPut("testCassa", "employees", "Mr Test one",
96                         testObject, null);
97         assertEquals(ResultType.SUCCESS, returnType.getResult());
98     }
99
100     @Test
101     public void Test3_atomicPutWithDeleteLock() throws Exception {
102         testObject = new PreparedQueryObject();
103         testObject = CassandraCQL.setPreparedInsertQueryObject2();
104         ReturnType returnType = MusicCore.atomicPutWithDeleteLock("testCassa", "employees",
105                         "Mr Test two", testObject, null);
106         assertEquals(ResultType.SUCCESS, returnType.getResult());
107     }
108
109     @Test
110     public void Test4_atomicGetWithDeleteLock() throws Exception {
111         testObject = new PreparedQueryObject();
112         testObject = CassandraCQL.setPreparedGetQuery();
113         ResultSet resultSet = MusicCore.atomicGetWithDeleteLock("testCassa", "employees",
114                         "Mr Test one", testObject);
115         List<Row> rows = resultSet.all();
116         assertEquals(1, rows.size());
117     }
118
119     @Test
120     public void Test5_atomicGet() throws Exception {
121         testObject = new PreparedQueryObject();
122         testObject = CassandraCQL.setPreparedGetQuery();
123         ResultSet resultSet =
124                         MusicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
125         List<Row> rows = resultSet.all();
126         assertEquals(1, rows.size());
127     }
128
129     @Test
130     public void Test6_createLockReference() throws Exception {
131         lockId = MusicCore.createLockReference(lockName);
132         assertNotNull(lockId);
133     }
134
135     @Test
136     public void Test7_acquireLockwithLease() throws Exception {
137         ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000);
138         assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
139     }
140
141     @Test
142     public void Test8_acquireLock() throws Exception {
143         ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId);
144         assertEquals(ResultType.SUCCESS, lockStatus.getResult());
145     }
146
147     @Test
148     public void Test9_release() throws Exception {
149         MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
150         MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
151         MusicCore.whoseTurnIsIt(lockName);
152         MusicLockState mls = MusicCore.getMusicLockState(lockName);
153         boolean voluntaryRelease = true;
154         MusicLockState mls1 = MusicCore.releaseLock(lockId, voluntaryRelease);
155         assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
156         assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
157     }
158
159     @Test
160     public void Test10_create() {
161         MusicCore.pureZkCreate("/nodeName");
162     }
163
164     @Test
165     public void Test11_write() {
166         MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes());
167     }
168
169     @Test
170     public void Test12_read() {
171         byte[] data = MusicCore.pureZkRead("nodeName");
172         String data1 = new String(data);
173         assertEquals("I'm Test", data1);
174     }
175
176 }