Skip to content

Commit 97f70ff

Browse files
Merge pull request #162 from phocassoftware/ability-to-specify-class-to-build-all-options-for-testing
ability to specify all options for a test with a class
2 parents 881dc42 + 69d01d2 commit 97f70ff

File tree

6 files changed

+144
-18
lines changed

6 files changed

+144
-18
lines changed

graphql-database-manager-test/src/main/java/com/phocassoftware/graphql/database/manager/test/TestDatabaseProvider.java

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@
2222
import org.junit.jupiter.api.extension.*;
2323
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
2424

25+
import com.fasterxml.jackson.databind.ObjectMapper;
2526
import com.phocassoftware.graphql.database.manager.Database;
2627
import com.phocassoftware.graphql.database.manager.VirtualDatabase;
2728
import com.phocassoftware.graphql.database.manager.dynamo.DynamoDbManager;
28-
import com.phocassoftware.graphql.database.manager.test.annotations.ProviderFunction;
29+
import com.phocassoftware.graphql.database.manager.test.annotations.*;
2930
import com.phocassoftware.graphql.database.manager.test.annotations.TestDatabase;
3031

3132
import software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient;
@@ -61,9 +62,9 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
6162
var testDatabase = getTestDatabase(testMethod);
6263

6364
if (testDatabase != null) {
64-
return Stream
65-
.of(testDatabase.providers())
66-
.map(t -> create(t))
65+
return testDatabase
66+
.providers()
67+
.stream()
6768
.anyMatch(provider -> parameterContext.getParameter().getType().isAssignableFrom(provider.type()));
6869
}
6970

@@ -89,11 +90,14 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
8990
public void beforeEach(ExtensionContext extensionContext) throws Exception {
9091
var wrapper = getServer();
9192
final var testMethod = extensionContext.getRequiredTestMethod();
93+
94+
TestDatabaseSetup setup;
95+
9296
var testDatabase = getTestDatabase(testMethod);
9397

9498
var classPath = testDatabase.classPath();
9599
var hashed = testDatabase.hashed();
96-
var objectMapper = testDatabase.objectMapper().getConstructor().newInstance().get();
100+
var objectMapper = testDatabase.objectMapper();
97101

98102
final var withHistory = Arrays
99103
.stream(testMethod.getParameters())
@@ -126,9 +130,9 @@ public void beforeEach(ExtensionContext extensionContext) throws Exception {
126130
} else if (type.isAssignableFrom(Database.class)) {
127131
return provider.getDatabase();
128132
} else {
129-
var builder = Stream
130-
.of(testDatabase.providers())
131-
.map(t -> create(t))
133+
var builder = testDatabase
134+
.providers()
135+
.stream()
132136
.filter(p -> type.isAssignableFrom(p.type()))
133137
.findAny()
134138
.orElse(null);
@@ -180,19 +184,37 @@ private ServerWrapper getServer() throws Exception {
180184

181185
}
182186

183-
private TestDatabase getTestDatabase(AnnotatedElement annotatedElement) {
184-
var annotation = annotatedElement.getAnnotation(TestDatabase.class);
185-
if (annotation != null) {
186-
return annotation;
187-
}
188-
for (var a : annotatedElement.getAnnotations()) {
189-
annotation = getTestDatabase(a.annotationType());
187+
private TestDatabaseSetup getTestDatabase(AnnotatedElement annotatedElement) {
188+
try {
189+
var annotation = annotatedElement.getAnnotation(TestDatabase.class);
190+
var classBased = annotatedElement.getAnnotation(TestDatabaseBuilder.class);
191+
if (annotation == null && classBased == null) {
192+
for (var a : annotatedElement.getAnnotations()) {
193+
var setup = getTestDatabase(a.annotationType());
194+
if (setup != null) {
195+
return setup;
196+
}
197+
}
198+
}
190199
if (annotation != null) {
191-
return annotation;
200+
var providers = Stream
201+
.of(annotation.providers())
202+
.map(t -> create(t))
203+
.toList();
204+
205+
var objectMapper = annotation.objectMapper().getConstructor().newInstance().get();
206+
return new TestDatabaseAnnotationSetup(annotation.classPath(), annotation.hashed(), objectMapper, providers);
192207
}
208+
209+
return classBased.value().getConstructor().newInstance();
210+
211+
} catch (ReflectiveOperationException e) {
212+
throw new RuntimeException(e);
193213
}
194-
return null;
195214
}
196215

197216
record ServerWrapper(DynamoDbClient client, DynamoDbAsyncClient clientAsync, DynamoDbStreamsAsyncClient streamClient) {}
217+
218+
record TestDatabaseAnnotationSetup(String classPath, boolean hashed, ObjectMapper objectMapper, List<? extends ProviderFunction<?>> providers) implements
219+
TestDatabaseSetup {}
198220
}

graphql-database-manager-test/src/main/java/com/phocassoftware/graphql/database/manager/test/annotations/TestDatabase.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,5 @@
3333
Class<? extends Supplier<ObjectMapper>> objectMapper();
3434

3535
Class<? extends ProviderFunction<?>>[] providers() default {};
36+
3637
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
13+
package com.phocassoftware.graphql.database.manager.test.annotations;
14+
15+
import java.lang.annotation.*;
16+
import java.util.function.Supplier;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.junit.jupiter.api.extension.ExtendWith;
20+
21+
import com.fasterxml.jackson.databind.ObjectMapper;
22+
import com.phocassoftware.graphql.database.manager.test.TestDatabaseProvider;
23+
24+
@Target(ElementType.ANNOTATION_TYPE)
25+
@Retention(RetentionPolicy.RUNTIME)
26+
@Test
27+
@ExtendWith(TestDatabaseProvider.class)
28+
public @interface TestDatabaseBuilder {
29+
Class<? extends TestDatabaseSetup> value();
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
package com.phocassoftware.graphql.database.manager.test.annotations;
13+
14+
import java.util.List;
15+
16+
import com.fasterxml.jackson.databind.ObjectMapper;
17+
18+
public interface TestDatabaseSetup {
19+
20+
default List<? extends ProviderFunction<?>> providers() {
21+
return List.of();
22+
}
23+
24+
String classPath();
25+
26+
boolean hashed();
27+
28+
ObjectMapper objectMapper();
29+
30+
}

graphql-database-manager-test/src/test/java/com/phocassoftware/graphql/database/manager/test/DynamoDbQueryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
final class DynamoDbQueryTest {
2424

25-
@TestDatabase
25+
@TestDatabaseClassBased
2626
void testSimpleQuery(final Database db) throws InterruptedException, ExecutionException {
2727
db.put(new SimpleTable("garry")).get();
2828
db.put(new SimpleTable("bob")).get();
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
3+
* in compliance with the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License
8+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
9+
* or implied. See the License for the specific language governing permissions and limitations under
10+
* the License.
11+
*/
12+
package com.phocassoftware.graphql.database.manager.test;
13+
14+
import java.lang.annotation.Retention;
15+
import java.lang.annotation.RetentionPolicy;
16+
17+
import com.fasterxml.jackson.databind.ObjectMapper;
18+
import com.phocassoftware.graphql.database.manager.test.annotations.TestDatabaseSetup;
19+
20+
@Retention(RetentionPolicy.RUNTIME)
21+
@com.phocassoftware.graphql.database.manager.test.annotations.TestDatabaseBuilder(value = TestDatabaseClassBased.ClassBaseSetup.class)
22+
public @interface TestDatabaseClassBased {
23+
24+
class ClassBaseSetup implements TestDatabaseSetup {
25+
26+
@Override
27+
public String classPath() {
28+
return "com.phocassoftware.graphql.database.manager.test";
29+
}
30+
31+
@Override
32+
public boolean hashed() {
33+
return true;
34+
}
35+
36+
@Override
37+
public ObjectMapper objectMapper() {
38+
return new ObjectMapperCreator().get();
39+
}
40+
41+
}
42+
43+
}

0 commit comments

Comments
 (0)