2 * ============LICENSE_START==========================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * ============LICENSE_END=============================================
20 * ====================================================================
22 package org.onap.music.e2eTests;
24 import java.io.BufferedReader;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.io.InputStreamReader;
28 import java.util.ArrayList;
29 import java.util.HashMap;
31 import javax.ws.rs.core.MediaType;
32 import org.onap.music.datastore.jsonobjects.JsonDelete;
33 import org.onap.music.datastore.jsonobjects.JsonInsert;
34 import org.onap.music.datastore.jsonobjects.JsonKeySpace;
35 import org.onap.music.datastore.jsonobjects.JsonTable;
36 import com.sun.jersey.api.client.Client;
37 import com.sun.jersey.api.client.ClientResponse;
38 import com.sun.jersey.api.client.WebResource;
39 import com.sun.jersey.api.client.config.ClientConfig;
40 import com.sun.jersey.api.client.config.DefaultClientConfig;
41 import com.sun.jersey.api.json.JSONConfiguration;
43 public class TestMusicE2E {
45 ArrayList<String> lockNames;
46 MusicConnector musicHandle;
48 public TestMusicE2E(String[] musicIps) {
49 lockNames = new ArrayList<String>();
50 musicHandle = new MusicConnector(musicIps);
54 public void createVotingKeyspace() {
55 keyspaceName = "VotingAppForMusic" + System.currentTimeMillis();
56 Map<String, Object> replicationInfo = new HashMap<String, Object>();
57 replicationInfo.put("class", "SimpleStrategy");
58 replicationInfo.put("replication_factor", 1);
59 String durabilityOfWrites = "false";
60 Map<String, String> consistencyInfo = new HashMap<String, String>();
61 consistencyInfo.put("type", "eventual");
62 JsonKeySpace jsonKp = new JsonKeySpace();
63 jsonKp.setConsistencyInfo(consistencyInfo);
64 jsonKp.setDurabilityOfWrites(durabilityOfWrites);
65 jsonKp.setReplicationInfo(replicationInfo);
67 ClientConfig clientConfig = new DefaultClientConfig();
69 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
71 Client client = Client.create(clientConfig);
73 WebResource webResource = client
74 .resource(musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName);
76 ClientResponse response = webResource.accept("application/json").type("application/json")
77 .post(ClientResponse.class, jsonKp);
79 if (response.getStatus() < 200 || response.getStatus() > 299)
80 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
84 public void createVotingTable() {
85 Map<String, String> fields = new HashMap<String, String>();
86 fields.put("name", "text");
87 fields.put("count", "varint");
88 fields.put("PRIMARY KEY", "(name)");
91 Map<String, String> consistencyInfo = new HashMap<String, String>();
92 consistencyInfo.put("type", "eventual");
94 JsonTable jtab = new JsonTable();
95 jtab.setFields(fields);
96 jtab.setConsistencyInfo(consistencyInfo);
98 ClientConfig clientConfig = new DefaultClientConfig();
100 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
102 Client client = Client.create(clientConfig);
103 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
104 + "/tables/votecount";
105 System.out.println("create url:" + url);
106 WebResource webResource = client.resource(url);
108 ClientResponse response = webResource.accept("application/json").type("application/json")
109 .post(ClientResponse.class, jtab);
111 if (response.getStatus() < 200 || response.getStatus() > 299)
112 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
116 private void checkMusicVersion() {
117 Client client = Client.create();
118 System.out.println(musicHandle.getMusicNodeURL() + "/version");
121 // System.setProperty("sun.net.http.allowRestrictedHeaders", "true");
122 WebResource webResource = client.resource(musicHandle.getMusicNodeURL() + "/version");
125 ClientResponse response = webResource.accept("text/plain").header("Connection", "close")
126 .get(ClientResponse.class);
128 if (response.getStatus() != 200) {
129 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
132 String output = response.getEntity(String.class);
134 // System.out.println("Output from Server .... \n");
135 System.out.println(output);
139 private void createEntryForCandidate(String candidateName) {
140 Map<String, Object> values = new HashMap<String, Object>();
141 values.put("name", candidateName);
142 values.put("count", 0);
144 Map<String, String> consistencyInfo = new HashMap<String, String>();
145 consistencyInfo.put("type", "eventual");
147 JsonInsert jIns = new JsonInsert();
148 jIns.setValues(values);
149 jIns.setConsistencyInfo(consistencyInfo);
150 ClientConfig clientConfig = new DefaultClientConfig();
152 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
154 Client client = Client.create(clientConfig);
156 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
157 + "/tables/votecount/rows";
158 WebResource webResource = client.resource(url);
160 ClientResponse response = webResource.accept("application/json").type("application/json")
161 .post(ClientResponse.class, jIns);
163 if (response.getStatus() < 200 || response.getStatus() > 299)
164 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus() + "url:"
165 + url + " candidate name:" + candidateName);
170 private String createLock(String lockName) {
171 Client client = Client.create();
172 String msg = musicHandle.getMusicNodeURL() + "/locks/create/" + lockName;
173 WebResource webResource = client.resource(msg);
174 System.out.println(msg);
175 WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
177 ClientResponse response = wb.post(ClientResponse.class);
179 if (response.getStatus() != 200) {
180 throw new RuntimeException(
181 "Failed : HTTP error code : " + response.getStatus() + "url:" + msg);
184 String output = response.getEntity(String.class);
186 // System.out.println("Server response .... \n");
187 // System.out.println(output);
191 private boolean acquireLock(String lockId) {
192 Client client = Client.create();
193 String msg = musicHandle.getMusicNodeURL() + "/locks/acquire/" + lockId;
194 System.out.println(msg);
195 WebResource webResource = client.resource(msg);
198 WebResource.Builder wb = webResource.accept(MediaType.TEXT_PLAIN);
200 ClientResponse response = wb.get(ClientResponse.class);
202 if (response.getStatus() != 200) {
203 throw new RuntimeException(
204 "Failed : HTTP error code : " + response.getStatus() + "url:" + msg);
207 String output = response.getEntity(String.class);
208 Boolean status = Boolean.parseBoolean(output);
209 System.out.println("Server response .... \n");
210 System.out.println(output);
214 private void unlock(String lockId) {
215 Client client = Client.create();
216 WebResource webResource =
217 client.resource(musicHandle.getMusicNodeURL() + "/locks/release/" + lockId);
219 ClientResponse response = webResource.delete(ClientResponse.class);
222 if (response.getStatus() != 204) {
223 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
227 private void updateVoteCountAtomically(String candidateName, int count) {
229 * create lock for the candidate. The music API dictates that the lock name must be of the
230 * form keyspacename.tableName.primaryKeyName
232 System.out.println("trying to acquire lock!");
234 String lockName = keyspaceName + ".votecount." + candidateName;
235 lockNames.add(lockName);
236 String lockId = createLock(lockName);
237 while (acquireLock(lockId) != true);
239 System.out.println("acquired lock!");
240 // update candidate entry if you have the lock
241 Map<String, Object> values = new HashMap<String, Object>();
242 values.put("count", count);
244 Map<String, String> consistencyInfo = new HashMap<String, String>();
245 consistencyInfo.put("type", "critical");
246 consistencyInfo.put("lockId", lockId);
248 JsonInsert jIns = new JsonInsert();
249 jIns.setValues(values);
250 jIns.setConsistencyInfo(consistencyInfo);
251 ClientConfig clientConfig = new DefaultClientConfig();
253 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
255 Client client = Client.create(clientConfig);
256 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
257 + "/tables/votecount/rows?name=" + candidateName;
258 System.out.println(url);
259 WebResource webResource = client.resource(url);
261 ClientResponse response = webResource.accept("application/json").type("application/json")
262 .put(ClientResponse.class, jIns);
264 if (response.getStatus() < 200 || response.getStatus() > 299)
265 throw new RuntimeException(
266 "Failed : HTTP error code : " + response.getStatus() + "url:" + url);
268 // release lock now that the operation is done
273 private void deleteCandidateEntryEventually(String candidateName) {
274 Map<String, String> consistencyInfo = new HashMap<String, String>();
275 consistencyInfo.put("type", "eventual");
277 JsonDelete jDel = new JsonDelete();
278 jDel.setConsistencyInfo(consistencyInfo);
279 ClientConfig clientConfig = new DefaultClientConfig();
281 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
283 Client client = Client.create(clientConfig);
284 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
285 + "/tables/votecount/rows?name=" + candidateName;
286 System.out.println(url);
287 WebResource webResource = client.resource(url);
289 ClientResponse response = webResource.accept("application/json").type("application/json")
290 .delete(ClientResponse.class, jDel);
292 if (response.getStatus() < 200 || response.getStatus() > 299)
293 throw new RuntimeException(
294 "Failed : HTTP error code : " + response.getStatus() + "url:" + url);
298 public Map<String, Object> readVoteCountForCandidate(String candidateName) {
299 ClientConfig clientConfig = new DefaultClientConfig();
301 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
303 Client client = Client.create(clientConfig);
304 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
305 + "/tables/votecount/rows?name=" + candidateName;
306 WebResource webResource = client.resource(url);
308 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
310 if (response.getStatus() < 200 || response.getStatus() > 299)
311 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
313 Map<String, Object> output = response.getEntity(Map.class);
317 public Map<String, Object> readAllVotes() {
318 ClientConfig clientConfig = new DefaultClientConfig();
320 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
322 Client client = Client.create(clientConfig);
323 String url = musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName
324 + "/tables/votecount/rows";
325 WebResource webResource = client.resource(url);
327 ClientResponse response = webResource.accept("application/json").get(ClientResponse.class);
329 if (response.getStatus() < 200 || response.getStatus() > 299)
330 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
332 Map<String, Object> output = response.getEntity(Map.class);
338 * Unable to use this because of the error: Exception in thread "main"
339 * com.sun.jersey.api.client.ClientHandlerException: java.net.ProtocolException: HTTP method
340 * DELETE doesn't support output. Seems to be a error in the rest java combination according to
343 private void dropKeySpace() {
344 Map<String, String> consistencyInfo = new HashMap<String, String>();
345 consistencyInfo.put("type", "eventual");
347 JsonKeySpace jsonKp = new JsonKeySpace();
348 jsonKp.setConsistencyInfo(consistencyInfo);
350 ClientConfig clientConfig = new DefaultClientConfig();
352 clientConfig.getFeatures().put(JSONConfiguration.FEATURE_POJO_MAPPING, Boolean.TRUE);
354 Client client = Client.create(clientConfig);
356 WebResource webResource = client
357 .resource(musicHandle.getMusicNodeURL() + "/keyspaces/" + keyspaceName);
359 ClientResponse response =
360 webResource.type("application/json").delete(ClientResponse.class, jsonKp);
362 if (response.getStatus() < 200 || response.getStatus() > 299)
363 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
366 private void deleteLock(String lockName) {
367 Client client = Client.create();
368 WebResource webResource = client
369 .resource(musicHandle.getMusicNodeURL() + "/locks/delete/" + lockName);
371 ClientResponse response = webResource.delete(ClientResponse.class);
374 if (response.getStatus() != 204) {
375 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
379 private void resetMusic() {
380 Client client = Client.create();
381 WebResource webResource = client.resource(musicHandle.getMusicNodeURL() + "/reset");
383 ClientResponse response = webResource.delete(ClientResponse.class);
386 if (response.getStatus() != 204) {
387 throw new RuntimeException("Failed : HTTP error code : " + response.getStatus());
392 public void deleteAllLocks() {
393 for (String lockName : lockNames) {
394 deleteLock(lockName);
399 public void bootStrap() {
401 createVotingKeyspace();
407 // the next few lines just create an entry in the voting table for all these candidates with
409 createEntryForCandidate("Popeye");
411 createEntryForCandidate("Judy");
413 createEntryForCandidate("Flash");
415 createEntryForCandidate("Mickey");
419 public void overAllTests() {
420 // update the count atomically
421 updateVoteCountAtomically("Popeye", 5);
423 updateVoteCountAtomically("Judy", 7);
425 updateVoteCountAtomically("Mickey", 8);
427 updateVoteCountAtomically("Flash", 2);
431 System.out.println(readAllVotes());
433 System.out.println(readVoteCountForCandidate("Popeye"));
435 System.out.println(readVoteCountForCandidate("Flash"));
437 deleteCandidateEntryEventually("Mickey");
439 System.out.println(readAllVotes());
446 public void flipTest() {
450 public static String executeBashScript(String pathToScript, String arg1, String arg2) {
452 ProcessBuilder pb = new ProcessBuilder(pathToScript, arg1, arg2);
453 final Process process = pb.start();
454 InputStream is = process.getInputStream();
455 InputStreamReader isr = new InputStreamReader(is);
456 BufferedReader br = new BufferedReader(isr);
457 return br.readLine();
458 } catch (IOException e) {
464 public static void main(String[] args) {
465 long start = System.currentTimeMillis();
466 for (int i = 0; i < 2; ++i) {
467 TestMusicE2E vHandle = new TestMusicE2E(args);
468 vHandle.overAllTests();
470 System.out.println("=====================================");
471 System.out.println("Test no." + i + " completed:");
473 long diff = System.currentTimeMillis() - start;
474 System.out.println(diff);