1 /*******************************************************************************
2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Modifications Copyright © 2021 Orange.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
23 *******************************************************************************/
25 package org.onap.dmaap.mr.test.support;
27 import org.onap.dmaap.mr.client.MRBatchingPublisher;
28 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
29 import org.slf4j.Logger;
31 import java.util.Collection;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
37 * A helper for unit testing systems that use a MRPublisher. When setting
38 * up your test, inject an instance into MRClientFactory to have it return
43 public class MRBatchingPublisherMock implements MRBatchingPublisher {
45 public Entry(String partition, String msg) {
46 fPartition = partition;
51 public String toString() {
55 public final String fPartition;
56 public final String fMessage;
59 public MRBatchingPublisherMock() {
60 fCaptures = new LinkedList<>();
63 public interface Listener {
64 void onMessage(Entry e);
67 public void addListener(Listener listener) {
68 fListeners.add(listener);
71 public List<Entry> getCaptures() {
72 return getCaptures(new MessageFilter() {
74 public boolean match(String msg) {
80 public interface MessageFilter {
81 boolean match(String msg);
84 public List<Entry> getCaptures(MessageFilter filter) {
85 final LinkedList<Entry> result = new LinkedList<>();
86 for (Entry capture : fCaptures) {
87 if (filter.match(capture.fMessage)) {
94 public int received() {
95 return fCaptures.size();
103 public int send(String partition, String msg) {
104 final Entry e = new Entry(partition, msg);
107 for (Listener l : fListeners) {
114 public int send(Message msg) {
115 return send(msg.fPartition, msg.fMsg);
119 public int send(String msg) {
125 public int send(Collection<Message> msgs) {
127 for (Message m : msgs) {
134 public int getPendingMessageCount() {
139 public List<Message> close(long timeout, TimeUnit timeoutUnits) {
140 return new LinkedList<>();
144 public void close() {
148 public void setApiCredentials(String apiKey, String apiSecret) {
152 public void clearApiCredentials() {
156 public void logTo(Logger log) {
159 private final LinkedList<Entry> fCaptures;
160 private LinkedList<Listener> fListeners = new LinkedList<>();
163 public MRPublisherResponse sendBatchWithResponse() {
164 // TODO Auto-generated method stub