1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
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 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 * ============LICENSE_END=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.test.dmaap;
24 import java.io.InputStream;
25 import java.util.Properties;
26 import java.util.Scanner;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.client.Entity;
31 import javax.ws.rs.client.WebTarget;
32 import javax.ws.rs.core.Response;
34 import junit.framework.TestCase;
36 import org.apache.log4j.Logger;
37 import org.apache.http.HttpStatus;
38 import org.json.JSONObject;
40 import com.att.nsa.drumlin.till.data.sha1HmacSigner;
42 public class DmaapApiKeyTest {
44 private static final Logger LOGGER = Logger.getLogger(DmaapApiKeyTest.class);
45 Client client = ClientBuilder.newClient();
46 Properties prop = LoadPropertyFile.getPropertyFileData();
47 String url = prop.getProperty("url");
48 WebTarget target = client.target(url);
49 String date = prop.getProperty("date");
52 public JSONObject returnKey(ApiKeyBean apikeybean) {
53 LOGGER.info("Call to return newly created key");
54 target = client.target(url);
55 target = target.path("/apiKeys/create");
56 Response response = target.request().post(Entity.json(apikeybean));
57 assertStatus(response);
58 LOGGER.info("successfully created keys");
59 InputStream is = (InputStream) response.getEntity();
60 Scanner s = new Scanner(is);
61 s.useDelimiter("\\A");
62 JSONObject dataObj = new JSONObject(s.next());
64 LOGGER.info("key details :" + dataObj.toString());
69 public void testCreateKey() {
70 LOGGER.info("test case create key");
71 ApiKeyBean apiKeyBean = new ApiKeyBean("nm254w@att.com", "Creating Api Key.");
72 returnKey(apiKeyBean);
73 LOGGER.info("Successfully returned after creating key");
76 public void assertStatus(Response response) {
77 assertTrue(response.getStatus() == HttpStatus.SC_OK);
80 // 2. get Allkey details
81 public void testAllKey() {
82 LOGGER.info("test case get all key");
83 target = target.path("/apiKeys");
84 Response response = target.request().get();
85 assertStatus(response);
86 LOGGER.info("successfully returned after get all key");
87 InputStream is = (InputStream) response.getEntity();
88 Scanner s = new Scanner(is);
89 s.useDelimiter("\\A");
90 LOGGER.info("Details of key: " + s.next());
95 // 3. get specific key
96 public void testSpecificKey() {
97 LOGGER.info("test case get specific key");
99 ApiKeyBean apiKeyBean = new ApiKeyBean("ai039@att.com", "Creating Api Key.");
101 apiKey = (String) returnKey(apiKeyBean).get("key");
102 target = client.target(url);
103 target = target.path("/apiKeys/");
104 target = target.path(apiKey);
105 Response response = target.request().get();
106 assertStatus(response);
107 LOGGER.info("successfully returned after fetching specific key");
112 public void testUpdateKey() {
113 LOGGER.info("test case update key");
115 String secretKey = "";
116 final String serverCalculatedSignature;
117 final String X_CambriaAuth;
118 final String X_CambriaDate;
121 ApiKeyBean apiKeyBean = new ApiKeyBean("ai039@att.com", "Creating Api Key for update");
122 ApiKeyBean apiKeyBean1 = new ApiKeyBean("ai03911@att.com", "updating Api Key.");
123 jsonObj = returnKey(apiKeyBean);
124 apiKey = (String) jsonObj.get("key");
125 secretKey = (String) jsonObj.get("secret");
127 serverCalculatedSignature = sha1HmacSigner.sign(date, secretKey);
128 X_CambriaAuth = apiKey + ":" + serverCalculatedSignature;
129 X_CambriaDate = date;
130 target = client.target(url);
131 target = target.path("/apiKeys/" + apiKey);
132 Response response1 = target.request().header("X-CambriaAuth", X_CambriaAuth)
133 .header("X-CambriaDate", X_CambriaDate).put(Entity.json(apiKeyBean1));
134 assertStatus(response1);
135 LOGGER.info("successfully returned after updating key");
139 public void testDeleteKey() {
140 LOGGER.info("test case delete key");
142 String secretKey = "";
143 final String serverCalculatedSignature;
144 final String X_CambriaAuth;
145 final String X_CambriaDate;
147 ApiKeyBean apiKeyBean = new ApiKeyBean("ai039@att.com", "Creating Api Key.");
148 jsonObj = returnKey(apiKeyBean);
149 apiKey = (String) jsonObj.get("key");
150 secretKey = (String) jsonObj.get("secret");
151 serverCalculatedSignature = sha1HmacSigner.sign(date, secretKey);
152 X_CambriaAuth = apiKey + ":" + serverCalculatedSignature;
153 X_CambriaDate = date;
154 target = client.target(url);
155 target = target.path("/apiKeys/" + apiKey);
156 Response response2 = target.request().header("X-CambriaAuth", X_CambriaAuth)
157 .header("X-CambriaDate", X_CambriaDate).delete();
158 assertStatus(response2);
159 LOGGER.info("successfully returned after deleting key");