diff --git a/generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java b/generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java index 1a6af4b..231a0f8 100644 --- a/generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java +++ b/generator/src/main/java/dev/cdevents/generator/CDEventsGenerator.java @@ -30,7 +30,6 @@ private CDEventsGenerator() { private static final int VERSION_INDEX = 4; private static final int SUBSTRING_PIPELINE_INDEX = 8; - private static final String TARGET_PACKAGE = "src/main/java/dev/cdevents/events"; /** * Event JsonSchema files location. diff --git a/sdk/src/main/java/dev/cdevents/constants/CDEventConstants.java b/sdk/src/main/java/dev/cdevents/constants/CDEventConstants.java index 09aabc4..2cc007a 100644 --- a/sdk/src/main/java/dev/cdevents/constants/CDEventConstants.java +++ b/sdk/src/main/java/dev/cdevents/constants/CDEventConstants.java @@ -1,5 +1,7 @@ package dev.cdevents.constants; +import java.io.File; + public final class CDEventConstants { private CDEventConstants() { @@ -8,7 +10,7 @@ private CDEventConstants() { /** * Event JsonSchema files location. */ - public static final String SCHEMA_FOLDER = "../spec/schemas"; + public static final String SCHEMA_FOLDER = ".." + File.separator + "spec" + File.separator + "schemas"; /** * CDEvents Version. diff --git a/sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java b/sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java index 4ad92d6..a0ada0c 100644 --- a/sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java +++ b/sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java @@ -1,14 +1,41 @@ package dev.cdevents.constants; -import static org.assertj.core.api.Assertions.assertThat; - +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import dev.cdevents.constants.CDEventConstants.CDEventTypes; import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; -import dev.cdevents.constants.CDEventConstants.CDEventTypes; +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; public class CDEventTypesTest { @Test void eventTypeNamesAreUnique() { assertThat(CDEventTypes.values()).extracting(CDEventTypes::getEventType).doesNotHaveDuplicates(); } + + @Test + void eventTypesMatchesWithSchemaContextTypes() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + File folder = new File(CDEventConstants.SCHEMA_FOLDER); + List eventTypeList = new ArrayList<>(); + if (folder.isDirectory()) { + File[] files = folder.listFiles((dir, name) -> name.toLowerCase().endsWith(".json")); + if (files != null) { + for (File file : files) { + JsonNode rootNode = objectMapper.readTree(file); + JsonNode contextNode = rootNode.get("properties").get("context").get("properties"); + String eventType = contextNode.get("type").get("enum").get(0).asText(); + eventTypeList.add(eventType.split("\\d+\\.\\d+\\.\\d+")[0]); + } + } + } + assertThat(CDEventTypes.values()).extracting(CDEventTypes::getEventType).hasSameElementsAs(eventTypeList); + } } \ No newline at end of file diff --git a/sdk/src/test/java/dev/cdevents/events/CDEventsGeneratorTest.java b/sdk/src/test/java/dev/cdevents/events/CDEventsGeneratorTest.java new file mode 100644 index 0000000..059a2e8 --- /dev/null +++ b/sdk/src/test/java/dev/cdevents/events/CDEventsGeneratorTest.java @@ -0,0 +1,58 @@ +package dev.cdevents.events; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import dev.cdevents.constants.CDEventConstants; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; + +public class CDEventsGeneratorTest { + + private static final int SUBJECT_INDEX = 2; + private static final int PREDICATE_INDEX = 3; + + private static final String EVENTS_PACKAGE = "src/main/java/dev/cdevents/events"; + + @Test + void testCDEventClassGeneratedForEachSchemaFile() throws IOException { + File eventsFolder = new File(EVENTS_PACKAGE); + File[] eventFiles = eventsFolder.listFiles((dir, name) -> !name.equals("package-info.java") && name.endsWith(".java")); + List schemaClassFileList = getSchemaClassFileList(); + + assertThat(eventFiles).extracting(File::getName).hasSameElementsAs(schemaClassFileList); + assertThat(eventFiles.length).isEqualTo(schemaClassFileList.size()); + } + + private List getSchemaClassFileList() throws IOException { + ObjectMapper objectMapper = new ObjectMapper(); + File schemaFolder = new File(CDEventConstants.SCHEMA_FOLDER); + List schemaClassFileList = new ArrayList<>(); + if (schemaFolder.isDirectory()) { + File[] files = schemaFolder.listFiles((dir, name) -> name.toLowerCase().endsWith(".json")); + if (files != null) { + for (File file : files) { + JsonNode rootNode = objectMapper.readTree(file); + JsonNode contextNode = rootNode.get("properties").get("context").get("properties"); + String eventType = contextNode.get("type").get("enum").get(0).asText(); + String[] type = eventType.split("\\."); + String subject = type[SUBJECT_INDEX]; + String predicate = type[PREDICATE_INDEX]; + String capitalizedSubject = StringUtils.capitalize(subject); + String capitalizedPredicate = StringUtils.capitalize(predicate); + String classFileName = StringUtils.join(new String[]{capitalizedSubject, capitalizedPredicate, "CDEvent", ".java"}); + schemaClassFileList.add(classFileName); + } + } + } + return schemaClassFileList; + } +}