3131import static org .testng .Assert .fail ;
3232
3333public class ProxyTests extends BaseIntegrationTest {
34- private Client client ;
34+ private ThreadLocal < Client > client = new ThreadLocal <>() ;
3535
36- private WireMockServer proxy = new WireMockServer (8666 );
37-
38- private WireMockServer privateProxy = new WireMockServer (WireMockConfiguration .options ()
39- .port (8667 )
40- .notifier (new Slf4jNotifier (true )));
36+ private ThreadLocal <WireMockServer > proxy = new ThreadLocal <>();
4137
4238 @ BeforeMethod (groups = { "integration" })
4339 public void setUp () throws IOException {
44- proxy .start ();
45- ClickHouseNode node = getServer (ClickHouseProtocol .HTTP );
46- proxy .addStubMapping (post (urlMatching ("/.*" ))
47- .willReturn (aResponse ().proxiedFrom ("http://localhost:" + node .getPort ())).build ());
48- client = new Client .Builder ()
49- .addEndpoint (Protocol .HTTP , "clickhouse" , 8123 , false )
50- .setUsername ("default" )
51- .setPassword ("" )
52- .useNewImplementation (System .getProperty ("client.tests.useNewImplementation" , "false" ).equals ("true" ))
53- .addProxy (ProxyType .HTTP , "localhost" , 8666 )
54- .build ();
55-
56- // private proxy
57- privateProxy .start ();
58- // stub order - from least specific to most specific
59- privateProxy .addStubMapping (post (urlMatching ("/.*" ))
60- .willReturn (aResponse ()
61- .withStatus (HttpStatus .SC_PROXY_AUTHENTICATION_REQUIRED )
62- .withHeader (HttpHeaders .PROXY_AUTHENTICATE , "Basic realm=\" Access DB\" " )).build ());
63- privateProxy .addStubMapping (post (urlMatching ("/.*" ))
64- .withHeader (HttpHeaders .PROXY_AUTHORIZATION , equalTo ("Basic dXNlcjpwYXNz" ))
65- .willReturn (aResponse ().proxiedFrom ("http://localhost:" + node .getPort ())).build ());
6640 }
6741
6842 @ AfterMethod (groups = { "integration" })
6943 public void teardown () {
70- proxy .stop ();
71- privateProxy .stop ();
72- client .close ();
44+ proxy .get ().stop ();
45+ client .get ().close ();
7346 }
7447
75- private void createTable (String tableQuery ) throws ClickHouseException {
76- try (ClickHouseClient client = ClickHouseClient .builder ().config (new ClickHouseConfig ())
77- .nodeSelector (ClickHouseNodeSelector .of (ClickHouseProtocol .HTTP ))
78- .build ()) {
79- client .read (getServer (ClickHouseProtocol .HTTP )).query (tableQuery ).executeAndWait ().close ();
80- }
81- }
82-
8348 @ Test (groups = { "integration" })
8449 public void testSimpleQuery () throws Exception {
85- List <GenericRecord > records = client .queryAll ("select timezone()" );
50+ client .set (clientBuilder (initProxy (), false ).build ());
51+ addProxyStub ();
52+
53+ List <GenericRecord > records = client .get ().queryAll ("select timezone()" );
8654 Assert .assertEquals (records .stream ().findFirst ().get ().getString (1 ), "UTC" );
8755 }
8856
8957 @ Test (groups = { "integration" })
9058 public void testInsert () throws Exception {
9159 String tableName = "simple_pojo_disable_proxy_table" ;
9260 String createSQL = SamplePOJO .generateTableCreateSQL (tableName );
93- System . out . println ( createSQL );
94- createTable ( createSQL );
61+ client . set ( clientBuilder ( initProxy (), false ). build () );
62+ addProxyStub ( );
9563
96- client .register (SamplePOJO .class , client .getTableSchema (tableName , "default" ));
64+ client .get ().execute (createSQL ).get ();
65+ client .get ().register (SamplePOJO .class , client .get ().getTableSchema (tableName , "default" ));
9766 List <Object > simplePOJOs = new ArrayList <>();
9867
9968 for (int i = 0 ; i < 1000 ; i ++) {
10069 simplePOJOs .add (new SamplePOJO ());
10170 }
10271
10372 try {
104- InsertResponse response = client .insert (tableName , simplePOJOs ).get (120 , TimeUnit .SECONDS );
73+ InsertResponse response = client .get (). insert (tableName , simplePOJOs ).get (120 , TimeUnit .SECONDS );
10574 Assert .assertEquals (response .getWrittenRows (), 1000 );
10675 } catch (Exception e ) {
10776 fail ("Should not have thrown exception." , e );
@@ -110,50 +79,63 @@ public void testInsert() throws Exception {
11079
11180 @ Test (groups = { "integration" })
11281 public void testPrivateProxyWithoutAuth () {
113- Client localClient = null ;
82+ client .set (clientBuilder (initProxy (), true ).build ());
83+ addPrivateProxyStub ();
84+
11485 try {
115- localClient = new Client .Builder ()
116- .addEndpoint (Protocol .HTTP , "clickhouse" , 8123 , false )
117- .setUsername ("user" )
118- .setPassword ("" )
119- .useNewImplementation (true )
120- // .useNewImplementation(System.getProperty("client.tests.useNewImplementation", "false").equals("true"))
121- .addProxy (ProxyType .HTTP , "localhost" , 8667 )
122- .build ();
123- List <GenericRecord > records = localClient .queryAll ("select timezone()" );
86+ client .get ().execute ("select 1" ).get ();
12487 Assert .fail ("Should have thrown exception." );
125- } catch (ClientException e ) {
88+ } catch (Exception e ) {
12689 e .printStackTrace ();
12790 Assert .assertTrue (e .getCause () instanceof ClientMisconfigurationException );
128- } finally {
129- if (localClient != null ) {
130- localClient .close ();
131- }
132-
13391 }
13492 }
13593
13694 @ Test (groups = { "integration" })
13795 public void testPrivateProxyWithCredentials () {
138- Client localClient = null ;
96+ client .set (clientBuilder (initProxy (), true )
97+ .setProxyCredentials ("user" , "pass" ).build ());
98+ addPrivateProxyStub ();
99+
139100 try {
140- localClient = new Client .Builder ()
141- .addEndpoint (Protocol .HTTP , "clickhouse" , 8123 , false )
142- .setUsername ("user" )
143- .setPassword ("" )
144- .useNewImplementation (true )
145- .addProxy (ProxyType .HTTP , "localhost" , 8667 )
146- .setProxyCredentials ("user" , "pass" )
147- .build ();
148- List <GenericRecord > records = localClient .queryAll ("select timezone()" );
149- Assert .assertEquals (records .stream ().findFirst ().get ().getString (1 ), "UTC" );
150- } catch (Exception e ) {
101+ client .get ().execute ("select 1" );
102+ } catch (ClientException e ) {
103+ e .printStackTrace ();
151104 Assert .fail ("Should not have thrown exception." , e );
152- } finally {
153- if (localClient != null ) {
154- localClient .close ();
155- }
156-
157105 }
158106 }
107+
108+ private Client .Builder clientBuilder (int proxyPort , boolean onlyNewImplementation ) {
109+ return new Client .Builder ()
110+ .addEndpoint (Protocol .HTTP , "clickhouse" , 8123 , false )
111+ .setUsername ("default" )
112+ .setPassword ("" )
113+ .useNewImplementation (onlyNewImplementation ? onlyNewImplementation :
114+ System .getProperty ("client.tests.useNewImplementation" , "false" ).equals ("true" ))
115+ .addProxy (ProxyType .HTTP , "localhost" , proxyPort );
116+ }
117+
118+ private int initProxy () {
119+ WireMockServer wireMock = new WireMockServer (WireMockConfiguration .options ().notifier (new Slf4jNotifier (true )));
120+ wireMock .start ();
121+ proxy .set (wireMock );
122+ return wireMock .port ();
123+ }
124+
125+ private void addProxyStub () {
126+ final int targetPort = getServer (ClickHouseProtocol .HTTP ).getPort ();
127+ proxy .get ().addStubMapping (post (urlMatching ("/.*" ))
128+ .willReturn (aResponse ().proxiedFrom ("http://localhost:" + targetPort )).build ());
129+ }
130+
131+ private void addPrivateProxyStub () {
132+ final int targetPort = getServer (ClickHouseProtocol .HTTP ).getPort ();
133+ proxy .get ().addStubMapping (post (urlMatching ("/.*" ))
134+ .willReturn (aResponse ()
135+ .withStatus (HttpStatus .SC_PROXY_AUTHENTICATION_REQUIRED )
136+ .withHeader (HttpHeaders .PROXY_AUTHENTICATE , "Basic realm=\" Access DB\" " )).build ());
137+ proxy .get ().addStubMapping (post (urlMatching ("/.*" ))
138+ .withHeader (HttpHeaders .PROXY_AUTHORIZATION , equalTo ("Basic dXNlcjpwYXNz" ))
139+ .willReturn (aResponse ().proxiedFrom ("http://localhost:" + targetPort )).build ());
140+ }
159141}
0 commit comments