Sonar Fixes to increate Coverage on Unit tests
[music.git] / src / test / java / org / onap / music / unittests / TestMusicCoreIntegration.java
1 /*
2  * ============LICENSE_START==========================================
3  * org.onap.music
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
10  * 
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  * 
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.
18  * 
19  * ============LICENSE_END=============================================
20  * ====================================================================
21  */
22 package org.onap.music.unittests;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.util.List;
30
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;
47
48 import com.datastax.driver.core.ResultSet;
49 import com.datastax.driver.core.Row;
50
51 @FixMethodOrder(MethodSorters.NAME_ASCENDING)
52 public class TestMusicCoreIntegration {
53
54         static TestingServer zkServer;
55         static PreparedQueryObject testObject;
56         static String lockId = null;
57         static String lockName = "ks1.tb1.pk1";
58
59         @BeforeClass
60         public static void init() throws Exception {
61                 try {
62                         MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
63                         zkServer = new TestingServer(2181, new File("/tmp/zk"));
64                         MusicCore.mLockHandle = new MusicLockingService();
65                 } catch (Exception e) {
66                         e.printStackTrace();
67                 }
68                 System.out.println("####Port:" + zkServer.getPort());
69         }
70
71         @AfterClass
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();
81                 zkServer.stop();
82
83         }
84
85         @Test
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);
95                 assertTrue(result);
96         }
97
98         @Test
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());
104         }
105
106         @Test
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,
111                                 null);
112                 assertEquals(ResultType.SUCCESS, returnType.getResult());
113         }
114
115         @Test
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());
122         }
123
124         @Test
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());
131         }
132
133         @Test
134         public void Test6_createLockReference() throws Exception {
135                 lockId = MusicCore.createLockReference(lockName);
136                 assertNotNull(lockId);
137         }
138
139         @Test
140         public void Test7_acquireLockwithLease() throws Exception {
141                 ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000);
142                 assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
143         }
144
145         @Test
146         public void Test8_acquireLock() throws Exception {
147                 ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId);
148                 assertEquals(ResultType.SUCCESS, lockStatus.getResult());
149         }
150
151         @Test
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());
161         }
162
163         @Test
164         public void Test10_create() {
165                 MusicCore.pureZkCreate("/nodeName");
166         }
167
168         @Test
169         public void Test11_write() {
170                 MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes());
171         }
172
173         @Test
174         public void Test12_read() {
175                 byte[] data = MusicCore.pureZkRead("nodeName");
176                 String data1 = new String(data);
177                 assertEquals("I'm Test", data1);
178         }
179
180 }