[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / tools / ValidatorUtilTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2018 IBM Intellectual Property. All rights reserved.
6  *  ================================================================================
7  *  Modifications Copyright © 2021 Orange.
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  *        http://www.apache.org/licenses/LICENSE-2.0
13  *
14  *  Unless required by applicable law or agreed to in writing, software
15  *  distributed under the License is distributed on an "AS IS" BASIS,
16  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  *  See the License for the specific language governing permissions and
18  *  limitations under the License.
19  *  ============LICENSE_END=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.tools;
26
27 import org.junit.Test;
28 import org.onap.dmaap.mr.client.ProtocolType;
29
30 import java.util.Properties;
31
32 import static org.junit.Assert.*;
33
34 public class ValidatorUtilTest {
35
36     @Test
37     public void testValidateForDME2WithNullServiceName() {
38         Properties props = new Properties();
39         props.setProperty("TransportType", ProtocolType.DME2.getValue());
40         try {
41             ValidatorUtil.validatePublisher(props);
42             fail();
43         } catch (IllegalArgumentException e) {
44             assertEquals("ServiceName is needed", e.getMessage());
45         }
46
47     }
48
49     @Test
50     public void testValidateForDME2WithNullTopic() {
51         Properties props = new Properties();
52         props.setProperty("TransportType", ProtocolType.DME2.getValue());
53         props.setProperty("ServiceName", "ServiceName");
54         try {
55             ValidatorUtil.validatePublisher(props);
56             fail();
57         } catch (IllegalArgumentException e) {
58             assertEquals("topic is needed", e.getMessage());
59         }
60
61     }
62
63     @Test
64     public void testValidateForDME2WithNullUserName() {
65         Properties props = new Properties();
66         props.setProperty("TransportType", ProtocolType.DME2.getValue());
67         props.setProperty("ServiceName", "ServiceName");
68         props.setProperty("topic", "topic");
69         try {
70             ValidatorUtil.validatePublisher(props);
71             fail();
72         } catch (IllegalArgumentException e) {
73             assertEquals("username is needed", e.getMessage());
74         }
75
76     }
77
78     @Test
79     public void testValidateForDME2WithNullPassword() {
80         Properties props = new Properties();
81         props.setProperty("TransportType", ProtocolType.DME2.getValue());
82         props.setProperty("ServiceName", "ServiceName");
83         props.setProperty("topic", "topic");
84         props.setProperty("username", "username");
85
86         try {
87             ValidatorUtil.validatePublisher(props);
88             fail();
89         } catch (IllegalArgumentException e) {
90             assertEquals("password is needed", e.getMessage());
91         }
92
93     }
94
95
96     @Test
97     public void testValidateForNonDME2WithNullServiceName() {
98         Properties props = new Properties();
99         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
100         try {
101             ValidatorUtil.validatePublisher(props);
102             fail();
103         } catch (IllegalArgumentException e) {
104             assertEquals("host is needed", e.getMessage());
105         }
106
107     }
108
109     @Test
110     public void testValidateForNonDME2WithNullTopic() {
111         Properties props = new Properties();
112         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
113         props.setProperty("host", "ServiceName");
114         try {
115             ValidatorUtil.validatePublisher(props);
116             fail();
117         } catch (IllegalArgumentException e) {
118             assertEquals("topic is needed", e.getMessage());
119         }
120
121     }
122
123     @Test
124     public void testValidateForNonDME2WithNullContenttype() {
125         Properties props = new Properties();
126         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
127         props.setProperty("host", "ServiceName");
128         props.setProperty("topic", "topic");
129         try {
130             ValidatorUtil.validatePublisher(props);
131             fail();
132         } catch (IllegalArgumentException e) {
133             assertEquals("contenttype is needed", e.getMessage());
134         }
135
136     }
137
138
139     @Test
140     public void testValidateForNonDME2WithNullUserName() {
141         Properties props = new Properties();
142         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
143         props.setProperty("host", "ServiceName");
144         props.setProperty("topic", "topic");
145         props.setProperty("contenttype", "contenttype");
146         try {
147             ValidatorUtil.validatePublisher(props);
148             fail();
149         } catch (IllegalArgumentException e) {
150             assertEquals("username is needed", e.getMessage());
151         }
152
153     }
154
155     @Test
156     public void testValidateForNonDME2WithNullPassword() {
157         Properties props = new Properties();
158         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
159         props.setProperty("host", "ServiceName");
160         props.setProperty("topic", "topic");
161         props.setProperty("username", "username");
162         props.setProperty("contenttype", "contenttype");
163
164         try {
165             ValidatorUtil.validatePublisher(props);
166             fail();
167         } catch (IllegalArgumentException e) {
168             assertEquals("password is needed", e.getMessage());
169         }
170
171     }
172
173     @Test
174     public void testValidateForNonDME2WithAuthKey() {
175         Properties props = new Properties();
176         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
177         props.setProperty("host", "ServiceName");
178         props.setProperty("topic", "topic");
179         props.setProperty("username", "username");
180         props.setProperty("contenttype", "contenttype");
181         props.setProperty("password", "password");
182
183         try {
184             ValidatorUtil.validatePublisher(props);
185             fail();
186         } catch (IllegalArgumentException e) {
187             assertEquals("authKey is needed", e.getMessage());
188         }
189
190     }
191
192     @Test
193     public void testValidateForNonDME2WithOutAuthDate() {
194         Properties props = new Properties();
195         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
196         props.setProperty("host", "ServiceName");
197         props.setProperty("topic", "topic");
198         props.setProperty("username", "username");
199         props.setProperty("contenttype", "contenttype");
200         props.setProperty("password", "password");
201         props.setProperty("authKey", "authKey");
202
203
204         try {
205             ValidatorUtil.validatePublisher(props);
206             fail();
207         } catch (IllegalArgumentException e) {
208             assertEquals("authDate is needed", e.getMessage());
209         }
210     }
211
212     @Test
213     public void testValidateForNonDME2WithAuthDate() {
214         Properties props = new Properties();
215         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
216         props.setProperty("host", "ServiceName");
217         props.setProperty("topic", "topic");
218         props.setProperty("username", "username");
219         props.setProperty("contenttype", "contenttype");
220         props.setProperty("password", "password");
221         props.setProperty("authKey", "authKey");
222         props.setProperty("authDate", "authDate");
223
224         try {
225             ValidatorUtil.validatePublisher(props);
226             fail();
227         } catch (IllegalArgumentException e) {
228             assertEquals("maxBatchSize is needed", e.getMessage());
229         }
230     }
231
232
233     @Test
234     public void testValidateForNonDME2WithMaxAgeMs() {
235         Properties props = new Properties();
236         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
237         props.setProperty("host", "ServiceName");
238         props.setProperty("topic", "topic");
239         props.setProperty("username", "username");
240         props.setProperty("contenttype", "contenttype");
241         props.setProperty("password", "password");
242         props.setProperty("authKey", "authKey");
243         props.setProperty("authDate", "authDate");
244         props.setProperty("maxBatchSize", "maxBatchSize");
245
246         try {
247             ValidatorUtil.validatePublisher(props);
248             fail();
249         } catch (IllegalArgumentException e) {
250             assertEquals("maxAgeMs is needed", e.getMessage());
251         }
252
253
254     }
255
256     @Test
257     public void testValidateForNonDME2WithMessageSentThreadOccurrence() {
258         Properties props = new Properties();
259         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
260         props.setProperty("host", "ServiceName");
261         props.setProperty("topic", "topic");
262         props.setProperty("username", "username");
263         props.setProperty("contenttype", "contenttype");
264         props.setProperty("password", "password");
265         props.setProperty("authKey", "authKey");
266         props.setProperty("authDate", "authDate");
267         props.setProperty("maxBatchSize", "maxBatchSize");
268         props.setProperty("maxAgeMs", "maxAgeMs");
269
270         try {
271             ValidatorUtil.validatePublisher(props);
272             fail();
273         } catch (IllegalArgumentException e) {
274             assertEquals("MessageSentThreadOccurrence is needed", e.getMessage());
275         }
276
277     }
278
279
280     @Test
281     public void testValidateSubscriberWithoutGroup() {
282         Properties props = new Properties();
283         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
284         props.setProperty("host", "ServiceName");
285         props.setProperty("topic", "topic");
286         props.setProperty("username", "username");
287         props.setProperty("contenttype", "contenttype");
288         props.setProperty("password", "password");
289         props.setProperty("authKey", "authKey");
290         props.setProperty("authDate", "authDate");
291         props.setProperty("maxBatchSize", "maxBatchSize");
292         props.setProperty("maxAgeMs", "maxAgeMs");
293
294         try {
295             ValidatorUtil.validateSubscriber(props);
296             fail();
297         } catch (IllegalArgumentException e) {
298             assertEquals("group is needed", e.getMessage());
299         }
300     }
301
302     @Test
303     public void testValidateSubscriberWithoutCustomer() {
304         Properties props = new Properties();
305         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
306         props.setProperty("host", "ServiceName");
307         props.setProperty("topic", "topic");
308         props.setProperty("username", "username");
309         props.setProperty("contenttype", "contenttype");
310         props.setProperty("password", "password");
311         props.setProperty("authKey", "authKey");
312         props.setProperty("authDate", "authDate");
313         props.setProperty("maxBatchSize", "maxBatchSize");
314         props.setProperty("maxAgeMs", "maxAgeMs");
315         props.setProperty("group", "group");
316
317         try {
318             ValidatorUtil.validateSubscriber(props);
319             fail();
320         } catch (IllegalArgumentException e) {
321             assertEquals("Consumer (id) is needed", e.getMessage());
322         }
323     }
324
325     @Test
326     public void testValidatePublisher() {
327         Properties props = new Properties();
328         props.setProperty("TransportType", ProtocolType.AUTH_KEY.getValue());
329         props.setProperty("host", "ServiceName");
330         props.setProperty("topic", "topic");
331         props.setProperty("username", "username");
332         props.setProperty("contenttype", "contenttype");
333         props.setProperty("password", "password");
334         props.setProperty("authKey", "authKey");
335         props.setProperty("authDate", "authDate");
336         props.setProperty("maxBatchSize", "maxBatchSize");
337         props.setProperty("maxAgeMs", "maxAgeMs");
338         props.setProperty("MessageSentThreadOccurrence", "10");
339
340         try {
341             ValidatorUtil.validatePublisher(props);
342         } catch (IllegalArgumentException e) {
343             fail();
344             return;
345         }
346
347         props.remove("MessageSentThreadOccurrence");
348         props.setProperty("MessageSentThreadOccurance", "10");
349         try {
350             ValidatorUtil.validatePublisher(props);
351         } catch (IllegalArgumentException e) {
352             fail();
353         }
354     }
355
356
357
358 }