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 import org.onap.dmaap.mr.client.impl.MRConstants;
36 public class MRConstantsTest extends TestCase
39 public void testPlainHost () throws IOException
41 final String rawTopic = "bar";
42 final String result = MRConstants.makeUrl ( rawTopic );
43 assertEquals ( "/events/" + "bar", result );
47 public void testHostWithProtocol () throws IOException
49 final String rawTopic = "bar";
50 final String result = MRConstants.makeUrl ( rawTopic );
51 assertEquals ( "/events/" + "bar", result );
55 public void testHostWithProtocolAndPort () throws IOException
57 final String rawTopic = "bar";
58 final String result = MRConstants.makeUrl ( rawTopic );
59 assertEquals ( "/events/" + "bar", result );
63 public void testHostWithPort () throws IOException
65 final String rawTopic = "bar";
66 final String result = MRConstants.makeUrl ( rawTopic );
67 assertEquals ( "/events/" + "bar", result );
71 public void testHostWithPortAndEscapedTopic () throws IOException
73 final String rawTopic = "bar?bell";
74 final String result = MRConstants.makeUrl ( rawTopic );
75 assertEquals ( "/events/" + "bar%3Fbell", result );
79 public void testConsumerPlainHost () throws IOException
81 final String rawTopic = "bar";
82 final String rawGroup = "group";
83 final String rawId = "id";
84 final String result = MRConstants.makeConsumerUrl ( rawTopic, rawGroup, rawId );
85 assertEquals ( "/events/" + "bar/group/id", result );
89 public void testCreateHostList ()
91 final ArrayList<String> in = new ArrayList<String> ();
96 final Collection<HttpHost> hosts = MRConstants.createHostsList ( in );
97 assertEquals ( 3, hosts.size () );
99 final Iterator<HttpHost> it = hosts.iterator ();
100 final HttpHost first = it.next ();
101 assertEquals ( MRConstants.kStdMRServicePort, first.getPort () );
102 assertEquals ( "foo", first.getHostName () );
104 final HttpHost second = it.next ();
105 assertEquals ( MRConstants.kStdMRServicePort, second.getPort () );
106 assertEquals ( "bar", second.getHostName () );
108 final HttpHost third = it.next ();
109 assertEquals ( 80, third.getPort () );
110 assertEquals ( "baz", third.getHostName () );
113 private static final String[][] hostTests =
115 { "host", "host", "" + MRConstants.kStdMRServicePort },
116 { ":oops", null, "-1" },
117 { "host:1.3", null, "-1" },
118 { "host:13", "host", "13" },
119 { "host:", "host", "" + MRConstants.kStdMRServicePort },
123 public void testHostParse ()
125 for ( String[] test : hostTests )
127 final String hostIn = test[0];
128 final String hostOut = test[1];
129 final int portOut = Integer.parseInt ( test[2] );
133 final HttpHost hh = MRConstants.hostForString ( hostIn );
134 assertEquals ( hostOut, hh.getHostName () );
135 assertEquals ( portOut, hh.getPort () );
137 catch ( IllegalArgumentException x )
139 assertEquals ( -1, portOut );