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