153ed297002f9836c1faa2dab49bd652a698b95e
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / client / impl / MRConsumerImplTest.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.LinkedList;
26 import java.util.Properties;
27
28 import junit.framework.TestCase;
29
30 import org.junit.Test;
31
32 import org.onap.dmaap.mr.client.impl.MRConstants;
33 import org.onap.dmaap.mr.client.impl.MRConsumerImpl;
34
35 public class MRConsumerImplTest extends TestCase
36 {
37         @Test
38         public void testNullFilter () throws IOException
39         {
40                 final LinkedList<String> hosts = new LinkedList<String> ();
41                 hosts.add ( "localhost:8080" );
42                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, -1, null, null, null );
43                 final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid","http" ), -1, -1 );
44                 assertEquals ("http://localhost:8080/events/" + "topic/cg/cid", url );
45         }
46
47         @Test
48         public void testFilterWithNoTimeoutOrLimit () throws IOException
49         {
50                 final LinkedList<String> hosts = new LinkedList<String> ();
51                 hosts.add ( "localhost:8080" );
52                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, -1, "filter", null, null );
53                 final String url = c.createUrlPath ( MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid" ,"http"),-1, -1 );
54                 assertEquals ("http://localhost:8080/events/" + "topic/cg/cid?filter=filter", url );
55         }
56
57         @Test
58         public void testTimeoutNoLimitNoFilter () throws IOException
59         {
60                 final LinkedList<String> hosts = new LinkedList<String> ();
61                 hosts.add ( "localhost:8080" );
62                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", 30000, -1, null, null, null );
63                 final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid","http" ), 30000, -1 );
64                 assertEquals ( "http://localhost:8080/events/"  + "topic/cg/cid?timeout=30000", url );
65         }
66
67         @Test
68         public void testNoTimeoutWithLimitNoFilter () throws IOException
69         {
70                 final LinkedList<String> hosts = new LinkedList<String> ();
71                 hosts.add ( "localhost:8080" );
72                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, 100, null, null, null );
73                 final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid","http" ), -1, 100 );
74                 assertEquals ( "http://localhost:8080/events/"  + "topic/cg/cid?limit=100", url );
75         }
76
77         @Test
78         public void testWithTimeoutWithLimitWithFilter () throws IOException
79         {
80                 final LinkedList<String> hosts = new LinkedList<String> ();
81                 hosts.add ( "localhost:8080" );
82                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", 1000, 400, "f", null, null );
83                 final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid" ,"http"), 1000, 400 );
84                 assertEquals ("http://localhost:8080/events/"  + "topic/cg/cid?timeout=1000&limit=400&filter=f", url );
85         }
86
87         @Test
88         public void testFilterEncoding () throws IOException
89         {
90                 final LinkedList<String> hosts = new LinkedList<String> ();
91                 hosts.add ( "localhost:8080" );
92                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }", null, null );
93                 final String url = c.createUrlPath (MRConstants.makeConsumerUrl ( "localhost:8080", "topic", "cg", "cid","http" ), -1, -1 );
94                 assertEquals ( "http://localhost:8080/events/"  + "topic/cg/cid?filter=%7B+%22foo%22%3D%22bar%22bar%22+%7D", url );
95         }
96         
97         @Test
98         public void testFetchWithReturnConsumerResponse () throws IOException
99         {
100                 final LinkedList<String> hosts = new LinkedList<String> ();
101                 hosts.add ( "localhost:8080" );
102                 Properties properties = new Properties();
103                 properties.load(MRSimplerBatchConsumerTest.class.getClassLoader().getResourceAsStream("dme2/consumer.properties"));
104                 
105                 final MRConsumerImpl c = new MRConsumerImpl ( hosts, "topic", "cg", "cid", -1, -1, "{ \"foo\"=\"bar\"bar\" }", null, null );
106                 c.fetchWithReturnConsumerResponse();
107             c.setProtocolFlag("HTTPAAF");
108                 c.fetchWithReturnConsumerResponse();
109                 assertTrue(true);
110         }
111 }