1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
29 import junit.framework.TestCase;
31 import org.apache.http.HttpHost;
32 import org.junit.Test;
34 public class MRConstantsTest extends TestCase
37 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
47 final String rawTopic = "bar";
48 final String result = MRConstants.makeUrl ( rawTopic );
49 assertEquals ( "/events/" + "bar", result );
53 public void testHostWithProtocolAndPort () throws IOException
55 final String rawTopic = "bar";
56 final String result = MRConstants.makeUrl ( rawTopic );
57 assertEquals ( "/events/" + "bar", result );
61 public void testHostWithPort () throws IOException
63 final String rawTopic = "bar";
64 final String result = MRConstants.makeUrl ( rawTopic );
65 assertEquals ( "/events/" + "bar", result );
69 public void testHostWithPortAndEscapedTopic () throws IOException
71 final String rawTopic = "bar?bell";
72 final String result = MRConstants.makeUrl ( rawTopic );
73 assertEquals ( "/events/" + "bar%3Fbell", result );
77 public void testConsumerPlainHost () throws IOException
79 final String rawTopic = "bar";
80 final String rawGroup = "group";
81 final String rawId = "id";
82 final String result = MRConstants.makeConsumerUrl ( rawTopic, rawGroup, rawId );
83 assertEquals ( "/events/" + "bar/group/id", result );
87 public void testCreateHostList ()
89 final ArrayList<String> in = new ArrayList<String> ();
94 final Collection<HttpHost> hosts = MRConstants.createHostsList ( in );
95 assertEquals ( 3, hosts.size () );
97 final Iterator<HttpHost> it = hosts.iterator ();
98 final HttpHost first = it.next ();
99 assertEquals ( MRConstants.STD_MR_SERVICE_PORT, first.getPort () );
100 assertEquals ( "foo", first.getHostName () );
102 final HttpHost second = it.next ();
103 assertEquals ( MRConstants.STD_MR_SERVICE_PORT, second.getPort () );
104 assertEquals ( "bar", second.getHostName () );
106 final HttpHost third = it.next ();
107 assertEquals ( 80, third.getPort () );
108 assertEquals ( "baz", third.getHostName () );
111 private static final String[][] hostTests =
113 { "host", "host", "" + MRConstants.STD_MR_SERVICE_PORT},
114 { ":oops", null, "-1" },
115 { "host:1.3", null, "-1" },
116 { "host:13", "host", "13" },
117 { "host:", "host", "" + MRConstants.STD_MR_SERVICE_PORT},
121 public void testHostParse ()
123 for ( String[] test : hostTests )
125 final String hostIn = test[0];
126 final String hostOut = test[1];
127 final int portOut = Integer.parseInt ( test[2] );
131 final HttpHost hh = MRConstants.hostForString ( hostIn );
132 assertEquals ( hostOut, hh.getHostName () );
133 assertEquals ( portOut, hh.getPort () );
135 catch ( IllegalArgumentException x )
137 assertEquals ( -1, portOut );