Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DRAFT: Validate Java enum values against CML #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/cml/TLAs-context.cml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BoundedContext TLA_Resolver {
String meaning;
Set<String> alternativeMeanings;
String link;
- TLAState status;
- TLAStatus status;

def ThreeLetterAbbreviation() : write [ -> PROPOSED ];
def accept() : write [ PROPOSED -> ACCEPTED ];
Expand All @@ -37,10 +37,10 @@ BoundedContext TLA_Resolver {
String name;
}

enum TLAState {
enum TLAStatus {
aggregateLifecycle

PROPOSED, ACCEPTED, DECLINED, ARCHIVED;
DRAFTED, PROPOSED, ACCEPTED, DECLINED, ARCHIVED;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@

public enum TLAStatus {

PROPOSED, ACCEPTED, DECLINED, ARCHIVED
DRAFTED, PROPOSED, ACCEPTED, DECLINED, ARCHIVED

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,19 @@

package org.contextmapper.sample.tlas.domain;

import com.tngtech.archunit.core.domain.JavaClass;
import com.tngtech.archunit.core.domain.JavaEnumConstant;
import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import org.contextmapper.archunit.AbstractTacticArchUnitTest;
import org.contextmapper.tactic.dsl.tacticdsl.Enum;
import org.contextmapper.tactic.dsl.tacticdsl.EnumValue;
import org.eclipse.xtext.EcoreUtil2;
import org.junit.jupiter.api.Test;

import java.util.Optional;

import static com.tngtech.archunit.core.domain.JavaClass.Predicates.resideInAPackage;
import static com.tngtech.archunit.core.domain.properties.HasType.Predicates.rawType;
import static com.tngtech.archunit.lang.syntax.ArchRuleDefinition.classes;
Expand Down Expand Up @@ -56,4 +66,34 @@ void domainModelClassesShouldBeAnnotatedWithJMolecules() {
.check(classes);
}

@Test
void statusEnumsShouldBeModelledInCML() {
classes().that()
.areEnums()
.and().haveSimpleNameEndingWith("Status")
.should(new ArchCondition<>("adhere to CML enum values") {
@Override
public void check(JavaClass statusEnum, ConditionEvents events) {
Optional<? extends Enum> optionalCmlStatus = EcoreUtil2.eAllOfType(context, Enum.class).stream()
.filter(o -> o.getName().equals(statusEnum.getSimpleName()))
.findAny();
events.add(new SimpleConditionEvent(optionalCmlStatus, optionalCmlStatus.isPresent(),
String.format("The Status enum '%s' is not modelled in CML.", statusEnum.getSimpleName())));

if (optionalCmlStatus.isPresent()) {
Enum cmlStatus = optionalCmlStatus.get();
for (JavaEnumConstant javaEnumConstant : statusEnum.getEnumConstants()) {
Optional<EnumValue> cmlEnumValue = cmlStatus.getValues().stream()
.filter(v -> v.getName().equals(javaEnumConstant.name()))
.findAny();
events.add(new SimpleConditionEvent(statusEnum, cmlEnumValue.isPresent(),
String.format("The status enum '%s' does not have a value called '%s' in CML.",
cmlStatus.getName(), javaEnumConstant.name())));
}
}
}
})
.check(classes);
}

}