[DMAAP-CLIENT] First sonar issues review part2
[dmaap/messagerouter/dmaapclient.git] / src / test / java / org / onap / dmaap / mr / test / support / MRBatchingPublisherMock.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
13  *
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=========================================================
20  *
21  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  *
23  *******************************************************************************/
24
25 package org.onap.dmaap.mr.test.support;
26
27 import org.onap.dmaap.mr.client.MRBatchingPublisher;
28 import org.onap.dmaap.mr.client.response.MRPublisherResponse;
29 import org.slf4j.Logger;
30
31 import java.util.Collection;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.concurrent.TimeUnit;
35
36 /**
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
39  * the mock client.
40  *
41  * @author author
42  */
43 public class MRBatchingPublisherMock implements MRBatchingPublisher {
44     public class Entry {
45         public Entry(String partition, String msg) {
46             fPartition = partition;
47             fMessage = msg;
48         }
49
50         @Override
51         public String toString() {
52             return fMessage;
53         }
54
55         public final String fPartition;
56         public final String fMessage;
57     }
58
59     public MRBatchingPublisherMock() {
60         fCaptures = new LinkedList<>();
61     }
62
63     public interface Listener {
64         void onMessage(Entry e);
65     }
66
67     public void addListener(Listener listener) {
68         fListeners.add(listener);
69     }
70
71     public List<Entry> getCaptures() {
72         return getCaptures(new MessageFilter() {
73             @Override
74             public boolean match(String msg) {
75                 return true;
76             }
77         });
78     }
79
80     public interface MessageFilter {
81         boolean match(String msg);
82     }
83
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)) {
88                 result.add(capture);
89             }
90         }
91         return result;
92     }
93
94     public int received() {
95         return fCaptures.size();
96     }
97
98     public void reset() {
99         fCaptures.clear();
100     }
101
102     @Override
103     public int send(String partition, String msg) {
104         final Entry e = new Entry(partition, msg);
105
106         fCaptures.add(e);
107         for (Listener l : fListeners) {
108             l.onMessage(e);
109         }
110         return 1;
111     }
112
113     @Override
114     public int send(Message msg) {
115         return send(msg.fPartition, msg.fMsg);
116     }
117
118     @Override
119     public int send(String msg) {
120         return 1;
121
122     }
123
124     @Override
125     public int send(Collection<Message> msgs) {
126         int sum = 0;
127         for (Message m : msgs) {
128             sum += send(m);
129         }
130         return sum;
131     }
132
133     @Override
134     public int getPendingMessageCount() {
135         return 0;
136     }
137
138     @Override
139     public List<Message> close(long timeout, TimeUnit timeoutUnits) {
140         return new LinkedList<>();
141     }
142
143     @Override
144     public void close() {
145     }
146
147     @Override
148     public void setApiCredentials(String apiKey, String apiSecret) {
149     }
150
151     @Override
152     public void clearApiCredentials() {
153     }
154
155     @Override
156     public void logTo(Logger log) {
157     }
158
159     private final LinkedList<Entry> fCaptures;
160     private LinkedList<Listener> fListeners = new LinkedList<>();
161
162     @Override
163     public MRPublisherResponse sendBatchWithResponse() {
164         // TODO Auto-generated method stub
165         return null;
166     }
167 }