### Question:
SqlHandler extends XRayHandler { @Override public void handleRequest(Event event) { if (incrementSqlTransactionCount() > 1) { return; } ServiceDownstreamRequestEvent requestEvent = (ServiceDownstreamRequestEvent) event; Statement statement = (Statement) requestEvent.getRequest(); boolean recordSql = XRaySDKConfiguration.getInstance().shouldCollectSqlQueries(); final Connection connection; try { connection = statement.getConnection(); } catch (SQLException e) { log.debug("Encountered exception when creating subsegment for query of " + requestEvent.getService() + ", starting blank subsegment", e); AWSXRay.beginSubsegment(SqlSubsegments.DEFAULT_DATABASE_NAME); return; } SqlSubsegments.forQuery(connection, recordSql ? requestEvent.getOperation() : null); }  @Override void handleRequest(Event event); @Override void handleResponse(Event event);  }### Answer:
@Test public void testSubsegmentCreatedWithoutSql() { XRaySDKConfiguration.getInstance().init(); AWSXRay.beginSegment("test"); handler.handleRequest(requestEvent); Subsegment sqlSub = AWSXRay.getCurrentSubsegment(); assertThat(sqlSub.isInProgress()).isTrue(); assertThat(sqlSub.getName()).isEqualTo(DB + "@example.com"); assertThat(sqlSub.getNamespace()).isEqualTo(Namespace.REMOTE.toString()); assertThat(sqlSub.getSql()).doesNotContainKey(SqlSubsegments.SANITIZED_QUERY); }
@Test public void testSubsegmentCreatedWithSql() { URL configFile = SqlHandlerTest.class.getResource("/com/amazonaws/xray/agent/collectSqlConfig.json"); XRaySDKConfiguration.getInstance().init(configFile); AWSXRay.beginSegment("test"); handler.handleRequest(requestEvent); Subsegment sqlSub = AWSXRay.getCurrentSubsegment(); assertThat(sqlSub.isInProgress()).isTrue(); assertThat(sqlSub.getName()).isEqualTo(DB + "@example.com"); assertThat(sqlSub.getNamespace()).isEqualTo(Namespace.REMOTE.toString()); assertThat(sqlSub.getSql()).containsEntry(SqlSubsegments.SANITIZED_QUERY, QUERY); }
@Test public void testSubsegmentCreatedDespiteException() throws SQLException { when(mockStatement.getConnection()).thenThrow(new SQLException()); XRaySDKConfiguration.getInstance().init(); AWSXRay.beginSegment("test"); handler.handleRequest(requestEvent); Subsegment sqlSub = AWSXRay.getCurrentSubsegment(); assertThat(sqlSub.isInProgress()).isTrue(); assertThat(sqlSub.getName()).isEqualTo(SqlSubsegments.DEFAULT_DATABASE_NAME); }