d912f9df6dd6574d832036150c7ee53f2c23dd77
[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 public class MRConstantsTest extends TestCase
35 {
36         @Test
37         public void testPlainHost () throws IOException
38         {
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         {
47                 final String rawTopic = "bar";
48                 final String result = MRConstants.makeUrl (  rawTopic );
49                 assertEquals ( "/events/"  + "bar", result );
50         }
51
52         @Test
53         public void testHostWithProtocolAndPort () throws IOException
54         {
55                 final String rawTopic = "bar";
56                 final String result = MRConstants.makeUrl ( rawTopic );
57                 assertEquals ( "/events/" + "bar", result );
58         }
59
60         @Test
61         public void testHostWithPort () throws IOException
62         {
63                 final String rawTopic = "bar";
64                 final String result = MRConstants.makeUrl ( rawTopic );
65                 assertEquals ( "/events/" + "bar", result );
66         }
67
68         @Test
69         public void testHostWithPortAndEscapedTopic () throws IOException
70         {
71                 final String rawTopic = "bar?bell";
72                 final String result = MRConstants.makeUrl ( rawTopic );
73                 assertEquals ( "/events/" + "bar%3Fbell", result );
74         }
75
76         @Test
77         public void testConsumerPlainHost () throws IOException
78         {
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 );
84         }
85
86         @Test
87         public void testCreateHostList ()
88         {
89                 final ArrayList<String> in = new ArrayList<String> ();
90                 in.add ( "foo" );
91                 in.add ( "bar" );
92                 in.add ( "baz:80" );
93
94                 final Collection<HttpHost> hosts = MRConstants.createHostsList ( in );
95                 assertEquals ( 3, hosts.size () );
96
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 () );
101
102                 final HttpHost second = it.next ();
103                 assertEquals ( MRConstants.STD_MR_SERVICE_PORT, second.getPort () );
104                 assertEquals ( "bar", second.getHostName () );
105
106                 final HttpHost third = it.next ();
107                 assertEquals ( 80, third.getPort () );
108                 assertEquals ( "baz", third.getHostName () );
109         }
110
111         private static final String[][] hostTests =
112         {
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},
118         };
119
120         @Test
121         public void testHostParse ()
122         {
123                 for ( String[] test : hostTests )
124                 {
125                         final String hostIn = test[0];
126                         final String hostOut = test[1];
127                         final int portOut = Integer.parseInt ( test[2] );
128
129                         try
130                         {
131                                 final HttpHost hh = MRConstants.hostForString ( hostIn );
132                                 assertEquals ( hostOut, hh.getHostName () );
133                                 assertEquals ( portOut, hh.getPort () );
134                         }
135                         catch ( IllegalArgumentException x )
136                         {
137                                 assertEquals ( -1, portOut );
138                         }
139                 }
140         }
141 }