update the package name
[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  *  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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.client.impl;
23
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Collection;
27 import java.util.Iterator;
28
29 import junit.framework.TestCase;
30
31 import org.apache.http.HttpHost;
32 import org.junit.Test;
33
34 import org.onap.dmaap.mr.client.impl.MRConstants;
35
36 public class MRConstantsTest extends TestCase
37 {
38         @Test
39         public void testPlainHost () throws IOException
40         {
41                 final String rawTopic = "bar";
42                 final String result = MRConstants.makeUrl ( rawTopic );
43                 assertEquals ( "/events/"  + "bar", result );
44         }
45
46         @Test
47         public void testHostWithProtocol () throws IOException
48         {
49                 final String rawTopic = "bar";
50                 final String result = MRConstants.makeUrl (  rawTopic );
51                 assertEquals ( "/events/"  + "bar", result );
52         }
53
54         @Test
55         public void testHostWithProtocolAndPort () throws IOException
56         {
57                 final String rawTopic = "bar";
58                 final String result = MRConstants.makeUrl ( rawTopic );
59                 assertEquals ( "/events/" + "bar", result );
60         }
61
62         @Test
63         public void testHostWithPort () throws IOException
64         {
65                 final String rawTopic = "bar";
66                 final String result = MRConstants.makeUrl ( rawTopic );
67                 assertEquals ( "/events/" + "bar", result );
68         }
69
70         @Test
71         public void testHostWithPortAndEscapedTopic () throws IOException
72         {
73                 final String rawTopic = "bar?bell";
74                 final String result = MRConstants.makeUrl ( rawTopic );
75                 assertEquals ( "/events/" + "bar%3Fbell", result );
76         }
77
78         @Test
79         public void testConsumerPlainHost () throws IOException
80         {
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 );
86         }
87
88         @Test
89         public void testCreateHostList ()
90         {
91                 final ArrayList<String> in = new ArrayList<String> ();
92                 in.add ( "foo" );
93                 in.add ( "bar" );
94                 in.add ( "baz:80" );
95
96                 final Collection<HttpHost> hosts = MRConstants.createHostsList ( in );
97                 assertEquals ( 3, hosts.size () );
98
99                 final Iterator<HttpHost> it = hosts.iterator ();
100                 final HttpHost first = it.next ();
101                 assertEquals ( MRConstants.kStdMRServicePort, first.getPort () );
102                 assertEquals ( "foo", first.getHostName () );
103
104                 final HttpHost second = it.next ();
105                 assertEquals ( MRConstants.kStdMRServicePort, second.getPort () );
106                 assertEquals ( "bar", second.getHostName () );
107
108                 final HttpHost third = it.next ();
109                 assertEquals ( 80, third.getPort () );
110                 assertEquals ( "baz", third.getHostName () );
111         }
112
113         private static final String[][] hostTests =
114         {
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 },
120         };
121
122         @Test
123         public void testHostParse ()
124         {
125                 for ( String[] test : hostTests )
126                 {
127                         final String hostIn = test[0];
128                         final String hostOut = test[1];
129                         final int portOut = Integer.parseInt ( test[2] );
130
131                         try
132                         {
133                                 final HttpHost hh = MRConstants.hostForString ( hostIn );
134                                 assertEquals ( hostOut, hh.getHostName () );
135                                 assertEquals ( portOut, hh.getPort () );
136                         }
137                         catch ( IllegalArgumentException x )
138                         {
139                                 assertEquals ( -1, portOut );
140                         }
141                 }
142         }
143 }