Skip to content

Commit fb761c3

Browse files
committed
add more test cases
1 parent c94ac62 commit fb761c3

File tree

8 files changed

+478
-202
lines changed

8 files changed

+478
-202
lines changed

.github/workflows/ci-java.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ on:
2828
env:
2929
CLIENT_ID: ${{ secrets.CLIENT_ID }}
3030
CLIENT_SECRET: ${{secrets.CLIENT_SECRET}}
31-
VAAS_URL: "wss://gateway.production.vaas.gdatasecurity.de"
31+
VAAS_URL: "https://gateway.production.vaas.gdatasecurity.de"
3232
TOKEN_URL: "https://account.gdata.de/realms/vaas-production/protocol/openid-connect/token"
3333
VAAS_CLIENT_ID: ${{ secrets.VAAS_CLIENT_ID }}
3434
VAAS_USER_NAME: ${{ secrets.VAAS_USER_NAME }}
@@ -54,7 +54,7 @@ jobs:
5454
run: |
5555
echo "CLIENT_ID=${{ secrets.STAGING_CLIENT_ID }}" >> $GITHUB_ENV
5656
echo "CLIENT_SECRET=${{ secrets.STAGING_CLIENT_SECRET }}" >> $GITHUB_ENV
57-
echo "VAAS_URL=wss://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
57+
echo "VAAS_URL=https://gateway.staging.vaas.gdatasecurity.de" >> $GITHUB_ENV
5858
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-staging/protocol/openid-connect/token" >> $GITHUB_ENV
5959
echo "VAAS_CLIENT_ID=${{ secrets.STAGING_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
6060
echo "VAAS_USER_NAME=${{ secrets.STAGING_VAAS_USER_NAME }}" >> $GITHUB_ENV
@@ -65,7 +65,7 @@ jobs:
6565
run: |
6666
echo "CLIENT_ID=${{ secrets.DEVELOP_CLIENT_ID }}" >> $GITHUB_ENV
6767
echo "CLIENT_SECRET=${{ secrets.DEVELOP_CLIENT_SECRET }}" >> $GITHUB_ENV
68-
echo "VAAS_URL=wss://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
68+
echo "VAAS_URL=https://gateway.develop.vaas.gdatasecurity.de" >> $GITHUB_ENV
6969
echo "TOKEN_URL=https://account-staging.gdata.de/realms/vaas-develop/protocol/openid-connect/token" >> $GITHUB_ENV
7070
echo "VAAS_CLIENT_ID=${{ secrets.DEVELOP_VAAS_CLIENT_ID }}" >> $GITHUB_ENV
7171
echo "VAAS_USER_NAME=${{ secrets.DEVELOP_VAAS_USER_NAME }}" >> $GITHUB_ENV

java/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ dependencies {
2525
implementation 'io.github.cdimascio:dotenv-java:3.0.2'
2626
testImplementation 'org.testng:testng:7.10.2'
2727
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.11.3'
28+
testImplementation 'org.mockito:mockito-core:5.14.2'
29+
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.11.3'
2830
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.11.3'
2931
testRuntimeOnly 'org.junit.platform:junit-platform-launcher:1.11.3'
3032

java/src/main/java/de/gdata/vaas/ClientCredentialsGrantAuthenticator.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package de.gdata.vaas;
22

33
import com.google.gson.JsonParser;
4+
5+
import de.gdata.vaas.exceptions.VaasAuthenticationException;
46
import lombok.Getter;
57
import lombok.NonNull;
68
import org.jetbrains.annotations.NotNull;
@@ -27,13 +29,21 @@ public class ClientCredentialsGrantAuthenticator implements IAuthenticator {
2729
@NonNull
2830
private final URI tokenEndpoint;
2931

30-
private static final HttpClient httpClient = HttpClient.newHttpClient();
32+
private final HttpClient httpClient;
3133

3234

3335
public ClientCredentialsGrantAuthenticator(String clientId, String clientSecret, @NotNull URI tokenEndpoint) {
3436
this.tokenEndpoint = tokenEndpoint;
3537
this.clientId = clientId;
3638
this.clientSecret = clientSecret;
39+
this.httpClient = HttpClient.newHttpClient();
40+
}
41+
42+
public ClientCredentialsGrantAuthenticator(String clientId, String clientSecret, @NotNull URI tokenEndpoint, HttpClient httpClient) {
43+
this.tokenEndpoint = tokenEndpoint;
44+
this.clientId = clientId;
45+
this.clientSecret = clientSecret;
46+
this.httpClient = httpClient;
3747
}
3848

3949
public ClientCredentialsGrantAuthenticator(String clientId, String clientSecret)
@@ -45,7 +55,7 @@ private String encodeValue(String value) throws UnsupportedEncodingException {
4555
return URLEncoder.encode(value, StandardCharsets.UTF_8);
4656
}
4757

48-
public String getToken() throws IOException, InterruptedException {
58+
public String getToken() throws IOException, InterruptedException, VaasAuthenticationException {
4959
Map<String, String> requestParams = new HashMap<>();
5060
requestParams.put("client_id", this.clientId);
5161
requestParams.put("grant_type", "client_credentials");
@@ -69,8 +79,7 @@ public String getToken() throws IOException, InterruptedException {
6979
.send(request, HttpResponse.BodyHandlers.ofString());
7080

7181
if (response.statusCode() != 200) {
72-
throw new IOException("Failed to upload file. HTTP Status Code: " + response.statusCode() + " Error: "
73-
+ response.body());
82+
throw new VaasAuthenticationException();
7483
}
7584
var bodyJsonObject = JsonParser.parseString(response.body()).getAsJsonObject();
7685
var tokenJsonElement = bodyJsonObject.get("access_token");

java/src/main/java/de/gdata/vaas/IAuthenticator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44

5+
import de.gdata.vaas.exceptions.VaasAuthenticationException;
6+
57
public interface IAuthenticator {
68
/**
79
* Get access token from identity provider
@@ -10,6 +12,7 @@ public interface IAuthenticator {
1012
* request
1113
* @throws InterruptedException if the operation is interrupted
1214
* @return the access token
15+
* @throws VaasAuthenticationException
1316
*/
14-
public String getToken() throws IOException, InterruptedException;
17+
public String getToken() throws IOException, InterruptedException, VaasAuthenticationException;
1518
}

java/src/main/java/de/gdata/vaas/IVaas.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,22 @@
88
import java.security.NoSuchAlgorithmException;
99
import java.util.concurrent.CompletableFuture;
1010

11+
import de.gdata.vaas.exceptions.VaasAuthenticationException;
12+
import de.gdata.vaas.exceptions.VaasClientException;
1113
import de.gdata.vaas.messages.VaasVerdict;
1214
import de.gdata.vaas.options.ForFileOptions;
1315
import de.gdata.vaas.options.ForSha256Options;
1416
import de.gdata.vaas.options.ForStreamOptions;
1517
import de.gdata.vaas.options.ForUrlOptions;
1618

1719
public interface IVaas {
18-
public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256) throws URISyntaxException, IOException, InterruptedException;
19-
public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256, ForSha256Options options) throws URISyntaxException, IOException, InterruptedException;
20-
public CompletableFuture<VaasVerdict> forStream(InputStream stream, long contentLength) throws URISyntaxException, IOException, InterruptedException;
21-
public CompletableFuture<VaasVerdict> forStream(InputStream stream, long contentLength, ForStreamOptions options) throws URISyntaxException, IOException, InterruptedException;
22-
public CompletableFuture<VaasVerdict> forFile(Path file) throws NoSuchAlgorithmException, IOException, URISyntaxException, InterruptedException;
23-
public CompletableFuture<VaasVerdict> forFile(Path file, ForFileOptions options) throws NoSuchAlgorithmException, IOException, URISyntaxException, InterruptedException;
24-
public CompletableFuture<VaasVerdict> forUrl(URL url) throws URISyntaxException, IOException, InterruptedException;
25-
public CompletableFuture<VaasVerdict> forUrl(URL url, ForUrlOptions options) throws URISyntaxException, IOException, InterruptedException;
20+
public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256) throws URISyntaxException, IOException, InterruptedException, VaasClientException, VaasAuthenticationException;
21+
public CompletableFuture<VaasVerdict> forSha256(Sha256 sha256, ForSha256Options options) throws URISyntaxException, IOException, InterruptedException, VaasClientException, VaasAuthenticationException;
22+
public CompletableFuture<VaasVerdict> forStream(InputStream stream, long contentLength) throws URISyntaxException, IOException, InterruptedException, VaasAuthenticationException;
23+
public CompletableFuture<VaasVerdict> forStream(InputStream stream, long contentLength, ForStreamOptions options) throws URISyntaxException, IOException, InterruptedException, VaasAuthenticationException;
24+
public CompletableFuture<VaasVerdict> forFile(Path file) throws NoSuchAlgorithmException, IOException, URISyntaxException, InterruptedException, VaasAuthenticationException;
25+
public CompletableFuture<VaasVerdict> forFile(Path file, ForFileOptions options) throws NoSuchAlgorithmException, IOException, URISyntaxException, InterruptedException, VaasAuthenticationException;
26+
public CompletableFuture<VaasVerdict> forUrl(URL url) throws URISyntaxException, IOException, InterruptedException, VaasAuthenticationException;
27+
public CompletableFuture<VaasVerdict> forUrl(URL url, ForUrlOptions options) throws URISyntaxException, IOException, InterruptedException, VaasAuthenticationException;
2628

2729
}

java/src/main/java/de/gdata/vaas/ResourceOwnerPasswordGrantAuthenticator.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package de.gdata.vaas;
22

33
import com.google.gson.JsonParser;
4+
5+
import de.gdata.vaas.exceptions.VaasAuthenticationException;
46
import lombok.Getter;
57
import lombok.NonNull;
68
import org.jetbrains.annotations.NotNull;
@@ -28,13 +30,22 @@ public class ResourceOwnerPasswordGrantAuthenticator implements IAuthenticator {
2830
@NonNull
2931
private final URI tokenEndpoint;
3032

31-
private static final HttpClient httpClient = HttpClient.newHttpClient();
33+
private final HttpClient httpClient;
3234

3335
public ResourceOwnerPasswordGrantAuthenticator(String clientId, String username, String password, @NotNull URI tokenEndpoint) {
3436
this.tokenEndpoint = tokenEndpoint;
3537
this.clientId = clientId;
3638
this.username = username;
3739
this.password = password;
40+
this.httpClient = HttpClient.newHttpClient();
41+
}
42+
43+
public ResourceOwnerPasswordGrantAuthenticator(String clientId, String username, String password, @NotNull URI tokenEndpoint, HttpClient httpClient) {
44+
this.tokenEndpoint = tokenEndpoint;
45+
this.clientId = clientId;
46+
this.username = username;
47+
this.password = password;
48+
this.httpClient = httpClient;
3849
}
3950

4051
public ResourceOwnerPasswordGrantAuthenticator(String clientId, String username, String password)
@@ -46,7 +57,7 @@ private String encodeValue(String value) throws UnsupportedEncodingException {
4657
return URLEncoder.encode(value, StandardCharsets.UTF_8);
4758
}
4859

49-
public String getToken() throws IOException, InterruptedException {
60+
public String getToken() throws IOException, InterruptedException, VaasAuthenticationException {
5061
Map<String, String> requestParams = new HashMap<>();
5162
requestParams.put("client_id", this.clientId);
5263
requestParams.put("grant_type", "password");
@@ -71,8 +82,7 @@ public String getToken() throws IOException, InterruptedException {
7182
.send(request, HttpResponse.BodyHandlers.ofString());
7283

7384
if (response.statusCode() != 200) {
74-
throw new IOException("Failed to upload file. HTTP Status Code: " + response.statusCode() + " Error: "
75-
+ response.body());
85+
throw new VaasAuthenticationException();
7686
}
7787
var bodyJsonObject = JsonParser.parseString(response.body()).getAsJsonObject();
7888
var tokenJsonElement = bodyJsonObject.get("access_token");

0 commit comments

Comments
 (0)