|
| 1 | +package com.marklogic.hub; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.io.File; |
| 6 | +import java.io.IOException; |
| 7 | +import java.io.InputStream; |
| 8 | +import java.nio.file.Files; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | + |
| 14 | +import org.apache.commons.io.FileUtils; |
| 15 | +import org.custommonkey.xmlunit.XMLUnit; |
| 16 | +import org.junit.After; |
| 17 | +import org.junit.Assert; |
| 18 | +import org.junit.Before; |
| 19 | +import org.junit.BeforeClass; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +import com.marklogic.hub.flow.FlowType; |
| 23 | + |
| 24 | +public class ScaffoldingValidatorTest extends HubTestBase { |
| 25 | + |
| 26 | + private static final String pluginPath = "./test-plugins"; |
| 27 | + private static final File pluginsDir = new File(pluginPath); |
| 28 | + private static final String TEST_ENTITY_NAME = "test-entity"; |
| 29 | + |
| 30 | + @BeforeClass |
| 31 | + public static void setupClass() throws IOException { |
| 32 | + XMLUnit.setIgnoreWhitespace(true); |
| 33 | + } |
| 34 | + |
| 35 | + @Before |
| 36 | + public void setup() throws IOException { |
| 37 | + PluginFormat pluginFormat = PluginFormat.XQUERY; |
| 38 | + createPlugins(TEST_ENTITY_NAME, FlowType.INPUT, pluginFormat); |
| 39 | + createPlugins(TEST_ENTITY_NAME, FlowType.CONFORMANCE, pluginFormat); |
| 40 | + } |
| 41 | + |
| 42 | + @After |
| 43 | + public void teardown() throws IOException { |
| 44 | + FileUtils.deleteDirectory(pluginsDir); |
| 45 | + } |
| 46 | + |
| 47 | + private void createPlugins(String entityName, FlowType flowType, PluginFormat pluginFormat) throws IOException { |
| 48 | + |
| 49 | + String flowName = entityName + flowType + "-flow"; |
| 50 | + String flowTypePath = Scaffolding.getAbsolutePath(pluginPath, "entities", entityName, flowType.toString()); |
| 51 | + String flowPath = Scaffolding.getAbsolutePath(flowTypePath, flowName); |
| 52 | + |
| 53 | + List<Plugin> plugins = new ArrayList<>(); |
| 54 | + if (flowType.equals(FlowType.CONFORMANCE)) { |
| 55 | + plugins.add(createPluginObj(flowPath, "collector", flowType, pluginFormat)); |
| 56 | + } |
| 57 | + plugins.add(createPluginObj(flowPath, "content", flowType, pluginFormat)); |
| 58 | + plugins.add(createPluginObj(flowPath, "headers", flowType, pluginFormat)); |
| 59 | + plugins.add(createPluginObj(flowPath, "triples", flowType, pluginFormat)); |
| 60 | + |
| 61 | + for (Plugin plugin : plugins) { |
| 62 | + createFile(plugin.getParentDirectory(), plugin.getFilename(), plugin.getTemplateFilePath()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + private Plugin createPluginObj(String flowPath, String pluginType, FlowType flowType, PluginFormat pluginFormat) { |
| 67 | + String parentDirectory = Scaffolding.getAbsolutePath(flowPath, pluginType); |
| 68 | + String filename = pluginType + "." + pluginFormat; |
| 69 | + String templateFilePath = "scaffolding/" + flowType + "/" + pluginFormat + "/" + pluginType + "." + pluginFormat; |
| 70 | + return new Plugin(parentDirectory, filename, templateFilePath); |
| 71 | + } |
| 72 | + |
| 73 | + private class Plugin { |
| 74 | + |
| 75 | + private String parentDirectory; |
| 76 | + private String filename; |
| 77 | + private String templateFilePath; |
| 78 | + |
| 79 | + public Plugin(String parentDirectory, String filename, String templateFilePath) { |
| 80 | + this.parentDirectory = parentDirectory; |
| 81 | + this.filename = filename; |
| 82 | + this.templateFilePath = templateFilePath; |
| 83 | + } |
| 84 | + |
| 85 | + public String getParentDirectory() { |
| 86 | + return parentDirectory; |
| 87 | + } |
| 88 | + |
| 89 | + public String getFilename() { |
| 90 | + return filename; |
| 91 | + } |
| 92 | + |
| 93 | + public String getTemplateFilePath() { |
| 94 | + return templateFilePath; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private static void createFile(String parentDirectory, String filename, String templateFilePath) throws IOException { |
| 99 | + File parentDirectoryFile = new File(parentDirectory); |
| 100 | + parentDirectoryFile.mkdirs(); |
| 101 | + writeFile(templateFilePath, Paths.get(parentDirectoryFile.getPath(), filename)); |
| 102 | + } |
| 103 | + |
| 104 | + private static void writeFile(String srcFile, Path dstFile) |
| 105 | + throws IOException { |
| 106 | + InputStream inputStream = Scaffolding.class.getClassLoader() |
| 107 | + .getResourceAsStream(srcFile); |
| 108 | + Files.copy(inputStream, dstFile); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void testIsUniqueRestServiceExtension() throws IOException { |
| 113 | + String restServiceExtensionName = "test-rest-service"; |
| 114 | + boolean isUnique = ScaffoldingValidator.isUniqueRestServiceExtension(pluginsDir, restServiceExtensionName); |
| 115 | + assertTrue("The rest service extension "+ restServiceExtensionName + " is not yet existing so it should be unique.", isUnique); |
| 116 | + try { |
| 117 | + Scaffolding.createRestExtension(TEST_ENTITY_NAME, restServiceExtensionName, FlowType.CONFORMANCE, PluginFormat.XQUERY, pluginsDir); |
| 118 | + } catch (ScaffoldingValidationException e) { |
| 119 | + Assert.fail(e.getMessage()); |
| 120 | + } |
| 121 | + isUnique = ScaffoldingValidator.isUniqueRestServiceExtension(pluginsDir, restServiceExtensionName); |
| 122 | + assertFalse("At this point, the rest service extension "+ restServiceExtensionName + " is already existing so it should not be unique.", isUnique); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testIsUniqueRestTransform() throws IOException { |
| 127 | + String restTransformName = "test-rest-transform"; |
| 128 | + boolean isUnique = ScaffoldingValidator.isUniqueRestTransform(pluginsDir, restTransformName); |
| 129 | + assertTrue("The rest transform "+ restTransformName + " is not yet existing so it should be unique.", isUnique); |
| 130 | + try { |
| 131 | + Scaffolding.createRestTransform(TEST_ENTITY_NAME, restTransformName, FlowType.CONFORMANCE, PluginFormat.XQUERY, pluginsDir); |
| 132 | + } catch (ScaffoldingValidationException e) { |
| 133 | + Assert.fail(e.getMessage()); |
| 134 | + } |
| 135 | + isUnique = ScaffoldingValidator.isUniqueRestTransform(pluginsDir, restTransformName); |
| 136 | + assertFalse("At this point, the rest service extension "+ restTransformName + " is already existing so it should not be unique.", isUnique); |
| 137 | + } |
| 138 | +} |
0 commit comments