Skip to content

Commit

Permalink
chore: use Java 17 code style
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-cudennec authored and sliesche committed Jan 18, 2024
1 parent 36843d6 commit fbb8add
Show file tree
Hide file tree
Showing 53 changed files with 387 additions and 434 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
public class JerseyClientExampleApplication extends Application<JerseyClientExampleConfiguration> {

public static final String CARS_PATH = URI.create("cars").toString();
private JerseyClientBundle<Configuration> jerseyClientBundle =
private final JerseyClientBundle<Configuration> jerseyClientBundle =
JerseyClientBundle.builder().withConsumerTokenProvider(c -> "MyConsumerToken").build();

private Client externalClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,13 @@ private void setupTokenEndpoint(boolean withBody) {

if (withBody) {
response.withBody(
"{\"access_token\": \"xxxx.yyyy.zzzz\",\n"
+ " \"expires_in\": 60,\n"
+ " \"token_type\": \"Bearer\"}");
"""
{
"access_token": "xxxx.yyyy.zzzz",
"expires_in": 60,
"token_type": "Bearer"
}
""");
}

WIRE.stubFor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class ClientWithoutConsumerTokenTestApp extends Application<ClientTestConfig> {

private JerseyClientBundle<Configuration> jerseyClientBundle =
private final JerseyClientBundle<Configuration> jerseyClientBundle =
JerseyClientBundle.builder().build();

private ClientTestEndPoint clientTestEndPoint;
Expand Down Expand Up @@ -42,7 +42,7 @@ public void run(ClientTestConfig configuration, Environment environment) {
environment.jersey().register(clientTestEndPoint);
}

public JerseyClientBundle getJerseyClientBundle() {
public JerseyClientBundle<Configuration> getJerseyClientBundle() {
return jerseyClientBundle;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public class AuthBuilder {

private final RSAPrivateKey privateKey;

private String keyId;
private final String keyId;

private String issuer;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ public class OpaClassExtension extends AbstractOpa implements BeforeAllCallback,

private String opaClientTimeoutBackup = null;

/**
* @return the base url of the mock server
*/
public String getUrl() {
return wireMockExtension.baseUrl();
}

/** Resets all stubs and requests that were made to the mock server. */
public void reset() {
wireMockExtension.resetAll();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class AuthBuilderTest {

private AuthBuilder authBuilder = AuthClassExtension.builder().build().auth();
private final AuthBuilder authBuilder = AuthClassExtension.builder().build().auth();

@Test
void shouldAddIntegerClaim() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,7 @@ void shouldThrowExceptionIfUnexpectedToken() {
onRequest().withHttpMethod(method).withPath(path).withJwt(jwt);
// then

assertThrows(
VerificationException.class,
() -> {
OPA_EXTENSION.verify(1, requestExtraBuilder);
});
assertThrows(VerificationException.class, () -> OPA_EXTENSION.verify(1, requestExtraBuilder));
}

@RetryingTest(5)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ void shouldAllowAccess() {
// given
OPA_EXTENSION.mock(onRequest().withHttpMethod(method).withPath(path).allow());
// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
PrincipalInfo principalInfo = response.readEntity(PrincipalInfo.class);
assertThat(principalInfo.getConstraints().getConstraint()).isNull();
assertThat(principalInfo.getConstraints().isFullAccess()).isFalse();
assertThat(principalInfo.getJwt()).isNull();
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
PrincipalInfo principalInfo = response.readEntity(PrincipalInfo.class);
assertThat(principalInfo.getConstraints().getConstraint()).isNull();
assertThat(principalInfo.getConstraints().isFullAccess()).isFalse();
assertThat(principalInfo.getJwt()).isNull();
}
}

@Test
Expand All @@ -79,15 +80,17 @@ void shouldAllowAccessWithConstraints() {
.addConstraint("agent_ids", "A1")));

// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
PrincipalInfo principalInfo = response.readEntity(PrincipalInfo.class);

assertThat(principalInfo.getConstraints().getConstraint())
.contains(entry("customer_ids", asList("1", "2")), entry("agent_ids", singletonList("A1")));
assertThat(principalInfo.getConstraints().isFullAccess()).isFalse();
assertThat(principalInfo.getJwt()).isNull();
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
PrincipalInfo principalInfo = response.readEntity(PrincipalInfo.class);

assertThat(principalInfo.getConstraints().getConstraint())
.contains(
entry("customer_ids", asList("1", "2")), entry("agent_ids", singletonList("A1")));
assertThat(principalInfo.getConstraints().isFullAccess()).isFalse();
assertThat(principalInfo.getJwt()).isNull();
}
}

@Test
Expand All @@ -96,9 +99,10 @@ void shouldDenyAccess() {
// given
OPA_EXTENSION.mock(onRequest().withHttpMethod(method).withPath(path).deny());
// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
}
}

@Test
Expand All @@ -112,9 +116,10 @@ void shouldDenyAccessWithConstraints() {
.deny()
.withConstraint(new ConstraintModel().addConstraint("customer_is", "1")));
// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
}
}

@Test
Expand All @@ -123,9 +128,10 @@ void shouldDenyAccessIfOpaResponseIsBroken() {
// given
OPA_EXTENSION.mock(onAnyRequest().serverError());
// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
}
}

@Test
Expand All @@ -134,9 +140,10 @@ void shouldDenyAccessIfOpaResponseEmpty() {
// given
OPA_EXTENSION.mock(onAnyRequest().emptyResponse());
// when
Response response = doGetRequest();
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
try (Response response = doGetRequest()) {
// then
assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_FORBIDDEN);
}
}

@Test
Expand All @@ -146,10 +153,11 @@ void shouldAllowAccessForLongerPathAndPost() {
String longerPath = "resources/actions";
OPA_EXTENSION.mock(onRequest().withHttpMethod("POST").withPath(longerPath).allow());
// when
Response response = doPostRequest(longerPath);
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
OPA_EXTENSION.verify(1, "POST", longerPath);
try (Response response = doPostRequest(longerPath)) {
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
OPA_EXTENSION.verify(1, "POST", longerPath);
}
}

@Test
Expand All @@ -159,24 +167,26 @@ void shouldNotInvokeOpenApiUrls() {
String excludedPath = "openapi.json";
OPA_EXTENSION.mock(onRequest().withHttpMethod("GET").withPath(excludedPath).allow());
// when
Response response = doGetRequest(excludedPath);
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
OPA_EXTENSION.verify(0, "GET", excludedPath);
try (Response response = doGetRequest(excludedPath)) {
// then
assertThat(response.getStatus()).isEqualTo(SC_OK);
OPA_EXTENSION.verify(0, "GET", excludedPath);
}
}

@Test
@RetryingTest(5)
void shouldIncludeHealthCheck() {
Response response =
try (Response response =
DW.client()
.target("http://localhost:" + DW.getAdminPort()) // NOSONAR
.path("healthcheck")
.request(MediaType.APPLICATION_JSON_TYPE)
.get();
.get()) {

assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.readEntity(String.class)).contains(PolicyExistsHealthCheck.DEFAULT_NAME);
assertThat(response.getStatus()).isEqualTo(SC_INTERNAL_SERVER_ERROR);
assertThat(response.readEntity(String.class)).contains(PolicyExistsHealthCheck.DEFAULT_NAME);
}
}

private Response doGetRequest() {
Expand Down
Loading

0 comments on commit fbb8add

Please sign in to comment.