### Question:
SqlSubsegments { public static Subsegment forQuery(Connection connection, @Nullable String query) { DatabaseMetaData metadata = null; String subsegmentName = DEFAULT_DATABASE_NAME; try { metadata = connection.getMetaData(); String database = connection.getCatalog(); URI normalizedUri = new URI(new URI(metadata.getURL()).getSchemeSpecificPart()); subsegmentName = database + "@" + normalizedUri.getHost(); } catch (URISyntaxException e) { logger.debug("Unable to parse database URI. Falling back to default '" + DEFAULT_DATABASE_NAME + "' for subsegment name.", e); } catch (SQLException e) { logger.debug("Encountered exception while retrieving metadata for SQL subsegment " + ", starting blank subsegment instead"); return AWSXRay.beginSubsegment(subsegmentName); } Subsegment subsegment = AWSXRay.beginSubsegment(subsegmentName); subsegment.setNamespace(Namespace.REMOTE.toString()); try { subsegment.putSql(URL, metadata.getURL()); subsegment.putSql(USER, metadata.getUserName()); subsegment.putSql(DRIVER_VERSION, metadata.getDriverVersion()); subsegment.putSql(DATABASE_TYPE, metadata.getDatabaseProductName()); subsegment.putSql(DATABASE_VERSION, metadata.getDatabaseProductVersion()); } catch (SQLException e) { logger.debug("Encountered exception while populating SQL subsegment metadata", e); } if (query != null) { subsegment.putSql(SANITIZED_QUERY, query); } return subsegment; } private  SqlSubsegments(); static Subsegment forQuery(Connection connection, @Nullable String query); static final String URL; static final String USER; static final String DRIVER_VERSION; static final String DATABASE_TYPE; static final String DATABASE_VERSION; static final String SANITIZED_QUERY; static final String DEFAULT_DATABASE_NAME; }### Answer:
@Test void testCreateSubsegmentWithoutSql() { expectedSqlParams = new HashMap<>(); expectedSqlParams.put("url", URL); expectedSqlParams.put("user", USER); expectedSqlParams.put("driver_version", DRIVER_VERSION); expectedSqlParams.put("database_type", DB_TYPE); expectedSqlParams.put("database_version", DB_VERSION); Subsegment sub = SqlSubsegments.forQuery(connection, null); assertThat(sub.getName()).isEqualTo(CATALOG + "@" + HOST); assertThat(sub.getNamespace()).isEqualTo(Namespace.REMOTE.toString()); assertThat(sub.getSql()).containsAllEntriesOf(expectedSqlParams); }
@Test void testCreateSubsegmentWithSql() { expectedSqlParams = new HashMap<>(); expectedSqlParams.put("url", URL); expectedSqlParams.put("user", USER); expectedSqlParams.put("driver_version", DRIVER_VERSION); expectedSqlParams.put("database_type", DB_TYPE); expectedSqlParams.put("database_version", DB_VERSION); expectedSqlParams.put("sanitized_query", SQL); Subsegment sub = SqlSubsegments.forQuery(connection, SQL); assertThat(sub.getName()).isEqualTo(CATALOG + "@" + HOST); assertThat(sub.getNamespace()).isEqualTo(Namespace.REMOTE.toString()); assertThat(sub.getSql()).containsAllEntriesOf(expectedSqlParams); }
@Test void testCreateSubsegmentWhenConnectionThrowsException() throws SQLException { when(connection.getMetaData()).thenThrow(new SQLException()); Subsegment sub = SqlSubsegments.forQuery(connection, SQL); assertThat(AWSXRay.getCurrentSubsegment()).isEqualTo(sub); assertThat(sub.getName()).isEqualTo(SqlSubsegments.DEFAULT_DATABASE_NAME); assertThat(sub.isInProgress()).isTrue(); assertThat(sub.getParentSegment().getSubsegments()).contains(sub); }