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.io.IOException;
25 import java.util.LinkedList;
26 import java.util.List;
28 import org.slf4j.Logger;
30 import org.onap.dmaap.mr.client.MRConsumer;
31 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
34 * A helper for unit testing systems that use a MRConsumer. When setting
35 * up your test, inject an instance into MRClientFactory to have it return
41 public class MRConsumerMock implements MRConsumer
45 public Entry ( long waitMs, int statusCode, List<String> msgs )
48 fStatusCode = statusCode;
50 fMsgs = new LinkedList<> ( msgs );
53 public Entry ( long waitMs, int statusCode, String statusMsg )
56 fStatusCode = statusCode;
57 fStatusMsg = statusMsg;
61 public LinkedList<String> run () throws IOException
65 Thread.sleep ( fWaitMs );
66 if ( fStatusCode >= 200 && fStatusCode <= 299 )
70 throw new IOException ( "" + fStatusCode + " " + fStatusMsg );
72 catch ( InterruptedException e )
74 Thread.currentThread().interrupt();
75 throw new IOException ( e );
79 private final long fWaitMs;
80 private final int fStatusCode;
81 private final String fStatusMsg;
82 private final LinkedList<String> fMsgs;
85 public MRConsumerMock ()
87 fReplies = new LinkedList<> ();
96 public void setApiCredentials ( String apiKey, String apiSecret )
101 public void clearApiCredentials ()
105 public synchronized void add ( Entry e )
110 public void addImmediateMsg ( String msg )
112 addDelayedMsg ( 0, msg );
115 public void addDelayedMsg ( long delay, String msg )
117 final LinkedList<String> list = new LinkedList<> ();
119 add ( new Entry ( delay, 200, list ) );
122 public void addImmediateMsgGroup ( List<String> msgs )
124 addDelayedMsgGroup ( 0, msgs );
127 public void addDelayedMsgGroup ( long delay, List<String> msgs )
129 final LinkedList<String> list = new LinkedList<> ( msgs );
130 add ( new Entry ( delay, 200, list ) );
133 public void addImmediateError ( int statusCode, String statusText )
135 add ( new Entry ( 0, statusCode, statusText ) );
139 public Iterable<String> fetch () throws IOException
141 return fetch ( -1, -1 );
145 public Iterable<String> fetch ( int timeoutMs, int limit ) throws IOException
147 return fReplies.size () > 0 ? fReplies.removeFirst ().run() : new LinkedList<String>();
151 public void logTo ( Logger log )
155 private final LinkedList<Entry> fReplies;
158 public MRConsumerResponse fetchWithReturnConsumerResponse() {
159 // TODO Auto-generated method stub
164 public MRConsumerResponse fetchWithReturnConsumerResponse(int timeoutMs,
166 // TODO Auto-generated method stub