changeset 530:ab132a470e21

StringSourceNodeDefinition
author Devel 2
date Thu, 31 Aug 2017 15:32:31 +0200
parents a19cc6e1ee65
children 229736339e0c
files stress-tester/src/main/java/com/passus/st/config/StringSourceNodeDefinition.java stress-tester/src/test/java/com/passus/st/config/NodeAsserts.java stress-tester/src/test/java/com/passus/st/config/StringSourceNodeDefinitionTest.java stress-tester/src/test/resources/com/passus/st/config/simple_mvel.txt
diffstat 4 files changed, 254 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/main/java/com/passus/st/config/StringSourceNodeDefinition.java	Thu Aug 31 15:32:31 2017 +0200
@@ -0,0 +1,117 @@
+package com.passus.st.config;
+
+import com.passus.commons.utils.ArrayUtils;
+import com.passus.config.CMapNode;
+import com.passus.config.CNode;
+import com.passus.config.CTupleNode;
+import com.passus.config.CValueNode;
+import static com.passus.config.ConfigurationUtils.extractString;
+import static com.passus.config.ConfigurationUtils.validateType;
+import com.passus.config.NodeType;
+import com.passus.config.schema.NodeDefinition;
+import static com.passus.config.schema.validation.NodeValidationMessages.ONE_CHILD_REQUIRED_MSG;
+import static com.passus.config.schema.validation.NodeValidationMessages.TUPLE_NOT_DEFINED_MSG;
+import com.passus.config.validation.Errors;
+import static com.passus.config.validation.ValidationDefaultMessages.VALUE_SHOULD_NOT_BE_NULL_MSG;
+import java.io.File;
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+import org.apache.commons.io.FileUtils;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class StringSourceNodeDefinition extends NodeDefinition<StringSourceNodeDefinition> {
+
+    public static final StringSourceNodeDefinition INSTANCE = new StringSourceNodeDefinition();
+    
+    private final static Set<Class<? extends CNode>> SUPPORTED = ArrayUtils.asUnmodifiableSet(CValueNode.class, CMapNode.class);
+
+    @Override
+    public Set<Class<? extends CNode>> getSupportedNode() {
+        return SUPPORTED;
+    }
+
+    @Override
+    protected void doValidate(CNode node, Errors errors) {
+        if (node.getType() == NodeType.MAP) {
+            CMapNode mapNode = (CMapNode) node;
+            List<CTupleNode> children = mapNode.getChildren();
+            if (children.size() != 1) {
+                errors.reject(mapNode, ONE_CHILD_REQUIRED_MSG);
+            } else {
+                CTupleNode tuple = children.get(0);
+                String tupleName = tuple.getName().toLowerCase();
+                if (!tupleName.equals("$text") && !tupleName.equals("$file")) {
+                    errors.reject(tuple, TUPLE_NOT_DEFINED_MSG, tuple.getName());
+                    return;
+                }
+
+                try {
+                    errors.pushNestedPath(tuple.getName());
+                    if (validateType(tuple.getNode(), NodeType.VALUE, errors)) {
+                        CValueNode valNode = (CValueNode) tuple.getNode();
+                        if (valNode.getValue() == null) {
+                            errors.reject(valNode, VALUE_SHOULD_NOT_BE_NULL_MSG);
+                        }
+                    }
+                } finally {
+                    errors.popNestedPath();
+                }
+            }
+        } else if (node.getType() == NodeType.VALUE) {
+            CValueNode valNode = (CValueNode) node;
+            if (valNode.getValue() == null) {
+                errors.reject(valNode, VALUE_SHOULD_NOT_BE_NULL_MSG);
+            }
+        }
+    }
+
+    @Override
+    protected CNode doTransform(CNode node, Errors errors, boolean reverse) {
+        if (reverse) {
+            throw new UnsupportedOperationException("Not supported yet.");
+        } else {
+            if (node.getType() == NodeType.MAP) {
+                CMapNode mapNode = (CMapNode) node;
+                CTupleNode tuple = mapNode.getFirstChild();
+                String tupleName = tuple.getName().toLowerCase();
+                try {
+                    errors.pushNestedPath(tuple.getName());
+                    if (tupleName.equalsIgnoreCase("$text")) {
+                        return tuple.getNode();
+                    } else if (tupleName.equalsIgnoreCase("$file")) {
+                        String fileName = extractString(tuple, errors);
+                        if (fileName != null) {
+                            File file = new File(fileName);
+                            if (!file.exists() || !file.isFile()) {
+                                errors.reject(tuple.getNode(), "File does not exist or is not regular file.");
+                            } else if (!file.canRead()) {
+                                errors.reject(tuple.getNode(), "File not readable.");
+                            } else {
+                                try {
+                                    String content = FileUtils.readFileToString(file);
+                                    return new CValueNode(content);
+                                } catch (IOException ex) {
+                                    errors.reject(tuple.getNode(), "Error occured during reading the file.");
+                                }
+                            }
+                        }
+                    }
+                } finally {
+                    errors.popNestedPath();
+                }
+            }
+
+            return node;
+        }
+    }
+
+    @Override
+    protected boolean doEquals(CNode node1, CNode node2) {
+        throw new UnsupportedOperationException("Not supported yet.");
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/config/NodeAsserts.java	Thu Aug 31 15:32:31 2017 +0200
@@ -0,0 +1,29 @@
+package com.passus.st.config;
+
+import com.passus.config.schema.CNodeLocation;
+import com.passus.config.validation.CNodeError;
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.fail;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class NodeAsserts {
+
+    private NodeAsserts() {
+
+    }
+
+    public static void assertNodeLoc(CNodeError nodeError, String key, int row, int column) {
+        CNodeLocation loc = nodeError.getLocation();
+        if (loc == null) {
+            fail("ObjectError does not cotains NodeLocation.");
+            return;
+        }
+
+        assertEquals(key, loc.getKey());
+        assertEquals(row, loc.getRow());
+        assertEquals(column, loc.getColumn());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/java/com/passus/st/config/StringSourceNodeDefinitionTest.java	Thu Aug 31 15:32:31 2017 +0200
@@ -0,0 +1,107 @@
+package com.passus.st.config;
+
+import com.passus.commons.utils.ResourceUtils;
+import com.passus.config.CCompositeNode;
+import com.passus.config.Configuration;
+import com.passus.config.ConfigurationImpl;
+import static com.passus.config.YamlConfigurationReader.readNodeFromString;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.keyNameVaryListDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.mapDef;
+import static com.passus.config.schema.ConfigurationSchemaBuilder.tupleDef;
+import com.passus.config.schema.KeyNameVaryListNodeDefinition;
+import com.passus.config.schema.MapNodeDefinition;
+import static com.passus.config.schema.validation.NodeValidationMessages.ONE_CHILD_REQUIRED_MSG;
+import static com.passus.config.schema.validation.NodeValidationMessages.TUPLE_NOT_DEFINED_MSG;
+import com.passus.config.validation.CNodeError;
+import com.passus.config.validation.Errors;
+import static com.passus.config.validation.ValidationDefaultMessages.VALUE_SHOULD_NOT_BE_NULL_MSG;
+import static com.passus.st.config.NodeAsserts.assertNodeLoc;
+import java.io.File;
+import static java.lang.String.format;
+import java.util.List;
+import static org.testng.AssertJUnit.assertEquals;
+import org.testng.annotations.Test;
+
+/**
+ *
+ * @author Mirosław Hawrot
+ */
+public class StringSourceNodeDefinitionTest {
+
+    @Test
+    public void testValidateMethod() throws Exception {
+        Errors errors = new Errors();
+        MapNodeDefinition mapDef = mapDef(
+                tupleDef("content", new StringSourceNodeDefinition())
+        );
+
+        CCompositeNode rootNode = readNodeFromString(
+                "content: null\n" //nie moze byc null
+                + "content: value\n" //ok
+                + "content: {$text: value}\n" //ok
+                + "content: {$text: null}\n" //nie moze byc null
+                + "content: {$file: value}\n" //ok
+                + "content: {$file: null}\n" //nie moze byc null
+                + "content: {$invalid: value}\n" //nieprawidlowy klucz
+                + "content: {}\n" //nieprawidlowy klucz
+        );
+
+        mapDef.validate(rootNode, errors);
+
+        assertEquals(5, errors.getErrorCount());
+        List<CNodeError> nodeErrors = errors.getNodeErrors();
+
+        CNodeError nodeError = nodeErrors.get(0);
+        assertEquals(VALUE_SHOULD_NOT_BE_NULL_MSG, nodeError.getMessage());
+        assertNodeLoc(nodeError, "content", 1, 10);
+
+        nodeError = nodeErrors.get(1);
+        assertEquals(VALUE_SHOULD_NOT_BE_NULL_MSG, nodeError.getMessage());
+        assertNodeLoc(nodeError, "content.$text", 4, 18);
+
+        nodeError = nodeErrors.get(2);
+        assertEquals(VALUE_SHOULD_NOT_BE_NULL_MSG, nodeError.getMessage());
+        assertNodeLoc(nodeError, "content.$file", 6, 18);
+
+        nodeError = nodeErrors.get(3);
+        assertEquals(format(TUPLE_NOT_DEFINED_MSG, "$invalid"), nodeError.getMessage());
+        assertNodeLoc(nodeError, "content", 7, 11);
+
+        nodeError = nodeErrors.get(4);
+        assertEquals(ONE_CHILD_REQUIRED_MSG, nodeError.getMessage());
+        //assertNodeLoc(nodeError, "content", 8, -1);
+
+    }
+
+    @Test
+    public void testTransformMethod() throws Exception {
+        File mvelFile = ResourceUtils.getFile("com/passus/st/config/simple_mvel.txt");
+        String mvelContent = ResourceUtils.readStringFromFile("com/passus/st/config/simple_mvel.txt");
+
+        CCompositeNode rootNode = readNodeFromString(
+                "list:\n"
+                + "  content1: value1\n"
+                + "  content2: {$text: value2}\n"
+                + "  content3: {$file: \"" + mvelFile.getAbsolutePath().replace("\\", "\\\\") + "\"}\n"
+        );
+
+        KeyNameVaryListNodeDefinition listDef = keyNameVaryListDef()
+                .add("content1", StringSourceNodeDefinition.INSTANCE)
+                .add("content2", StringSourceNodeDefinition.INSTANCE)
+                .add("content3", StringSourceNodeDefinition.INSTANCE);
+
+        MapNodeDefinition rootNodeDef = mapDef(
+                tupleDef("list", listDef)
+        );
+
+        Errors errors = new Errors();
+        CCompositeNode transRootNode = (CCompositeNode) rootNodeDef.transform(rootNode, errors);
+
+        assertEquals(0, errors.getErrorCount());
+        Configuration config = new ConfigurationImpl(transRootNode);
+
+        assertEquals("value1", config.get("list.content1"));
+        assertEquals("value2", config.get("list.content2"));
+        assertEquals(mvelContent, config.get("list.content3"));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/stress-tester/src/test/resources/com/passus/st/config/simple_mvel.txt	Thu Aug 31 15:32:31 2017 +0200
@@ -0,0 +1,1 @@
+return 1;
\ No newline at end of file