71c5e0fd4d984aa7f3a2c47efe9e4b8e28275790
[dmaap/messagerouter/msgrtr.git] / src / test / java / com / att / mr / test / dme2 / DME2ProducerTest.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 com.att.mr.test.dme2;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26 import java.util.HashMap;
27 import java.util.Properties;
28
29 import org.apache.log4j.Logger;
30
31 import junit.framework.TestCase;
32
33 import com.att.aft.dme2.api.DME2Client;
34 import com.att.aft.dme2.api.DME2Exception;
35 import com.att.aft.dme2.internal.jackson.map.ObjectMapper;
36 import com.att.mr.test.dmaap.DmaapAdminTest;
37
38 public class DME2ProducerTest extends TestCase {
39         private static final Logger LOGGER = Logger.getLogger(DmaapAdminTest.class);
40
41         public void testProducer() {
42                 DME2TopicTest topicTestObj = new DME2TopicTest();
43
44                 Properties props = LoadPropertyFile.getPropertyFileDataProducer();
45                 String latitude = props.getProperty("Latitude");
46                 String longitude = props.getProperty("Longitude");
47                 String version = props.getProperty("Version");
48                 String serviceName = props.getProperty("ServiceName");
49                 String env = props.getProperty("Environment");
50                 String partner = props.getProperty("Partner");
51                 String protocol = props.getProperty("Protocol");
52                 String url = protocol + "://DME2SEARCH/" + "service=" + serviceName + "/" + "version=" + version + "/"
53                                 + "envContext=" + env + "/" + "partner=" + partner;
54                 LoadPropertyFile.loadAFTProperties(latitude, longitude);
55                 HashMap<String, String> hm = new HashMap<String, String>();
56                 hm.put("AFT_DME2_EP_READ_TIMEOUT_MS", "50000");
57                 hm.put("AFT_DME2_ROUNDTRIP_TIMEOUT_MS", "240000");
58                 hm.put("AFT_DME2_EP_CONN_TIMEOUT", "5000");
59                 // checking whether topic exist or not
60                 if (!topicTestObj.topicExist(url, props, hm)) {
61                         // if topic doesn't exist then create the topic
62                         topicTestObj.createTopic(url, props, hm);
63                         // after creating the topic publish on that topic
64                         publishMessage(url, props, hm);
65                 } else {
66                         // if topic already exist start publishing on the topic
67                         publishMessage(url, props, hm);
68                 }
69
70         }
71
72         public void publishMessage(String url, Properties props, HashMap<String, String> mapData) {
73                 try {
74                         LOGGER.info("Call to publish message ");
75                         DME2Client sender = new DME2Client(new URI(url), 5000L);
76                         sender.setAllowAllHttpReturnCodes(true);
77                         sender.setMethod(props.getProperty("MethodTypePost"));
78                         String subcontextpathPublish = props.getProperty("SubContextPathproducer") + props.getProperty("newTopic");
79                         sender.setSubContext(subcontextpathPublish);
80                         String jsonStringApiBean = new ObjectMapper().writeValueAsString(new ApiKeyBean("example@att.com",
81                                         "description"));
82                         sender.setPayload(jsonStringApiBean);
83
84                         sender.setCredentials(props.getProperty("user"), props.getProperty("password"));
85                         sender.addHeader("content-type", props.getProperty("contenttype"));
86                         LOGGER.info("Publishing message");
87                         String reply = sender.sendAndWait(5000L);
88                         // assertTrue(LoadPropertyFile.isValidJsonString(reply));
89                         assertNotNull(reply);
90                         LOGGER.info("response =" + reply);
91
92                 } catch (DME2Exception e) {
93                         e.printStackTrace();
94                 } catch (URISyntaxException e) {
95                         e.printStackTrace();
96                 } catch (Exception e) {
97                         e.printStackTrace();
98                 }
99
100         }
101 }