ArchUnit test for Sharded fields for a domain class #1395
Answered
by
hankem
Vignesh413
asked this question in
Q&A
-
|
I need to write an ArchUnit test case for validating the existence of public abstract class Parent<ID> {
@Id
private ID id;
public final static String ID_FIELD = "_id";
private String parent;
public final static String PARENT_FIELD = "parent";
}
@Document(collection = COLLECTION_NAME)
@Sharded(shardKey = { "_id.studentId" })
public class Student extends Parent<Id> {
public static final String COLLECTION_NAME = "student";
public static class Id {
private String studentId;
public final static String STUDENT_ID_FIELD = "studentId";
private String schoolId;
public final static String SCHOOL_ID_FIELD = "schoolId";
public Id() {}
public Id(String studentId, String schoolId) {
this.studentId = studentId;
this.schoolId = schoolId;
}
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
hankem
Jan 6, 2025
Replies: 1 comment 2 replies
-
There are some static methodsimport static com.tngtech.archunit.base.DescribedPredicate.describe;
import static com.tngtech.archunit.lang.conditions.ArchConditions.have;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;that allow you to write @ArchTest
ArchRule shardedClassesHaveShardKey =
classes()
.that().areAnnotatedWith(Sharded.class)
.should(have(describe("a shardKey", shardedClass ->
shardedClass.getAnnotationOfType(Sharded.class).shardKey().length > 0
))); |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ah, sorry; your requirements weren't clear to me initially. And I'm still not entirely sure. 😅
It looks like you have to recurse on a field hierarchy, which is tricky if generic types are involved.
But if all
@Shardedclasses inherit fromParent<ID>, which handles the_idfield, and if there are no further generics, something like this might already be good enough: