Skip to content

Commit

Permalink
Change Test annotation with exceptions to assertThatThrownBy
Browse files Browse the repository at this point in the history
  • Loading branch information
Damans227 authored and findepi committed Oct 15, 2021
1 parent 3f0b9b1 commit 6c4b5ef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static io.airlift.configuration.testing.ConfigAssertions.assertFullMapping;
import static io.airlift.configuration.testing.ConfigAssertions.assertRecordedDefaults;
import static io.airlift.configuration.testing.ConfigAssertions.recordDefaults;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TestSqlEnvironmentConfig
{
Expand Down Expand Up @@ -54,10 +55,12 @@ public void testExplicitPropertyMappings()
assertFullMapping(properties, expected);
}

@Test(expectedExceptions = ParsingException.class, expectedExceptionsMessageRegExp = "\\Qline 1:9: mismatched input '.'. Expecting: ',', <EOF>\\E")
@Test
public void testInvalidPath()
{
SqlEnvironmentConfig config = new SqlEnvironmentConfig().setPath("too.many.qualifiers");
new SqlPath(config.getPath()).getParsedPath();
assertThatThrownBy(() -> new SqlPath(config.getPath()).getParsedPath())
.isInstanceOf(ParsingException.class)
.hasMessageMatching("\\Qline 1:9: mismatched input '.'. Expecting: ',', <EOF>\\E");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import static io.trino.sql.tree.ComparisonExpression.Operator.LESS_THAN;
import static io.trino.sql.tree.ComparisonExpression.Operator.NOT_EQUAL;
import static io.trino.testing.TestingSession.testSessionBuilder;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
Expand All @@ -88,7 +89,7 @@ public void tearDown()
queryRunner = null;
}

@Test(expectedExceptions = IllegalStateException.class)
@Test
public void testDoesNotAllowOuterJoin()
{
PlanNodeIdAllocator planNodeIdAllocator = new PlanNodeIdAllocator();
Expand All @@ -103,7 +104,9 @@ public void testDoesNotAllowOuterJoin()
ImmutableList.of(a1),
ImmutableList.of(b1),
Optional.empty());
toMultiJoinNode(queryRunner.getMetadata(), outerJoin, noLookup(), planNodeIdAllocator, DEFAULT_JOIN_LIMIT, false, testSessionBuilder().build(), new TypeAnalyzer(new SqlParser(), queryRunner.getMetadata()), p.getTypes());
assertThatThrownBy(() -> toMultiJoinNode(queryRunner.getMetadata(), outerJoin, noLookup(), planNodeIdAllocator, DEFAULT_JOIN_LIMIT, false, testSessionBuilder().build(), new TypeAnalyzer(new SqlParser(), queryRunner.getMetadata()), p.getTypes()))
.isInstanceOf(IllegalStateException.class)
.hasMessageMatching("join type must be.*");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static io.airlift.http.client.HttpStatus.OK;
import static io.airlift.http.client.testing.TestingResponse.mockResponse;
import static java.lang.String.format;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

Expand Down Expand Up @@ -79,7 +80,7 @@ public void createAuthenticatedPrincipalSuccess()
assertEquals(principal.getName(), username, "Test principal name from expired cache.");
}

@Test(expectedExceptions = AccessDeniedException.class)
@Test
public void createAuthenticatedPrincipalWrongOrg()
{
String org = "my18CharOrgId"; // As if from ssalesforce.allowed-organizations property.
Expand All @@ -93,10 +94,12 @@ public void createAuthenticatedPrincipalWrongOrg()

HttpClient testHttpClient = new TestingHttpClient((request -> mockResponse(OK, ANY_TEXT_TYPE, xmlResponse)));
SalesforceBasicAuthenticator authenticator = new SalesforceBasicAuthenticator(config, testHttpClient);
authenticator.createAuthenticatedPrincipal(username, password);
assertThatThrownBy(() -> authenticator.createAuthenticatedPrincipal(username, password))
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied:.*");
}

@Test(expectedExceptions = AccessDeniedException.class)
@Test
public void createAuthenticatedPrincipalBadPass()
{
String org = "my18CharOrgId"; // As if from salesforce.allowed-organizations property.
Expand All @@ -110,7 +113,9 @@ public void createAuthenticatedPrincipalBadPass()

HttpClient testHttpClient = new TestingHttpClient((request -> mockResponse(HttpStatus.INTERNAL_SERVER_ERROR, ANY_TEXT_TYPE, xmlResponse)));
SalesforceBasicAuthenticator authenticator = new SalesforceBasicAuthenticator(config, testHttpClient);
authenticator.createAuthenticatedPrincipal(username, password);
assertThatThrownBy(() -> authenticator.createAuthenticatedPrincipal(username, password))
.isInstanceOf(AccessDeniedException.class)
.hasMessageMatching("Access Denied: Invalid response for login\n.*");
}

@Test
Expand Down Expand Up @@ -192,7 +197,7 @@ public void createAuthenticatedPrincipalRealSuccess()
}

// Test a real login for a different org.
@Test(expectedExceptions = AccessDeniedException.class, description = "Test got wrong org for real, yo!")
@Test
public void createAuthenticatedPrincipalRealWrongOrg()
{
// Skip this test if SALESFORCE_TEST_FORREAL is not set to TRUE.
Expand All @@ -212,7 +217,9 @@ public void createAuthenticatedPrincipalRealWrongOrg()
HttpClient testHttpClient = new JettyHttpClient();
SalesforceBasicAuthenticator authenticator = new SalesforceBasicAuthenticator(config, testHttpClient);

authenticator.createAuthenticatedPrincipal(username, password);
assertThatThrownBy(() -> authenticator.createAuthenticatedPrincipal(username, password))
.isInstanceOf(AccessDeniedException.class)
.hasMessage("Test got wrong org for real, yo!");
}

// Test a real login for a different org.
Expand Down Expand Up @@ -241,7 +248,7 @@ public void createAuthenticatedPrincipalRealAllOrgs()
}

// Test a login with a bad password.
@Test(expectedExceptions = AccessDeniedException.class, description = "Test bad password for real, yo!")
@Test
public void createAuthenticatedPrincipalRealBadPassword()
{
// Skip this test if SALESFORCE_TEST_FORREAL is not set to TRUE.
Expand All @@ -263,6 +270,8 @@ public void createAuthenticatedPrincipalRealBadPassword()
.setAllowedOrganizations(org);
HttpClient testHttpClient = new JettyHttpClient();
SalesforceBasicAuthenticator authenticator = new SalesforceBasicAuthenticator(config, testHttpClient);
authenticator.createAuthenticatedPrincipal(username, "NotMyPassword");
assertThatThrownBy(() -> authenticator.createAuthenticatedPrincipal(username, "NotMyPassword"))
.isInstanceOf(AccessDeniedException.class)
.hasMessage("Test bad password for real, yo!");
}
}

0 comments on commit 6c4b5ef

Please sign in to comment.