Sonar and CLM fixes and Unit test
[music.git] / src / test / java / org / onap / music / unittests / MusicLockingServiceTest.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
23 package org.onap.music.unittests;
24
25 import static org.junit.Assert.*;
26 import java.io.File;
27 import org.apache.curator.test.TestingServer;
28 import org.junit.After;
29 import org.junit.AfterClass;
30 import org.junit.Before;
31 import org.junit.BeforeClass;
32 import org.junit.Test;
33 import org.onap.music.exceptions.MusicLockingException;
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.ZkStatelessLockService;
38 import org.onap.music.lockingservice.MusicLockState.LockStatus;
39
40 public class MusicLockingServiceTest {
41     
42     static MusicLockingService mLockHandle;
43     static TestingServer zkServer;
44     
45     @BeforeClass
46     public static void init() throws Exception {
47         try {
48             zkServer = new TestingServer(2181,new File("/tmp/zk"));
49
50         } catch (Exception e) {
51             e.printStackTrace();
52         }
53         System.out.println("####" + zkServer.getPort());
54         try {
55             mLockHandle = new MusicLockingService();
56         } catch (MusicServiceException e) {
57             e.printStackTrace();
58         }
59         
60     
61     }
62
63     @AfterClass
64     public static void tearDownAfterClass() throws Exception {
65         zkServer.stop();
66         mLockHandle.close();
67     }
68
69     @Before
70     public void setUp() throws Exception {}
71
72     @After
73     public void tearDown() throws Exception {
74     }
75
76     @Test
77     public void testMusicLockingService() {
78         assertTrue(mLockHandle != null);
79     }
80
81     @Test
82     public void testGetzkLockHandle() {
83         ZkStatelessLockService lockHandle = mLockHandle.getzkLockHandle();
84         assertTrue(lockHandle != null);
85     }
86
87     @Test
88     public void testMusicLockingServiceString() {
89        // MusicLockingService mLockTest = new MusicLockingService("localhost");
90        // assertTrue(mLockTest != null);
91        // mLockTest.close();
92     }
93
94     @Test
95     public void testCreateLockaIfItDoesNotExist() {
96         
97         mLockHandle.createLockaIfItDoesNotExist("/ks1.tb1.pk1");
98         MusicLockState mls = null;
99         try {
100            // mls = mLockHandle.
101             mls = mLockHandle.getLockState("ks1.tb1.pk1");
102         } catch (MusicLockingException e) {
103             e.printStackTrace();
104         }
105         System.out.println("Lock Holder:" + mls.getLockHolder());
106         assertFalse(mls.getLeaseStartTime() > 0);
107     }
108     
109     @Test
110     public void testSetLockState() {
111         MusicLockState musicLockState = new MusicLockState(LockStatus.UNLOCKED, "id1");
112         mLockHandle.setLockState("ks1.tb1.pk1",musicLockState);
113         MusicLockState mls = null;
114         try {
115              mls = mLockHandle.getLockState("ks1.tb1.pk1");
116         } catch (MusicLockingException e) {
117              e.printStackTrace();
118         }
119         assertEquals(musicLockState.getLockHolder(), mls.getLockHolder());
120
121     }
122
123 //    @Test
124 //   public void testGetLockState() {
125 //        MusicLockState mls = null;
126 //        try {
127 //             mls = mLockHandle.getLockState("ks1.tb1.pk1");
128 //        } catch (MusicLockingException e) {
129 //             e.printStackTrace();
130 //        }
131 //        assertTrue(mls.getLockHolder().equals("id1"));
132 //    }
133
134 //    @Test
135 //    public void testCreateLockId() {
136 //        
137 //        fail("Not yet implemented"); // TODO
138 //    }
139 //
140 //    @Test
141 //    public void testIsMyTurn() {
142 //        fail("Not yet implemented"); // TODO
143 //    }
144 //
145 //    @Test
146 //    public void testUnlockAndDeleteId() {
147 //        fail("Not yet implemented"); // TODO
148 //    }
149 //
150 //    @Test
151 //    public void testDeleteLock() {
152 //        fail("Not yet implemented"); // TODO
153 //    }
154 //
155 //    @Test
156 //    public void testWhoseTurnIsIt() {
157 //        fail("Not yet implemented"); // TODO
158 //    }
159 //
160 //    @Test
161 //    public void testProcess() {
162 //        fail("Not yet implemented"); // TODO
163 //    }
164 //
165 //    @Test
166 //    public void testClose() {
167 //        fail("Not yet implemented"); // TODO
168 //    }
169 //
170 //    @Test
171 //    public void testLockIdExists() {
172 //        fail("Not yet implemented"); // TODO
173 //    }
174
175 }