1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
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
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=========================================================
19 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
21 *******************************************************************************/
22 package org.onap.dmaap.mr.test.support;
24 import java.util.Collection;
25 import java.util.LinkedList;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
29 import org.slf4j.Logger;
31 import org.onap.dmaap.mr.client.MRBatchingPublisher;
32 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
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
42 public class MRBatchingPublisherMock implements MRBatchingPublisher
46 public Entry ( String partition, String msg )
48 fPartition = partition;
53 public String toString ()
58 public final String fPartition;
59 public final String fMessage;
62 public MRBatchingPublisherMock ()
64 fCaptures = new LinkedList<> ();
67 public interface Listener
69 void onMessage ( Entry e );
71 public void addListener ( Listener listener )
73 fListeners.add ( listener );
76 public List<Entry> getCaptures ()
78 return getCaptures ( new MessageFilter () { @Override public boolean match ( String msg ) { return true; } } );
81 public interface MessageFilter
83 boolean match ( String msg );
86 public List<Entry> getCaptures ( MessageFilter filter )
88 final LinkedList<Entry> result = new LinkedList<> ();
89 for ( Entry capture : fCaptures )
91 if ( filter.match ( capture.fMessage ) )
93 result.add ( capture );
99 public int received ()
101 return fCaptures.size();
110 public int send ( String partition, String msg )
112 final Entry e = new Entry ( partition, msg );
115 for ( Listener l : fListeners )
123 public int send ( message msg )
125 return send ( msg.fPartition, msg.fMsg );
128 public int send ( String msg )
135 public int send ( Collection<message> msgs )
138 for ( message m : msgs )
146 public int getPendingMessageCount ()
152 public List<message> close ( long timeout, TimeUnit timeoutUnits )
154 return new LinkedList<> ();
163 public void setApiCredentials ( String apiKey, String apiSecret )
168 public void clearApiCredentials ()
173 public void logTo ( Logger log )
177 private final LinkedList<Entry> fCaptures;
178 private LinkedList<Listener> fListeners = new LinkedList<> ();
180 public MRPublisherResponse sendBatchWithResponse() {
181 // TODO Auto-generated method stub