Skip to content

Commit

Permalink
Merge pull request #816 from BlueLort/omarnsr-create-database-tests
Browse files Browse the repository at this point in the history
Adding contract-tests for successful 'CREATE DATABASE' for MySQL and Postgres
  • Loading branch information
thpierce authored Jun 20, 2024
2 parents e16cb84 + b468d57 commit 077e781
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static io.opentelemetry.proto.trace.v1.Span.SpanKind.SPAN_KIND_CLIENT;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThat;

import io.opentelemetry.proto.common.v1.KeyValue;
import io.opentelemetry.proto.metrics.v1.ExponentialHistogramDataPoint;
Expand All @@ -31,9 +30,11 @@

public class JdbcContractTestBase extends ContractTestBase {
protected static final String DB_NAME = "testdb";
protected static final String DB_USER = "sa";
protected static final String CREATE_DB_NAME = "testdb2";
protected static final String DB_USER = "root";
protected static final String DB_PASSWORD = "password";
protected static final String DB_OPERATION = "SELECT";
protected static final String DB_SELECT_OPERATION = "SELECT";
protected static final String DB_CREATE_DATABASE_OPERATION = "CREATE DATABASE";
protected static final String DB_RESOURCE_TYPE = "DB::Connection";

@Override
Expand Down Expand Up @@ -130,20 +131,29 @@ protected void assertSemanticConventionsSpanAttributes(
.satisfiesOnlyOnce(
rss -> {
assertThat(rss.getSpan().getKind()).isEqualTo(SPAN_KIND_CLIENT);
assertThat(rss.getSpan().getName())
.isEqualTo(String.format("%s %s.%s", dbOperation, dbName, dbSqlTable));
assertThat(rss.getSpan().getStatus().getCode().toString()).isEqualTo(otelStatusCode);
assertSemanticConventionForOperation(rss, dbOperation, dbName, dbSqlTable);
var attributesList = rss.getSpan().getAttributesList();
assertSemanticConventionsAttributes(
attributesList, dbSqlTable, dbSystem, dbOperation, dbUser, dbName, jdbcUrl);
attributesList, dbSystem, dbUser, dbName, jdbcUrl);
assertSemanticConventionsAttributesForOperation(
attributesList, dbOperation, dbSqlTable);
});
}

private void assertSemanticConventionForOperation(
ResourceScopeSpan rss, String dbOperation, String dbName, String dbSqlTable) {
if (dbOperation.equals(DB_CREATE_DATABASE_OPERATION)) {
assertThat(rss.getSpan().getName()).isEqualTo(String.format("%s", dbName));
} else if (dbOperation.equals(DB_SELECT_OPERATION)) {
assertThat(rss.getSpan().getName())
.isEqualTo(String.format("%s %s.%s", dbOperation, dbName, dbSqlTable));
}
}

protected void assertSemanticConventionsAttributes(
List<KeyValue> attributesList,
String dbSqlTable,
String dbSystem,
String dbOperation,
String dbUser,
String dbName,
String jdbcUrl) {
Expand All @@ -167,35 +177,51 @@ protected void assertSemanticConventionsAttributes(
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_NAME);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbName);
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_SQL_TABLE);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbSqlTable);
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_STATEMENT);
assertThat(attribute.getValue().getStringValue())
.isEqualTo(
String.format("%s count(*) from %s", dbOperation.toLowerCase(), dbSqlTable));
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_USER);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbUser);
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_OPERATION);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbOperation);
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_SYSTEM);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbSystem);
});
}

private void assertSemanticConventionsAttributesForOperation(
List<KeyValue> attributesList, String dbOperation, String dbSqlTable) {
if (dbOperation.equals(DB_CREATE_DATABASE_OPERATION)) {
assertThat(attributesList)
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_STATEMENT);
assertThat(attribute.getValue().getStringValue())
.isEqualTo(String.format("%s %s", dbOperation.toLowerCase(), CREATE_DB_NAME));
});
} else if (dbOperation.equals(DB_SELECT_OPERATION)) {
assertThat(attributesList)
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_SQL_TABLE);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbSqlTable);
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_STATEMENT);
assertThat(attribute.getValue().getStringValue())
.isEqualTo(
String.format(
"%s count(*) from %s", dbOperation.toLowerCase(), dbSqlTable));
})
.satisfiesOnlyOnce(
attribute -> {
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_OPERATION);
assertThat(attribute.getValue().getStringValue()).isEqualTo(dbOperation);
});
}
}

protected void assertMetricAttributes(
List<ResourceScopeMetric> resourceScopeMetrics,
String method,
Expand Down Expand Up @@ -292,7 +318,7 @@ protected void assertSuccess(
String jdbcUrl,
String type,
String identifier) {
var path = "success";
var path = "success/" + dbOperation;
var method = "GET";
var otelStatusCode = "STATUS_CODE_UNSET";
var dbSqlTable = "employee";
Expand Down Expand Up @@ -351,7 +377,7 @@ protected void assertFault(
String jdbcUrl,
String type,
String identifier) {
var path = "fault";
var path = "fault/" + dbOperation;
var method = "GET";
var otelStatusCode = "STATUS_CODE_ERROR";
var dbSqlTable = "userrr";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,14 @@ public class JdbcH2Test extends JdbcContractTestBase {
private static final String DB_PLATFORM = "org.hibernate.dialect.H2Dialect";

@Test
public void testSuccess() {
assertSuccess(DB_SYSTEM, DB_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
public void testSuccessSelect() {
assertSuccess(
DB_SYSTEM, DB_SELECT_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
}

@Test
public void testFault() {
assertFault(DB_SYSTEM, DB_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
public void testFaultSelect() {
assertFault(DB_SYSTEM, DB_SELECT_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void afterEach() {
}

@Test
public void testSuccess() {
public void testSuccessCreateDatabase() {
assertSuccess(
DB_SYSTEM,
DB_OPERATION,
DB_CREATE_DATABASE_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
Expand All @@ -61,10 +61,22 @@ public void testSuccess() {
}

@Test
public void testFault() {
public void testSuccessSelect() {
assertSuccess(
DB_SYSTEM,
DB_SELECT_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
DB_RESOURCE_TYPE,
MYSQL_IDENTIFIER);
}

@Test
public void testFaultSelect() {
assertFault(
DB_SYSTEM,
DB_OPERATION,
DB_SELECT_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public void afterEach() {
}

@Test
public void testSuccess() {
public void testSuccessCreateDatabase() {
assertSuccess(
DB_SYSTEM,
DB_OPERATION,
DB_CREATE_DATABASE_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
Expand All @@ -61,10 +61,22 @@ public void testSuccess() {
}

@Test
public void testFault() {
public void testSuccessSelect() {
assertSuccess(
DB_SYSTEM,
DB_SELECT_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
DB_RESOURCE_TYPE,
POSTGRES_IDENTIFIER);
}

@Test
public void testFaultSelect() {
assertFault(
DB_SYSTEM,
DB_OPERATION,
DB_SELECT_OPERATION,
DB_USER,
DB_NAME,
DB_CONNECTION_STRING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,25 @@ public void prepareDB() {
logger.info("Application Ready");
}

@GetMapping("/success")
@GetMapping("/success/CREATE DATABASE")
@ResponseBody
public ResponseEntity<String> success() {
public ResponseEntity<String> successCreateDatabase() {
jdbcTemplate.execute("create database testdb2");
return ResponseEntity.ok().body("success");
}

@GetMapping("/success/SELECT")
@ResponseBody
public ResponseEntity<String> successSelect() {
int count = jdbcTemplate.queryForObject("select count(*) from employee", Integer.class);
return (count == 1)
? ResponseEntity.ok().body("success")
: ResponseEntity.badRequest().body("failed");
}

@GetMapping("/fault")
@GetMapping("/fault/SELECT")
@ResponseBody
public ResponseEntity<String> failure() {
public ResponseEntity<String> failureSelect() {
int count = jdbcTemplate.queryForObject("select count(*) from userrr", Integer.class);
return ResponseEntity.ok().body("success");
}
Expand Down

0 comments on commit 077e781

Please sign in to comment.