First sonar issues review
[dmaap/messagerouter/dmaapclient.git] / src / main / java / org / onap / dmaap / mr / test / support / MRConsumerMock.java
1 /*******************************************************************************
2  *  ============LICENSE_START=======================================================
3  *  org.onap.dmaap
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
11  *  
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=========================================================
18  *
19  *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
20  *  
21  *******************************************************************************/
22 package org.onap.dmaap.mr.test.support;
23
24 import java.io.IOException;
25 import java.util.LinkedList;
26 import java.util.List;
27
28 import org.slf4j.Logger;
29
30 import org.onap.dmaap.mr.client.MRConsumer;
31 import org.onap.dmaap.mr.client.response.MRConsumerResponse;
32
33 /**
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
36  * the mock client.
37  * 
38  * @author author
39  *
40  */
41 public class MRConsumerMock implements MRConsumer
42 {
43         public class Entry
44         {
45                 public Entry ( long waitMs, int statusCode, List<String> msgs )
46                 {
47                         fWaitMs = waitMs;
48                         fStatusCode = statusCode;
49                         fStatusMsg = null;
50                         fMsgs = new LinkedList<> ( msgs );
51                 }
52
53                 public Entry ( long waitMs, int statusCode, String statusMsg )
54                 {
55                         fWaitMs = waitMs;
56                         fStatusCode = statusCode;
57                         fStatusMsg = statusMsg;
58                         fMsgs = null;
59                 }
60
61                 public LinkedList<String> run () throws IOException
62                 {
63                         try
64                         {
65                                 Thread.sleep ( fWaitMs );
66                                 if ( fStatusCode >= 200 && fStatusCode <= 299 )
67                                 {
68                                         return fMsgs;
69                                 }
70                                 throw new IOException ( "" + fStatusCode + " " + fStatusMsg );
71                         }
72                         catch ( InterruptedException e )
73                         {
74                                 Thread.currentThread().interrupt();
75                                 throw new IOException ( e );
76                         }
77                 }
78
79                 private final long fWaitMs;
80                 private final int fStatusCode;
81                 private final String fStatusMsg;
82                 private final LinkedList<String> fMsgs;
83         }
84
85         public MRConsumerMock ()
86         {
87                 fReplies = new LinkedList<> ();
88         }
89
90         @Override
91         public void close ()
92         {
93         }
94
95         @Override
96         public void setApiCredentials ( String apiKey, String apiSecret )
97         {
98         }
99
100         @Override
101         public void clearApiCredentials ()
102         {
103         }
104
105         public synchronized void add ( Entry e )
106         {
107                 fReplies.add ( e );
108         }
109
110         public void addImmediateMsg ( String msg )
111         {
112                 addDelayedMsg ( 0, msg );
113         }
114
115         public void addDelayedMsg ( long delay, String msg )
116         {
117                 final LinkedList<String> list = new LinkedList<> ();
118                 list.add ( msg );
119                 add ( new Entry ( delay, 200, list ) );
120         }
121
122         public void addImmediateMsgGroup ( List<String> msgs )
123         {
124                 addDelayedMsgGroup ( 0, msgs );
125         }
126
127         public void addDelayedMsgGroup ( long delay, List<String> msgs )
128         {
129                 final LinkedList<String> list = new LinkedList<> ( msgs );
130                 add ( new Entry ( delay, 200, list ) );
131         }
132
133         public void addImmediateError ( int statusCode, String statusText )
134         {
135                 add ( new Entry ( 0, statusCode, statusText ) );
136         }
137
138         @Override
139         public Iterable<String> fetch () throws IOException
140         {
141                 return fetch ( -1, -1 );
142         }
143
144         @Override
145         public Iterable<String> fetch ( int timeoutMs, int limit ) throws IOException
146         {
147                 return fReplies.size () > 0 ? fReplies.removeFirst ().run() : new LinkedList<String>();
148         }
149
150         @Override
151         public void logTo ( Logger log )
152         {
153         }
154
155         private final LinkedList<Entry> fReplies;
156
157         @Override
158         public MRConsumerResponse fetchWithReturnConsumerResponse() {
159                 // TODO Auto-generated method stub
160                 return null;
161         }
162
163         @Override
164         public MRConsumerResponse fetchWithReturnConsumerResponse(int timeoutMs,
165                         int limit) {
166                 // TODO Auto-generated method stub
167                 return null;
168         }
169 }