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