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