update the package name
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / support / MRBatchingPublisherMock.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.test.support;
23
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28
29 import org.slf4j.Logger;
30
31 import org.onap.dmaap.mr.client.MRBatchingPublisher;
32 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
33
34 /**
35  * A helper for unit testing systems that use a MRPublisher. When setting
36  * up your test, inject an instance into MRClientFactory to have it return
37  * the mock client.
38  * 
39  * @author author
40  *
41  */
42 public class MRBatchingPublisherMock implements MRBatchingPublisher
43 {
44         public class Entry
45         {
46                 public Entry ( String partition, String msg )
47                 {
48                         fPartition = partition;
49                         fMessage = msg;
50                 }
51
52                 @Override
53                 public String toString ()
54                 {
55                         return fMessage;
56                 }
57                 
58                 public final String fPartition;
59                 public final String fMessage;
60         }
61
62         public MRBatchingPublisherMock ()
63         {
64                 fCaptures = new LinkedList<> ();
65         }
66
67         public interface Listener
68         {
69                 void onMessage ( Entry e );
70         }
71         public void addListener ( Listener listener )
72         {
73                 fListeners.add ( listener );
74         }
75         
76         public List<Entry> getCaptures ()
77         {
78                 return getCaptures ( new MessageFilter () { @Override public boolean match ( String msg ) { return true; } } );
79         }
80
81         public interface MessageFilter
82         {
83                 boolean match ( String msg );
84         }
85
86         public List<Entry> getCaptures ( MessageFilter filter )
87         {
88                 final LinkedList<Entry> result = new LinkedList<> ();
89                 for ( Entry capture : fCaptures )
90                 {
91                         if ( filter.match ( capture.fMessage ) )
92                         {
93                                 result.add ( capture );
94                         }
95                 }
96                 return result;
97         }
98
99         public int received ()
100         {
101                 return fCaptures.size();
102         }
103
104         public void reset ()
105         {
106                 fCaptures.clear ();
107         }
108
109         @Override
110         public int send ( String partition, String msg )
111         {
112                 final Entry e = new Entry ( partition, msg ); 
113
114                 fCaptures.add ( e );
115                 for ( Listener l : fListeners )
116                 {
117                         l.onMessage ( e );
118                 }
119                 return 1;
120         }
121
122         @Override
123         public int send ( message msg )
124         {
125                 return send ( msg.fPartition, msg.fMsg );
126         }
127         @Override
128         public int send ( String msg )
129         {
130                 return 1;
131                 
132         }
133
134         @Override
135         public int send ( Collection<message> msgs )
136         {
137                 int sum = 0;
138                 for ( message m : msgs )
139                 {
140                         sum += send ( m );
141                 }
142                 return sum;
143         }
144
145         @Override
146         public int getPendingMessageCount ()
147         {
148                 return 0;
149         }
150
151         @Override
152         public List<message> close ( long timeout, TimeUnit timeoutUnits )
153         {
154                 return new LinkedList<> ();
155         }
156
157         @Override
158         public void close ()
159         {
160         }
161
162         @Override
163         public void setApiCredentials ( String apiKey, String apiSecret )
164         {
165         }
166
167         @Override
168         public void clearApiCredentials ()
169         {
170         }
171
172         @Override
173         public void logTo ( Logger log )
174         {
175         }
176
177         private final LinkedList<Entry> fCaptures;
178         private LinkedList<Listener> fListeners = new LinkedList<> ();
179         @Override
180         public MRPublisherResponse sendBatchWithResponse() {
181                 // TODO Auto-generated method stub
182                 return null;
183         }
184 }