1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.client.impl;
27 import junit.framework.TestCase;
28 import org.apache.http.HttpHost;
29 import org.junit.Test;
31 import java.io.IOException;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.Iterator;
36 public class MRConstantsTest extends TestCase {
38 public void testPlainHost() throws IOException {
39 final String rawTopic = "bar";
40 final String result = MRConstants.makeUrl(rawTopic);
41 assertEquals("/events/" + "bar", result);
45 public void testHostWithProtocol() throws IOException {
46 final String rawTopic = "bar";
47 final String result = MRConstants.makeUrl(rawTopic);
48 assertEquals("/events/" + "bar", result);
52 public void testHostWithProtocolAndPort() throws IOException {
53 final String rawTopic = "bar";
54 final String result = MRConstants.makeUrl(rawTopic);
55 assertEquals("/events/" + "bar", result);
59 public void testHostWithPort() throws IOException {
60 final String rawTopic = "bar";
61 final String result = MRConstants.makeUrl(rawTopic);
62 assertEquals("/events/" + "bar", result);
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);
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);
82 public void testCreateHostList() {
83 final ArrayList<String> in = new ArrayList<String>();
88 final Collection<HttpHost> hosts = MRConstants.createHostsList(in);
89 assertEquals(3, hosts.size());
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());
96 final HttpHost second = it.next();
97 assertEquals(MRConstants.STD_MR_SERVICE_PORT, second.getPort());
98 assertEquals("bar", second.getHostName());
100 final HttpHost third = it.next();
101 assertEquals(80, third.getPort());
102 assertEquals("baz", third.getHostName());
105 private static final String[][] hostTests =
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},
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]);
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);