You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by dh...@apache.org on 2017/08/03 07:46:26 UTC

[5/7] camel git commit: CAMEL-10744: Updated camel salesforce maven plugin to generate json schema file with types for Salesforce objects

CAMEL-10744: Updated camel salesforce maven plugin to generate json schema file with types for Salesforce objects


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/5eaaf981
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/5eaaf981
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/5eaaf981

Branch: refs/heads/master
Commit: 5eaaf9810a65caa602ff720cdcbba8ffeaaf9530
Parents: ef94599
Author: Dhiraj Bokde <dh...@yahoo.com>
Authored: Thu Aug 3 00:16:56 2017 -0700
Committer: Dhiraj Bokde <dh...@yahoo.com>
Committed: Thu Aug 3 00:43:53 2017 -0700

----------------------------------------------------------------------
 .../apache/camel/maven/CamelSalesforceMojo.java | 72 +++++++++++++++++---
 .../CamelSalesforceMojoIntegrationTest.java     | 33 +++++++--
 2 files changed, 90 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/5eaaf981/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
index a169047..6a48ea7 100644
--- a/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
+++ b/components/camel-salesforce/camel-salesforce-maven-plugin/src/main/java/org/apache/camel/maven/CamelSalesforceMojo.java
@@ -24,6 +24,8 @@ import java.io.Writer;
 import java.lang.reflect.Field;
 import java.net.URI;
 import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.nio.file.Path;
 import java.security.GeneralSecurityException;
 import java.util.ArrayList;
 import java.util.Arrays;
@@ -272,6 +274,24 @@ public class CamelSalesforceMojo extends AbstractMojo {
     @Parameter(property = "camelSalesforce.useStringsForPicklists", defaultValue = "false")
     protected Boolean useStringsForPicklists;
 
+    /**
+     * Generate JSON Schema for DTOs, instead of Java Objects.
+     */
+    @Parameter(property = "camelSalesforce.jsonSchema")
+    protected boolean jsonSchema;
+
+    /**
+     * Schema ID for JSON Schema for DTOs.
+     */
+    @Parameter(property = "camelSalesforce.jsonSchemaId", defaultValue = JsonUtils.DEFAULT_ID_PREFIX)
+    protected String jsonSchemaId;
+
+    /**
+     * Schema ID for JSON Schema for DTOs.
+     */
+    @Parameter(property = "camelSalesforce.jsonSchemaFilename", defaultValue = "salesforce-dto-schema.json")
+    protected String jsonSchemaFilename;
+
     VelocityEngine engine;
 
     private long responseTimeout;
@@ -402,22 +422,52 @@ public class CamelSalesforceMojo extends AbstractMojo {
                 }
             }
 
-            getLog().info("Generating Java Classes...");
-            // generate POJOs for every object description
-            final GeneratorUtility utility = new GeneratorUtility(useStringsForPicklists);
-            // should we provide a flag to control timestamp generation?
-            final String generatedDate = new Date().toString();
-            for (SObjectDescription description : descriptions) {
-                if (IGNORED_OBJECTS.contains(description.getName())) {
-                    continue;
+            if (!jsonSchema) {
+
+                getLog().info("Generating Java Classes...");
+                // generate POJOs for every object description
+                final GeneratorUtility utility = new GeneratorUtility(useStringsForPicklists);
+                // should we provide a flag to control timestamp generation?
+                final String generatedDate = new Date().toString();
+                for (SObjectDescription description : descriptions) {
+                    if (IGNORED_OBJECTS.contains(description.getName())) {
+                        continue;
+                    }
+                    try {
+                        processDescription(pkgDir, description, utility, generatedDate);
+                    } catch (IOException e) {
+                        throw new MojoExecutionException("Unable to generate source files for: " + description.getName(), e);
+                    }
+                }
+
+                getLog().info(String.format("Successfully generated %s Java Classes", descriptions.size() * 2));
+
+            } else {
+
+                getLog().info("Generating JSON Schema...");
+                // generate JSON schema for every object description
+                final ObjectMapper schemaObjectMapper = JsonUtils.createSchemaObjectMapper();
+                final Set<Object> allSchemas = new HashSet<>();
+                for (SObjectDescription description : descriptions) {
+                    if (IGNORED_OBJECTS.contains(description.getName())) {
+                        continue;
+                    }
+                    try {
+                        allSchemas.add(JsonUtils.getSObjectJsonSchema(schemaObjectMapper, description, jsonSchemaId, true));
+                    } catch (IOException e) {
+                        throw new MojoExecutionException("Unable to generate JSON Schema types for: " + description.getName(), e);
+                    }
                 }
+
+                final Path schemaFilePath = outputDirectory.toPath().resolve(jsonSchemaFilename);
                 try {
-                    processDescription(pkgDir, description, utility, generatedDate);
+                    Files.write(schemaFilePath, JsonUtils.getJsonSchemaString(schemaObjectMapper, allSchemas, jsonSchemaId).getBytes("UTF-8"));
                 } catch (IOException e) {
-                    throw new MojoExecutionException("Unable to generate source files for: " + description.getName(), e);
+                    throw new MojoExecutionException("Unable to generate JSON Schema source file: " + schemaFilePath, e);
                 }
+
+                getLog().info(String.format("Successfully generated %s JSON Types in file %s", descriptions.size() * 2, schemaFilePath));
             }
-            getLog().info(String.format("Successfully generated %s Java Classes", descriptions.size() * 2));
 
         } finally {
             // remember to stop the client

http://git-wip-us.apache.org/repos/asf/camel/blob/5eaaf981/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoIntegrationTest.java
----------------------------------------------------------------------
diff --git a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoIntegrationTest.java b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoIntegrationTest.java
index 8856b7e..2f75608 100644
--- a/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoIntegrationTest.java
+++ b/components/camel-salesforce/camel-salesforce-maven-plugin/src/test/java/org/apache/camel/maven/CamelSalesforceMojoIntegrationTest.java
@@ -25,10 +25,15 @@ import java.util.Properties;
 
 import org.apache.camel.component.salesforce.SalesforceEndpointConfig;
 import org.apache.camel.component.salesforce.SalesforceLoginConfig;
+import org.apache.camel.component.salesforce.api.utils.JsonUtils;
 import org.apache.maven.plugin.logging.SystemStreamLog;
 import org.junit.Assert;
 import org.junit.Test;
 
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
+import com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema;
+
 public class CamelSalesforceMojoIntegrationTest {
 
     private static final String TEST_LOGIN_PROPERTIES = "../test-salesforce-login.properties";
@@ -47,6 +52,26 @@ public class CamelSalesforceMojoIntegrationTest {
         // TODO check that the generated code compiles
     }
 
+    @Test
+    public void testExecuteJsonSchema() throws Exception {
+        CamelSalesforceMojo mojo = createMojo();
+        mojo.jsonSchema = true;
+        mojo.jsonSchemaFilename = "test-schema.json";
+        mojo.jsonSchemaId = JsonUtils.DEFAULT_ID_PREFIX;
+
+        // generate code
+        mojo.execute();
+
+        // validate generated schema
+        File schemaFile = mojo.outputDirectory.toPath().resolve("test-schema.json").toFile();
+        Assert.assertTrue("Output file was not created",
+                schemaFile.exists());
+        ObjectMapper objectMapper = JsonUtils.createObjectMapper();
+        JsonSchema jsonSchema = objectMapper.readValue(schemaFile, JsonSchema.class);
+        Assert.assertTrue("Expected root JSON schema with oneOf element",
+                jsonSchema.isObjectSchema() && !((ObjectSchema)jsonSchema).getOneOf().isEmpty());
+    }
+
     protected CamelSalesforceMojo createMojo() throws IOException {
         CamelSalesforceMojo mojo = new CamelSalesforceMojo();
 
@@ -82,10 +107,10 @@ public class CamelSalesforceMojoIntegrationTest {
         try {
             stream = new FileInputStream(TEST_LOGIN_PROPERTIES);
             properties.load(stream);
-            mojo.clientId = properties.getProperty("clientId");
-            mojo.clientSecret = properties.getProperty("clientSecret");
-            mojo.userName = properties.getProperty("userName");
-            mojo.password = properties.getProperty("password");
+            mojo.clientId = properties.getProperty("salesforce.client.id");
+            mojo.clientSecret = properties.getProperty("salesforce.client.secret");
+            mojo.userName = properties.getProperty("salesforce.username");
+            mojo.password = properties.getProperty("salesforce.password");
         } catch (FileNotFoundException e) {
             throw new FileNotFoundException("Create a properties file named "
                     + TEST_LOGIN_PROPERTIES + " with clientId, clientSecret, userName, password"