ec1a63ab2de97ee2e8d086d1693b562c7e0bf00b
[dmaap/messagerouter/msgrtr.git] / src / test / java / org / onap / dmaap / mr / test / dmaap / DMaapTopicTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.test.dmaap;
23
24 import java.io.InputStream;
25 import java.util.Properties;
26 import java.util.Scanner;
27
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;
33
34 import junit.framework.TestCase;
35
36 import org.apache.http.HttpStatus;
37 import org.json.JSONObject;
38 import org.apache.log4j.Logger;
39
40 import com.att.nsa.drumlin.till.data.sha1HmacSigner;
41
42 public class DMaapTopicTest {
43         /*private static final Logger LOGGER = Logger.getLogger(DMaapTopicTest.class);
44         Client client = ClientBuilder.newClient();
45         String topicapikey, topicsecretKey, serverCalculatedSignature;
46         Properties prop = LoadPropertyFile.getPropertyFileData();
47         String topicName = prop.getProperty("topicName");
48         String url = prop.getProperty("url");
49         String date = prop.getProperty("date");
50         WebTarget target = client.target(url);
51         DmaapApiKeyTest keyInstance = new DmaapApiKeyTest();
52
53
54         public void createTopic(String name) {
55                 if (!topicExist(name)) {
56                         TopicBean topicbean = new TopicBean();
57                         topicbean.setDescription("creating topic");
58                         topicbean.setPartitionCount(1);
59                         topicbean.setReplicationCount(1);
60                         topicbean.setTopicName(name);
61                         topicbean.setTransactionEnabled(true);
62                         target = client.target(url);
63                         target = target.path("/topics/create");
64                         JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("nm254w@att.com", "topic creation"));
65                         topicapikey = (String) jsonObj.get("key");
66                         topicsecretKey = (String) jsonObj.get("secret");
67                         serverCalculatedSignature = sha1HmacSigner.sign(date, topicsecretKey);
68                         Response response = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
69                                         .header("X-CambriaDate", date).post(Entity.json(topicbean));
70                         keyInstance.assertStatus(response);
71                 }
72
73         }
74
75         public boolean topicExist(String topicName) {
76                 target = target.path("/topics/" + topicName);
77                 InputStream is, issecret;
78                 Response response = target.request().get();
79                 if (response.getStatus() == HttpStatus.SC_OK) {
80                         is = (InputStream) response.getEntity();
81                         Scanner s = new Scanner(is);
82                         s.useDelimiter("\\A");
83                         JSONObject dataObj = new JSONObject(s.next());
84                         s.close();
85                         // get owner of a topic
86                         topicapikey = (String) dataObj.get("owner");
87                         target = client.target(url);
88                         target = target.path("/apiKeys/");
89                         target = target.path(topicapikey);
90                         Response response2 = target.request().get();
91                         issecret = (InputStream) response2.getEntity();
92                         Scanner st = new Scanner(issecret);
93                         st.useDelimiter("\\A");
94                         JSONObject dataObj1 = new JSONObject(st.next());
95                         st.close();
96                         // get secret key of this topic//
97                         topicsecretKey = (String) dataObj1.get("secret");
98                         serverCalculatedSignature = sha1HmacSigner.sign(date, topicsecretKey);
99                         return true;
100                 } else
101                         return false;
102         }
103
104         public void testCreateTopic() {
105                 LOGGER.info("test case create topic");
106                 createTopic(topicName);
107                 LOGGER.info("Returning after create topic");
108         }
109
110         public void testOneTopic() {
111                 LOGGER.info("test case get specific topic name " + topicName);
112                 createTopic(topicName);
113                 target = client.target(url);
114                 target = target.path("/topics/");
115                 target = target.path(topicName);
116                 Response response = target.request().get();
117                 LOGGER.info("Successfully returned after fetching topic" + topicName);
118                 keyInstance.assertStatus(response);
119                 InputStream is = (InputStream) response.getEntity();
120                 Scanner s = new Scanner(is);
121                 s.useDelimiter("\\A");
122                 JSONObject dataObj = new JSONObject(s.next());
123                 LOGGER.info("Details of " + topicName + " : " + dataObj.toString());
124                 s.close();
125         }
126
127         public void testdeleteTopic() {
128                 LOGGER.info("test case delete topic name " + topicName);
129                 createTopic(topicName);
130                 target = client.target(url);
131                 target = target.path("/topics/");
132                 target = target.path(topicName);
133                 Response response = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
134                                 .header("X-CambriaDate", date).delete();
135                 keyInstance.assertStatus(response);
136                 LOGGER.info("Successfully returned after deleting topic" + topicName);
137         }
138
139         public void testAllTopic() {
140                 LOGGER.info("test case fetch all topic");
141                 target = client.target(url);
142                 target = target.path("/topics");
143                 Response response = target.request().get();
144                 keyInstance.assertStatus(response);
145                 LOGGER.info("successfully returned after fetching all the topic");
146                 InputStream is = (InputStream) response.getEntity();
147                 Scanner s = new Scanner(is);
148                 s.useDelimiter("\\A");
149                 JSONObject dataObj = new JSONObject(s.next());
150                 s.close();
151                 LOGGER.info("List of all topics " + dataObj.toString());
152         }
153
154         public void testPublisherForTopic() {
155                 LOGGER.info("test case get all publishers for topic: " + topicName);
156                 // creating topic to check
157                 createTopic(topicName);
158                 target = client.target(url);
159                 target = target.path("/topics/");
160                 target = target.path(topicName);
161                 target = target.path("/producers");
162                 // checking all producer for a particular topic
163                 Response response = target.request().get();
164                 keyInstance.assertStatus(response);
165                 LOGGER.info("Successfully returned after getting all the publishers" + topicName);
166         }
167
168         public void testPermitPublisherForTopic() {
169                 LOGGER.info("test case permit user for topic " + topicName);
170                 JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("ai039a@att.com", "adding user to "));
171                 String userapikey = (String) jsonObj.get("key");
172                 createTopic(topicName);
173                 // adding user to a topic//
174                 target = client.target(url);
175                 target = target.path("/topics/");
176                 target = target.path(topicName);
177                 target = target.path("/producers/");
178                 target = target.path(userapikey);
179                 Response response = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
180                                 .header("X-CambriaDate", date).put(Entity.json(""));
181                 keyInstance.assertStatus(response);
182                 LOGGER.info("successfully returned after permiting the user for topic " + topicName);
183         }
184
185         public void testDenyPublisherForTopic() {
186                 LOGGER.info("test case denying user for topic " + topicName);
187                 JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("ai039a@att.com", "adding user to "));
188                 String userapikey = (String) jsonObj.get("key");
189                 createTopic(topicName);
190                 // adding user to a topic//
191                 target = client.target(url);
192                 target = target.path("/topics/");
193                 target = target.path(topicName);
194                 target = target.path("/producers/");
195                 target = target.path(userapikey);
196                 target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
197                                 .header("X-CambriaDate", date).put(Entity.json(""));
198                 // deleting user who is just added//
199                 target = client.target(url);
200                 target = target.path("/topics/");
201                 target = target.path(topicName);
202                 target = target.path("/producers/");
203                 target = target.path(userapikey);
204                 Response response2 = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
205                                 .header("X-CambriaDate", date).delete();
206                 keyInstance.assertStatus(response2);
207                 LOGGER.info("successfully returned after denying the user for topic " + topicName);
208         }
209
210         public void testConsumerForTopic() {
211                 LOGGER.info("test case get all consumers for topic: " + topicName);
212                 // creating topic to check
213                 createTopic(topicName);
214                 target = client.target(url);
215                 target = target.path("/topics/");
216                 target = target.path(topicName);
217                 target = target.path("/consumers");
218                 // checking all consumer for a particular topic
219                 Response response = target.request().get();
220                 keyInstance.assertStatus(response);
221                 LOGGER.info("Successfully returned after getting all the consumers" + topicName);
222         }
223
224         public void testPermitConsumerForTopic() {
225                 LOGGER.info("test case get all consumer for topic: " + topicName);
226                 // creating user for adding to topic//
227                 JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("ai039a@att.com", "adding user to "));
228                 String userapikey = (String) jsonObj.get("key");
229                 createTopic(topicName);
230                 // adding user to a topic//
231                 target = client.target(url);
232                 target = target.path("/topics/");
233                 target = target.path(topicName);
234                 target = target.path("/consumers/");
235                 target = target.path(userapikey);
236                 Response response = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
237                                 .header("X-CambriaDate", date).put(Entity.json(""));
238                 keyInstance.assertStatus(response);
239                 LOGGER.info("Successfully returned after getting all the consumers" + topicName);
240         }
241
242         public void testDenyConsumerForTopic() {
243                 LOGGER.info("test case denying consumer for topic " + topicName);
244                 // creating user for adding and deleting from topic//
245                 JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("ai039a@att.com", "adding user to "));
246                 String userapikey = (String) jsonObj.get("key");
247                 createTopic(topicName);
248                 // adding user to a topic//
249                 target = client.target(url);
250                 target = target.path("/topics/");
251                 target = target.path(topicName);
252                 target = target.path("/consumers/");
253                 target = target.path(userapikey);
254                 target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
255                                 .header("X-CambriaDate", date).put(Entity.json(""));
256                 // deleting user who is just added//
257                 target = client.target(url);
258                 target = target.path("/topics/");
259                 target = target.path(topicName);
260                 target = target.path("/consumers/");
261                 target = target.path(userapikey);
262                 Response response2 = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
263                                 .header("X-CambriaDate", date).delete();
264                 keyInstance.assertStatus(response2);
265                 LOGGER.info("successfully returned after denying the consumer for topic " + topicName);
266         }*/
267 }