Skip to content

Commit

Permalink
add unit testing for CDEventClass generated for each schema file
Browse files Browse the repository at this point in the history
Signed-off-by: Jalander Ramagiri <[email protected]>
  • Loading branch information
Jalander Ramagiri authored and afrittoli committed Dec 14, 2023
1 parent 8a4c12e commit 5d17b75
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dev.cdevents.constants;

import java.io.File;

public final class CDEventConstants {

private CDEventConstants() {
Expand All @@ -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.
Expand Down
33 changes: 30 additions & 3 deletions sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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);
}
}
58 changes: 58 additions & 0 deletions sdk/src/test/java/dev/cdevents/events/CDEventsGeneratorTest.java
Original file line number Diff line number Diff line change
@@ -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<String> schemaClassFileList = getSchemaClassFileList();

assertThat(eventFiles).extracting(File::getName).hasSameElementsAs(schemaClassFileList);
assertThat(eventFiles.length).isEqualTo(schemaClassFileList.size());
}

private List<String> getSchemaClassFileList() throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
File schemaFolder = new File(CDEventConstants.SCHEMA_FOLDER);
List<String> 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;
}
}

0 comments on commit 5d17b75

Please sign in to comment.