DMAAP-MR - Merge MR repos
[dmaap/messagerouter/messageservice.git] / src / test / java / org / onap / dmaap / mr / test / dmaap / DMaapPubSubTest.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 public class DMaapPubSubTest {
25 /*      private static final Logger LOGGER = Logger.getLogger(DMaapTopicTest.class);
26         Client client = ClientBuilder.newClient();
27         String url = LoadPropertyFile.getPropertyFileData().getProperty("url");
28         WebTarget target = client.target(url);
29         String topicapikey;
30         String topicsecretKey;
31         String serverCalculatedSignature;
32         String date = LoadPropertyFile.getPropertyFileData().getProperty("date");
33         // changes by islam
34         String topic_name = LoadPropertyFile.getPropertyFileData().getProperty("topicName");
35         DmaapApiKeyTest keyInstance = new DmaapApiKeyTest();
36
37
38         public void testProduceMessage() {
39                 LOGGER.info("test case publish message");
40                 // DMaapTopicTest topicCreation = new DMaapTopicTest();
41                 DmaapApiKeyTest keyInstance = new DmaapApiKeyTest();
42                 // creating topic
43                 createTopic(topic_name);
44
45                 target = client.target(url);
46                 target = target.path("/events/");
47                 target = target.path(topic_name);
48                 Response response2 = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
49                                 .header("X-CambriaDate", date).post(Entity.json("{message:producing first message}"));
50                 keyInstance.assertStatus(response2);
51                 LOGGER.info("successfully published message");
52         }
53
54         public void testConsumeMessage() {
55                 LOGGER.info("test case subscribing message");
56                 createTopic(topic_name);
57                 target = client.target(url);
58                 target = target.path("/events/");
59                 target = target.path(topic_name);
60                 target = target.path("consumGrp");
61                 target = target.path(topicapikey);
62                 Response response = target.request().get();
63                 keyInstance.assertStatus(response);
64                 LOGGER.info("successfully consumed messages");
65                 InputStream is = (InputStream) response.getEntity();
66                 Scanner s = new Scanner(is);
67                 s.useDelimiter("\\A");
68                 String data = s.next();
69                 s.close();
70                 LOGGER.info("Consumed Message data: " + data);
71         }
72
73         public void createTopic(String name) {
74                 if (!topicExist(name)) {
75                         TopicBean topicbean = new TopicBean();
76                         topicbean.setDescription("creating topic");
77                         topicbean.setPartitionCount(1);
78                         topicbean.setReplicationCount(1);
79                         topicbean.setTopicName(name);
80                         topicbean.setTransactionEnabled(true);
81                         target = client.target(url);
82                         target = target.path("/topics/create");
83                         JSONObject jsonObj = keyInstance.returnKey(new ApiKeyBean("ai039a@att.com", "topic creation"));
84                         topicapikey = (String) jsonObj.get("key");
85                         topicsecretKey = (String) jsonObj.get("secret");
86                         serverCalculatedSignature = sha1HmacSigner.sign(date, topicsecretKey);
87                         Response response = target.request().header("X-CambriaAuth", topicapikey + ":" + serverCalculatedSignature)
88                                         .header("X-CambriaDate", date).post(Entity.json(topicbean));
89                         keyInstance.assertStatus(response);
90                 }
91         }
92
93         public boolean topicExist(String topicName) {
94                 target = target.path("/topics/" + topicName);
95                 InputStream is, issecret;
96                 Response response = target.request().get();
97                 if (response.getStatus() == HttpStatus.SC_OK) {
98                         is = (InputStream) response.getEntity();
99                         Scanner s = new Scanner(is);
100                         s.useDelimiter("\\A");
101                         JSONObject dataObj = new JSONObject(s.next());
102                         s.close();
103                         // get owner of a topic
104                         topicapikey = (String) dataObj.get("owner");
105                         target = client.target(url);
106                         target = target.path("/apiKeys/");
107                         target = target.path(topicapikey);
108                         Response response2 = target.request().get();
109                         issecret = (InputStream) response2.getEntity();
110                         Scanner st = new Scanner(issecret);
111                         st.useDelimiter("\\A");
112                         JSONObject dataObj1 = new JSONObject(st.next());
113                         st.close();
114                         // get secret key of this topic//
115                         topicsecretKey = (String) dataObj1.get("secret");
116                         serverCalculatedSignature = sha1HmacSigner.sign(date, topicsecretKey);
117                         return true;
118                 } else
119                         return false;
120         }*/
121 }