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.messagerouter.dmaapclient.nsa.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.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;
 
  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
 
  41 public class MRBatchingPublisherMock implements MRBatchingPublisher
 
  45                 public Entry ( String partition, String msg )
 
  47                         fPartition = partition;
 
  52                 public String toString ()
 
  57                 public final String fPartition;
 
  58                 public final String fMessage;
 
  61         public MRBatchingPublisherMock ()
 
  63                 fCaptures = new LinkedList<Entry> ();
 
  66         public interface Listener
 
  68                 void onMessage ( Entry e );
 
  70         public void addListener ( Listener listener )
 
  72                 fListeners.add ( listener );
 
  75         public List<Entry> getCaptures ()
 
  77                 return getCaptures ( new MessageFilter () { @Override public boolean match ( String msg ) { return true; } } );
 
  80         public interface MessageFilter
 
  82                 boolean match ( String msg );
 
  85         public List<Entry> getCaptures ( MessageFilter filter )
 
  87                 final LinkedList<Entry> result = new LinkedList<Entry> ();
 
  88                 for ( Entry capture : fCaptures )
 
  90                         if ( filter.match ( capture.fMessage ) )
 
  92                                 result.add ( capture );
 
  98         public int received ()
 
 100                 return fCaptures.size();
 
 109         public int send ( String partition, String msg )
 
 111                 final Entry e = new Entry ( partition, msg ); 
 
 114                 for ( Listener l : fListeners )
 
 122         public int send ( message msg )
 
 124                 return send ( msg.fPartition, msg.fMsg );
 
 127         public int send ( String msg )
 
 134         public int send ( Collection<message> msgs )
 
 137                 for ( message m : msgs )
 
 145         public int getPendingMessageCount ()
 
 151         public List<message> close ( long timeout, TimeUnit timeoutUnits )
 
 153                 return new LinkedList<message> ();
 
 162         public void setApiCredentials ( String apiKey, String apiSecret )
 
 167         public void clearApiCredentials ()
 
 172         public void logTo ( Logger log )
 
 176         private final LinkedList<Entry> fCaptures;
 
 177         private LinkedList<Listener> fListeners = new LinkedList<Listener> ();
 
 179         public MRPublisherResponse sendBatchWithResponse() {
 
 180                 // TODO Auto-generated method stub