23
23
import org .apache .http .HttpHost ;
24
24
import org .elasticsearch .client .RequestOptions ;
25
25
import org .elasticsearch .client .RestClient ;
26
+ import org .junit .AfterClass ;
27
+ import org .junit .BeforeClass ;
26
28
import org .junit .Test ;
27
29
30
+ import java .io .IOException ;
31
+
28
32
import static co .elastic .clients .base .UserAgent .DEFAULT_NAME ;
29
33
import static co .elastic .clients .base .UserAgent .DEFAULT_VERSION ;
30
34
import static org .junit .Assert .assertEquals ;
@@ -41,32 +45,40 @@ public class UserAgentTest {
41
45
private static final String CUSTOM_NAME_2 = "MegaClient" ;
42
46
private static final String CUSTOM_VERSION_2 = "6.7.8" ;
43
47
48
+ public static RestClient restClient ;
49
+
50
+ @ BeforeClass
51
+ public static void setUp () {
52
+ restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
53
+ }
54
+
55
+ @ AfterClass
56
+ public static void tearDown () throws IOException {
57
+ restClient .close ();
58
+ }
59
+
44
60
@ Test
45
61
public void testDefaultUserAgent () throws Exception {
46
- RestClient restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
47
62
Transport transport = new RestClientTransport (restClient , null );
48
63
assertEquals (DEFAULT_USER_AGENT , transport .userAgent ());
49
64
}
50
65
51
66
@ Test
52
67
public void testCustomUserAgent () throws Exception {
53
- RestClient restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
54
68
Transport transport = new RestClientTransport (restClient , null , null ,
55
69
new UserAgent (CUSTOM_NAME , CUSTOM_VERSION ));
56
70
assertEquals (CUSTOM_USER_AGENT , transport .userAgent ());
57
71
}
58
72
59
73
@ Test
60
74
public void testManualUserAgent () throws Exception {
61
- RestClient restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
62
75
Transport transport = new RestClientTransport (restClient , null ,
63
76
RequestOptions .DEFAULT .toBuilder ().addHeader ("User-Agent" , CUSTOM_USER_AGENT ).build ());
64
77
assertEquals (CUSTOM_USER_AGENT , transport .userAgent ());
65
78
}
66
79
67
80
@ Test
68
81
public void testMultipleUserAgentsThrowsException () throws Exception {
69
- RestClient restClient = RestClient .builder (new HttpHost ("localhost" , 9200 )).build ();
70
82
Exception exception = assertThrows (IllegalArgumentException .class , () -> {
71
83
Transport transport = new RestClientTransport (restClient , null ,
72
84
RequestOptions .DEFAULT .toBuilder ().addHeader ("User-Agent" , CUSTOM_USER_AGENT ).build (),
0 commit comments