-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit testing for CDEventClass generated for each schema file
Signed-off-by: Jalander Ramagiri <[email protected]>
- Loading branch information
Showing
4 changed files
with
91 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 30 additions & 3 deletions
33
sdk/src/test/java/dev/cdevents/constants/CDEventTypesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
sdk/src/test/java/dev/cdevents/events/CDEventsGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |