Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package com.predic8.membrane.annot.generator.kubernetes.model;

import com.fasterxml.jackson.databind.node.*;
import org.jetbrains.annotations.NotNull;

import java.util.*;

Expand Down Expand Up @@ -148,11 +149,22 @@ private void addProperties(ObjectNode node) {
private static ObjectNode createPropertyNode(AbstractSchema<?> property) {
ObjectNode propertyNode = property.json(jnf.objectNode());
if (property.getEnumValues() != null && !property.getEnumValues().isEmpty()) {
propertyNode.set("enum", getEnumNode(property));
propertyNode = createEnumNode(property, propertyNode);
}
return propertyNode;
}

private static @NotNull ObjectNode createEnumNode(AbstractSchema<?> property, ObjectNode propertyNode) {
ObjectNode unlimitedNode = propertyNode.deepCopy(); // a string (unrestricted)
ObjectNode limitedNode = propertyNode; // a string restricted by 'enum'
limitedNode.set("enum", getEnumNode(property));
propertyNode = jnf.objectNode();
// The 'anyOf' construction will suggest the pre-defined enum values (lowercase enum values) but allow other
// spellings.
propertyNode.set("anyOf", jnf.arrayNode().add(limitedNode).add(unlimitedNode));
return propertyNode;
}

private static ArrayNode getEnumNode(AbstractSchema<?> property) {
var enumValues = jnf.arrayNode();
property.getEnumValues().forEach(enumValues::add);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.predic8.membrane.annot;

import com.predic8.membrane.annot.beanregistry.BeanRegistry;
import com.predic8.membrane.annot.util.CompilerHelper;
import com.predic8.membrane.annot.util.CompilerResult;
import org.junit.jupiter.api.Test;

import static com.predic8.membrane.annot.SpringConfigurationXSDGeneratingAnnotationProcessorTest.MC_MAIN_DEMO;
import static com.predic8.membrane.annot.util.CompilerHelper.*;
import static com.predic8.membrane.annot.util.StructureAssertionUtil.*;

public class SpringEnumParsingTest {

private static final String TRIVIAL_ENUM_EXAMPLE = """
package com.predic8.membrane.demo;
import com.predic8.membrane.annot.*;
import java.util.List;
@MCElement(name="root")
public class DemoElement {
MyEnum value;

public MyEnum getValue() { return value; }
@MCAttribute
public void setValue(MyEnum value) { this.value = value; }
}
---
package com.predic8.membrane.demo;
public enum MyEnum {
VALUE1, VALUE2;
}
""";

@Test
public void checkEnumParsing() {
var sources = splitSources(MC_MAIN_DEMO + TRIVIAL_ENUM_EXAMPLE);
var result = CompilerHelper.compile(sources, false);
assertCompilerResult(true, result);

assertThatXMLValueWasTranslatedTo(result, "VALUE1", "VALUE1");
assertThatXMLValueWasTranslatedTo(result, "value1", "VALUE1");
}

private static void assertThatXMLValueWasTranslatedTo(CompilerResult result, String xmlValue, String expectedToStringValue) {
BeanRegistry br = parseXML(result, SpringParsingTest.wrapSpring("""
<root value="%s" />""".formatted(xmlValue)));

assertStructure(
br,
clazz("DemoElement",
property("value", convertedToString(expectedToStringValue))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import com.predic8.membrane.annot.beanregistry.BeanRegistry;
import com.predic8.membrane.annot.beanregistry.SpringContextAdapter;
import com.predic8.membrane.annot.util.CompilerHelper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.context.ConfigurableApplicationContext;

Expand All @@ -28,9 +27,9 @@
import static com.predic8.membrane.annot.util.StructureAssertionUtil.*;
import static com.predic8.membrane.annot.util.StructureAssertionUtil.clazz;

public class ParsingTest {
public class SpringParsingTest {

private String wrapSpring(String content) {
public static String wrapSpring(String content) {
return """
<spring:beans xmlns="http://membrane-soa.org/demo/1/"
xmlns:spring="http://www.springframework.org/schema/beans"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package com.predic8.membrane.annot;

import com.predic8.membrane.annot.beanregistry.BeanRegistry;
import com.predic8.membrane.annot.util.CompilerHelper;
import com.predic8.membrane.annot.util.CompilerResult;
import org.junit.jupiter.api.Test;

import static com.predic8.membrane.annot.SpringConfigurationXSDGeneratingAnnotationProcessorTest.MC_MAIN_DEMO;
import static com.predic8.membrane.annot.util.CompilerHelper.*;
import static com.predic8.membrane.annot.util.StructureAssertionUtil.*;
import static com.predic8.membrane.annot.util.StructureAssertionUtil.convertedToString;

public class YAMLEnumParsingTest {

private static final String TRIVIAL_ENUM_EXAMPLE = """
package com.predic8.membrane.demo;
import com.predic8.membrane.annot.*;
import java.util.List;
@MCElement(name="root", topLevel=true, component=false)
public class DemoElement {
MyEnum value;

public MyEnum getValue() { return value; }
@MCAttribute
public void setValue(MyEnum value) { this.value = value; }
}
---
package com.predic8.membrane.demo;
public enum MyEnum {
VALUE1, VALUE2;
}
""";

@Test
public void checkEnumParsing() {
var sources = splitSources(MC_MAIN_DEMO + TRIVIAL_ENUM_EXAMPLE);
var result = CompilerHelper.compile(sources, false);
assertCompilerResult(true, result);

assertThatXMLValueWasTranslatedTo(result, "value1", "VALUE1");
assertThatXMLValueWasTranslatedTo(result, "VALUE1", "VALUE1");
}

private static void assertThatXMLValueWasTranslatedTo(CompilerResult result, String xmlValue, String expectedToStringValue) {
BeanRegistry br = parseYAML(result, """
root:
value: %s""".formatted(xmlValue));

assertStructure(
br,
clazz("DemoElement",
property("value", convertedToString(expectedToStringValue))));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class CompilerHelper {
public static final String ANNOTATION_PROCESSOR_CLASSNAME = "com.predic8.membrane.annot.SpringConfigurationXSDGeneratingAnnotationProcessor";
public static final String APPLICATION_CONTEXT_CLASSNAME = "org.springframework.context.support.ClassPathXmlApplicationContext";
private static final Pattern PACKAGE_PATTERN = Pattern.compile("package\\s+([^;]+)\\s*;");
private static final Pattern CLASS_PATTERN = Pattern.compile("class\\s+([^\\s]+)\\s");
private static final Pattern CLASS_PATTERN = Pattern.compile("(class|enum|interface)\\s+([^\\s]+)\\s");

/**
* Compile the given source files.
Expand Down Expand Up @@ -199,7 +199,7 @@ private static String extractName(String source) {
Matcher m = CLASS_PATTERN.matcher(source);
if (!m.find())
throw new RuntimeException("No class name found in source:\n" + source);
return m.group(1);
return m.group(2);
}

private static String extractPackage(String source) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ public static Asserter value(Object value) {
return bean -> Assertions.assertEquals(value, bean);
}

public static Asserter isNull() {
return bean -> Assertions.assertNull(bean);
}

public static Asserter convertedToString(String value) {
return bean -> Assertions.assertEquals(value, bean.toString());
}

public static Asserter list(Asserter... asserters) {
return bean -> {
assertInstanceOf(List.class, bean);
Expand Down
Loading