Skip to content

Commit 48b9cd0

Browse files
fix: enhance branch coverage
1 parent 5414157 commit 48b9cd0

File tree

2 files changed

+143
-17
lines changed

2 files changed

+143
-17
lines changed

src/test/java/com/getindata/connectors/http/internal/utils/ConfigUtilsTest.java

Lines changed: 114 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.getindata.connectors.http.internal.utils;
22

3+
import java.net.*;
4+
import java.util.Arrays;
35
import java.util.Map;
46
import java.util.Optional;
57
import java.util.Properties;
@@ -51,29 +53,133 @@ public void shouldConvertNoProperty() {
5153
}
5254

5355
@Test
54-
public void shouldGetProxyConfigWithAuthenticator() {
56+
public void shouldGetProxyConfigWithAuthenticator() throws UnknownHostException {
5557
String proxyHost = "proxy";
56-
Integer proxyPort = 8080;
58+
Integer proxyPort = 9090;
5759
Optional<String> proxyUsername = Optional.of("username");
5860
Optional<String> proxyPassword = Optional.of("password");
5961

6062
ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword );
6163
assertThat(proxyConfig.getHost().equals("proxy"));
62-
assertThat(proxyConfig.getPort() == 8080);
6364
assertThat(proxyConfig.getAuthenticator().isPresent());
65+
66+
PasswordAuthentication auth = proxyConfig.getAuthenticator().orElseGet(null)
67+
.requestPasswordAuthenticationInstance(
68+
"proxy", // host
69+
InetAddress.getByName("127.0.0.1"), // address
70+
9090, // port
71+
"http", // protocol
72+
"Please authenticate", // prompt
73+
"basic", // scheme
74+
null, // URL
75+
Authenticator.RequestorType.SERVER // Requestor type
76+
);
77+
78+
PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null)
79+
.requestPasswordAuthenticationInstance(
80+
"proxy", // host
81+
InetAddress.getByName("127.0.0.1"), // address
82+
9090, // port
83+
"http", // protocol
84+
"Please authenticate", // prompt
85+
"basic", // scheme
86+
null, // URL
87+
Authenticator.RequestorType.PROXY // Requestor type
88+
);
89+
90+
assertThat(auth).isNull();
91+
assertThat(auth2).isNotNull();
92+
assertThat(auth2.getUserName().equals("username")).isTrue();
93+
assertThat(Arrays.equals(auth2.getPassword(), "password".toCharArray())).isTrue();
6494
}
6595

6696
@Test
67-
public void shouldGetProxyConfigWithoutAuthenticator() {
97+
public void shouldGetProxyConfigWithAuthenticatorServer() throws UnknownHostException {
6898
String proxyHost = "proxy";
6999
Integer proxyPort = 8080;
70100
Optional<String> proxyUsername = Optional.of("username");
71-
Optional<String> proxyPassword = Optional.empty();
101+
Optional<String> proxyPassword = Optional.of("password");
72102

73103
ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword );
74-
assertThat(proxyConfig.getHost().equals("proxy"));
75-
assertThat(proxyConfig.getPort() == 8080);
76-
assertThat(proxyConfig.getAuthenticator().isEmpty());
104+
assertThat(proxyConfig.getHost().equals("proxy")).isTrue();
105+
assertThat(proxyConfig.getAuthenticator().isPresent()).isTrue();
106+
107+
PasswordAuthentication auth = proxyConfig.getAuthenticator().orElseGet(null)
108+
.requestPasswordAuthenticationInstance(
109+
"proxy", // host
110+
InetAddress.getByName("127.0.0.1"), // address
111+
8080, // port
112+
"http", // protocol
113+
"Please authenticate", // prompt
114+
"basic", // scheme
115+
null, // URL
116+
Authenticator.RequestorType.SERVER // Requestor type
117+
);
118+
119+
PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null)
120+
.requestPasswordAuthenticationInstance(
121+
"proxy", // host
122+
InetAddress.getByName("127.0.0.1"), // address
123+
8080, // port
124+
"http", // protocol
125+
"Please authenticate", // prompt
126+
"basic", // scheme
127+
null, // URL
128+
Authenticator.RequestorType.PROXY // Requestor type
129+
);
130+
131+
assertThat(auth).isNull();
132+
assertThat(auth2).isNotNull();
133+
}
134+
135+
@Test
136+
public void shouldGetProxyConfigWithAuthenticatorWrongHost() throws UnknownHostException {
137+
String proxyHost = "proxy";
138+
Integer proxyPort = 8080;
139+
Optional<String> proxyUsername = Optional.of("username");
140+
Optional<String> proxyPassword = Optional.of("password");
141+
142+
ProxyConfig proxyConfig = new ProxyConfig(proxyHost, proxyPort, proxyUsername, proxyPassword );
143+
assertThat(proxyConfig.getHost().equals("proxy")).isTrue();
144+
assertThat(proxyConfig.getAuthenticator().isPresent()).isTrue();
145+
146+
PasswordAuthentication auth = proxyConfig.getAuthenticator().get()
147+
.requestPasswordAuthenticationInstance(
148+
"wrong", // host
149+
InetAddress.getByName("127.0.0.1"), // address
150+
8080, // port
151+
"http", // protocol
152+
"Please authenticate", // prompt
153+
"basic", // scheme
154+
null, // URL
155+
Authenticator.RequestorType.PROXY // Requestor type
156+
);
157+
158+
PasswordAuthentication auth2 = proxyConfig.getAuthenticator().orElseGet(null)
159+
.requestPasswordAuthenticationInstance(
160+
"proxy", // host
161+
InetAddress.getByName("127.0.0.1"), // address
162+
8080, // port
163+
"http", // protocol
164+
"Please authenticate", // prompt
165+
"basic", // scheme
166+
null, // URL
167+
Authenticator.RequestorType.PROXY // Requestor type
168+
);
169+
170+
assertThat(auth).isNull();
171+
assertThat(auth2).isNotNull();
172+
}
173+
174+
@Test
175+
public void shouldGetProxyConfigWithoutAuthenticator() throws MalformedURLException, UnknownHostException {
176+
String proxyHost = "proxy";
177+
Optional<String> proxyUsername = Optional.of("username");
178+
Optional<String> proxyPassword = Optional.empty();
179+
180+
ProxyConfig proxyConfig = new ProxyConfig(proxyHost, 80, proxyUsername, proxyPassword );
181+
assertThat(proxyConfig.getHost().equals("proxy")).isTrue();
182+
assertThat(proxyConfig.getAuthenticator().isEmpty()).isTrue();
77183
}
78184

79185
@Test

src/test/java/com/getindata/connectors/http/internal/utils/JavaNetHttpClientFactoryTest.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
package com.getindata.connectors.http.internal.utils;
22

3+
import java.net.Authenticator;
4+
import java.net.InetAddress;
5+
import java.net.PasswordAuthentication;
6+
import java.net.UnknownHostException;
37
import java.net.http.HttpClient;
8+
import java.util.Arrays;
49
import java.util.Properties;
510
import java.util.concurrent.ExecutorService;
611
import java.util.concurrent.Executors;
712

813
import org.apache.flink.configuration.Configuration;
914
import org.apache.flink.util.concurrent.ExecutorThreadFactory;
1015
import org.junit.jupiter.api.Test;
16+
import static org.assertj.core.api.Assertions.assertThat;
1117

1218
import com.getindata.connectors.http.internal.table.lookup.HttpLookupConfig;
1319
import com.getindata.connectors.http.internal.table.lookup.Slf4JHttpLookupPostRequestCallback;
@@ -16,7 +22,7 @@
1622
class JavaNetHttpClientFactoryTest {
1723

1824
@Test
19-
public void shouldGetClientWithAuthenticator() {
25+
public void shouldGetClientWithAuthenticator() throws UnknownHostException {
2026
Properties properties = new Properties();
2127
Configuration configuration = new Configuration();
2228
configuration.setString(SOURCE_PROXY_HOST, "google");
@@ -33,12 +39,26 @@ public void shouldGetClientWithAuthenticator() {
3339

3440
HttpClient client = JavaNetHttpClientFactory.createClient(lookupConfig);
3541

36-
assert(client.authenticator().isPresent());
37-
assert(client.proxy().isPresent());
42+
assertThat(client.authenticator().isPresent()).isTrue();
43+
assertThat(client.proxy().isPresent()).isTrue();
44+
45+
PasswordAuthentication auth = client.authenticator().get().requestPasswordAuthenticationInstance(
46+
"google", // host
47+
InetAddress.getByName("127.0.0.1"), // address
48+
8080, // port
49+
"http", // protocol
50+
"Please authenticate", // prompt
51+
"basic", // scheme
52+
null, // URL
53+
Authenticator.RequestorType.PROXY // Requestor type
54+
);
55+
56+
assertThat(auth.getUserName().equals("username")).isTrue();
57+
assertThat(Arrays.equals(auth.getPassword(), "password".toCharArray())).isTrue();
3858
}
3959

4060
@Test
41-
public void shouldGetClientWithoutAuthenticator() {
61+
public void shouldGetClientWithoutAuthenticator() throws UnknownHostException {
4262
Properties properties = new Properties();
4363
Configuration configuration = new Configuration();
4464
configuration.setString(SOURCE_PROXY_HOST, "google");
@@ -53,8 +73,8 @@ public void shouldGetClientWithoutAuthenticator() {
5373

5474
HttpClient client = JavaNetHttpClientFactory.createClient(lookupConfig);
5575

56-
assert(client.authenticator().isEmpty());
57-
assert(client.proxy().isPresent());
76+
assertThat(client.authenticator().isEmpty()).isTrue();
77+
assertThat(client.proxy().isPresent()).isTrue();
5878
}
5979

6080
@Test
@@ -70,8 +90,8 @@ public void shouldGetClientWithoutProxy() {
7090
.build();
7191

7292
HttpClient client = JavaNetHttpClientFactory.createClient(lookupConfig);
73-
assert(client.authenticator().isEmpty());
74-
assert(client.proxy().isEmpty());
93+
assertThat(client.authenticator().isEmpty()).isTrue();
94+
assertThat(client.proxy().isEmpty()).isTrue();
7595
}
7696

7797
@Test
@@ -86,7 +106,7 @@ public void shouldGetClientWithExecutor() {
86106
);
87107

88108
HttpClient client = JavaNetHttpClientFactory.createClient(properties, httpClientExecutor);
89-
assert(client.followRedirects().equals(HttpClient.Redirect.NORMAL));
109+
assertThat(client.followRedirects().equals(HttpClient.Redirect.NORMAL)).isTrue();
90110
}
91111

92112
}

0 commit comments

Comments
 (0)