11package org .phoebus .channelfinder .processors .aa ;
22
3- import static org .junit .jupiter .api .Assertions .assertEquals ;
4- import static org .junit .jupiter .api .Assertions .assertTrue ;
3+ import static org .mockito .ArgumentMatchers .anyMap ;
4+ import static org .mockito .ArgumentMatchers .anyString ;
5+ import static org .mockito .Mockito .times ;
6+ import static org .mockito .Mockito .verify ;
7+ import static org .mockito .Mockito .when ;
58import static org .phoebus .channelfinder .processors .aa .AAChannelProcessorIT .activeProperty ;
69import static org .phoebus .channelfinder .processors .aa .AAChannelProcessorIT .archiveProperty ;
710import static org .phoebus .channelfinder .processors .aa .AAChannelProcessorIT .inactiveProperty ;
811
912import com .fasterxml .jackson .core .JsonProcessingException ;
10- import com .fasterxml .jackson .databind .ObjectMapper ;
11- import java .io .IOException ;
1213import java .util .List ;
1314import java .util .Map ;
14- import java .util .concurrent .TimeUnit ;
15- import java .util .concurrent .atomic .AtomicInteger ;
1615import java .util .stream .Collectors ;
1716import java .util .stream .IntStream ;
1817import java .util .stream .Stream ;
19- import okhttp3 .mockwebserver .MockResponse ;
20- import okhttp3 .mockwebserver .MockWebServer ;
21- import okhttp3 .mockwebserver .RecordedRequest ;
22- import org .junit .jupiter .api .AfterEach ;
23- import org .junit .jupiter .api .BeforeEach ;
2418import org .junit .jupiter .params .ParameterizedTest ;
2519import org .junit .jupiter .params .provider .Arguments ;
2620import org .junit .jupiter .params .provider .MethodSource ;
2721import org .phoebus .channelfinder .configuration .AAChannelProcessor ;
2822import org .phoebus .channelfinder .entity .Channel ;
23+ import org .phoebus .channelfinder .service .external .ArchiverService ;
2924import org .phoebus .channelfinder .service .model .archiver .aa .ArchiveAction ;
3025import org .springframework .beans .factory .annotation .Autowired ;
3126import org .springframework .boot .test .autoconfigure .web .servlet .WebMvcTest ;
3227import org .springframework .test .context .TestPropertySource ;
28+ import org .springframework .test .context .bean .override .mockito .MockitoBean ;
3329
3430@ WebMvcTest (AAChannelProcessor .class )
3531@ TestPropertySource (value = "classpath:application_test_multi.properties" )
@@ -40,26 +36,7 @@ class AAChannelProcessorMultiArchiverIT {
4036 public static final String NOT_BEING_ARCHIVED = "Not being archived" ;
4137 public static final String OWNER = "owner" ;
4238 @ Autowired AAChannelProcessor aaChannelProcessor ;
43-
44- MockWebServer mockQueryArchiverAppliance ;
45- MockWebServer mockPostArchiverAppliance ;
46- ObjectMapper objectMapper ;
47-
48- @ BeforeEach
49- void setUp () throws IOException {
50- mockQueryArchiverAppliance = new MockWebServer ();
51- mockQueryArchiverAppliance .start (17664 );
52- mockPostArchiverAppliance = new MockWebServer ();
53- mockPostArchiverAppliance .start (17665 );
54-
55- objectMapper = new ObjectMapper ();
56- }
57-
58- @ AfterEach
59- void teardown () throws IOException {
60- mockQueryArchiverAppliance .shutdown ();
61- mockPostArchiverAppliance .shutdown ();
62- }
39+ @ MockitoBean ArchiverService archiverService ;
6340
6441 static Stream <Arguments > provideArguments () {
6542 List <Channel > channels =
@@ -119,147 +96,44 @@ static Stream<Arguments> provideArguments() {
11996 void testProcessMultiArchivers (
12097 List <Channel > channels ,
12198 Map <String , String > namesToStatuses ,
122- Map <ArchiveAction , List <String >> actionsToNames ,
123- int expectedProcessedChannels )
124- throws JsonProcessingException , InterruptedException {
125-
126- // Request to policies
127- Map <String , String > policyList = Map .of ("policy" , "description" );
128- mockQueryArchiverAppliance .enqueue (
129- new MockResponse ()
130- .setBody (objectMapper .writeValueAsString (policyList ))
131- .addHeader ("Content-Type" , "application/json" ));
99+ Map <ArchiveAction , List <String >> actionsToNames )
100+ throws JsonProcessingException {
101+ when (archiverService .getAAPolicies (anyString ())).thenReturn (List .of ("policy" ));
132102
133103 // Request to archiver status
134104 List <Map <String , String >> archivePVStatuses =
135105 namesToStatuses .entrySet ().stream ()
136106 .map (entry -> Map .of ("pvName" , entry .getKey (), "status" , entry .getValue ()))
137107 .toList ();
138- mockQueryArchiverAppliance .enqueue (
139- new MockResponse ()
140- .setBody (objectMapper .writeValueAsString (archivePVStatuses ))
141- .addHeader ("Content-Type" , "application/json" ));
108+ when (archiverService .getStatuses (anyMap (), anyString (), anyString ()))
109+ .thenReturn (archivePVStatuses );
142110
143111 // Requests to archiver
144112 actionsToNames .forEach (
145113 (key , value ) -> {
146- List <Map <String , String >> archiverResponse =
147- value .stream ()
148- .map (channel -> Map .of ("pvName" , channel , "status" , key + " request submitted" ))
149- .toList ();
150- try {
151- mockQueryArchiverAppliance .enqueue (
152- new MockResponse ()
153- .setBody (objectMapper .writeValueAsString (archiverResponse ))
154- .addHeader ("Content-Type" , "application/json" ));
155- } catch (JsonProcessingException e ) {
156- throw new RuntimeException (e );
157- }
114+ when (archiverService .configureAA (anyMap (), anyString ())).thenReturn ((long ) value .size ());
158115 });
159116
160117 // Request to policies
161- mockPostArchiverAppliance .enqueue (
162- new MockResponse ()
163- .setBody (objectMapper .writeValueAsString (policyList ))
164- .addHeader ("Content-Type" , "application/json" ));
118+ when (archiverService .getAAPolicies (anyString ())).thenReturn (List .of ("policy" ));
165119
166120 // Request to archiver status
167- mockPostArchiverAppliance .enqueue (
168- new MockResponse ()
169- .setBody (objectMapper .writeValueAsString (archivePVStatuses ))
170- .addHeader ("Content-Type" , "application/json" ));
121+ when (archiverService .getStatuses (anyMap (), anyString (), anyString ()))
122+ .thenReturn (archivePVStatuses );
171123
172124 // Requests to archiver
173125 actionsToNames .forEach (
174126 (key , value ) -> {
175- List <Map <String , String >> archiverResponse =
176- value .stream ()
177- .map (channel -> Map .of ("pvName" , channel , "status" , key + " request submitted" ))
178- .toList ();
179- try {
180- mockPostArchiverAppliance .enqueue (
181- new MockResponse ()
182- .setBody (objectMapper .writeValueAsString (archiverResponse ))
183- .addHeader ("Content-Type" , "application/json" ));
184- } catch (JsonProcessingException e ) {
185- throw new RuntimeException (e );
186- }
127+ when (archiverService .configureAA (anyMap (), anyString ())).thenReturn ((long ) value .size ());
187128 });
188129
189130 aaChannelProcessor .process (channels );
190131
191- AtomicInteger expectedQueryRequests = new AtomicInteger (1 );
192- RecordedRequest requestQueryPolicy =
193- mockQueryArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
194- assert requestQueryPolicy != null ;
195- assertEquals ("/mgmt/bpl/getPolicyList" , requestQueryPolicy .getPath ());
196-
197- expectedQueryRequests .addAndGet (1 );
198- RecordedRequest requestQueryStatus =
199- mockQueryArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
200- assert requestQueryStatus != null ;
201- assert requestQueryStatus .getRequestUrl () != null ;
202- assertEquals ("/mgmt/bpl/getPVStatus" , requestQueryStatus .getRequestUrl ().encodedPath ());
203-
204- AtomicInteger expectedPostRequests = new AtomicInteger (1 );
205- RecordedRequest requestPostPolicy = mockPostArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
206- assert requestPostPolicy != null ;
207- assertEquals ("/mgmt/bpl/getPolicyList" , requestPostPolicy .getPath ());
208-
209- expectedPostRequests .addAndGet (1 );
210- RecordedRequest requestPostStatus = mockPostArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
211- assert requestPostStatus != null ;
212- assert requestPostStatus .getRequestUrl () != null ;
213- assertEquals ("/mgmt/bpl/getPVStatus" , requestPostStatus .getRequestUrl ().encodedPath ());
214-
215- while (mockQueryArchiverAppliance .getRequestCount () > 0 ) {
216- RecordedRequest requestAction = null ;
217- try {
218- requestAction = mockQueryArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
219- } catch (InterruptedException e ) {
220- throw new RuntimeException (e );
221- }
222- if (requestAction == null ) {
223- break ;
224- }
225- expectedQueryRequests .addAndGet (1 );
226- assert requestAction .getPath () != null ;
227- assertTrue (requestAction .getPath ().startsWith ("/mgmt/bpl" ));
228- ArchiveAction key =
229- actionFromEndpoint (requestAction .getPath ().substring ("/mgmt/bpl" .length ()));
230- String body = requestAction .getBody ().readUtf8 ();
231- actionsToNames .get (key ).forEach (pv -> assertTrue (body .contains (pv )));
232- }
233-
234- while (mockPostArchiverAppliance .getRequestCount () > 0 ) {
235- RecordedRequest requestAction = null ;
236- try {
237- requestAction = mockPostArchiverAppliance .takeRequest (2 , TimeUnit .SECONDS );
238- } catch (InterruptedException e ) {
239- throw new RuntimeException (e );
240- }
241- if (requestAction == null ) {
242- break ;
243- }
244- expectedPostRequests .addAndGet (1 );
245- assert requestAction .getPath () != null ;
246- assertTrue (requestAction .getPath ().startsWith ("/mgmt/bpl" ));
247- ArchiveAction key =
248- actionFromEndpoint (requestAction .getPath ().substring ("/mgmt/bpl" .length ()));
249- String body = requestAction .getBody ().readUtf8 ();
250- actionsToNames .get (key ).forEach (pv -> assertTrue (body .contains (pv )));
251- }
252-
253- assertEquals (mockPostArchiverAppliance .getRequestCount (), expectedPostRequests .get ());
254- assertEquals (mockQueryArchiverAppliance .getRequestCount (), expectedQueryRequests .get ());
255- }
132+ // Verifications
133+ verify (archiverService , times (2 )).getAAPolicies (anyString ());
256134
257- public ArchiveAction actionFromEndpoint (final String endpoint ) {
258- for (ArchiveAction action : ArchiveAction .values ()) {
259- if (action .getEndpoint ().equals (endpoint )) {
260- return action ;
261- }
135+ if (!namesToStatuses .isEmpty ()) {
136+ verify (archiverService , times (2 )).getStatuses (anyMap (), anyString (), anyString ());
262137 }
263- return null ;
264138 }
265139}
0 commit comments