[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / MRConstantsTest.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
4  *  ================================================================================
5  *  Copyright © 2017 AT&T 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.client.impl;
26
27 import junit.framework.TestCase;
28 import org.apache.http.HttpHost;
29 import org.junit.Test;
30
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Iterator;
35
36 public class MRConstantsTest extends TestCase {
37     @Test
38     public void testPlainHost() throws IOException {
39         final String rawTopic = "bar";
40         final String result = MRConstants.makeUrl(rawTopic);
41         assertEquals("/events/" + "bar", result);
42     }
43
44     @Test
45     public void testHostWithProtocol() throws IOException {
46         final String rawTopic = "bar";
47         final String result = MRConstants.makeUrl(rawTopic);
48         assertEquals("/events/" + "bar", result);
49     }
50
51     @Test
52     public void testHostWithProtocolAndPort() throws IOException {
53         final String rawTopic = "bar";
54         final String result = MRConstants.makeUrl(rawTopic);
55         assertEquals("/events/" + "bar", result);
56     }
57
58     @Test
59     public void testHostWithPort() throws IOException {
60         final String rawTopic = "bar";
61         final String result = MRConstants.makeUrl(rawTopic);
62         assertEquals("/events/" + "bar", result);
63     }
64
65     @Test
66     public void testHostWithPortAndEscapedTopic() throws IOException {
67         final String rawTopic = "bar?bell";
68         final String result = MRConstants.makeUrl(rawTopic);
69         assertEquals("/events/" + "bar%3Fbell", result);
70     }
71
72     @Test
73     public void testConsumerPlainHost() throws IOException {
74         final String rawTopic = "bar";
75         final String rawGroup = "group";
76         final String rawId = "id";
77         final String result = MRConstants.makeConsumerUrl(rawTopic, rawGroup, rawId);
78         assertEquals("/events/" + "bar/group/id", result);
79     }
80
81     @Test
82     public void testCreateHostList() {
83         final ArrayList<String> in = new ArrayList<String>();
84         in.add("foo");
85         in.add("bar");
86         in.add("baz:80");
87
88         final Collection<HttpHost> hosts = MRConstants.createHostsList(in);
89         assertEquals(3, hosts.size());
90
91         final Iterator<HttpHost> it = hosts.iterator();
92         final HttpHost first = it.next();
93         assertEquals(MRConstants.STD_MR_SERVICE_PORT, first.getPort());
94         assertEquals("foo", first.getHostName());
95
96         final HttpHost second = it.next();
97         assertEquals(MRConstants.STD_MR_SERVICE_PORT, second.getPort());
98         assertEquals("bar", second.getHostName());
99
100         final HttpHost third = it.next();
101         assertEquals(80, third.getPort());
102         assertEquals("baz", third.getHostName());
103     }
104
105     private static final String[][] hostTests =
106             {
107                     {"host", "host", "" + MRConstants.STD_MR_SERVICE_PORT},
108                     {":oops", null, "-1"},
109                     {"host:1.3", null, "-1"},
110                     {"host:13", "host", "13"},
111                     {"host:", "host", "" + MRConstants.STD_MR_SERVICE_PORT},
112             };
113
114     @Test
115     public void testHostParse() {
116         for (String[] test : hostTests) {
117             final String hostIn = test[0];
118             final String hostOut = test[1];
119             final int portOut = Integer.parseInt(test[2]);
120
121             try {
122                 final HttpHost hh = MRConstants.hostForString(hostIn);
123                 assertEquals(hostOut, hh.getHostName());
124                 assertEquals(portOut, hh.getPort());
125             } catch (IllegalArgumentException x) {
126                 assertEquals(-1, portOut);
127             }
128         }
129     }
130 }