aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/music/unittests/TestMusicCoreIntegration.java
blob: d327d0f03c28ac47f157ed9c14e97b27aa4d4b5a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
 * ============LICENSE_START========================================== org.onap.music
 * =================================================================== Copyright (c) 2017 AT&T
 * Intellectual Property ===================================================================
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 * 
 * ============LICENSE_END=============================================
 * ====================================================================
 */
package org.onap.music.unittests;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.util.List;
import org.apache.curator.test.TestingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.onap.music.datastore.PreparedQueryObject;
import org.onap.music.exceptions.MusicQueryException;
import org.onap.music.exceptions.MusicServiceException;
import org.onap.music.lockingservice.MusicLockState;
import org.onap.music.lockingservice.MusicLockingService;
import org.onap.music.lockingservice.MusicLockState.LockStatus;
import org.onap.music.main.MusicCore;
import org.onap.music.main.MusicUtil;
import org.onap.music.main.ResultType;
import org.onap.music.main.ReturnType;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;

@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class TestMusicCoreIntegration {

    static TestingServer zkServer;
    static PreparedQueryObject testObject;
    static String lockId = null;
    static String lockName = "ks1.tb1.pk1";

    @BeforeClass
    public static void init() throws Exception {
        try {
            MusicCore.mDstoreHandle = CassandraCQL.connectToEmbeddedCassandra();
            zkServer = new TestingServer(2181, new File("/tmp/zk"));
            MusicCore.mLockHandle = new MusicLockingService();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("####Port:" + zkServer.getPort());
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("After class");
        testObject = new PreparedQueryObject();
        testObject.appendQueryString(CassandraCQL.dropKeyspace);
        MusicCore.eventualPut(testObject);
        MusicCore.deleteLock(lockName);
        MusicCore.mDstoreHandle.close();
        MusicCore.mLockHandle.getzkLockHandle().close();
        MusicCore.mLockHandle.close();
        zkServer.stop();

    }

    @Test
    public void Test1_SetUp() throws MusicServiceException, MusicQueryException {
        MusicCore.mLockHandle = new MusicLockingService();
        ResultType result = ResultType.FAILURE;
        testObject = new PreparedQueryObject();
        testObject.appendQueryString(CassandraCQL.createKeySpace);
        MusicCore.eventualPut(testObject);
        testObject = new PreparedQueryObject();
        testObject.appendQueryString(CassandraCQL.createTableEmployees);
        result = MusicCore.nonKeyRelatedPut(testObject, MusicUtil.EVENTUAL);
        assertEquals(ResultType.SUCCESS, result);
    }

    @Test
    public void Test2_atomicPut() throws Exception {
        testObject = new PreparedQueryObject();
        testObject = CassandraCQL.setPreparedInsertQueryObject1();
        ReturnType returnType = MusicCore.atomicPut("testCassa", "employees", "Mr Test one",
                        testObject, null);
        assertEquals(ResultType.SUCCESS, returnType.getResult());
    }

    @Test
    public void Test3_atomicPutWithDeleteLock() throws Exception {
        testObject = new PreparedQueryObject();
        testObject = CassandraCQL.setPreparedInsertQueryObject2();
        ReturnType returnType = MusicCore.atomicPutWithDeleteLock("testCassa", "employees",
                        "Mr Test two", testObject, null);
        assertEquals(ResultType.SUCCESS, returnType.getResult());
    }

    @Test
    public void Test4_atomicGetWithDeleteLock() throws Exception {
        testObject = new PreparedQueryObject();
        testObject = CassandraCQL.setPreparedGetQuery();
        ResultSet resultSet = MusicCore.atomicGetWithDeleteLock("testCassa", "employees",
                        "Mr Test one", testObject);
        List<Row> rows = resultSet.all();
        assertEquals(1, rows.size());
    }

    @Test
    public void Test5_atomicGet() throws Exception {
        testObject = new PreparedQueryObject();
        testObject = CassandraCQL.setPreparedGetQuery();
        ResultSet resultSet =
                        MusicCore.atomicGet("testCassa", "employees", "Mr Test two", testObject);
        List<Row> rows = resultSet.all();
        assertEquals(1, rows.size());
    }

    @Test
    public void Test6_createLockReference() throws Exception {
        lockId = MusicCore.createLockReference(lockName);
        assertNotNull(lockId);
    }

    @Test
    public void Test7_acquireLockwithLease() throws Exception {
        ReturnType lockLeaseStatus = MusicCore.acquireLockWithLease(lockName, lockId, 1000);
        assertEquals(ResultType.SUCCESS, lockLeaseStatus.getResult());
    }

    @Test
    public void Test8_acquireLock() throws Exception {
        ReturnType lockStatus = MusicCore.acquireLock(lockName, lockId);
        assertEquals(ResultType.SUCCESS, lockStatus.getResult());
    }

    @Test
    public void Test9_release() throws Exception {
        MusicLockState musicLockState = new MusicLockState(LockStatus.LOCKED, "id1");
        MusicLockState musicLockState1 = new MusicLockState(LockStatus.UNLOCKED, "id1");
        MusicCore.whoseTurnIsIt(lockName);
        MusicLockState mls = MusicCore.getMusicLockState(lockName);
        boolean voluntaryRelease = true;
        MusicLockState mls1 = MusicCore.releaseLock(lockId, voluntaryRelease);
        assertEquals(musicLockState.getLockStatus(), mls.getLockStatus());
        assertEquals(musicLockState1.getLockStatus(), mls1.getLockStatus());
    }

    @Test
    public void Test10_create() {
        MusicCore.pureZkCreate("/nodeName");
    }

    @Test
    public void Test11_write() {
        MusicCore.pureZkWrite("nodeName", "I'm Test".getBytes());
    }

    @Test
    public void Test12_read() {
        byte[] data = MusicCore.pureZkRead("nodeName");
        String data1 = new String(data);
        assertEquals("I'm Test", data1);
    }

}