Skip to content

Commit ad57f81

Browse files
committed
Creating contract-tests for successful 'CREATE DATABASE' for MySQL and Postgres.
1 parent e16cb84 commit ad57f81

File tree

10 files changed

+526
-298
lines changed

10 files changed

+526
-298
lines changed

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/jdbc/JdbcContractTestBase.java

Lines changed: 41 additions & 284 deletions
Large diffs are not rendered by default.

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/jdbc/JdbcH2Test.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.junit.jupiter.api.Test;
2020
import org.junit.jupiter.api.TestInstance;
2121
import org.testcontainers.junit.jupiter.Testcontainers;
22+
import software.amazon.opentelemetry.appsignals.test.jdbc.operationtests.DBOperation;
2223

2324
@Testcontainers(disabledWithoutDocker = true)
2425
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -32,12 +33,13 @@ public class JdbcH2Test extends JdbcContractTestBase {
3233

3334
@Test
3435
public void testSuccess() {
35-
assertSuccess(DB_SYSTEM, DB_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
36+
assertSuccess(
37+
DB_SYSTEM, DBOperation.SELECT, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
3638
}
3739

3840
@Test
3941
public void testFault() {
40-
assertFault(DB_SYSTEM, DB_OPERATION, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
42+
assertFault(DB_SYSTEM, DBOperation.SELECT, DB_USER, DB_NAME, DB_CONNECTION_STRING, null, null);
4143
}
4244

4345
@Override

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/jdbc/JdbcMySQLTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,19 @@
1717

1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.stream.Stream;
2021
import org.junit.jupiter.api.AfterEach;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.TestInstance;
2324
import org.junit.jupiter.api.TestInstance.Lifecycle;
25+
import org.junit.jupiter.params.ParameterizedTest;
26+
import org.junit.jupiter.params.provider.MethodSource;
2427
import org.testcontainers.containers.MySQLContainer;
2528
import org.testcontainers.containers.wait.strategy.Wait;
2629
import org.testcontainers.images.PullPolicy;
2730
import org.testcontainers.junit.jupiter.Testcontainers;
2831
import org.testcontainers.lifecycle.Startable;
32+
import software.amazon.opentelemetry.appsignals.test.jdbc.operationtests.DBOperation;
2933

3034
@Testcontainers(disabledWithoutDocker = true)
3135
@TestInstance(Lifecycle.PER_CLASS)
@@ -48,11 +52,16 @@ public void afterEach() {
4852
mySQLContainer.stop();
4953
}
5054

51-
@Test
52-
public void testSuccess() {
55+
private static Stream<DBOperation> dbOperations() {
56+
return Stream.of(DBOperation.SELECT, DBOperation.CREATE_DATABASE);
57+
}
58+
59+
@ParameterizedTest
60+
@MethodSource("dbOperations")
61+
public void testSuccess(DBOperation operation) {
5362
assertSuccess(
5463
DB_SYSTEM,
55-
DB_OPERATION,
64+
operation,
5665
DB_USER,
5766
DB_NAME,
5867
DB_CONNECTION_STRING,
@@ -64,7 +73,7 @@ public void testSuccess() {
6473
public void testFault() {
6574
assertFault(
6675
DB_SYSTEM,
67-
DB_OPERATION,
76+
DBOperation.SELECT,
6877
DB_USER,
6978
DB_NAME,
7079
DB_CONNECTION_STRING,

appsignals-tests/contract-tests/src/test/java/software/amazon/opentelemetry/appsignals/test/jdbc/JdbcPostgresTest.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,18 @@
1717

1818
import java.util.List;
1919
import java.util.Map;
20+
import java.util.stream.Stream;
2021
import org.junit.jupiter.api.AfterEach;
2122
import org.junit.jupiter.api.Test;
2223
import org.junit.jupiter.api.TestInstance;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.MethodSource;
2326
import org.testcontainers.containers.PostgreSQLContainer;
2427
import org.testcontainers.containers.wait.strategy.Wait;
2528
import org.testcontainers.images.PullPolicy;
2629
import org.testcontainers.junit.jupiter.Testcontainers;
2730
import org.testcontainers.lifecycle.Startable;
31+
import software.amazon.opentelemetry.appsignals.test.jdbc.operationtests.DBOperation;
2832

2933
@Testcontainers(disabledWithoutDocker = true)
3034
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@@ -48,11 +52,16 @@ public void afterEach() {
4852
postgreSqlContainer.stop();
4953
}
5054

51-
@Test
52-
public void testSuccess() {
55+
private static Stream<DBOperation> dbOperations() {
56+
return Stream.of(DBOperation.SELECT, DBOperation.CREATE_DATABASE);
57+
}
58+
59+
@ParameterizedTest
60+
@MethodSource("dbOperations")
61+
public void testSuccess(DBOperation operation) {
5362
assertSuccess(
5463
DB_SYSTEM,
55-
DB_OPERATION,
64+
operation,
5665
DB_USER,
5766
DB_NAME,
5867
DB_CONNECTION_STRING,
@@ -64,7 +73,7 @@ public void testSuccess() {
6473
public void testFault() {
6574
assertFault(
6675
DB_SYSTEM,
67-
DB_OPERATION,
76+
DBOperation.SELECT,
6877
DB_USER,
6978
DB_NAME,
7079
DB_CONNECTION_STRING,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.opentelemetry.appsignals.test.jdbc.operationtests;
17+
18+
public enum DBOperation {
19+
CREATE_DATABASE("CREATE DATABASE", "testdb2"),
20+
SELECT("SELECT", "testdb");
21+
22+
DBOperation(String value, String targetDB) {
23+
this.value = value;
24+
this.targetDB = targetDB;
25+
}
26+
27+
private final String value;
28+
private final String targetDB;
29+
30+
public String getTargetDB() {
31+
return targetDB;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return value;
37+
}
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.opentelemetry.appsignals.test.jdbc.operationtests;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
import static software.amazon.opentelemetry.appsignals.test.jdbc.operationtests.DBOperation.CREATE_DATABASE;
20+
21+
import software.amazon.opentelemetry.appsignals.test.utils.ResourceScopeSpan;
22+
import software.amazon.opentelemetry.appsignals.test.utils.SemanticConventionsConstants;
23+
24+
public class JdbcCreateDatabaseOperationTester extends JdbcOperationTester {
25+
26+
public JdbcCreateDatabaseOperationTester(
27+
String dbSystem, String dbUser, String jdbcUrl, String dbName, String dbTable) {
28+
super(dbSystem, dbUser, jdbcUrl, dbName, CREATE_DATABASE.toString(), dbTable);
29+
}
30+
31+
@Override
32+
protected void assertOperationSemanticConventions(ResourceScopeSpan resourceScopeSpan) {
33+
assertThat(resourceScopeSpan.getSpan().getName()).isEqualTo(String.format("%s", this.dbName));
34+
35+
var attributesList = resourceScopeSpan.getSpan().getAttributesList();
36+
assertThat(attributesList)
37+
.satisfiesOnlyOnce(
38+
attribute -> {
39+
assertThat(attribute.getKey()).isEqualTo(SemanticConventionsConstants.DB_STATEMENT);
40+
assertThat(attribute.getValue().getStringValue())
41+
.isEqualTo(
42+
String.format(
43+
"%s %s",
44+
CREATE_DATABASE.toString().toLowerCase(), CREATE_DATABASE.getTargetDB()));
45+
});
46+
}
47+
}

0 commit comments

Comments
 (0)