1
1
/*
2
- * Copyright 2020 the original author or authors.
2
+ * Copyright 2020-2021 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
19
19
import static org .elasticsearch .search .internal .SearchContext .*;
20
20
import static org .mockito .Mockito .*;
21
21
22
- import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
23
22
import reactor .core .publisher .Mono ;
23
+ import reactor .test .StepVerifier ;
24
24
25
+ import java .net .URI ;
26
+ import java .util .Optional ;
25
27
import java .util .function .Function ;
26
28
29
+ import org .elasticsearch .ElasticsearchStatusException ;
30
+ import org .elasticsearch .action .get .GetRequest ;
27
31
import org .elasticsearch .action .search .SearchRequest ;
28
32
import org .elasticsearch .client .Request ;
29
33
import org .elasticsearch .index .query .QueryBuilders ;
30
34
import org .elasticsearch .search .builder .SearchSourceBuilder ;
31
- import org .junit .jupiter .api .BeforeEach ;
35
+ import org .elasticsearch .search .fetch .subphase .FetchSourceContext ;
36
+ import org .junit .jupiter .api .DisplayName ;
32
37
import org .junit .jupiter .api .Test ;
33
38
import org .junit .jupiter .api .extension .ExtendWith ;
34
39
import org .mockito .ArgumentCaptor ;
35
40
import org .mockito .Mock ;
41
+ import org .mockito .Spy ;
36
42
import org .mockito .junit .jupiter .MockitoExtension ;
43
+ import org .springframework .http .HttpStatus ;
37
44
import org .springframework .web .reactive .function .client .ClientResponse ;
38
- import reactor .test .StepVerifier ;
45
+ import org .springframework .web .reactive .function .client .WebClient ;
46
+ import org .springframework .web .util .UriBuilder ;
39
47
40
48
/**
41
49
* @author Peter-Josef Meisch
@@ -46,30 +54,24 @@ class DefaultReactiveElasticsearchClientTest {
46
54
@ Mock private HostProvider hostProvider ;
47
55
48
56
@ Mock private Function <SearchRequest , Request > searchRequestConverter ;
57
+ @ Spy private RequestCreator requestCreator ;
49
58
50
- private DefaultReactiveElasticsearchClient client ;
51
-
52
- @ BeforeEach
53
- void setUp () {
54
- client = new DefaultReactiveElasticsearchClient (hostProvider , new RequestCreator () {
55
- @ Override
56
- public Function <SearchRequest , Request > search () {
57
- return searchRequestConverter ;
58
- }
59
- }) {
60
- @ Override
61
- public Mono <ClientResponse > execute (ReactiveElasticsearchClientCallback callback ) {
62
- return Mono .empty ();
63
- }
64
- };
65
- }
59
+ @ Mock private WebClient webClient ;
66
60
67
61
@ Test
68
62
void shouldSetAppropriateRequestParametersOnCount () {
69
63
64
+ when (requestCreator .search ()).thenReturn (searchRequestConverter );
70
65
SearchRequest searchRequest = new SearchRequest ("someindex" ) //
71
66
.source (new SearchSourceBuilder ().query (QueryBuilders .matchAllQuery ()));
72
67
68
+ ReactiveElasticsearchClient client = new DefaultReactiveElasticsearchClient (hostProvider , requestCreator ) {
69
+ @ Override
70
+ public Mono <ClientResponse > execute (ReactiveElasticsearchClientCallback callback ) {
71
+ return Mono .empty ();
72
+ }
73
+ };
74
+
73
75
client .count (searchRequest ).as (StepVerifier ::create ).verifyComplete ();
74
76
75
77
ArgumentCaptor <SearchRequest > captor = ArgumentCaptor .forClass (SearchRequest .class );
@@ -79,4 +81,31 @@ void shouldSetAppropriateRequestParametersOnCount() {
79
81
assertThat (source .trackTotalHitsUpTo ()).isEqualTo (TRACK_TOTAL_HITS_ACCURATE );
80
82
assertThat (source .fetchSource ()).isEqualTo (FetchSourceContext .DO_NOT_FETCH_SOURCE );
81
83
}
84
+
85
+ @ Test // #1712
86
+ @ DisplayName ("should throw ElasticsearchStatusException on server 5xx with empty body" )
87
+ void shouldThrowElasticsearchStatusExceptionOnServer5xxWithEmptyBody () {
88
+
89
+ when (hostProvider .getActive (any ())).thenReturn (Mono .just (webClient ));
90
+ WebClient .RequestBodyUriSpec requestBodyUriSpec = mock (WebClient .RequestBodyUriSpec .class );
91
+ when (requestBodyUriSpec .uri ((Function <UriBuilder , URI >) any ())).thenReturn (requestBodyUriSpec );
92
+ when (requestBodyUriSpec .attribute (any (), any ())).thenReturn (requestBodyUriSpec );
93
+ when (requestBodyUriSpec .headers (any ())).thenReturn (requestBodyUriSpec );
94
+ when (webClient .method (any ())).thenReturn (requestBodyUriSpec );
95
+
96
+ ClientResponse clientResponse = mock (ClientResponse .class );
97
+ when (clientResponse .statusCode ()).thenReturn (HttpStatus .SERVICE_UNAVAILABLE );
98
+ ClientResponse .Headers headers = mock (ClientResponse .Headers .class );
99
+ when (headers .contentType ()).thenReturn (Optional .empty ());
100
+ when (clientResponse .headers ()).thenReturn (headers );
101
+ when (clientResponse .body (any ())).thenReturn (Mono .empty ());
102
+ when (requestBodyUriSpec .exchange ()).thenReturn (Mono .just (clientResponse ));
103
+
104
+ ReactiveElasticsearchClient client = new DefaultReactiveElasticsearchClient (hostProvider , requestCreator );
105
+
106
+ client .get (new GetRequest ("42" )) //
107
+ .as (StepVerifier ::create ) //
108
+ .expectError (ElasticsearchStatusException .class ) //
109
+ .verify (); //
110
+ }
82
111
}
0 commit comments