added test case to TestDmaapProducerImpl.java
[appc.git] / appc-adapters / appc-dmaap-adapter / appc-dmaap-adapter-bundle / src / test / java / org / onap / appc / adapter / messaging / dmaap / impl / TestDmaapProducerImpl.java
1 /* 
2  * ============LICENSE_START======================================================= 
3  * ONAP : APPC 
4  * ================================================================================ 
5  * Copyright (C) 2018 AT&T Intellectual Property. All rights reserved. 
6  * ============================================================================= 
7  * Modifications Copyright (C) 2018 IBM. 
8  * ============================================================================= 
9  * Licensed under the Apache License, Version 2.0 (the "License"); 
10  * you may not use this file except in compliance with the License. 
11  * You may obtain a copy of the License at 
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0 
14  * 
15  * Unless required by applicable law or agreed to in writing, software 
16  * distributed under the License is distributed on an "AS IS" BASIS, 
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
18  * See the License for the specific language governing permissions and 
19  * limitations under the License. 
20  * ============LICENSE_END========================================================= 
21  */
22
23 package org.onap.appc.adapter.messaging.dmaap.impl;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.fail;
29
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.HashSet;
33 import java.util.Properties;
34 import java.util.Set;
35
36 import org.junit.Before;
37 import org.junit.Ignore;
38 import org.junit.Test;
39
40 public class TestDmaapProducerImpl {
41     String[] hostList = { "192.168.1.1" };
42     Collection<String> hosts = new HashSet<String>(Arrays.asList(hostList));
43
44     String topic = "JunitTopicOne";
45     String group = "junit-client";
46     String id = "junit-consumer-one";
47     String key = "key";
48     String secret = "secret";
49     String filter = null;
50
51     private DmaapProducerImpl producer;
52
53     @Before
54     public void setUp() {
55         producer = new DmaapProducerImpl(hosts, topic, null, null);
56     }
57
58     @Test
59     public void testDmaapProducerImplSingleTopic() {
60         producer = new DmaapProducerImpl(hosts, topic, key, secret);
61
62         assertNotNull(producer);
63
64         Properties props = producer.getProperties();
65
66         assertNotNull(props);
67
68         assertEquals("key", props.getProperty("username"));
69         assertEquals("secret", props.getProperty("password"));
70     }
71
72     @Test
73     public void testDmaapProducerImplMultipleTopic() {
74         String[] topicList = { "topic1", "topic2" };
75         Set<String> topicNames = new HashSet<String>(Arrays.asList(topicList));
76
77         producer = new DmaapProducerImpl(hosts, topicNames, key, secret);
78
79         assertNotNull(producer);
80
81         Properties props = producer.getProperties();
82
83         assertNotNull(props);
84
85         assertEquals("key", props.getProperty("username"));
86         assertEquals("secret", props.getProperty("password"));
87
88     }
89
90     @Test
91     public void testDmaapProducerImplNoUserPass() {
92         assertNotNull(producer);
93
94         Properties props = producer.getProperties();
95
96         assertNotNull(props);
97
98         assertNull(props.getProperty("username"));
99         assertNull(props.getProperty("password"));
100     }
101
102     @Test
103     public void testUpdateCredentials() {
104         assertNotNull(producer);
105
106         Properties props = producer.getProperties();
107
108         assertNotNull(props);
109
110         assertNull(props.getProperty("username"));
111         assertNull(props.getProperty("password"));
112
113         producer.updateCredentials(key, secret);
114
115         props = producer.getProperties();
116
117         assertNotNull(props);
118
119         assertEquals("key", props.getProperty("username"));
120         assertEquals("secret", props.getProperty("password"));
121
122     }
123
124     @Test
125     public void testPost() {
126         boolean successful = producer.post("partition", "data");
127         assertEquals(true, successful);
128     }
129
130     @Test
131     public void testCloseNoClient() {
132         producer = new DmaapProducerImpl(hosts, topic, key, secret);
133
134         assertNotNull(producer);
135
136         producer.close();
137     }
138
139     
140     @Test
141     public void testCloseWithClient() {
142         producer.post("partition", "data");
143         assertNotNull(producer);
144         producer.close();
145     }
146
147     @Test
148     public void testUseHttps() {
149         producer = new DmaapProducerImpl(hosts, topic, key, secret);
150
151         assertNotNull(producer);
152
153         assertEquals(false, producer.isHttps());
154
155         producer.useHttps(true);
156
157         assertEquals(true, producer.isHttps());
158
159     }
160
161 }