Skip to content

Commit

Permalink
[Enhancement] Replace JUnit assertEquals() with Hamcrest matchers ass…
Browse files Browse the repository at this point in the history
…ertThat() (opensearch-project#4544)

Signed-off-by: Prabhas Kurapati <[email protected]>
  • Loading branch information
prabhask5 authored Jul 10, 2024
1 parent 9d32a8c commit 0b2109a
Show file tree
Hide file tree
Showing 155 changed files with 4,279 additions and 3,675 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.apache.hc.core5.reactor.ssl.TlsDetails;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Assume;
import org.junit.Before;

Expand All @@ -48,10 +47,12 @@
import org.opensearch.security.bwc.helper.RestHelper;
import org.opensearch.test.rest.OpenSearchRestTestCase;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.anyOf;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.is;

public class SecurityBackwardsCompatibilityIT extends OpenSearchRestTestCase {

Expand Down Expand Up @@ -199,7 +200,7 @@ public void testDataIngestionAndSearchBackwardsCompatibility() throws Exception

public void testNodeStats() throws IOException {
List<Response> responses = RestHelper.requestAgainstAllNodes(client(), "GET", "_nodes/stats", null);
responses.forEach(r -> Assert.assertEquals(200, r.getStatusLine().getStatusCode()));
responses.forEach(r -> assertThat(r.getStatusLine().getStatusCode(), is(200)));
}

@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -246,7 +247,7 @@ private void ingestData(String index) throws IOException {
"_bulk?refresh=wait_for",
RestHelper.toHttpEntity(bulkRequestBody.toString())
);
responses.forEach(r -> assertEquals(200, r.getStatusLine().getStatusCode()));
responses.forEach(r -> assertThat(r.getStatusLine().getStatusCode(), is(200)));
}
}

Expand All @@ -264,7 +265,7 @@ private void searchMatchAll(String index) throws IOException {
index + "/_search",
RestHelper.toHttpEntity(matchAllQuery)
);
responses.forEach(r -> assertEquals(200, r.getStatusLine().getStatusCode()));
responses.forEach(r -> assertThat(r.getStatusLine().getStatusCode(), is(200)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@ public void parallelPutRequests() throws Exception {
case HttpStatus.SC_OK:
break;
default:
Assert.assertEquals(HttpStatus.SC_CONFLICT, sc);
assertThat(sc, is(HttpStatus.SC_CONFLICT));
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,12 @@
import org.opensearch.transport.BindTransportException;

import static java.util.Objects.requireNonNull;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.opensearch.test.framework.cluster.NodeType.CLIENT;
import static org.opensearch.test.framework.cluster.NodeType.CLUSTER_MANAGER;
import static org.opensearch.test.framework.cluster.NodeType.DATA;
import static org.opensearch.test.framework.cluster.PortAllocator.TCP;
import static org.junit.Assert.assertEquals;

/**
* Encapsulates all the logic to start a local OpenSearch cluster - without any configuration of the security plugin.
Expand Down Expand Up @@ -339,7 +340,7 @@ public void waitForCluster(ClusterHealthStatus status, TimeValue timeout, int ex
log.debug("... cluster state ok {} with {} nodes", healthResponse.getStatus().name(), healthResponse.getNumberOfNodes());
}

assertEquals(expectedNodeCount, healthResponse.getNumberOfNodes());
assertThat(healthResponse.getNumberOfNodes(), is(expectedNodeCount));

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

Expand Down Expand Up @@ -156,11 +158,11 @@ public void testParsePrevGeneratedJwt() {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("horst", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
Assert.assertEquals(5, credentials.getAttributes().size());
Assert.assertEquals("854113533", credentials.getAttributes().get("attr.jwt.nbf"));
Assert.assertEquals("4853843133", credentials.getAttributes().get("attr.jwt.exp"));
assertThat(credentials.getUsername(), is("horst"));
assertThat(credentials.getBackendRoles().size(), is(0));
assertThat(credentials.getAttributes().size(), is(5));
assertThat(credentials.getAttributes().get("attr.jwt.nbf"), is("854113533"));
assertThat(credentials.getAttributes().get("attr.jwt.exp"), is("4853843133"));
}

@Test
Expand Down Expand Up @@ -211,9 +213,9 @@ public void testBearer() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
Assert.assertEquals(2, credentials.getAttributes().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(0));
assertThat(credentials.getAttributes().size(), is(2));
}

@Test
Expand Down Expand Up @@ -259,8 +261,8 @@ public void testRoles() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(2, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(2));
}

@Test
Expand All @@ -272,8 +274,8 @@ public void testNullClaim() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -285,8 +287,8 @@ public void testNonStringClaim() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(1, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(1));
Assert.assertTrue(credentials.getBackendRoles().contains("123"));
}

Expand All @@ -299,8 +301,8 @@ public void testRolesMissing() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -323,8 +325,8 @@ public void testAlternativeSubject() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Dr. Who", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Dr. Who"));
assertThat(credentials.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -336,8 +338,8 @@ public void testNonStringAlternativeSubject() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("false", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("false"));
assertThat(credentials.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -358,8 +360,8 @@ public void testUrlParam() throws Exception {
AuthCredentials credentials = jwtAuth.extractCredentials(req.asSecurityRequest(), null);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
Assert.assertEquals(0, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
assertThat(credentials.getBackendRoles().size(), is(0));
}

@Test
Expand Down Expand Up @@ -411,8 +413,8 @@ public void testRS256() throws Exception {
);

Assert.assertNotNull(creds);
Assert.assertEquals("Leonard McCoy", creds.getUsername());
Assert.assertEquals(0, creds.getBackendRoles().size());
assertThat(creds.getUsername(), is("Leonard McCoy"));
assertThat(creds.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -437,8 +439,8 @@ public void testES512() throws Exception {
);

Assert.assertNotNull(creds);
Assert.assertEquals("Leonard McCoy", creds.getUsername());
Assert.assertEquals(0, creds.getBackendRoles().size());
assertThat(creds.getUsername(), is("Leonard McCoy"));
assertThat(creds.getBackendRoles().size(), is(0));
}

@Test
Expand All @@ -452,8 +454,8 @@ public void testRolesArray() throws Exception {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("John Doe", credentials.getUsername());
Assert.assertEquals(3, credentials.getBackendRoles().size());
assertThat(credentials.getUsername(), is("John Doe"));
assertThat(credentials.getBackendRoles().size(), is(3));
Assert.assertTrue(credentials.getBackendRoles().contains("a"));
Assert.assertTrue(credentials.getBackendRoles().contains("b"));
Assert.assertTrue(credentials.getBackendRoles().contains("3rd"));
Expand All @@ -468,7 +470,7 @@ public void testRequiredAudienceWithCorrectAudience() {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
}

@Test
Expand All @@ -493,7 +495,7 @@ public void testRequiredAudienceWithCorrectAtLeastOneAudience() {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
}

@Test
Expand All @@ -518,7 +520,7 @@ public void testRequiredIssuerWithCorrectAudience() {
);

Assert.assertNotNull(credentials);
Assert.assertEquals("Leonard McCoy", credentials.getUsername());
assertThat(credentials.getUsername(), is("Leonard McCoy"));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
import org.opensearch.security.user.AuthCredentials;
import org.opensearch.security.util.FakeRestRequest;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;

public class HTTPJwtKeyByOpenIdConnectAuthenticatorTest {

protected static MockIpdServer mockIdpServer;
Expand Down Expand Up @@ -58,10 +61,10 @@ public void basicTest() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand All @@ -80,10 +83,10 @@ public void jwksUriTest() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand Down Expand Up @@ -134,10 +137,10 @@ public void jwksMatchAtLeastOneRequiredAudienceInClaimTest() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand Down Expand Up @@ -185,8 +188,8 @@ public void jwksUriMissingTest() {
);
});

Assert.assertEquals("Authentication backend failed", exception.getMessage());
Assert.assertEquals(OpenSearchSecurityException.class, exception.getClass());
assertThat(exception.getMessage(), is("Authentication backend failed"));
assertThat(exception.getClass(), is(OpenSearchSecurityException.class));
}

@Test
Expand All @@ -208,10 +211,10 @@ public void testEscapeKid() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand All @@ -231,10 +234,10 @@ public void bearerTest() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand All @@ -255,8 +258,8 @@ public void testRoles() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(TestJwts.TEST_ROLES, creds.getBackendRoles());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getBackendRoles(), is(TestJwts.TEST_ROLES));
}

@Test
Expand Down Expand Up @@ -366,10 +369,10 @@ public void testRS256() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

@Test
Expand Down Expand Up @@ -404,10 +407,10 @@ public void testPeculiarJsonEscaping() {
);

Assert.assertNotNull(creds);
Assert.assertEquals(TestJwts.MCCOY_SUBJECT, creds.getUsername());
Assert.assertEquals(List.of(TestJwts.TEST_AUDIENCE).toString(), creds.getAttributes().get("attr.jwt.aud"));
Assert.assertEquals(0, creds.getBackendRoles().size());
Assert.assertEquals(4, creds.getAttributes().size());
assertThat(creds.getUsername(), is(TestJwts.MCCOY_SUBJECT));
assertThat(creds.getAttributes().get("attr.jwt.aud"), is(List.of(TestJwts.TEST_AUDIENCE).toString()));
assertThat(creds.getBackendRoles().size(), is(0));
assertThat(creds.getAttributes().size(), is(4));
}

}
Loading

0 comments on commit 0b2109a

Please sign in to comment.